-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (33 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var through = require('through2');
function gulpTrimLines(options) {
var leadingPattern, trailingPattern, patterns, regex;
// ensure the option defaults
options = options || {};
options.leading = options.hasOwnProperty('leading') ? !!options.leading : true;
options.trailing = options.hasOwnProperty('trailing') ? !!options.trailing : true;
options.leadingPattern = options.leadingPattern || '[ \\t]+';
options.trailingPattern = options.trailingPattern || '[ \\t]+';
options.encoding = options.encoding || 'utf8';
// create the regex
if (options.pattern) {
regex = new RegExp(options.pattern, 'gm');
}
else {
patterns = [];
if (options.leading) {
patterns.push('^' + options.leadingPattern);
}
if (options.trailing) {
patterns.push(options.trailingPattern + '$');
}
regex = patterns.length > 0 ? new RegExp(patterns.join('|'), 'gm') : null;
}
return through.obj(function(file, encoding, done) {
if (regex && (file.contents !== null)) {
file.contents = new Buffer(file.contents.toString(options.encoding).replace(regex, ""));
}
this.push(file);
return done();
});
};
module.exports = gulpTrimLines;