Skip to content

Commit

Permalink
add ignore.some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Isao Yagi committed Jan 23, 2013
1 parent 74b0a1e commit 9ae1215
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions index.js
Expand Up @@ -10,25 +10,6 @@ var fs = require('fs'),
Stream = require('stream');


/**
* @param {object} err fs.stat() Error object, or null
* @param {object} stat fs.Stats object, see `man 2 stat`, http://bit.ly/Sb0KRd
* @param {string} item Pathname
* @return {string} Type of filesystem item and name of event emitted
*/
function typer(err, stat) {
'use strict';
var type = 'other';
if (err) {
type = 'error';
} else if (stat.isFile()) {
type = 'file';
} else if (stat.isDirectory()) {
type = 'dir';
}
return type;
}

/**
* @param {object} err fs.stat() Error object, or null
* @param {object} stat fs.Stats object, see `man 2 stat`, http://bit.ly/Sb0KRd
Expand All @@ -40,6 +21,12 @@ function typeSetter(err, stat, pathname) {
// stub for user-provided event category typer
}

function match(str) { // because str.match doesn't work as a bare callback
return function(re) {
return str.match(re);
}
}

/**
* @param {string} item File system path.
* @param {array} list Items remaining to fs.stat().
Expand Down Expand Up @@ -69,8 +56,28 @@ function getStatCb(item, list, self) {
}
}

/**
* @param {object} err fs.stat() Error object, or null
* @param {object} stat fs.Stats object, see `man 2 stat`, http://bit.ly/Sb0KRd
* @param {string} item Pathname
* @return {string} Type of filesystem item and name of event emitted
*/
function typer(err, stat, pathname) {
var type = 'other';
if (err) {
type = 'error';
} else if (self.ignore.length && self.ignore.some(match(pathname))) {
type = 'ignored';
} else if (stat.isFile()) {
type = 'file';
} else if (stat.isDirectory()) {
type = 'dir';
}
return type;
}

return function statCb(err, stat) {
var type = self.typeSetter(err, stat, item) || typer(err, stat);
var type = self.typeSetter(err, stat, item) || typer(err, stat, item);

self.emit(type, {type: type, pathname: item, stat: stat});
self.emit('*', {type: type, pathname: item, stat: stat});
Expand Down Expand Up @@ -107,13 +114,11 @@ function absolutely(list) {

/**
* @constructor
* @param {function} fn Function to allow custom event categories/types.
* @param {mixed}
*/
function Scan(typeSetterFn) {
function Scan(/* strings, regexps, or arrays of such to ignore */) {
this.count = 0;
if ('function' === typeof typeSetterFn) {
this.typeSetter = typeSetterFn;
}
this.ignore = arguments.length ? [].concat(arguments) : [];
}

Scan.prototype = Object.create(Stream.prototype, {
Expand Down

0 comments on commit 9ae1215

Please sign in to comment.