filter function still called with directories when nodir: true
See original GitHub issueI just updated to version 4
(was on version 2).
I noticed that with the option nodir: true
, the library was actually looking only at the files, which is expected, but unfortunately it does so only at depth 0, and never goes inside of the directories.
It is a bit of a problem, because if one wants to find specific files (using a filter
function), you will need instead to do something like:
const path = require('path');
const klawSync = require('klaw-sync');
function filterFn(item) {
return item.stats.isDirectory() || /[some regex]/.test(path.basename(item.path));
}
const paths = klawSync([FOLDER_PATH], { filter: filterFn });
and then you will need to loop on the paths
one more time to filter the directories out yourself.
const files = paths.filter(function(item) {
return item.stats.isFile();
});
It would great if the default with nodir: true
was to traverse all the directories, and call the filter
function only for the files.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (7 by maintainers)
Top Results From Across the Web
manidlou/node-klaw-sync: Node.js recursive ... - GitHub
This can be useful when you have a filter function and still want to traverse all subdirectories even if your filter function doesn't...
Read more >gulp.src() include files but ignore all folders - Stack Overflow
Just use the nodir option when you call gulp.src . This will actually test the files being read off the filesystem instead of...
Read more >glob - npm
nodir Do not match directories, only files. (Note: to match only directories, simply put a / at the end of the pattern.) ignore...
Read more >Excel FILTER function with formula examples - Ablebits
The FILTER function in Excel is used to filter a range of data based ... the criteria supplied as a Boolean array (TRUE...
Read more >FILTER function - Microsoft Support
How to use the FILTER function in Excel to filter a range of data based on criteria you define.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@manidlou @Geelik should we re-open this? The depthLimit looks like it’s working, but the original problem of filtering directories even with
nodir: true
noted by @nicolasroger17 is still not resolved.His example still doesn’t work (with or without)
nodir: true
if there’s noitem.stats.isDirectory() ||
in the filter function.But maybe a better solution than simply always traversing when
nodir: true
would be to another option calledtraverseAll
that would ensure all directories are traversed regardless ofnodir
. This would give the benefit of providing backwards compatibility with the current behavior.This could be achieved by building on #12 and further simplifying the logic in the for loop like so:
If you think that makes sense I’ll be happy to make a PR.
Released in
v6.0.0
.