Skip to content

Commit

Permalink
feat(reader): Ability to use depth in the custom deep function
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Nov 19, 2017
1 parent 2716299 commit bacf80d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/directory-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ DirectoryReader.prototype.processItem = function processItem (dir, item, done) {
// (i.e. options.basePath and options.sep)
stats.path = itemPath;

// Add depth of the path to the fs.Stats object for use this in the filter function
stats.depth = dir.depth;

if (reader.shouldRecurse(stats, posixPath, maxDepthReached)) {
// Add this subdirectory to the queue
reader.queue.push({
Expand Down
26 changes: 26 additions & 0 deletions test/specs/deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@ describe('options.deep', function () {
});
}
},
{
it: 'should recurse based on a custom function with depth property',
args: ['test/dir', {
deep: function (stats) {
return stats.depth !== 1;
},
}],
assert: function (error, data) {
expect(error).to.be.null;
expect(data).to.have.same.members(this.omitSubdir(dir.deep.data));
},
streamAssert: function (errors, data, files, dirs, symlinks) {
expect(errors.length).to.equal(0);
expect(data).to.have.same.members(this.omitSubdir(dir.deep.data));
expect(files).to.have.same.members(this.omitSubdir(dir.deep.files));
expect(dirs).to.have.same.members(this.omitSubdir(dir.deep.dirs));
expect(symlinks).to.have.same.members(this.omitSubdir(dir.deep.symlinks));
},

// Omits the contents of the "subdir" directory
omitSubdir: function (paths) {
return paths.filter(function (p) {
return p.split(path.sep).length <= 2;
});
}
},
{
it: 'should return all deep contents if custom deep function always returns true',
args: ['test/dir', {
Expand Down

0 comments on commit bacf80d

Please sign in to comment.