Skip to content

Commit

Permalink
Feature: Limits recursive search depth. Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
nick spragg committed Mar 20, 2016
1 parent eb88373 commit 3f9fb4a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -91,6 +91,16 @@ const files = FileHound.create()
.find();
```

#### Limiting the depth of a recursive search

Find all files but _only_ in the current directory (recursion off):

```js
const files = FileHound.create()
.depth(0)
.find();
```

#### Combining multiple searches

Find all the files that are _either_ over 1K _or_ have the `.json` file extension:
Expand Down
21 changes: 13 additions & 8 deletions lib/filehound.js
Expand Up @@ -14,6 +14,7 @@ const sizeMatcher = require('./files').sizeMatcher;
const extMatcher = require('./files').extMatcher;
const findSubDirectories = require('./files').findSubDirectories;
const notSubDirectory = require('./files').notSubDirectory;
const pathDepth = require('./files').pathDepth;

function isDefined(value) {
return value !== undefined;
Expand All @@ -27,6 +28,10 @@ function readFiles(dir) {
return bluebird.resolve(fsp.readdir(dir));
}

function getDepth(root, dir) {
return pathDepth(dir) - pathDepth(root);
}

class FileHound {
constructor() {
this.searchPaths = new Set();
Expand Down Expand Up @@ -54,6 +59,10 @@ class FileHound {
return isMatch(file);
}

_atMaxDepth(root, dir) {
return isDefined(this.maxDepth) && (getDepth(root, dir) > this.maxDepth);
}

addFilter(filter) {
this.filters.push(filter);
return this;
Expand Down Expand Up @@ -94,16 +103,12 @@ class FileHound {
return this;
}

_search(dir, depth) {
depth = depth || 0;

if (isDefined(this.maxDepth) && depth > this.maxDepth) {
return [];
}
_search(root, dir) {
if (this._atMaxDepth(root, dir)) return [];

return this._getFiles(dir)
.map((file) => {
return isDirectory(file) ? this._search(file, ++depth) : file;
return isDirectory(file) ? this._search(root, file) : file;
})
.reduce(flatten, [])
.filter((file) => {
Expand All @@ -113,7 +118,7 @@ class FileHound {

find(cb) {
const searches = bluebird.map(this.getSearchPaths(), (dir) => {
return this._search(dir);
return this._search(dir, dir);
});
return bluebird.all(searches).reduce(flatten).asCallback(cb);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/files.js
Expand Up @@ -44,6 +44,10 @@ function getSubDirectories(base, allPaths) {
});
}

function splitPath(dir) {
return dir.split(path.sep);
}

module.exports.joinWith = (dir) => {
return (file) => {
return path.join(dir, file);
Expand Down Expand Up @@ -119,3 +123,7 @@ module.exports.isDirectory = (file) => {
};

module.exports.isSubDirectory = isSubDirectory;

module.exports.pathDepth = (dir) => {
return splitPath(dir).length;
};
8 changes: 6 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "filehound",
"version": "1.0.2",
"version": "1.1.0",
"description": "Fluent interface for finding files",
"main": "index.js",
"scripts": {
Expand All @@ -17,7 +17,11 @@
"file",
"recursive",
"search",
"filehound"
"filehound",
"lister",
"find",
"directory",
"walk"
],
"engines": {
"node": ">=4.0.0"
Expand Down
5 changes: 5 additions & 0 deletions test/files.js
Expand Up @@ -14,5 +14,10 @@ describe('Files', () => {
const isSubDirectory = files.notSubDirectory(['./fixtures', './fixtures/nested']);
assert.strictEqual(isSubDirectory('./fixtures/nested'), false);
});

it('returns the depth of a given path', () => {
const path = '/a/b/c';
assert.equal(files.pathDepth(path), 4);
});
});
});

0 comments on commit 3f9fb4a

Please sign in to comment.