Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

File path filters #18

Merged
merged 6 commits into from

2 participants

@ZECTBynmo

I added a new option that allows you the user to filter paths that contain certain string keys. It only works for async walking, but it really helps read large directories fast, when you know you can ignore certain things.

The overhead for non-users should be just a variable declaration and an if statement per directory read.

@coolaj86
Owner

is there a reason to call this asyncFilters instead of just filters?

@ZECTBynmo

Yes: it only works for asynchronous walking.

@ZECTBynmo

Try that, it should work for both.

@coolaj86 coolaj86 merged commit 49e9260 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 19, 2013
  1. @ZECTBynmo

    Add path filters to options

    ZECTBynmo authored
  2. @ZECTBynmo
  3. @ZECTBynmo

    Fix typo in README

    ZECTBynmo authored
  4. @ZECTBynmo

    better wording

    ZECTBynmo authored
Commits on Apr 20, 2013
  1. @ZECTBynmo
  2. @ZECTBynmo

    Code cleanup

    ZECTBynmo authored
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 4 deletions.
  1. +2 −0  README.md
  2. +28 −4 lib/walk.js
View
2  README.md
@@ -33,6 +33,8 @@ The Synchronous version still uses callbacks, so it is safe to use with other As
options = {
followLinks: false,
+ // filters: ["Temp", "_Temp"] // directories with these
+ // keys will be skipped
};
walker = walk.walk("/tmp", options);
View
32 lib/walk.js
@@ -35,6 +35,7 @@
me._wq = [];
me._wqueue = [me._wq];
me._wcurpath = undefined;
+ me._wfilters = options.filters;
me._wfirstrun = true;
me._wcurpath = pathname;
@@ -175,10 +176,23 @@
, me = this
;
- try {
- files = fs.readdirSync(me._wcurpath);
- } catch(e) {
- err = e;
+ // Stop directories that contain filter keywords
+ // from continuing through the walk process
+ if (me._wfilters != undefined) {
+ var shouldExclude = false;
+ for (var iFilter=0; iFilter<me._wfilters.length; ++iFilter) {
+ if (me._wcurpath.indexOf(me._wfilters[iFilter]) != -1 ) {
+ me._wNext();
+ }
+ }
+ }
+
+ if( !shouldExclude ) {
+ try {
+ files = fs.readdirSync(me._wcurpath);
+ } catch(e) {
+ err = e;
+ }
}
me._wReaddirHandler(err, files);
@@ -187,6 +201,16 @@
var me = this
;
+ // Stop directories that contain filter keywords
+ // from continuing through the walk process
+ if (me._wfilters != undefined) {
+ for (var iFilter=0; iFilter<me._wfilters.length; ++iFilter) {
+ if (me._wcurpath.indexOf(me._wfilters[iFilter]) != -1 ) {
+ me._wNext();
+ }
+ }
+ }
+
// TODO how to remove this anony?
fs.readdir(me._wcurpath, function (err, files) {
me._wReaddirHandler(err, files);
Something went wrong with that request. Please try again.