-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjslt.js
301 lines (246 loc) · 9.5 KB
/
jslt.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
class JSLT {
constructor() {
}
transform(data, template) {
return JSLT.transform(data, template || this.template);
}
setTemplate(template) {
this.template = template;
}
static transform(data, template) {
errorStack = null;
var res = compileTemplate(data, arguments.length == 2 ? template : this.template);
if (errorStack) {
var ex = errorStack.reverse().join(".");
errorStack = null;
throw ex;
}
return res;
}
};
function compileTemplate(data, template) {
function visit(value) {
if (typeof value == "string") {
var res = /^{{([^}]*)}}$/.exec(value);
if (res) return resolveProp(res[1], data);
return value.replace(/{{([^}]+)}}/g, (str, p1) => resolveProp(p1, data));
}
if (value instanceof Array)
return value.map(visit);
if (value instanceof Object && !(value instanceof RegExp)) {
const entries = Object.entries(value);
const ops = entries.filter(e => e[0].startsWith("$"));
if (ops.length) return processUpdate(ops, data);
const obj = {};
for (var i = 0; i < entries.length; ++i) {
const [key, value] = entries[i];
obj[key] = visit(value);
if (errorStack) return error(key);
}
return obj;
}
return value;
}
return visit(template);
}
function resolveProp(name, scope) {
if (scope === undefined || scope === null || !name) return scope;
const name2 = name.replace(/\\(\.|\[|\])/g, (str, p1) => String.fromCodePoint(p1.codePointAt(0) + 0xE000));
const parts = name2.split("."), hasSpecialChars = name != name2;
for (var i = 0; i < parts.length; ++i) {
var reRes = /^([^[]+)(?:\[([^\]]+)\])?$/.exec(parts[i]);
var propName = reRes ? reRes[1] : parts[i];
if (hasSpecialChars)
propName = propName.replace(/[\uE000-\uF000]/g, str => String.fromCodePoint(str.codePointAt(0) - 0xE000));
scope = scope[propName];
if (!scope) return scope;
if (reRes && reRes[2]) {
const bracketName = hasSpecialChars ? reRes[2].replace(/[\uE000-\uF000]/g, s => String.fromCodePoint(s.codePointAt(0) - 0xE000)) : reRes[2];
scope = scope[bracketName];
if (!scope) return scope;
}
}
return scope;
}
function processQuery(query, self, global) {
function checkConds(entries, value) {
return entries.every(([opName, opValue]) => {
const opFunc = QueryOperators[opName];
if (!opFunc) return error(`${opName} - Unknown query operator`);
return opFunc(compileTemplate(global, opValue), value);
});
}
const entries = Object.entries(query);
const ops = entries.filter(e => e[0].startsWith("$"));
if (ops.length) return checkConds(ops, self);
return entries.every(([fieldName, fieldValue]) => checkConds(Object.entries(fieldValue), resolveProp(fieldName, self)));
}
function processUpdate(ops, data) {
var payload = data;
for (var i = 0; i < ops.length; ++i) {
const [opName, opValue] = ops[i];
const opFunc = UpdateOperators[opName] || UpdateOperators[opName.replace(/\d+$/, "")];
if (!opFunc) return error(`${opName} - Unknown operator`);
payload = opFunc(payload, opValue, data);
if (errorStack) return error(opName);
}
return payload;
}
var errorStack = null;
function error(err) {
if (errorStack) errorStack.push(err);
else errorStack = [ err ];
}
const QueryOperators = {
$eq(expected, actual) {
return expected == actual;
},
$ne(expected, actual) {
return expected != actual;
},
$gt(expected, actual) {
return actual > expected;
},
$lt(expected, actual) {
return actual < expected;
},
$gte(expected, actual) {
return actual >= expected;
},
$lte(expected, actual) {
return actual <= expected;
},
$in(expected, actual) {
if (!(expected instanceof Array)) throw "$in: argumment is not an array";
return expected.includes(actual);
},
$nin(expected, actual) {
if (!(expected instanceof Array)) throw "$nin: argumment is not an array";
return !expected.includes(actual);
},
$regex(expected, actual) {
if (expected instanceof RegExp) return expected.test(actual);
if (typeof expected !== "string") throw "$regex: invalid expression";
var re = new RegExp(expected);
return re.test(actual);
},
$type(expected, actual) {
var type = typeof actual;
if (type === "object") {
if (actual === null) type = null;
else if (actual instanceof Array) type = "array";
}
return expected instanceof Array ? expected.includes(type) : type == expected ;
}
};
const UpdateOperators = {
$fetch(input, args, global) {
return compileTemplate(input, args);
},
$translate(input, args, global) {
if (args instanceof Array) {
var obj = args.find(obj => typeof obj.from == "object" && obj.from !== null ?
processQuery(obj.from, input, global) :
(obj.hasOwnProperty("default") || obj.from === input));
return obj ? compileTemplate(global, obj.hasOwnProperty("default") ? obj.default : obj.to) : input;
}
if (!args) return error(" - Missing arguments");
if (!(args.from instanceof Array)) return error(`[from] - Expected an array, but received ${typeof args.from}`);
if (!(args.to instanceof Array)) return error(`[to] - Expected an array, but received ${typeof args.to}`);
var idx = args.from.indexOf(input);
return idx != -1 ? compileTemplate(global, args.to[idx]) : input;
},
$join(input, args, global) {
return args.map(item => compileTemplate(global, item)).join("");
},
$concat(input, args, global) {
return [].concat(...args.map(item => compileTemplate(global, item)));
},
$formatDate(input, args, global) {
return (new Date(input)).toLocaleString(compileTemplate(global, args && args.locales), compileTemplate(global, args && args.options));
},
$formatNumber(input, args, global) {
return Number(input).toLocaleString(compileTemplate(global, args && args.locales), compileTemplate(global, args && args.options));
},
$parseNumber(input, args, global) {
input = String(input).replace(new RegExp(`\\s|[${(args && args.groupSymbol) || ","}]`, "g"), "");
if (args && args.decimalSymbol) input.replace(new RegExp(`[${args.decimalSymbol}]`, "g"), ".");
return Number(input);
},
$parseDate(input, args, global) {
var tzRes = /^([\-\+])?(\d\d):?(\d\d)$/.exec(compileTemplate(global, args.timezone));
if (!tzRes) return error(`[timezone] - invalid timezone: ${args.timezone}`);
var tzOffset = (Number(tzRes[2]) * 60 + Number(tzRes[3])) * (tzRes[1] == "-" ? -1 : 1);
var format, day, month, year;
if (format = /^dd[\/.\- ]?MM[\/.\- ]?yyyy$/.exec(args.format)) {
[, day, month, year] = /^(\d{1,2})[\/.\- ]?(\d{1,2})[\/.\- ]?(\d{2,4})$/.exec(input) || [];
} else if (format = /^MM[\/.\- ]?dd[\/.\- ]?yyyy$/.exec(args.format)) {
[, month, day, year] = /^(\d{1,2})[\/.\- ]?(\d{1,2})[\/.\- ]?(\d{2,4})$/.exec(input) || [];
} else if (format = /^yyyy[\/.\- ]?MM[\/.\- ]?dd$/.exec(args.format)) {
[, year, month, day] = /^(\d{2,4})[\/.\- ]?(\d{1,2})[\/.\- ]?(\d{1,2})$/.exec(input) || [];
} else {
return error(`[format] - unsupported format: ${args.format}`);
}
day = Number(day), month = Number(month), year = Number(year);
if (day >= 1 && day <= 31 && month >= 1 && month <= 12 && year >= 0)
return new Date(Date.UTC(year, month - 1, day) - (tzOffset * 60000));
return error(`[input] - invalid date: ${input}`);
},
$replace(input, args, global) {
if (!(typeof args.newSubstr == "string")) return error(`[newSubstr] - missing / invalid`);
var firstArg;
if (typeof args.substr == "string") firstArg = args.substr
else if (args.regexp instanceof RegExp) firstArg = args.regexp;
else if (typeof args.regexp == "string") {
try { firstArg = new RegExp(args.regexp, "g");
} catch(ex) { return error(`[regexp] - invalid expression`); }
} else return error(`[substr/regexp] - missing or invalid`);
return String(input).replace(firstArg, args.newSubstr);
},
// Array
$map(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
var newGlobal = Object.create(global), retVal = [];
for (var i = 0; i < input.length; ++i) {
newGlobal.this = input[i];
retVal.push(compileTemplate(newGlobal, args));
if (errorStack) return error(`[${i}]`);
}
return retVal;
},
$filter(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
var newGlobal = Object.create(global), retVal = [];
for (var i = 0; i < input.length; ++i) {
newGlobal.this = input[i];
if (processQuery(args, input[i], newGlobal)) retVal.push(input[i]);
if (errorStack) return error(`[${i}]`);
}
return retVal;
},
$push(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
return input.concat(compileTemplate(global, args));
},
$unshift(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
return [ compileTemplate(global, args) ].concat(input);
},
$reverse(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
return [].concat(input).reverse();
},
$find(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
var newGlobal = Object.create(global);
return input.find(item => {
newGlobal.this = item;
return processQuery(args, item, newGlobal);
});
},
$sum(input, args, global) {
if (!(input instanceof Array)) return error(`[input] - Expected an array, but received ${typeof input}`);
return input.reduce((sum, cur) => sum + Number(cur), 0);
}
};
module.exports = JSLT;