Skip to content

Commit

Permalink
change option name, update readme and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Apr 23, 2017
1 parent c4c78c5 commit e76afdf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -6,7 +6,7 @@

### Added
- `filter` option. A function that gets one argument `fn({path: '', stats: {}})` and returns true to include or false to exclude the item.
- `noRecurseOnFilter` option to prevent unnecessary traversal of unwanted directories when `filter` function is used.
- `noRecurseOnFailedFilter` option to prevent unnecessary traversal of unwanted directories when `filter` function is used.

1.1.2 / 2017-02-17
------------------
Expand Down
27 changes: 17 additions & 10 deletions README.md
Expand Up @@ -20,11 +20,15 @@ Usage
### klawSync(directory[, options])

- `directory` `<String>`
- `options` `<Object>` *optional* (all options are `false` by default)
- `nodir` `<Boolean>` return only files (ignore directories)
- `nofile` `<Boolean>` return only directories (ignore files)
- `noRecurseOnFilter` `<Boolean>` when `filter` function is used, the default behavior is to read all directories even though they don't pass the `filter` function (won't be included but still will be traversed). Set `true` to prevent unnecessary traversal of unwanted directories
- `filter` `<Function>` function that gets one argument `fn({path: '', stats: {}})` and returns true to include or false to exclude the item
- `options` `<Object>` (optional) _all options are `false` by default_
- `nodir` `<Boolean>`
- return only files (ignore directories)
- `nofile` `<Boolean>`
- return only directories (ignore files)
- `noRecurseOnFailedFilter` `<Boolean>`
- when `filter` function is used, the default behavior is to read all directories even if they don't pass the `filter` function (won't be included but still will be traversed). If you set `true`, there will be neither inclusion nor traversal for directories that don't pass the `filter` function
- `filter` `<Function>`
- function that gets one argument `fn({path: '', stats: {}})` and returns true to include or false to exclude the item

- return: `<Array<Object>>` `[{path: '', stats: {}}]`

Expand Down Expand Up @@ -72,13 +76,14 @@ const dirs = klawSync('/some/dir', {nofile: true})

_**ignore `node_modules`**_

Notice here `noRecurseOnFilter: true` is used since we don't want anything from `node_modules` in this case (no inclusion and no traversal).
Notice here `noRecurseOnFailedFilter: true` option is used since we don't want anything from `node_modules` (no inclusion and no traversal).

```js
const klawSync = require('klaw-sync')

const filterFn = item => item.path.indexOf('node_modules') < 0
const paths = klawSync('/some/dir', { filter: filterFn, noRecurseOnFilter: true })

const paths = klawSync('/some/dir', { filter: filterFn, noRecurseOnFailedFilter: true })
```

_**ignore `node_modules` and `.git`**_
Expand All @@ -87,24 +92,26 @@ _**ignore `node_modules` and `.git`**_
const klawSync = require('klaw-sync')

const filterFn = item => item.path.indexOf('node_modules') < 0 && item.path.indexOf('.git') < 0
const paths = klawSync('/some/dir', { filter: filterFn, noRecurseOnFilter: true })

const paths = klawSync('/some/dir', { filter: filterFn, noRecurseOnFailedFilter: true })
```

_**get all `js` files**_

Here `noRecurseOnFilter` is not required since we are interested in all `js` files. In other words, although no directories pass the `filter` function, we still want to read them and see if they have any `js` files.
Here `noRecurseOnFailedFilter` option is not required since we are interested in all `js` files. In other words, although no directories pass the `filter` function, we still want to read them and see if they have any `js` files.

```js
const path = require('path')
const klawSync = require('klaw-sync')

const filterFn = item => path.extname(item.path) === '.js'

const paths = klawSync('/some/dir', { filter: filterFn })
```

_**filter based on stats**_

Again here `noRecurseOnFilter` is not required since we still want to read all directories even though they don't pass the `filter` function, to see if their contents pass the `filter` function.
Again here `noRecurseOnFailedFilter` option is not required since we still want to read all directories even though they don't pass the `filter` function, to see if their contents pass the `filter` function.

```js
const klawSync = require('klaw-sync')
Expand Down
14 changes: 7 additions & 7 deletions benchmark/bm.js
Expand Up @@ -12,7 +12,7 @@ function help () {
console.log(`npm run benchmark -- --dir=<rootdir> --nodir=true (ignore all directories)`)
}

function run (root, opts) {
function runBm (root, opts) {
if (!opts) {
const suite = Benchmark.Suite()
suite.add('walk-sync', function () {
Expand All @@ -31,8 +31,8 @@ function run (root, opts) {
}).on('cycle', function (ev) {
console.log(String(ev.target))
}).on('complete', function () {
console.log('\nSummary: Fastest is ' + this.filter('fastest').map('name'))
}).run({ 'async': false })
console.log('Fastest is ' + this.filter('fastest').map('name'))
}).run()
} else {
const suite = Benchmark.Suite()
suite.add('walk-sync', function () {
Expand All @@ -52,8 +52,8 @@ function run (root, opts) {
}).on('cycle', function (ev) {
console.log(String(ev.target))
}).on('complete', function () {
console.log('\nSummary: Fastest is ' + this.filter('fastest').map('name'))
}).run({ 'async': false })
console.log('Fastest is ' + this.filter('fastest').map('name'))
}).run()
}
}

Expand All @@ -66,9 +66,9 @@ if (!argv.dir) {
if (argv.nodir) {
console.log(`root dir: ${dir}`)
console.log('option.nodir: true\n')
run(dir, {nodir: true})
runBm(dir, {nodir: true})
} else {
console.log(`root dir: ${dir}\n`)
run(dir)
runBm(dir)
}
}
23 changes: 9 additions & 14 deletions klaw-sync.js
Expand Up @@ -12,27 +12,22 @@ function klawSync (dir, opts, ls) {
const stat = fs.lstatSync(pathItem)
const item = {path: pathItem, stats: stat}
if (stat.isDirectory()) {
if (!opts.nodir) {
if (opts.filter) {
if (opts.filter(item)) {
ls.push(item)
ls = klawSync(pathItem, opts, ls)
}
if (!opts.noRecurseOnFilter) ls = klawSync(pathItem, opts, ls)
} else {
if (opts.filter) {
if (opts.filter(item)) {
ls.push(item)
ls = klawSync(pathItem, opts, ls)
} else {
if (!opts.noRecurseOnFailedFilter) ls = klawSync(pathItem, opts, ls)
}
} else {
if (!opts.nodir) ls.push(item)
ls = klawSync(pathItem, opts, ls)
}
} else {
if (!opts.nofile) {
if (opts.filter) {
if (opts.filter(item)) ls.push(item)
} else {
ls.push(item)
}
if (opts.filter) {
if (opts.filter(item)) ls.push(item)
} else {
if (!opts.nofile) ls.push(item)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/manidlou/node-klaw-sync#readme",
"devDependencies": {
"benchmark": "^2.1.3",
"benchmark": "^2.1.4",
"fs-extra": "^1.0.0",
"glob": "^7.1.1",
"minimist": "^1.2.0",
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Expand Up @@ -146,7 +146,7 @@ describe('klaw-sync', () => {
})
})

it('should filter but not recurse if noRecurseOnFilter is true', () => {
it('should filter but not recurse if noRecurseOnFailedFilter is true', () => {
const dirToIgnore1 = path.join(TEST_DIR, 'node_modules')
const dirToIgnore2 = path.join(dirToIgnore1, 'somepkg')
fs.ensureDirSync(dirToIgnore2)
Expand All @@ -160,7 +160,7 @@ describe('klaw-sync', () => {
{path: FILES[2], stats: fs.statSync(FILES[2])}
]
const filterFunc = i => i.path.indexOf('node_modules') < 0
const items = klawSync(TEST_DIR, {filter: filterFunc, noRecurseOnFilter: true})
const items = klawSync(TEST_DIR, {filter: filterFunc, noRecurseOnFailedFilter: true})
assert.equal(items.length, paths.length)
items.forEach((p, i) => {
assert.deepEqual(p, paths[i])
Expand All @@ -184,7 +184,7 @@ describe('klaw-sync', () => {
{path: FILES[2], stats: fs.statSync(FILES[2])}
]
const filterFunc = i => i.path.indexOf('node_modules') < 0 && i.path.indexOf('.git') < 0
const items = klawSync(TEST_DIR, {filter: filterFunc, noRecurseOnFilter: true})
const items = klawSync(TEST_DIR, {filter: filterFunc, noRecurseOnFailedFilter: true})
assert.equal(items.length, paths.length)
items.forEach((p, i) => {
assert.deepEqual(p, paths[i])
Expand Down

0 comments on commit e76afdf

Please sign in to comment.