-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreckon.js
More file actions
280 lines (242 loc) · 6.39 KB
/
reckon.js
File metadata and controls
280 lines (242 loc) · 6.39 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/** MIT License **/
(function() {
'use strict';
/**
* Reckon settings
* @type {Object}
*/
var settings = {
stringPrototype: true,
delimStart: '{{',
delimEnd: '}}'
};
/**
* Create an instance
* @param {Object} params Configuration and data
*/
var Reckon = function(params) {
return new ReckonInstance(params);
}
/**
* The actual Reckon implementation
* @param {Object} params Configuration and data
*/
var ReckonInstance = function(params) {
/**
* Reckon version
* @type {String}
*/
this.version = '0.1.0';
/**
* Define delimiter that marks beginning of interpolation
* @type {String}
*/
this.delimStart = settings.delimStart;
/**
* Define delimiter that marks end of interpolation
* @type {String}
*/
this.delimEnd = settings.delimEnd;
/**
* Get the text from params
* @type {String}
*/
this.text = typeof params !=="undefined" && params.text !== "undefined" ? params.text : '';
/**
* Get the scope from the params and ensure its an array, if not, make it one
* @type {Array}
*/
this.scopes = typeof params !=="undefined" && typeof params.scope !== "undefined" ? [].concat(params.scope) : [];
/**
* Check if config needs to be applied from the global settings
*/
this.applyConfig();
/**
* Compile the initial input
*/
if (typeof params !=="undefined" && typeof params.text !== "undefined")
this.compile();
};
/**
* The reckon prototype
* @type {Object}
*/
ReckonInstance.prototype = Reckon.fn = {
/**
* The function responsible for interpolation
* @return {Reckon} return self object for chaining
*/
compile: function(param) {
/**
* Reference the instance
*/
var rInstance = this;
if (typeof param !== "undefined") {
rInstance.text = param.text;
rInstance.scopes = [].concat(param.scope);
}
/**
* The required regexp computed using delims in settings
* @type {RegExp}
*/
var re = new RegExp(['{%(.+?)%}|', this.delimStart, '(.+?)', this.delimEnd].join(''), 'g');
/**
* Save the raw text
* @type {String}
*/
rInstance.raw = rInstance.text;
/**
* Compute and assign to compiledText property of the same object
* @param {String} _ Matched string
* @param {String} $1 Content of first match group
* @param {String} $2 Content of second match group
* @return {String} Interpolated string
*/
rInstance.text = rInstance.text.replace(re, function(_, $1, $2) {
var computed;
/**
* If escaped value is found, interpolation will not happen
*/
if ($1) {
computed = $1;
}
/**
* Matched content, let's interpolate
*/
if ($2) {
/**
* Break out scope variables out of scope's box and evaluate the expr expression
*/
var scopeBox = function(expr, localScope) {
var variables = '';
/**
* If scope is a window object, no need to scopebox it
*/
if (typeof window !== "undefined" ? localScope !== window : true) {
for(var i in localScope) {
variables += 'var ' + i + ' = localScope.' + i + '; ';
}
}
var unbox = '(function() { ' + variables + ' try { return eval(expr);} catch(e) {} })()';
return eval(unbox);
};
if (rInstance.scopes.length) {
var numScopes = rInstance.scopes.length;
for (var i=0;i<numScopes;i++) {
/**
* Current Scope
* @type {String}
*/
var scope = rInstance.scopes[i];
/**
* Get the computation
* @type {Any}
*/
computed = scopeBox($2, scope);
/**
* If the computed property is a function, execute it in the context of the current scope
* @type {Unknown}
*/
if (typeof computed === "function") {
computed = computed.call(scope);
}
/**
* If the computed property is an object, get the string
*/
if (typeof computed === "object") {
computed = computed.toString();
}
/**
* Found what we were looking for, now break the loop
*/
if (computed !== undefined)
break;
}
} else {
/**
* If no scope is passed, let us assume the global scope
* @type {Any}
*/
computed = scopeBox($2, (typeof window !== "undefined" ? window : {}));
}
/**
* If no computations were found, we return the raw matched value back
* @type {String}
*/
computed = computed !== undefined ? computed : _;
}
/**
* Final computed value returned
*/
return computed;
});
/**
* Return self for chaining
*/
return rInstance;
},
toString: function() {
return this.text;
},
/**
* Modify in-built settings
* @param {Object} setting Delim settings
* @return {Object} Return self for chaining
*/
applyConfig: function(setting) {
if (typeof setting === "undefined") {
if (typeof window !== "undefined") {
if (typeof window.reckonSettings !== "undefined") {
if (typeof window.reckonSettings.delimStart) {
settings.delimStart = window.reckonSettings.delimStart;
}
if (typeof window.reckonSettings.delimEnd) {
settings.delimEnd = window.reckonSettings.delimEnd;
}
}
}
} else {
if (typeof setting.delimStart) {
settings.delimStart = setting.delimStart;
}
if (typeof setting.delimEnd) {
settings.delimEnd = setting.delimEnd;
}
}
this.delimStart = settings.delimStart;
this.delimEnd = settings.delimEnd;
return this;
}
}
/**
* Add method to string's prototype for easier access
*/
if (settings.stringPrototype === true) {
/**
* Reckon added to string's prototype
* @param {Array} scopes list of scope objects
* @return {String} the reckoned string
*/
String.prototype.reckon = function(scopes) {
return Reckon({text: this, scope: scopes}).toString();
}
}
if (typeof window !== "undefined") {
if (!window.reckon) {
window.reckon = Reckon;
/**
* Read user custom settings
*/
if (typeof window.reckonSettings !== "undefined") {
if (typeof window.reckonSettings.delimStart) {
settings.delimStart = window.reckonSettings.delimStart;
}
if (typeof window.reckonSettings.delimEnd) {
settings.delimEnd = window.reckonSettings.delimEnd;
}
}
}
} else {
module.exports = Reckon;
}
})();