Skip to content

Commit e62214d

Browse files
committed
update version
1 parent df830ca commit e62214d

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

Diff for: dist/leopard.js

+78-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* leopard v0.0.1
2+
* leopard v1.0.0
33
* (c) 2018 Ryan Liu
44
* @license WTFPL
55
*/
@@ -26,14 +26,32 @@ var escapeQuotes = function(str) {
2626
return str.replace(/"/g, '\\"')
2727
};
2828

29+
function Leopard() {}
30+
var p = Leopard.prototype;
31+
32+
/**
33+
* check if there is filters in expressions
34+
* expect format:
35+
* - 'name | capitalize | reverse'
36+
* and this will be compile into:
37+
* - 'reverse(capitalize(name))'
38+
*
39+
* @param {String} line
40+
* @return {String}
41+
*/
42+
var parseFilters = function(line) {
43+
var segments = line.split('|');
44+
return segments.reduce((accumulator, f) => f.trim() + '(' + accumulator.trim() + ')')
45+
};
46+
2947
/**
3048
* parse the given `tpl` and return Function body string
3149
*
3250
* @param {String} tpl
3351
* @param {Object} data
3452
* @return {String}
3553
*/
36-
var parser = function(tpl, data) {
54+
p.parse = function(tpl, data) {
3755
data = data || {};
3856
var delimeterRE = /<%(.+?)%>/g;
3957
var curMatched = null;
@@ -59,12 +77,14 @@ var parser = function(tpl, data) {
5977
var generate = function(line) {
6078
if (line.length > 0) {
6179
var type = line.charAt(0);
80+
6281
switch (type) {
82+
// for interpolations we should check filters
6383
case '=':
64-
push('escape(' + line.substr(1).trim() + ')');
84+
push('escape(' + parseFilters(line.substr(1).trim()) + ')');
6585
break
6686
case '-':
67-
push(line.substr(1).trim());
87+
push(parseFilters(line.substr(1).trim()));
6888
break
6989
default:
7090
body += line + '\n';
@@ -99,14 +119,62 @@ var parser = function(tpl, data) {
99119
* @param {Object} data
100120
* @return {String}
101121
*/
102-
var compiler = function(tpl, data) {
103-
var body = parser(tpl, data);
104-
var fun = new Function('escape', body);
105-
return fun.call(this, escape$1)
122+
p.compile = function(tpl, data) {
123+
var body = this.parse(tpl, data);
124+
// 注入过滤器
125+
var fun = new Function('escape', ...Object.keys(p), body);
126+
return fun.call(p, escape$1, ...Object.values(p))
106127
};
107128

108-
var leo = compiler;
109-
var src = leo;
129+
var instance = Leopard;
130+
131+
/**
132+
* util for adding filters to Leopard,
133+
* return Leopard for chaining invoking
134+
*
135+
* @param {String} name
136+
* @param {Function} handler
137+
* @return {Leopard}
138+
*/
139+
function filter(name, handler) {
140+
/* istanbul ignore if */
141+
if (typeof handler !== 'function') {
142+
throw new TypeError(
143+
'Leopard: filter requires a function as handler, but got \"' +
144+
typeof handler + '\" in filter \"' + name + '\"'
145+
)
146+
}
147+
/* istanbul ignore if */
148+
if (name in this.prototype) {
149+
throw new Error('Leopard: filter \"' + name + '\" has been declared')
150+
}
151+
this.prototype[name] = handler;
152+
return this
153+
}
154+
155+
var filter_1 = filter;
156+
157+
var capitalize = function(string) {
158+
return string.charAt(0).toUpperCase() + string.slice(1)
159+
};
160+
161+
var reverse = function(string) {
162+
return string.split('').reverse().join('')
163+
};
164+
165+
var presets = { capitalize, reverse };
166+
167+
// mount `filter` to Leopard as a util
168+
instance.filter = filter_1;
169+
170+
// mount presets to Leopard.prototype so that every instance can use them
171+
var presetFilters = Object.keys(presets);
172+
for (var i = 0, l = presetFilters.length, name; i < l; i++) {
173+
name = presetFilters[i];
174+
instance.filter(name, presets[name]);
175+
}
176+
177+
var src = instance;
110178

111179
return src;
112180

Diff for: dist/leopard.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leopard-template",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"description": "a simple HTML template engine",
55
"main": "./dist/leopard.js",
66
"scripts": {

Diff for: rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default [{
1414
file: './dist/leopard.js',
1515
format: 'umd',
1616
name: 'Leopard',
17-
banner: banner
17+
banner
1818
},
1919
plugins: [cjs()]
2020
}, {
@@ -23,7 +23,7 @@ export default [{
2323
file: './dist/leopard.min.js',
2424
format: 'umd',
2525
name: 'Leopard',
26-
banner: banner
26+
banner
2727
},
2828
plugins: [cjs(), uglify()]
2929
}]

0 commit comments

Comments
 (0)