From cd1f10727973ac3e1e2743d049a253235fff4c03 Mon Sep 17 00:00:00 2001 From: Daniel Chcouri <333222@gmail.com> Date: Tue, 13 May 2014 11:08:54 +0300 Subject: [PATCH] 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 --- lib/wrench.js | 19 +++++++++++++++++-- readme.md | 3 +++ tests/readdir.js | 10 ++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index 01f5e95..53a9a97 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -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 ); diff --git a/readme.md b/readme.md index 2d646d0..866aad6 100644 --- a/readme.md +++ b/readme.md @@ -54,6 +54,9 @@ wrench.rmdirSyncRecursive('my_directory_name', failSilently); // Recursively read directories contents. wrench.readdirSyncRecursive('my_directory_name'); +// Recursively read directories contents, filter results (efficient memory usage for big folders trees). +wrench.readdirSyncRecursive('my_directory_name', function (x) { return /\.txt$/.test(x); }); + // Recursively chmod the entire sub-tree of a directory wrench.chmodSyncRecursive('my_directory_name', 0755); diff --git a/tests/readdir.js b/tests/readdir.js index 5e8abf0..a2f5f12 100644 --- a/tests/readdir.js +++ b/tests/readdir.js @@ -30,6 +30,16 @@ module.exports = testCase({ checkResult(test, files); }, + test_readdirSyncRecursive_filter: function(test) { + var dir = path.join(__dirname, 'readdir'); + + var txt_files = wrench.readdirSyncRecursive(dir, function (x) {return /\.txt$/.test(x);}); + + test.deepEqual(txt_files, ['bar.txt', path.join('foo', 'lorem.txt')]); + + test.done(); + }, + test_readdirRecursive: function(test) { var dir = path.join(__dirname, 'readdir'); -- 2.39.5