-
Notifications
You must be signed in to change notification settings - Fork 0
/
vancat.js
415 lines (412 loc) · 18 KB
/
vancat.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory())
: typeof define === 'function' && define.amd
? define(factory)
: ((global = global || self), (global.Vancat = factory()));
})(this, function () {
'use strict';
/*!
* vancat.js - Fast and small templating engine
* https://github.com/serhat359/vancat-js
*/
var Vancat = (function () {
const registeredHelpers = {
not: (x) => !x,
};
const registeredPartials = {};
const registerHelper = (name, f) => (registeredHelpers[name] = f);
const registerPartial = (name, template) =>
(registeredPartials[name] = compileToStatements(template));
const compile = (template) => {
const statements = compileToStatements(template);
return (data, helpers = {}) => {
const parts = [];
const writer = (x) => parts.push(x);
const context = {
d: { $: data }, // Data of context
g(key) {
// Get variable result
return this.d[key] ?? helpers[key] ?? registeredHelpers[key] ?? this.d.$[key];
},
s(name, val) {
// Set variable
this.d[name] = val;
},
gd(name) {
// Get direct
return this.d[name];
},
r(data) {
// Replace variable
const old = this.d;
this.d = { $: data };
return old;
},
sd(data) {
// Set the entire context data
this.d = data;
},
};
runStatements(writer, context, statements);
return parts.join('');
};
};
const compileToStatements = (template) => {
const statements = [];
let end = 0;
let statement;
while (end < template.length) {
[statement, end] = getStatement(template, end);
if (statement === null) err('Unexpected end token');
if (statement !== undefined) statements.push(statement);
}
return statements;
};
const getStatement = (template, start) => {
if (start == template.length) err('Expected {{end}} but not found');
const i = template.indexOf('{{', start);
if (i == start) {
if (template[i + 2] === '/' && template[i + 3] === '*') {
// handle comment
const commentEnd = template.indexOf('*/}}', i + 4);
return [undefined, commentEnd + 4]; // Return undefined for comment
}
if (template[i + 2] === '>') {
// handle partial here
const [tokens, end] = getTokens(template, i + 3);
const templateName = tokens[0];
if (!templateName) err('Template name not specified');
const expr = getExpression(tokens, 1);
const statement = (writer, context) => {
const partialStatements = registeredPartials[templateName];
if (!partialStatements) err(`Partial not registered: ${templateName}`);
const newData = expr(context);
const oldData = context.r(newData);
runStatements(writer, context, partialStatements);
context.sd(oldData);
};
return [statement, end];
}
let [tokens, end] = getTokens(template, i + 2);
const first = tokens[0];
if (first === 'for') {
let loopType = tokens[2];
if (loopType !== 'in') {
const inIndex = tokens.indexOf('in');
if (inIndex > 2) {
tokens = mergeTokens(tokens, inIndex);
loopType = tokens[2];
} else err('Missing "in" in for-loop');
}
const [t1, t2] = tokens[1].split(',');
const loopValuesExpr = getExpression(tokens, 3);
let statements;
[statements, end] = getInnerStatements(template, end);
const forStatement = (writer, context) => {
const loopValues = loopValuesExpr(context);
if (loopValues == null)
err(`Value of '${tokens.slice(3).join(' ')}' was not iterable`);
const t1Old = context.gd(t1);
const t2Old = context.gd(t2);
if (isIterable(loopValues)) {
let i = 0;
for (const val of loopValues) {
context.s(t1, val);
if (t2) context.s(t2, i);
runStatements(writer, context, statements);
i++;
}
} else {
for (const key in loopValues) {
context.s(t2, key);
context.s(t1, loopValues[key]);
runStatements(writer, context, statements);
}
}
context.s(t1, t1Old);
context.s(t2, t2Old);
};
return [forStatement, end];
} else if (first === 'if') {
const ifExpr = getExpression(tokens, 1);
let ifStatements = [];
let elseStatements = [];
let elseIfGroups = [];
let nextType;
let statement;
while (true) {
nextType = getNextStatementType(template, end);
if (nextType === 'else') {
break;
}
if (nextType === 'end') {
[statement, end] = getStatement(template, end); // Read {{end}}
const ifStatement = (writer, context) => {
if (ifExpr(context)) runStatements(writer, context, ifStatements);
};
return [ifStatement, end];
} else {
[statement, end] = getStatement(template, end);
ifStatements.push(statement);
}
}
const createIfStatement = () => (writer, context) => {
if (ifExpr(context)) {
runStatements(writer, context, ifStatements);
return;
}
for (const [expr, statements] of elseIfGroups) {
if (expr(context)) {
runStatements(writer, context, statements);
return;
}
}
runStatements(writer, context, elseStatements);
};
// Has else here as the next type
while (true) {
[tokens, end] = getTokens(template, end + 2);
if (tokens.length == 1) {
while (true) {
[statement, end] = getStatement(template, end);
if (!statement) break;
elseStatements.push(statement);
}
const ifStatement = createIfStatement();
return [ifStatement, end];
}
// Else-if here
if (tokens[1] !== 'if') err('if missing from else-if statement');
let elseIfExpr = getExpression(tokens, 2);
let elseIfStatements = [];
while (true) {
nextType = getNextStatementType(template, end);
if (nextType === 'else') {
elseIfGroups.push([elseIfExpr, elseIfStatements]);
break;
}
if (nextType == null) {
[statement, end] = getStatement(template, end);
elseIfStatements.push(statement);
continue;
} else if (nextType === 'end') {
elseIfGroups.push([elseIfExpr, elseIfStatements]);
[statement, end] = getStatement(template, end); // Read {{end}}
const ifStatement = createIfStatement();
return [ifStatement, end];
}
err('Tag not closed with }}');
}
}
} else if (first === 'end') {
return [null, end]; // Return null for {{end}}
} else if (first === 'else') {
err('Unexpected else token');
} else if (first === 'set') {
const varName = tokens[1];
const expr = getExpression(tokens, 2);
const statement = (writer, context) => context.s(varName, expr(context));
return [statement, end];
}
// Get expression as statement
const expr = getExpression(tokens, 0);
const stmt = (writer, context) => writer(htmlEncode(expr(context)));
return [stmt, end];
} else if (i < 0) {
return [template.substring(start), template.length];
} else {
return [template.substring(start, i), i];
}
};
const getInnerStatements = (template, end) => {
const statements = [];
let statement;
while (true) {
[statement, end] = getStatement(template, end);
if (statement === null) break;
if (statement !== undefined) statements.push(statement);
}
return [statements, end];
};
const getNextStatementType = (template, start) => {
if (start + 1 < template.length && template[start] === '{' && template[start + 1] === '{') {
start += 2;
while (template[start] === ' ') start++;
const tempStart = start++;
while (start < template.length && template[start] !== '}' && template[start] !== ' ')
start++;
return template.substring(tempStart, start);
}
return null;
};
const getTokens = (template, i) => {
const tokens = [];
while (true) {
while (template[i] === ' ') i++;
if (template[i] === '}' && template[i + 1] === '}') return [tokens, i + 2];
if (template[i] === '|') {
tokens.push('|');
i++;
continue;
}
const start = i++;
while (
i < template.length &&
template[i] !== '}' &&
template[i] !== ' ' &&
template[i] !== '|'
) {
i++;
}
const token = template.substring(start, i);
if (!token) err('Tag not closed with }}');
tokens.push(token);
}
};
const getExpression = (tokens, start) => {
if (tokens.length - start == 0 || tokens[start] === '.') {
if (tokens.length == 0) err('Expression cannot be empty');
else err(`Expression expected after: ${tokens.slice(0, start).join(' ')}`);
}
const pipeIndex = tokens.findIndex((x, i) => i >= start && x === '|');
const end = pipeIndex >= 0 ? pipeIndex : tokens.length;
let firstExpr;
if (end - start == 1) {
firstExpr = getTokenAsExpression(tokens[start]);
} else {
const f = tokens[start];
assertFunction(f);
const argGroups = getArgGroups(tokens, start + 1);
if (argGroups.length == 1) {
const expr = argGroups[0];
return (context) => {
const func = getFunc(f, context);
return func(expr(context));
};
}
firstExpr = (context) => {
const func = getFunc(f, context);
const args = argGroups.map((expr) => expr(context));
return func.apply(null, args);
};
}
if (pipeIndex < 0) return firstExpr;
const pipeExprs = [];
let end2 = pipeIndex;
let pipeTokens;
while (end2 != tokens.length) {
[pipeTokens, end2] = getPipeTokens(tokens, end2 + 1);
const [f, ...argTokens] = pipeTokens;
assertFunction(f);
if (argTokens.length > 0) {
const argExprs = argTokens.map(getTokenAsExpression);
pipeExprs.push((lastValue, context) => {
const func = getFunc(f, context);
const args = argExprs.map((expr) => expr(context));
return func(lastValue, ...args);
});
} else {
pipeExprs.push((lastValue, context) => {
const func = getFunc(f, context);
return func(lastValue);
});
}
}
return (context) => {
let lastValue = firstExpr(context);
for (const pipeExpr of pipeExprs) {
lastValue = pipeExpr(lastValue, context);
}
return lastValue;
};
};
const getTokenAsExpression = (token) => {
const expr = getTokenAsExpressionInner(token);
return (context) => {
try {
return expr(context);
} catch (e) {
err(`Error while resolving: ${token}`);
}
};
};
const getTokenAsExpressionInner = (token) => {
const parsedNumber = Number(token);
if (!isNaN(parsedNumber)) return (context) => parsedNumber;
const subTokens = token.split('.');
for (const x of subTokens) {
if (!x) err(`Invalid member access expression: ${token}`);
}
const t = subTokens[0];
if (subTokens.length == 1) {
return (context) => context.g(t);
}
if (subTokens.length == 2) {
const k = subTokens[1];
return (context) => context.g(t)[k];
}
if (subTokens.length == 3) {
const k1 = subTokens[1];
const k2 = subTokens[2];
return (context) => context.g(t)[k1][k2];
}
return (context) => {
let o = context.g(t);
for (let i = 1; i < subTokens.length; i++) o = o[subTokens[i]];
return o;
};
};
const getArgGroups = (tokens, index) => {
const expressions = [];
while (index < tokens.length) expressions.push(getTokenAsExpression(tokens[index++]));
return expressions;
};
const getPipeTokens = (tokens, i) => {
const tok = [];
for (; i < tokens.length; i++) {
if (tokens[i] === '|') return [tok, i];
tok.push(tokens[i]);
}
return [tok, i];
};
const getFunc = (f, context) => {
const func = context.g(f);
if (typeof func !== 'function') err(`value of ${f} was not a function`);
return func;
};
const mergeTokens = (tokens, inIndex) => [
tokens[0],
tokens.slice(1, inIndex).join(''),
...tokens.slice(inIndex),
];
const runStatements = (writer, context, statements) => {
for (const stmt of statements)
if (typeof stmt === 'string') writer(stmt);
else stmt(writer, context);
};
const htmlEncode = (s) => {
s = String(s ?? '');
return /[&<>\'\"]/.test(s)
? s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"')
: s;
};
const isIterable = (obj) => {
if (obj == null) return false;
return typeof obj[Symbol.iterator] === 'function';
};
const assertFunction = (f) => {
if (f.includes('.')) err(`Function name cannot contain a dot character: ${f}`);
};
const err = (msg) => {
throw new Error(msg);
};
return { compile, registerHelper, registerPartial };
})();
return Vancat;
});