readdirSyncRecursive: add a filter argument

filter is an optional filter function that will be applied on the records of
each sub-directory to keep only the records you need. Avoiding redundencies
is critical for big folders trees
This commit is contained in:
Daniel Chcouri 2014-05-13 11:08:54 +03:00
parent 13f486d867
commit cd1f107279
3 changed files with 30 additions and 2 deletions

View file

@ -15,12 +15,21 @@ var fs = require("fs"),
_path = require("path"),
isWindows = !!process.platform.match(/^win/);
/* wrench.readdirSyncRecursive("directory_path");
/* wrench.readdirSyncRecursive("directory_path", filter);
*
* Recursively dives through directories and read the contents of all the
* children directories.
*
* filter is an optional filter function that will be applied on the records of
* each sub-directory to keep only the records you need. Avoiding redundencies
* is critical for big folders trees.
*
* Example:
*
* var txt_files = wrench.readdirSyncRecursive(dir, function (x) {return /\.txt$/.test(x);});
*
*/
exports.readdirSyncRecursive = function(baseDir) {
exports.readdirSyncRecursive = function(baseDir, filter) {
baseDir = baseDir.replace(/\/$/, '');
var readdirSyncRecursive = function(baseDir) {
@ -36,6 +45,12 @@ exports.readdirSyncRecursive = function(baseDir) {
curFiles = fs.readdirSync(baseDir);
nextDirs = curFiles.filter(isDir);
// filter results
if (typeof filter === 'function') {
curFiles = curFiles.filter(filter)
}
curFiles = curFiles.map(prependBaseDir);
files = files.concat( curFiles );