-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformString.js
27 lines (25 loc) · 928 Bytes
/
transformString.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
var result = '';
var transformString = function (source, transArr, isResult) {
if (isResult) {
result = '';
}
var searchIndex = Number.POSITIVE_INFINITY;
var searchLength = 0;
var searchItem = '';
transArr.map(function (item, index) {
if (source.indexOf('<## ' + item.key + ' ##>') != -1 && source.indexOf('<## ' + item.key + ' ##>') < searchIndex) {
searchIndex = source.indexOf('<## ' + item.key + ' ##>');
searchLength = ('<## ' + item.key + ' ##>').length;
searchItem = item.value;
}
})
if (searchIndex == Number.POSITIVE_INFINITY) {
result = result + source;
return result;
} else {
result = result + source.substring(0, searchIndex) + searchItem;
transformString(source.substring(searchIndex + searchLength), transArr, false);
return result;
}
}
module.exports = transformString;