Skip to content

Commit

Permalink
Update: Refactor implementation of through2
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra authored and phated committed Feb 21, 2017
1 parent f0ecccf commit 15ba101
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,6 @@ var minimatch = require('minimatch');
var glob2base = require('glob2base');
var path = require('path');

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

var isNegative = function(pattern) {
if (typeof pattern !== 'string') return true;
if (pattern[0] === '!') return true;
return false;
};

var isPositive = function(pattern) {
return !isNegative(pattern);
};

var unrelative = function(cwd, glob) {
var mod = '';
if (glob[0] === '!') {
mod = glob[0];
glob = glob.slice(1);
}
return mod+path.resolve(cwd, glob);
};

var gs = {
// creates a stream for a single glob or filter
createStream: function(ourGlob, negatives, opt) {
Expand All @@ -58,18 +33,8 @@ var gs = {
// extract base path from glob
var basePath = opt.base ? opt.base : glob2base(globber);

// needed filtering, check against negatives
function filterNegatives (filename, enc, cb) {
var matcha = isMatch.bind(null, filename, opt);
if (negatives.every(matcha)) {
cb(null, filename); // pass
} else {
cb(); // ignore
}
};

// create stream and map events from globber to it
var stream = through2.obj(negatives.length !== 0 ? filterNegatives : undefined);
var stream = through2.obj(negatives.length ? filterNegatives : undefined);

globber.on('error', stream.emit.bind(stream, 'error'));
globber.on('end', function(/* some args here so can't use bind directly */){
Expand All @@ -84,6 +49,15 @@ var gs = {
});

return stream;

function filterNegatives(filename, enc, cb) {
var matcha = isMatch.bind(null, filename, opt);
if (negatives.every(matcha)) {
cb(null, filename); // pass
} else {
cb(); // ignore
}
}
},

// creates a stream for multiple globs or filters
Expand Down Expand Up @@ -116,4 +90,30 @@ 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);
return true; // unknown glob type?
}

function isNegative(pattern) {
if (typeof pattern !== 'string') return true;
if (pattern[0] === '!') return true;
return false;
}

function isPositive(pattern) {
return !isNegative(pattern);
}

function unrelative(cwd, glob) {
var mod = '';
if (glob[0] === '!') {
mod = glob[0];
glob = glob.slice(1);
}
return mod+path.resolve(cwd, glob);
}


module.exports = gs;

0 comments on commit 15ba101

Please # to comment.