Skip to content

Commit

Permalink
feat(step): add new modify step
Browse files Browse the repository at this point in the history
  • Loading branch information
RWOverdijk committed Oct 10, 2017
1 parent 4706398 commit 5391005
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/step/modify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const replace = require('stream-replace');

module.exports = ({modify} = null, stream) => {
if (!modify) {
return stream;
}

const {patch} = modify;

patch.forEach(({pattern, append, prepend, custom}) => {
stream = stream.pipe(applyModification(pattern, {append, prepend, custom}));
});

return stream;
};

const applyModification = (pattern, {append, prepend, custom}) => {
return replace(pattern, (match, parameter, defaultValue) => {
if (typeof append !== 'undefined') {
return match + append;
}

if (typeof prepend !== 'undefined') {
return prepend + match;
}

if (typeof custom === 'function') {
return custom(match, parameter, defaultValue);
}

return match;
});
};

0 comments on commit 5391005

Please sign in to comment.