-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.js
111 lines (104 loc) · 2.89 KB
/
format.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* 轻量级的模板格式化函数
* eg:
*
* //格式化输出
* format("<h1>#{title}</h1>", {
* title:"标题"
* });
*
* //多个参数
* format("<dl><dt>#{1}</dt><dd>#{2}</dd></dl>", "标题", "描述");
*
* //转义
* format("<h1>#{title|e}</h1>", {
* title:"标题>>"
* });
*
* //函数
* format("<h1>#{getTitle}</h1>", {
* getTitle:function(key){
* return "标题>>";
* }
* });
*/
/**
* format 字符串格式化工具
*
* @param {string} source
* @param {object} opts
* @param {object} configs
* @access public
* @return string 格式化后的字符串
*/
function format(source, opts, config) {
var data = Array.prototype.slice.call(arguments, 1);
var toString = Object.prototype.toString;
// object as config
if (typeof data[1] === 'object') {
data = data.slice(1);
}
config = config || {};
var ld = config.ld || '\\{';
var rd = config.rd || '\\}';
var regex = new RegExp("#" + ld + "(.+?)" + rd, "g");
if (data.length) {
/* ie 下 Object.prototype.toString.call(null) == '[object Object]' */
data = data.length == 1 ? (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data;
return source.replace(regex, function(match, key) {
var filters, replacer, i, len, func;
if (!data) return '';
filters = key.split("|");
key = trim.call(filters[0]);
replacer = data[key];
// chrome 下 typeof /a/ == 'function'
if ('[object Function]' == toString.call(replacer)) {
replacer = replacer(key);
}
for (i = 1, len = filters.length; i < len; ++i) {
func = format.filters[trim.call(filters[i])];
if ('[object Function]' == toString.call(func)) {
replacer = func(replacer, key);
}
}
return (('undefined' == typeof replacer || replacer === null) ? '' : replacer);
});
}
return source;
};
format.filters = {
'escapeJs': function(str) {
if (!str || 'string' != typeof str) return str;
var i, len, charCode, ret = [];
for (i = 0, len = str.length; i < len; ++i) {
charCode = str.charCodeAt(i);
if (charCode > 255) {
ret.push(str.charAt(i));
} else {
ret.push('\\x' + charCode.toString(16));
}
}
return ret.join('');
},
'escapeString': function(str) {
if (!str || 'string' != typeof str) return str;
return str.replace(/["'<>\\\/`]/g, function($0) {
return '&#' + $0.charCodeAt(0) + ';';
});
},
'escapeUrl': function(str) {
if (!str || 'string' != typeof str) return str;
return encodeURIComponent(str);
},
'toInt': function(str) {
return parseInt(str, 10) || 0;
}
};
format.filters.js = format.filters.escapeJs;
format.filters.e = format.filters.escapeString;
format.filters.u = format.filters.escapeUrl;
format.filters.i = format.filters.toInt;
var trim = String.prototype.trim || function() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
module.exports = format;