Skip to content

Commit

Permalink
use Minimatch class to improve perf, fixes #29; fix RegExp handling
Browse files Browse the repository at this point in the history
  • Loading branch information
UltCombo committed Dec 31, 2014
1 parent 54e3163 commit f1ff266
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var Combine = require('ordered-read-streams');
var unique = require('unique-stream');

var glob = require('glob');
var minimatch = require('minimatch');
var Minimatch = require('minimatch').Minimatch;
var glob2base = require('glob2base');
var path = require('path');

Expand All @@ -25,7 +25,7 @@ var gs = {

// remove path relativity to make globs make sense
ourGlob = unrelative(opt.cwd, ourGlob);
negatives = negatives.map(unrelative.bind(null, opt.cwd));
negatives = negatives.map(unrelative.bind(null, opt.cwd)).map(toMatcher.bind(null, opt));

// create globbing stuff
var globber = new glob.Glob(ourGlob, opt);
Expand All @@ -51,7 +51,7 @@ var gs = {
return stream;

function filterNegatives(filename, enc, cb) {
var matcha = isMatch.bind(null, filename, opt);
var matcha = isMatch.bind(null, filename);
if (negatives.every(matcha)) {
cb(null, filename); // pass
} else {
Expand Down Expand Up @@ -99,9 +99,9 @@ var gs = {
}
};

function isMatch(file, opt, pattern) {
if (typeof pattern === 'string') return minimatch(file.path, pattern, opt);
if (pattern instanceof RegExp) return pattern.test(file.path);
function isMatch(file, matcher) {
if (matcher instanceof Minimatch) return matcher.match(file.path);
if (matcher instanceof RegExp) return matcher.test(file.path);
return true; // unknown glob type?
}

Expand All @@ -112,6 +112,7 @@ function isNegative(pattern) {
}

function unrelative(cwd, glob) {
if (typeof glob !== 'string') return glob;
var mod = '';
if (glob[0] === '!') {
mod = glob[0];
Expand All @@ -130,4 +131,9 @@ function toGlob(obj) {
return obj.glob;
}

function toMatcher(opt, glob) {
if (typeof glob === 'string') return new Minimatch(glob, opt);
return glob;
}

module.exports = gs;
18 changes: 18 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,5 +456,23 @@ describe('glob-stream', function() {
});
});

it('should handle RegExps as negative matchers', function(done) {
var stream = gs.create(['./fixtures/stuff/*.dmc', /run/], {cwd: __dirname});
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
should.exist(file.base);
should.exist(file.cwd);
String(file.cwd).should.equal(__dirname);
String(file.base).should.equal(join(__dirname, 'fixtures', 'stuff'+sep));
String(join(file.path,'')).should.equal(join(__dirname, './fixtures/stuff/run.dmc'));
done();
});
});

});
});

0 comments on commit f1ff266

Please sign in to comment.