Skip to content

Commit 01a795b

Browse files
authored
Merge pull request #33 from zanerock/work-liquid-labs/find-plus/32
Delete tests before passing options to custom tests
2 parents 11290b1 + 2f737f2 commit 01a795b

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ When using `excludePaths`, remember that the pattern does not prevent `find()` f
9393
9494
## Custom tests
9595
96-
When specifying custom `tests`, each function takes two arguments: `file` and `options`. The `file` is a is a [`fs.DirEnt`](https://nodejs.org/api/fs.html#class-fsdirent) or [`fs.Stats`](https://nodejs.org/api/fs.html#class-fsstats) object[^4] with `name`, `parentPath`, `absPath`, `relPath`, and `depth` properties added. The `options` object a copy of the options object passed to `find()` with `absRoot` added for convenience.
96+
When specifying custom `tests`, each function takes two arguments: `file` and `options`. The `file` is a is a [`fs.DirEnt`](https://nodejs.org/api/fs.html#class-fsdirent) or [`fs.Stats`](https://nodejs.org/api/fs.html#class-fsstats) object[^4] with `name`, `parentPath`, `absPath`, `relPath`, and `depth` properties added. The `options` object a copy of the options object passed to `find()` with `absRoot` added for convenience and the `tests` property removed. The test functions must return `true` or `false` to indicate the file in question should be included (if all other requirements are met) or must be excluded from the results, respectively.
9797
9898
[^4]: Both `DirEnt` and `Stat` objects provide a matching set of type identifier functions like`isDirectory()`, `isFIFO()`, `isSocket()`, etc.
9999

src/lib/traverse-dirs.mjs

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const traverseDirs = async(options) => {
2020
// we expect our received options to be independent of the users original 'options' passed to 'find', so it's OK to
2121
// modify here TODO: test input options are not modified when 'find()' is called.
2222
options.absRoot = absRoot
23+
const testOptions = Object.assign({}, options)
24+
delete testOptions.tests
25+
delete testOptions._traversedDirs
2326

2427
const accumulator = []
2528

@@ -29,7 +32,7 @@ const traverseDirs = async(options) => {
2932
_traversedDirs?.push(dirEntToFilePath(rootStat))
3033
}
3134
else {
32-
testForInclusionAndFrontier({ accumulator, file : rootStat, frontier }, options)
35+
testForInclusionAndFrontier({ accumulator, file : rootStat, frontier }, options, testOptions)
3336
}
3437

3538
let currDepth = 1
@@ -42,7 +45,7 @@ const traverseDirs = async(options) => {
4245
for (const file of files) {
4346
addFieldsToFile(file, { absRoot, depth : currDepth, parentPath : dirPath, root })
4447

45-
testForInclusionAndFrontier({ accumulator, file, frontier : newFrontier }, options)
48+
testForInclusionAndFrontier({ accumulator, file, frontier : newFrontier }, options, testOptions)
4649
}
4750
}
4851
// at this point we have processed all files at the current depth, so we work on the files at the next level
@@ -54,7 +57,8 @@ const traverseDirs = async(options) => {
5457
return accumulator
5558
}
5659

57-
const testForInclusionAndFrontier = ({ accumulator, file, frontier }, options) => {
60+
// we take test options so that we're not cloning the options every single time
61+
const testForInclusionAndFrontier = ({ accumulator, file, frontier }, options, testOptions) => {
5862
const {
5963
_traversedDirs,
6064
excludePaths,
@@ -63,7 +67,7 @@ const testForInclusionAndFrontier = ({ accumulator, file, frontier }, options) =
6367
tests
6468
} = options
6569

66-
const pass = !tests.some((t) => !t(file, options))
70+
const pass = !tests.some((t) => !t(file, testOptions))
6771
if (pass === true) {
6872
accumulator.push(file)
6973
}

0 commit comments

Comments
 (0)