Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
benchmark/klaw-sync-benchmark-fixtures/
npm-debug.log
.idea
24 changes: 7 additions & 17 deletions klaw-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@ function klawSync (dir, opts, ls) {
const pi = paths[i]
const st = opts.fs.statSync(pi)
const item = {path: pi, stats: st}
if (!st.isDirectory() || (opts.rootDepth &&
pi.split(path.sep).length - opts.rootDepth >= opts.depthLimit)) {
if (opts.filter) {
if (opts.filter(item) && !opts.nofile) ls.push(item)
} else if (!opts.nofile) {
ls.push(item)
}
} else {
if (opts.filter) {
if (opts.filter(item) && !opts.nodir) {
ls.push(item)
ls = klawSync(pi, opts, ls)
}
} else {
if (!opts.nodir) ls.push(item)
ls = klawSync(pi, opts, ls)
}
const isUnderDepthLimit = (!opts.rootDepth || pi.split(path.sep).length - opts.rootDepth < opts.depthLimit)

if (st.isDirectory()) {
if (!opts.nodir) { if ((opts.filter && opts.filter(item)) || !opts.filter) ls.push(item) }
if (isUnderDepthLimit && ((opts.filter && opts.filter(item)) || !opts.filter)) ls = klawSync(pi, opts, ls)
} else if (!st.isDirectory()) {
if (!opts.nofile) { if ((opts.filter && opts.filter(item)) || !opts.filter) ls.push(item) }
}
}
return ls
Expand Down
87 changes: 87 additions & 0 deletions test/klaw-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,93 @@ describe('klaw-sync', () => {
testDepthLimit(3, expected)
})

it('should honor depthLimit option -1 with nodir = true', () => {
const expected = ['a/b/c/d.txt', 'a/e.jpg', 'h/i/j/k.txt', 'h/i/l.txt', 'h/i/m.jpg', 't.txt']
testDepthLimitNoDir(-1, expected)
})

it('should honor depthLimit option 0 with nodir = true', () => {
const expected = ['t.txt']
testDepthLimitNoDir(0, expected)
})

it('should honor depthLimit option 1 with nodir = true', () => {
const expected = ['a/e.jpg', 't.txt']
testDepthLimitNoDir(1, expected)
})

it('should honor depthLimit option -1 with nodir = true and with a filter to search for a specific file', () => {
let expected = ['h/i/j/k.txt']
const fixtures = [
'a/b/c/d.txt',
'a/e.jpg',
'h/i/j/k.txt',
'h/i/l.txt',
'h/i/m.jpg',
't.txt'
]

const filterFunction = function (item) {
return item.stats.isDirectory() || path.basename(item.path) === 'k.txt'
}

fixtures.forEach(f => {
f = path.join(TEST_DIR, f)
fs.outputFileSync(f, path.basename(f, path.extname(f)))
})

const items = klawSync(TEST_DIR, {depthLimit: -1, nodir: true, filter: filterFunction}).map(i => i.path)
items.sort()
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
assert.deepStrictEqual(items, expected)
})

it('should return all files except under filtered out directory', () => {
let expected = ['a/b/c/d.txt', 'a/e.jpg', 't.txt']
const fixtures = [
'a/b/c/d.txt',
'a/e.jpg',
'h/i/j/k.txt',
'h/i/l.txt',
'h/i/m.jpg',
't.txt'
]

const filterFunction = function (item) {
return !item.stats.isDirectory() || (item.stats.isDirectory() && path.basename(item.path) !== 'i')
}

fixtures.forEach(f => {
f = path.join(TEST_DIR, f)
fs.outputFileSync(f, path.basename(f, path.extname(f)))
})

const items = klawSync(TEST_DIR, {depthLimit: -1, nodir: true, filter: filterFunction}).map(i => i.path)
items.sort()
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
assert.deepStrictEqual(items, expected)
})

function testDepthLimitNoDir (depthLimit, expected) {
const fixtures = [
'a/b/c/d.txt',
'a/e.jpg',
'h/i/j/k.txt',
'h/i/l.txt',
'h/i/m.jpg',
't.txt'
]
fixtures.forEach(f => {
f = path.join(TEST_DIR, f)
fs.outputFileSync(f, path.basename(f, path.extname(f)))
})

const items = klawSync(TEST_DIR, {depthLimit: depthLimit, nodir: true}).map(i => i.path)
items.sort()
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
assert.deepStrictEqual(items, expected)
}

function testDepthLimit (depthLimit, expected) {
const fixtures = [
'a/b/c/d.txt',
Expand Down