├── .gitignore
├── LICENSE
├── README.md
├── component.json
└── format.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .idea
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # format
2 | 轻量级的模板格式化函数
3 |
--------------------------------------------------------------------------------
/component.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "format",
3 | "version": "0.0.2",
4 | "main": "format.js",
5 | "description": "轻量级的模板格式化函数"
6 | }
--------------------------------------------------------------------------------
/format.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 轻量级的模板格式化函数
3 | * eg:
4 | *
5 | * //格式化输出
6 | * format("
#{title}
", {
7 | * title:"标题"
8 | * });
9 | *
10 | * //多个参数
11 | * format("- #{1}
- #{2}
", "标题", "描述");
12 | *
13 | * //转义
14 | * format("#{title|e}
", {
15 | * title:"标题>>"
16 | * });
17 | *
18 | * //函数
19 | * format("#{getTitle}
", {
20 | * getTitle:function(key){
21 | * return "标题>>";
22 | * }
23 | * });
24 | */
25 |
26 | /**
27 | * format 字符串格式化工具
28 | *
29 | * @param {string} source
30 | * @param {object} opts
31 | * @param {object} configs
32 | * @access public
33 | * @return string 格式化后的字符串
34 | */
35 | function format(source, opts, config) {
36 | var data = Array.prototype.slice.call(arguments, 1);
37 | var toString = Object.prototype.toString;
38 |
39 | // object as config
40 | if (typeof data[1] === 'object') {
41 | data = data.slice(1);
42 | }
43 | config = config || {};
44 | var ld = config.ld || '\\{';
45 | var rd = config.rd || '\\}';
46 | var regex = new RegExp("#" + ld + "(.+?)" + rd, "g");
47 |
48 | if (data.length) {
49 | /* ie 下 Object.prototype.toString.call(null) == '[object Object]' */
50 | data = data.length == 1 ? (opts !== null && (/\[object Array\]|\[object Object\]/.test(toString.call(opts))) ? opts : data) : data;
51 | return source.replace(regex, function(match, key) {
52 | var filters, replacer, i, len, func;
53 | if (!data) return '';
54 | filters = key.split("|");
55 | key = trim.call(filters[0]);
56 | replacer = data[key];
57 | // chrome 下 typeof /a/ == 'function'
58 | if ('[object Function]' == toString.call(replacer)) {
59 | replacer = replacer(key);
60 | }
61 | for (i = 1, len = filters.length; i < len; ++i) {
62 | func = format.filters[trim.call(filters[i])];
63 | if ('[object Function]' == toString.call(func)) {
64 | replacer = func(replacer, key);
65 | }
66 | }
67 | return (('undefined' == typeof replacer || replacer === null) ? '' : replacer);
68 | });
69 | }
70 | return source;
71 | };
72 |
73 | format.filters = {
74 | 'escapeJs': function(str) {
75 | if (!str || 'string' != typeof str) return str;
76 | var i, len, charCode, ret = [];
77 | for (i = 0, len = str.length; i < len; ++i) {
78 | charCode = str.charCodeAt(i);
79 | if (charCode > 255) {
80 | ret.push(str.charAt(i));
81 | } else {
82 | ret.push('\\x' + charCode.toString(16));
83 | }
84 | }
85 | return ret.join('');
86 | },
87 | 'escapeString': function(str) {
88 | if (!str || 'string' != typeof str) return str;
89 | return str.replace(/["'<>\\\/`]/g, function($0) {
90 | return '' + $0.charCodeAt(0) + ';';
91 | });
92 | },
93 | 'escapeUrl': function(str) {
94 | if (!str || 'string' != typeof str) return str;
95 | return encodeURIComponent(str);
96 | },
97 | 'toInt': function(str) {
98 | return parseInt(str, 10) || 0;
99 | }
100 | };
101 |
102 | format.filters.js = format.filters.escapeJs;
103 | format.filters.e = format.filters.escapeString;
104 | format.filters.u = format.filters.escapeUrl;
105 | format.filters.i = format.filters.toInt;
106 |
107 | var trim = String.prototype.trim || function() {
108 | return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
109 | };
110 |
111 | module.exports = format;
112 |
--------------------------------------------------------------------------------