Skip to content

Commit 27e192c

Browse files
authored
Merge pull request #12 from Geelik/master
Fixing logic issues
2 parents 60b7c5e + 9164cc8 commit 27e192c

3 files changed

Lines changed: 95 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
benchmark/klaw-sync-benchmark-fixtures/
33
npm-debug.log
4+
.idea

klaw-sync.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,13 @@ function klawSync (dir, opts, ls) {
1515
const pi = paths[i]
1616
const st = opts.fs.statSync(pi)
1717
const item = {path: pi, stats: st}
18-
if (!st.isDirectory() || (opts.rootDepth &&
19-
pi.split(path.sep).length - opts.rootDepth >= opts.depthLimit)) {
20-
if (opts.filter) {
21-
if (opts.filter(item) && !opts.nofile) ls.push(item)
22-
} else if (!opts.nofile) {
23-
ls.push(item)
24-
}
25-
} else {
26-
if (opts.filter) {
27-
if (opts.filter(item) && !opts.nodir) {
28-
ls.push(item)
29-
ls = klawSync(pi, opts, ls)
30-
}
31-
} else {
32-
if (!opts.nodir) ls.push(item)
33-
ls = klawSync(pi, opts, ls)
34-
}
18+
const isUnderDepthLimit = (!opts.rootDepth || pi.split(path.sep).length - opts.rootDepth < opts.depthLimit)
19+
20+
if (st.isDirectory()) {
21+
if (!opts.nodir) { if ((opts.filter && opts.filter(item)) || !opts.filter) ls.push(item) }
22+
if (isUnderDepthLimit && ((opts.filter && opts.filter(item)) || !opts.filter)) ls = klawSync(pi, opts, ls)
23+
} else if (!st.isDirectory()) {
24+
if (!opts.nofile) { if ((opts.filter && opts.filter(item)) || !opts.filter) ls.push(item) }
3525
}
3626
}
3727
return ls

test/klaw-sync.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,93 @@ describe('klaw-sync', () => {
229229
testDepthLimit(3, expected)
230230
})
231231

232+
it('should honor depthLimit option -1 with nodir = true', () => {
233+
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']
234+
testDepthLimitNoDir(-1, expected)
235+
})
236+
237+
it('should honor depthLimit option 0 with nodir = true', () => {
238+
const expected = ['t.txt']
239+
testDepthLimitNoDir(0, expected)
240+
})
241+
242+
it('should honor depthLimit option 1 with nodir = true', () => {
243+
const expected = ['a/e.jpg', 't.txt']
244+
testDepthLimitNoDir(1, expected)
245+
})
246+
247+
it('should honor depthLimit option -1 with nodir = true and with a filter to search for a specific file', () => {
248+
let expected = ['h/i/j/k.txt']
249+
const fixtures = [
250+
'a/b/c/d.txt',
251+
'a/e.jpg',
252+
'h/i/j/k.txt',
253+
'h/i/l.txt',
254+
'h/i/m.jpg',
255+
't.txt'
256+
]
257+
258+
const filterFunction = function (item) {
259+
return item.stats.isDirectory() || path.basename(item.path) === 'k.txt'
260+
}
261+
262+
fixtures.forEach(f => {
263+
f = path.join(TEST_DIR, f)
264+
fs.outputFileSync(f, path.basename(f, path.extname(f)))
265+
})
266+
267+
const items = klawSync(TEST_DIR, {depthLimit: -1, nodir: true, filter: filterFunction}).map(i => i.path)
268+
items.sort()
269+
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
270+
assert.deepStrictEqual(items, expected)
271+
})
272+
273+
it('should return all files except under filtered out directory', () => {
274+
let expected = ['a/b/c/d.txt', 'a/e.jpg', 't.txt']
275+
const fixtures = [
276+
'a/b/c/d.txt',
277+
'a/e.jpg',
278+
'h/i/j/k.txt',
279+
'h/i/l.txt',
280+
'h/i/m.jpg',
281+
't.txt'
282+
]
283+
284+
const filterFunction = function (item) {
285+
return !item.stats.isDirectory() || (item.stats.isDirectory() && path.basename(item.path) !== 'i')
286+
}
287+
288+
fixtures.forEach(f => {
289+
f = path.join(TEST_DIR, f)
290+
fs.outputFileSync(f, path.basename(f, path.extname(f)))
291+
})
292+
293+
const items = klawSync(TEST_DIR, {depthLimit: -1, nodir: true, filter: filterFunction}).map(i => i.path)
294+
items.sort()
295+
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
296+
assert.deepStrictEqual(items, expected)
297+
})
298+
299+
function testDepthLimitNoDir (depthLimit, expected) {
300+
const fixtures = [
301+
'a/b/c/d.txt',
302+
'a/e.jpg',
303+
'h/i/j/k.txt',
304+
'h/i/l.txt',
305+
'h/i/m.jpg',
306+
't.txt'
307+
]
308+
fixtures.forEach(f => {
309+
f = path.join(TEST_DIR, f)
310+
fs.outputFileSync(f, path.basename(f, path.extname(f)))
311+
})
312+
313+
const items = klawSync(TEST_DIR, {depthLimit: depthLimit, nodir: true}).map(i => i.path)
314+
items.sort()
315+
expected = expected.map(item => path.join(path.join(TEST_DIR, item)))
316+
assert.deepStrictEqual(items, expected)
317+
}
318+
232319
function testDepthLimit (depthLimit, expected) {
233320
const fixtures = [
234321
'a/b/c/d.txt',

0 commit comments

Comments
 (0)