-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
74 lines (59 loc) · 1.72 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var utils = exports;
utils.decorateTypes = function (item) {
var matchType = typeof item.match === 'string' ? 'string' : 'regex';
var replaceType = typeof item.replace;
var replace;
if (replaceType === "string") {
replace = item.replace
}
if (replaceType === "function") {
replace = item.replace || item.fn;
}
var output = {
match: item.match,
replace: replace,
paths: item.paths || [],
matchType: matchType,
replaceType: replaceType,
id: item.id,
active: true
};
return output;
};
utils.decorateInputs = function (item) {
if (item.matchType === 'regex') {
item.matchInput = item.match.source;
var flagnames = ['global', 'multiline', 'ignoreCase'];
var flags = flagnames.filter(function (name) {
return item.match[name];
})
.map(function (item) {
return item.charAt(0);
})
.join('');
item.matchFlags = flags;
}
if (item.matchType === 'string') {
item.matchInput = item.match;
}
if (item.replaceType === 'string') {
item.replaceInput = item.replace;
}
if (item.replaceType === 'function') {
item.replaceInput = item.replace
.toString()
.replace(/(^.*?\{|\}$)/g, "")
.trim();
}
return item;
};
utils.addId = function (item) {
var crypto = require('crypto');
var shasum = crypto.createHash('sha1');
['matchInput', 'matchType', 'replaceInput', 'replaceType']
.forEach(function (key) {
shasum.update(item[key] || "");
});
item.id = shasum.digest('hex');
return item;
};