-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcompress_attributes.js
56 lines (49 loc) · 1.48 KB
/
compress_attributes.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var through = require('through2');
/**
* Browserify transform that strips meta attributes out
* of the plotly.js bundles
*/
var WHITESPACE_BEFORE = '\\s*';
var OPTIONAL_COMMA = ',?';
// one line string with or without trailing comma
function makeStringRegex(attr) {
return makeRegex(
WHITESPACE_BEFORE + attr + ': \'.*\'' + OPTIONAL_COMMA
);
}
// joined array of strings with or without trailing comma
function makeJoinedArrayRegex(attr) {
return makeRegex(
WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + OPTIONAL_COMMA
);
}
// array with or without trailing comma
function makeArrayRegex(attr) {
return makeRegex(
WHITESPACE_BEFORE + attr + ': \\[[\\s\\S]*?\\]' + OPTIONAL_COMMA
);
}
function makeRegex(regexStr) {
return (
new RegExp(regexStr, 'g')
);
}
module.exports = function() {
var allChunks = [];
return through(function(chunk, enc, next) {
allChunks.push(chunk);
next();
}, function(done) {
var str = Buffer.concat(allChunks).toString('utf-8');
this.push(
str
.replace(makeStringRegex('description'), '')
.replace(makeJoinedArrayRegex('description'), '')
.replace(makeArrayRegex('requiredOpts'), '')
.replace(makeArrayRegex('otherOpts'), '')
.replace(makeStringRegex('role'), '')
.replace(makeStringRegex('hrName'), '')
);
done();
});
};