Skip to content

Commit

Permalink
refactor compress attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed Jan 20, 2021
1 parent bb88cab commit ed0195b
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions tasks/compress_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,46 @@ var through = require('through2');

// one line string with or without trailing comma
function makeStringRegex(attr) {
return attr + ': \'.*\'' + ',?';
return makeRegex(
attr + ': \'.*\'' + ',?'
);
}

// joined array of strings with or without trailing comma
function makeJoinedArrayRegex(attr) {
return attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + ',?';
return makeRegex(
attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + ',?'
);
}

// array with or without trailing comma
function makeArrayRegex(attr) {
return attr + ': \\[[\\s\\S]*?\\]' + ',?';
return makeRegex(
attr + ': \\[[\\s\\S]*?\\]' + ',?'
);
}

// ref: http://www.regexr.com/3cmac
var regexStr = [
makeStringRegex('description'),
makeJoinedArrayRegex('description'),
makeArrayRegex('requiredOpts'),
makeArrayRegex('otherOpts'),
makeStringRegex('hrName')
].join('|');

var regex = new RegExp(regexStr, 'g');
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) {
this.push(Buffer.concat(allChunks).toString().replace(regex, ''));
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('hrName'), '')
);
done();
});
};

0 comments on commit ed0195b

Please sign in to comment.