From 15ba10147750d148ff1cf57316f1150416cb4a75 Mon Sep 17 00:00:00 2001 From: contra Date: Mon, 30 Jun 2014 11:46:08 -0700 Subject: [PATCH] Update: Refactor implementation of through2 --- index.js | 72 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 01931ba..ce548fd 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 */){ @@ -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 @@ -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;