1
1
/**
2
- * leopard v0 .0.1
2
+ * leopard v1 .0.0
3
3
* (c) 2018 Ryan Liu
4
4
* @license WTFPL
5
5
*/
@@ -26,14 +26,32 @@ var escapeQuotes = function(str) {
26
26
return str . replace ( / " / g, '\\"' )
27
27
} ;
28
28
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
+
29
47
/**
30
48
* parse the given `tpl` and return Function body string
31
49
*
32
50
* @param {String } tpl
33
51
* @param {Object } data
34
52
* @return {String }
35
53
*/
36
- var parser = function ( tpl , data ) {
54
+ p . parse = function ( tpl , data ) {
37
55
data = data || { } ;
38
56
var delimeterRE = / < % ( .+ ?) % > / g;
39
57
var curMatched = null ;
@@ -59,12 +77,14 @@ var parser = function(tpl, data) {
59
77
var generate = function ( line ) {
60
78
if ( line . length > 0 ) {
61
79
var type = line . charAt ( 0 ) ;
80
+
62
81
switch ( type ) {
82
+ // for interpolations we should check filters
63
83
case '=' :
64
- push ( 'escape(' + line . substr ( 1 ) . trim ( ) + ')' ) ;
84
+ push ( 'escape(' + parseFilters ( line . substr ( 1 ) . trim ( ) ) + ')' ) ;
65
85
break
66
86
case '-' :
67
- push ( line . substr ( 1 ) . trim ( ) ) ;
87
+ push ( parseFilters ( line . substr ( 1 ) . trim ( ) ) ) ;
68
88
break
69
89
default :
70
90
body += line + '\n' ;
@@ -99,14 +119,62 @@ var parser = function(tpl, data) {
99
119
* @param {Object } data
100
120
* @return {String }
101
121
*/
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 ) )
106
127
} ;
107
128
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 ;
110
178
111
179
return src ;
112
180
0 commit comments