-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
232 lines (200 loc) · 4.39 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var Expression = require('expression');
var parse = require('format-parser');
var unique = require('uniq');
var debug = require('debug')('ripplejs/interpolate');
/**
* Run a value through all filters
*
* @param {Mixed} val Any value returned from an expression
* @param {Array} types The filters eg. currency | float | floor
* @param {Object} fns Mapping of filter names, eg. currency, to functions
* @return {Mixed}
*/
function filter(val, types, fns) {
fns = fns || {};
var filters = parse(types.join('|'));
filters.forEach(function(f){
var name = f.name.trim();
var fn = fns[name];
var args = f.args.slice();
args.unshift(val);
if(!fn) throw new Error('Missing filter named "' + name + '"');
val = fn.apply(null, args);
});
return val;
}
/**
* Create a new interpolator
*/
function Interpolate() {
this.match = /\{\{([^}]+)\}\}/g;
this.filters = {};
}
/**
* Hook for plugins
*
* @param {Function} fn
*
* @return {Interpolate}
*/
Interpolate.prototype.use = function(fn) {
fn(this);
return this;
};
/**
* Set the delimiters
*
* @param {Regex} match
*
* @return {Interpolate}
*/
Interpolate.prototype.delimiters = function(match) {
this.match = match;
return this;
};
/**
* Check if a string matches the delimiters
*
* @param {String} input
*
* @return {Array}
*/
Interpolate.prototype.matches = function(input) {
var test = new RegExp(this.match.source);
var matches = test.exec(input);
if(!matches) return [];
return matches;
};
/**
* Add a new filter
*
* @param {String} name
* @param {Function} fn
*
* @return {Interpolate}
*/
Interpolate.prototype.filter = function(name, fn){
this.filters[name] = fn;
return this;
};
/**
* Interpolate a string using the contents
* inside of the delimiters
*
* @param {String} input
* @param {Object} options
* @return {String}
*/
Interpolate.prototype.exec = function(input, options){
options = options || {};
var parts = input.split('|');
var expr = parts.shift();
var fn = new Expression(expr);
var val;
try {
val = fn.exec(options.scope, options.context);
}
catch (e) {
debug(e.message);
}
if(parts.length) {
val = filter(val, parts, options.filters || this.filters);
}
return val;
};
/**
* Check if a string has interpolation
*
* @param {String} input
*
* @return {Boolean}
*/
Interpolate.prototype.has = function(input) {
return input.search(this.match) > -1;
};
/**
* Interpolate as a string and replace each
* match with the interpolated value
*
* @return {String}
*/
Interpolate.prototype.replace = function(input, options){
var self = this;
return input.replace(this.match, function(_, match){
var val = self.exec(match, options);
return (val == null) ? '' : val;
});
};
/**
* Get the interpolated value from a string
*/
Interpolate.prototype.value = function(input, options){
var matches = this.matches(input);
if( matches.length === 0 ) return input;
if( matches[0].trim().length !== input.trim().length ) return this.replace(input, options);
return this.exec(matches[1], options);
};
/**
* Get all the interpolated values from a string
*
* @return {Array} Array of values
*/
Interpolate.prototype.values = function(input, options){
var self = this;
return this.map(input, function(match){
return self.value(match, options);
});
};
/**
* Find all the properties used in all expressions in a string
* @param {String} str
* @return {Array}
*/
Interpolate.prototype.props = function(str) {
var arr = [];
this.each(str, function(match, expr, filters){
var fn = new Expression(expr);
arr = arr.concat(fn.props);
});
return unique(arr);
};
/**
* Loop through each matched expression in a string
*
* @param {String} str
*
* @return {void}
*/
Interpolate.prototype.each = function(str, callback) {
var m;
var index = 0;
var re = this.match;
while (m = re.exec(str)) {
var parts = m[1].split('|');
var expr = parts.shift();
var filters = parts.join('|');
callback(m[0], expr, filters, index);
index++;
}
};
/**
* Map the string
*
* @param {String} str
* @param {Function} callback
*
* @return {Array}
*/
Interpolate.prototype.map = function(str, callback) {
var ret = [];
this.each(str, function(){
ret.push(callback.apply(null, arguments));
});
return ret;
};
/**
* Export the constructor
*
* @type {Function}
*/
module.exports = Interpolate;