Skip to content
Browse files

Add filter option

  • Loading branch information...
1 parent de75188 commit 9ba1a652d4e4d52e347a2d426f485c8def70205b @pvorb committed
Showing with 66 additions and 36 deletions.
  1. +8 −0 README.mkd
  2. +46 −32 diveSync.js
  3. +1 −1 package.json
  4. +11 −3 test/test.js
View
8 README.mkd
@@ -29,5 +29,13 @@ The default options are
// files in "dot directories", e.g. ".gitinore" or
// ".git/HEAD".
directories: false // - If set to true, this will show directories, too.
+ filter: function filter(path, dir) { return true; }
+ // - Function that returns true for all paths that should
+ // not be ignored.
}
```
+
+`filter` takes two arguments `(path, dir)`. `path` defines the path to file in
+the file system. `dir` is `true`, if `path` is a directory, otherwise `false`.
+
+You can use this to filter out specific files or directories by their pathname.
View
78 diveSync.js
@@ -1,51 +1,65 @@
var fs = require('fs'),
append = require('append');
-// General function
+// default options
+var defaultOpt = {
+ recursive: true,
+ all: false,
+ directories: false,
+ filter: function filter() {
+ return true;
+ }
+};
+
+// general function
var diveSync = function(dir, opt, action) {
// action is the last argument
action = arguments[arguments.length - 1];
- // default options
- var defaultOpt = {
- recursive: true,
- all: false,
- directories: false
- };
-
// ensure opt is an object
if (typeof opt != 'object')
opt = {};
opt = append(defaultOpt, opt);
- // Read the directory
- var list = fs.readdirSync(dir);
-
- // For every file in the list
- list.forEach(function(file) {
- if (opt.all || file[0] != '.') {
- // Full path of that file
- var path = dir + '/' + file;
- // Get the file's stats
- var stat = fs.statSync(path);
-
- // If the file is a directory
- if (stat && stat.isDirectory()) {
- // Call the action if enabled for directories
- if (opt.directories)
- action(null, path);
+ // apply filter
+ if (!opt.filter(dir, true)) return;
+
+ try {
+ // read the directory
+ var list = fs.readdirSync(dir);
- // Dive into the directory
- if (opt.recursive)
- diveSync(path, opt, action);
- } else {
- // Call the action
- action(null, path);
+ // for every file in the list
+ list.forEach(function (file) {
+ if (opt.all || file[0] != '.') {
+ // full path of that file
+ var path = dir + '/' + file;
+
+ // get the file's stats
+ var stat = fs.statSync(path);
+
+ // if the file is a directory
+ if (stat && stat.isDirectory()) {
+ // call the action if enabled for directories
+ if (opt.directories)
+ action(null, path);
+
+ // dive into the directory
+ if (opt.recursive)
+ diveSync(path, opt, action);
+ } else {
+ // apply filter
+ if (!opt.filter(path, false)) return;
+
+ // call the action
+ action(null, path);
+ }
}
- }
- });
+ });
+ } catch(err) {
+ action(err);
+ }
};
module.exports = diveSync;
View
2 package.json
@@ -4,7 +4,7 @@
"walk through directory trees and apply an action on every file (synchronous dive)",
"author": "Paul Vorbach <paul@vorb.de> (https://vorb.de)",
"contributors": [ "Dave Geddes" ],
- "version": "0.1.1",
+ "version": "0.2.1",
"main": "diveSync.js",
"repository": "git://github.com/pvorb/node-diveSync.git",
"tags": [
View
14 test/test.js
@@ -1,7 +1,15 @@
var diveSync = require("../");
-diveSync(process.cwd(), { directories: true, all: false, recursive: false },
- function(err, file) {
+diveSync(process.cwd(),
+ {
+ directories: false,
+ all: false,
+ recursive: true,
+ filter: function filter(path, dir) {
+// if (dir) return true;
+ return dir || /\.js$/i.test(path);
+ }
+ }, function(err, file) {
if (err) throw err;
- console.log(file);
+ console.log(file);
});

0 comments on commit 9ba1a65

Please sign in to comment.
Something went wrong with that request. Please try again.