-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (26 loc) · 724 Bytes
/
index.js
File metadata and controls
30 lines (26 loc) · 724 Bytes
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
/**
* Created by fuhuixiang on 16-6-12.
*/
'use strict';
const through = require('through2');
module.exports = (replaceObj, replaceStr)=> {
let searchObj = {};
if (typeof replaceObj === 'string') {
searchObj[replaceObj] = replaceStr;
} else {
searchObj = replaceObj;
}
let keys = Object.keys(searchObj),
dataKeys = keys.map((v)=> {
return new RegExp(v, "g");
});
return through.obj(function (file, enc, cb) {
let src = file.contents.toString();
keys.forEach((v, i)=> {
src = src.replace(dataKeys[i], searchObj[v]);
});
file.contents = new Buffer(src);
this.push(file);
cb();
});
};