Skip to content

Commit

Permalink
Refactor: Clean async/sync search methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nspragg committed Aug 21, 2016
1 parent 03757bc commit a7ae553
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions src/filehound.js
Expand Up @@ -21,13 +21,27 @@ function flatten(a, b) {
return a.concat(b);
}

// TODO: move to files
function getFiles(dir, read) {
return read(dir).map(files.joinWith(dir));
}

function getFilesSync(dir) {
return getFiles(dir, files.readFilesSync);
}

function getFilesAsync(dir) {
return getFiles(dir, files.readFiles);
}

class FileHound extends EventEmitter {
constructor() {
super();
this.filters = [];
this.searchPaths = [];
this.searchPaths.push(process.cwd());
this._filters = [];
this._searchPaths = [];
this._searchPaths.push(process.cwd());
this._ignoreHiddenDirectories = false;
this._isMatch = _.noop;
}

static create() {
Expand All @@ -39,22 +53,6 @@ class FileHound extends EventEmitter {
return bluebird.all(args).reduce(flatten, []);
}

_getFiles(dir) {
return files.readFiles(dir).map(files.joinWith(dir));
}

_getFilesSync(dir) {
return files.readFilesSync(dir).map(files.joinWith(dir));
}

_isMatch(file) {
let isMatch = compose(this.filters);
if (this.negateFilters) {
isMatch = negate(isMatch);
}
return isMatch(file);
}

_atMaxDepth(root, dir) {
const fn = files.getDepth;
return isDefined(this.maxDepth) && (fn(root, dir) > this.maxDepth);
Expand All @@ -65,37 +63,39 @@ class FileHound extends EventEmitter {
(this._ignoreHiddenDirectories && files.isHiddenDirectory(dir));
}

_search(root, dir) {
if (this._shouldFilterDirectory(root, dir)) return [];
_createMatcher() {
const isMatch = compose(this._filters);
if (this.negateFilters) {
return negate(isMatch);
}
return isMatch;
}

return this._getFiles(dir)
.map((file) => {
return files.isDirectory(file) ? this._search(root, file) : file;
})
.reduce(flatten, [])
.filter((file) => {
return this._isMatch(file);
})
.each((file) => {
this.emit('match', file);
});
_searchSync(dir) {
const root = dir;
return this.search(root, dir, getFilesSync);
}

_searchSync(root, dir) {
_searchAsync(dir) {
const root = dir;
return this.search(root, dir, getFilesAsync).each((file) => {
this.emit('match', file);
});
}

search(root, dir, getFiles) {
if (this._shouldFilterDirectory(root, dir)) return [];

return this._getFilesSync(dir)
return getFiles(dir)
.map((file) => {
return files.isDirectory(file) ? this._search(root, file) : file;
return files.isDirectory(file) ? this.search(root, file, getFiles) : file;
})
.reduce(flatten, [])
.filter((file) => {
return this._isMatch(file);
});
.filter(this._isMatch);
}

getSearchPaths() {
const excludeSubDirs = files.reducePaths(this.searchPaths);
const excludeSubDirs = files.reducePaths(this._searchPaths);
return arrays.copy(excludeSubDirs);
}

Expand All @@ -115,17 +115,17 @@ class FileHound extends EventEmitter {
}

addFilter(filter) {
this.filters.push(filter);
this._filters.push(filter);
return this;
}

paths() {
this.searchPaths = _.uniq(arrays.from(arguments));
this._searchPaths = _.uniq(arrays.from(arguments));
return this;
}

path() {
this.searchPaths = arrays.fromFirst(arguments);
this._searchPaths = arrays.fromFirst(arguments);
return this;
}

Expand Down Expand Up @@ -179,10 +179,10 @@ class FileHound extends EventEmitter {
}

find(cb) {
const searches = bluebird
.map(this.getSearchPaths(), (dir) => {
return this._search(dir, dir);
});
this._isMatch = this._createMatcher();

const sync = this._searchAsync.bind(this);
const searches = bluebird.map(this.getSearchPaths(), sync);

return bluebird
.all(searches)
Expand All @@ -198,12 +198,12 @@ class FileHound extends EventEmitter {
}

findSync() {
const paths = this.getSearchPaths();
const results = paths.map((path) => {
return this._searchSync(path, path);
});
this._isMatch = this._createMatcher();
const fn = this._searchSync.bind(this);

return results.reduce(flatten);
return this.getSearchPaths()
.map(fn)
.reduce(flatten);
}
}

Expand Down

0 comments on commit a7ae553

Please sign in to comment.