-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (50 loc) · 1.49 KB
/
index.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
/* jslint node: true */
"use strict";
global.modulesCache = global.modulesCache || {};
if(global.modulesCache['string.format']){
return;
} else {
global.modulesCache['string.format'] = true;
}
require('json.decycled');
String.format = format;
String.prototype.format = function(params){
return format(this.toString(),params);
};
function format(string,params){
var replaced = string;
replaced = replaced.replace(/\·\{(.*?)\}\·/gmi,function(match,capture,index,all){
var replace;
try {
replace = contextEval(params,capture);
replace = JSON.decycled(replace);
if(typeof replace === 'string'){
replace = replace.replace(/(^\"|\"$)/g,'').replace(/(\\n|\\r)/g,'\n').replace(/\\t/g,'\t');
}
return replace;
} catch(error){
return match;
}
});
replaced = replaced.replace(/\{([a-z0-9\$\_]+(?:\.[a-z0-9\-\_]+)*)\}/gmi,function(match,capture,index,all){
var replace;
try {
capture = '["'+capture.split('.').join('"]["')+'"]';
replace = eval('params'+capture);
replace = JSON.decycled(replace);
if(typeof replace === 'string'){
replace = replace.replace(/(^\"|\"$)/g,'').replace(/(\\n|\\r)/g,'\n').replace(/\\t/g,'\t');
}
return replace;
} catch(error){
return match;
}
});
return replaced;
}
function contextEval($__context,$__evaluation){
for(var i=0,k=Object.keys($__context),l=k.length;i<l;i++){
eval("var "+k[i]+" = $__context['"+k[i]+"'];");
}
return eval($__evaluation);
}