Skip to content

Commit ed0195b

Browse files
committed
refactor compress attrs
1 parent bb88cab commit ed0195b

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

tasks/compress_attributes.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,46 @@ var through = require('through2');
88

99
// one line string with or without trailing comma
1010
function makeStringRegex(attr) {
11-
return attr + ': \'.*\'' + ',?';
11+
return makeRegex(
12+
attr + ': \'.*\'' + ',?'
13+
);
1214
}
1315

1416
// joined array of strings with or without trailing comma
1517
function makeJoinedArrayRegex(attr) {
16-
return attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + ',?';
18+
return makeRegex(
19+
attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + ',?'
20+
);
1721
}
1822

1923
// array with or without trailing comma
2024
function makeArrayRegex(attr) {
21-
return attr + ': \\[[\\s\\S]*?\\]' + ',?';
25+
return makeRegex(
26+
attr + ': \\[[\\s\\S]*?\\]' + ',?'
27+
);
2228
}
2329

24-
// ref: http://www.regexr.com/3cmac
25-
var regexStr = [
26-
makeStringRegex('description'),
27-
makeJoinedArrayRegex('description'),
28-
makeArrayRegex('requiredOpts'),
29-
makeArrayRegex('otherOpts'),
30-
makeStringRegex('hrName')
31-
].join('|');
32-
33-
var regex = new RegExp(regexStr, 'g');
30+
function makeRegex(regexStr) {
31+
return (
32+
new RegExp(regexStr, 'g')
33+
);
34+
}
3435

3536
module.exports = function() {
3637
var allChunks = [];
3738
return through(function(chunk, enc, next) {
3839
allChunks.push(chunk);
3940
next();
4041
}, function(done) {
41-
this.push(Buffer.concat(allChunks).toString().replace(regex, ''));
42+
var str = Buffer.concat(allChunks).toString('utf-8');
43+
this.push(
44+
str
45+
.replace(makeStringRegex('description'), '')
46+
.replace(makeJoinedArrayRegex('description'), '')
47+
.replace(makeArrayRegex('requiredOpts'), '')
48+
.replace(makeArrayRegex('otherOpts'), '')
49+
.replace(makeStringRegex('hrName'), '')
50+
);
4251
done();
4352
});
4453
};

0 commit comments

Comments
 (0)