Skip to content

Commit 15ba101

Browse files
yocontraphated
authored andcommitted
Update: Refactor implementation of through2
1 parent f0ecccf commit 15ba101

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

index.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,6 @@ var minimatch = require('minimatch');
1111
var glob2base = require('glob2base');
1212
var path = require('path');
1313

14-
var isMatch = function(file, opt, pattern) {
15-
if (typeof pattern === 'string') return minimatch(file.path, pattern, opt);
16-
if (pattern instanceof RegExp) return pattern.test(file.path);
17-
return true; // unknown glob type?
18-
};
19-
20-
var isNegative = function(pattern) {
21-
if (typeof pattern !== 'string') return true;
22-
if (pattern[0] === '!') return true;
23-
return false;
24-
};
25-
26-
var isPositive = function(pattern) {
27-
return !isNegative(pattern);
28-
};
29-
30-
var unrelative = function(cwd, glob) {
31-
var mod = '';
32-
if (glob[0] === '!') {
33-
mod = glob[0];
34-
glob = glob.slice(1);
35-
}
36-
return mod+path.resolve(cwd, glob);
37-
};
38-
3914
var gs = {
4015
// creates a stream for a single glob or filter
4116
createStream: function(ourGlob, negatives, opt) {
@@ -58,18 +33,8 @@ var gs = {
5833
// extract base path from glob
5934
var basePath = opt.base ? opt.base : glob2base(globber);
6035

61-
// needed filtering, check against negatives
62-
function filterNegatives (filename, enc, cb) {
63-
var matcha = isMatch.bind(null, filename, opt);
64-
if (negatives.every(matcha)) {
65-
cb(null, filename); // pass
66-
} else {
67-
cb(); // ignore
68-
}
69-
};
70-
7136
// create stream and map events from globber to it
72-
var stream = through2.obj(negatives.length !== 0 ? filterNegatives : undefined);
37+
var stream = through2.obj(negatives.length ? filterNegatives : undefined);
7338

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

8651
return stream;
52+
53+
function filterNegatives(filename, enc, cb) {
54+
var matcha = isMatch.bind(null, filename, opt);
55+
if (negatives.every(matcha)) {
56+
cb(null, filename); // pass
57+
} else {
58+
cb(); // ignore
59+
}
60+
}
8761
},
8862

8963
// creates a stream for multiple globs or filters
@@ -116,4 +90,30 @@ var gs = {
11690
}
11791
};
11892

93+
function isMatch(file, opt, pattern) {
94+
if (typeof pattern === 'string') return minimatch(file.path, pattern, opt);
95+
if (pattern instanceof RegExp) return pattern.test(file.path);
96+
return true; // unknown glob type?
97+
}
98+
99+
function isNegative(pattern) {
100+
if (typeof pattern !== 'string') return true;
101+
if (pattern[0] === '!') return true;
102+
return false;
103+
}
104+
105+
function isPositive(pattern) {
106+
return !isNegative(pattern);
107+
}
108+
109+
function unrelative(cwd, glob) {
110+
var mod = '';
111+
if (glob[0] === '!') {
112+
mod = glob[0];
113+
glob = glob.slice(1);
114+
}
115+
return mod+path.resolve(cwd, glob);
116+
}
117+
118+
119119
module.exports = gs;

0 commit comments

Comments
 (0)