Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 5, 2021
1 parent 3c997ed commit 85ad746
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 81 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
32 changes: 14 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
'use strict'
import fs from 'fs'
import path from 'path'
import {toVFile} from 'to-vfile'

var fs = require('fs')
var path = require('path')
var vfile = require('to-vfile')
export const INCLUDE = 1
export const SKIP = 4
export const BREAK = 8

exports.INCLUDE = 1
exports.SKIP = 4
exports.BREAK = 8
exports.all = all
exports.one = one
// Find files or directories downwards.
export function findDown(test, paths, callback) {
return find(test, paths, callback)
}

// Find a file or a directory downwards.
function one(test, paths, callback) {
export function findDownOne(test, paths, callback) {
return find(test, paths, callback, true)
}

// Find files or directories downwards.
function all(test, paths, callback) {
return find(test, paths, callback)
}

// Find applicable files.
function find(test, paths, callback, one) {
var state = {checked: [], test: augment(test)}
Expand All @@ -41,7 +37,7 @@ function find(test, paths, callback, one) {
// Find files in `filePath`.
function visit(state, filePath, one, done) {
// Don’t walk into places multiple times.
if (state.checked.indexOf(filePath) > -1) {
if (state.checked.includes(filePath)) {
done([])
return
}
Expand All @@ -58,7 +54,7 @@ function visit(state, filePath, one, done) {
if (state.broken || !real) {
done([])
} else {
file = vfile(filePath)
file = toVFile(filePath)
result = state.test(file, stats)

if ((result & 1) === 1 /* Include. */) {
Expand Down Expand Up @@ -103,7 +99,7 @@ function visitAll(state, paths, cwd, one, done) {
var index = -1

while (++index < paths.length) {
each(path[index])
each(paths[index])
}

next()
Expand Down
24 changes: 10 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,29 @@
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"to-vfile": "^6.0.0"
"to-vfile": "^7.0.0"
},
"devDependencies": {
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
Expand All @@ -61,10 +58,9 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/no-fn-reference-in-iterator": "off",
"unicorn/prefer-includes": "off"
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
Expand Down
23 changes: 15 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Find [vfile][]s by searching the file system downwards.

## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.

[npm][]:

```sh
Expand All @@ -22,7 +25,7 @@ npm install vfile-find-down
```js
var findDown = require('vfile-find-down')

findDown.all('.md', console.log)
findDown('.md', console.log)
```

Yields:
Expand All @@ -37,7 +40,11 @@ null [ VFile {

## API

### `findDown.all(tests[, paths], callback)`
This package exports the following identifiers: `findDown`, `findDownOne`,
`INCLUDE`, `SKIP`, `BREAK`.
There is no default export.

### `findDown(tests[, paths], callback)`

Search for `tests` downwards.
Invokes callback with either an error or an array of files passing `tests`.
Expand Down Expand Up @@ -67,9 +74,9 @@ Place(s) to searching from (`Array.<string>` or `string`, default:

Function invoked with all matching files (`function cb(err[, files])`).

### `findDown.one(tests[, paths], callback)`
### `findDownOne(tests[, paths], callback)`

Like `findDown.all`, but invokes `callback` with the first found file, or
Like `findDown`, but invokes `callback` with the first found file, or
`null`.

### `function test(file, stats)`
Expand All @@ -79,13 +86,13 @@ Invoked with a [vfile][] and a [stats][] object.

###### Returns

* `true` or `findDown.INCLUDE` — Include the file in the results
* `findDown.SKIP` — Do not search inside this directory
* `findDown.BREAK` — Stop searching for files
* `true` or `INCLUDE` — Include the file in the results
* `SKIP` — Do not search inside this directory
* `BREAK` — Stop searching for files
* anything else is ignored: files are neither included nor skipped

The different flags can be combined by using the pipe operator:
`findDown.INCLUDE | findDown.SKIP`.
`INCLUDE | SKIP`.

## Contribute

Expand Down
Loading

0 comments on commit 85ad746

Please sign in to comment.