-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
593 lines (567 loc) · 19.4 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
'use strict';
/**
* Module dependenices
*/
const clone = require('@logoran/clone-deep');
const isPlainObject = require('is-plain-object');
const debug = require('debug')('object-templates');
// Argument name can't begin with '__ot_', because it maybe conflict which inner object name.
// Expansion object must have type and stub property. If a normal object has that two property, it should set isSimple = true.
// IF normal object has type stub and isSimple, the original isSimple change to '$', and other property name '$$$$' to '$''$$$$'.
// IF key name match the ${**} format, and don't want to replace, set '#' behand the key.
// IF key name match the ${**} format, and begin with '#' or '@', should add '@' behand the key.
function match(value) {
return value.search(/\$\{.*?\}/) !== -1 && value[0] != '#';
}
function escape(value, matched) {
if (matched) {
return (value[0] === '#' || value[0] === '@') ? `@${value}` : value;
}
return `#${value}`;
}
function reverseEscape(value, matched) {
if (matched) {
return value[0] === '@' ? value.substr(1) : value;
}
return value.search(/\$\{.*?\}/) !== -1 ? value.substr(1) : value;
}
function getItemName(value) {
if (typeof value === 'string') {
let itemname = value.match(/[\w-]+?$/);
if (itemname) {
return [value, itemname, 'seq'];
}
itemname = value.match(/\[\'[\w-]+?\'\]$/);
return [value, itemname.substr(2, itemname.length - 4), 'seq'];
} else {
return [value.name, value.item, value.seq || 'seq'];
}
}
function getKeyName(value) {
if (typeof value === 'string') {
const itemname = value.match(/[\w-]+?$/);
if (itemname) {
return [value, itemname, 'key'];
}
itemname = value.match(/\[\'[\w-]+?\'\]$/);
return [value, itemname.substr(2, itemname.length - 4), 'key'];
} else {
return [value.name, value.item, value.key || 'key'];
}
}
function isExpansion(value) {
if (value.type && value.stub) {
if (!value.isSimple) {
return true;
} else {
delete value.isSimple;
if (value['$'] !== undefined) {
value.isSimple = value['$'];
delete value['$'];
}
const __new = {};
for (let k in value) {
if (k.split('$').length === k.length + 1) {
__new[k.substr(1)] = value[k];
delete value[k];
}
}
Object.assign(value, __new);
}
return false;
}
}
function goodExpansion(value) {
const stub = value.stub;
const type = value.type;
if (type !== 'object' && type !== 'array') {
return false;
}
if (typeof stub === 'string') {
return true;
}
if (!(stub instanceof Array)) {
for (let key in stub) {
let one = stub[key].stub;
if (typeof one === 'string') {
continue;
}
if (!(one instanceof Array)) {
return false;
}
for (let s of one) {
if (typeof s !== 'string' && (!s.name || typeof s.name !== 'string' || !s.item || typeof s.item !== 'string')) {
return false;
}
}
}
return true;
}
for (let s of stub) {
if (typeof s !== 'string' && (!s.name || typeof s.name !== 'string' || !s.item || typeof s.item !== 'string')) {
return false;
}
}
return true;
}
function regSymbolEncode(value) {
return value.replace(/([\[\'\]\(\)])/g, '\\$1');
}
function objectTemplates(template, options) {
// the constants during all the compile and replace operating time.
const constants = options.constants || {};
// the argument for replace.
const _arguments = options._arguments || [];
// helper functions in replace.
const helpers = options.helpers || {};
// object type will be cloned which is not plain object.
const cloneInstances = options.cloneInstances || [];
// alias for replace.
const alias = options.alias || [];
// if allow undefined in object after replace result.
const allowUndefined = options.allowUndefined;
let statement = '';
debug('_arguments = %o, cloneInstances = %o, alias = %o, allowUndefined = %o', _arguments, cloneInstances, alias, !!allowUndefined);
function instanceClone(value) {
const constructor = value.constructor;
if (cloneInstances.includes(constructor)) {
const res = new constructor();
for (const key in value) {
res[key] = clone(value[key], instanceClone);
}
return res;
} else {
return clone(value);
}
}
function getConstAlias() {
let alias = [];
for (let key in constants) {
alias.push([key, `__ot_constants.${key}`]);
}
for (let key in helpers) {
alias.push([`${key}\\s*?(`, `__ot_helpers.${key}(`]);
}
return alias;
}
function getStatement(value, path = '', data = undefined, innerAlias = getConstAlias()) {
let statement;
// debug('getStatement %s before innerAlias %o', value, innerAlias);
for (let [a, v] of innerAlias) {
let r = RegExp('\\$\\{(.*?)' + regSymbolEncode(a) + '(.*?)\\}', 'g');
value = value.replace(r, '${$1' + v + '$2}');
}
// debug('getStatement %s before alias', value);
for (let [a, v] of alias) {
let r = RegExp('\\$\\{(.*?)' + regSymbolEncode(a) + '(.*?)\\}', 'g');
value = value.replace(r, '${$1' + v + '$2}');
}
// debug('getStatement %s before end replace', value);
value = value.replace(/\$\{(.*?)\}/g, '$1');
// debug('getStatement %s after', value);
try {
eval(`statement = ${value}`);
debug('value %s get result %o', value, statement);
return [false, statement];
} catch (e) {
if (data === undefined) {
statement = `return (${value});`;
} else if (data === false) {
statement = value;
} else if (data instanceof Array || allowUndefined) {
statement = `__ot_result${path} = (${value});`;
} else {
statement =
`__ot_temp = (${value});
if (__ot_temp !== undefined) {
__ot_result${path} = __ot_temp;
} else {
delete __ot_result${path};
}`;
}
debug('value %s get statement %s', value, statement);
return [true, statement];
}
}
function replaceFixed(data, innerAlias, path = '') {
if (data.value instanceof Array || isPlainObject(data.value)) {
statement += `
{
const __ot_result = __ot_fixed${path}.value;`;
replaceObject(data.value, innerAlias);
statement += `
__ot_items.splice(${data.index}, 0, __ot_result);
}`;
} else if ('string' === typeof data.value && match(data.value)) {
const [_ok, __statement] = getStatement(data.value, '', false, innerAlias);
if (_ok) {
statement += `
__ot_items.splice(${data.index}, 0, ${__statement});`;
// debug('replaceObject %o to statement %s', data, statement);
} else {
data.value = __statement;
statement += `
__ot_items.splice(${data.index}, 0, __ot_fixed${path}.value);`;
// debug('replaceObject %o to value %o', data, _statement);
}
} else {
statement += `
__ot_items.splice(${data.index}, 0, __ot_fixed${path}.value);`;
}
}
function hasCountExpand(data) {
if (data.count) {
return true;
}
let stubs = data.stub;
if (typeof stubs !== 'object') {
return false;
}
for (let key in stubs) {
if (stubs[key].count) {
return true;
}
}
return false;
}
function expandArray(data, name, innerAlias, countAlias, path = '') {
const __alias = [];
let endsm = '';
const stubs = (typeof data.stub === 'string') ? [data.stub] : data.stub;
if (path === '') {
statement += `
const __ot_data = __ot_temp.value;`;
} else {
statement += `
__ot_data = __ot_stub['${regSymbolEncode(path)}'].value;`;
}
if (data.count) {
statement += `
__ot_count = 0;`;
}
for (let i in stubs) {
const stub = stubs[i];
const [stubname, itemname, seq] = getItemName(stub);
statement += `
const __ot_src_${path}${i} = ${stubname};
for (let __ot_${itemname}_seq in __ot_src_${path}${i}) {
let ${itemname} = __ot_src_${path}${i}[__ot_${itemname}_seq];`;
__alias.push([`${itemname}.${seq}`, `__ot_${itemname}_seq`], [`${itemname}['${seq}']`, `__ot_${itemname}_seq`]);
endsm += '}';
}
if (data.value instanceof Array || typeof data.value === 'object') {
statement += `
const __ot_result = this.clone(__ot_data);`
replaceObject(data.value, innerAlias.concat(__alias));
} else if ('string' === typeof data.value && match(data.value)) {
const [ok, _statement] = getStatement(data.value, '', [], innerAlias.concat(__alias));
if (ok) {
statement += `let __ot_result;
${_statement}`;
} else {
value['value'] = _statement;
statement += `const __ot_result = __ot_data;`;
}
} else {
statement += `const __ot_result = __ot_data`;
}
statement += `
__ot_items.push(__ot_result);`
if (data.count) {
statement += `
__ot_count++;`;
}
statement += `
${endsm}`;
if (data.count) {
if (path === '') {
statement += `
const __ot_${name}count = __ot_count`;
countAlias.push([`${name}.${data.count}`, `__ot_${name}count`], [`${name}['${data.count}']`, `__ot_${name}count`]);
} else {
statement += `
const __ot_${path}count = __ot_count`;
countAlias.push([`${path}.${data.count}`, `__ot_${path}count`], [`${path}['${data.count}']`, `__ot_${path}count`]);
}
}
}
function expandObject(data, name, innerAlias, countAlias, path = '') {
const __alias = [];
let endsm = '';
const stubs = (typeof data.stub === 'string') ? [data.stub] : data.stub;
for (let i in stubs) {
const stub = stubs[i];
const [stubname, itemname, key] = getKeyName(stub);
statement += `
let __ot_src_${path}${i} = ${stubname};
for (let __ot_${itemname}_key in __ot_src_${path}${i}) {
let ${itemname} = __ot_src_${path}${i}[__ot_${itemname}_key];`;
__alias.push([`${itemname}.${key}`, `__ot_${itemname}_key`], [`${itemname}['${key}']`, `__ot_${itemname}_key`]);
endsm += '}';
}
if ('object' !== typeof data.value || data.value instanceof Array) {
throw 'Object expansion must expand to object';
}
replaceObject(data.value, innerAlias.concat(__alias));
statement += `
${endsm}`;
}
function replaceObject(data, innerAlias = getConstAlias()) {
const isArray = data instanceof Array;
for (let key in data) {
const value = data[key];
let ok, _statement, isExpan, name, nameTag;
if (isArray) {
isExpan = isExpansion(value);
name = key = Number(key);
nameTag = name;
} else {
const matched = match(key);
name = reverseEscape(key, matched);
if (matched) {
[ok, _statement] = getStatement(name, '', false, innerAlias);
if (!ok) {
name = _statement;
}
}
isExpan = isExpansion(value);
nameTag = `'${regSymbolEncode(name)}'`;
}
if (isExpan) {
if (!goodExpansion(value)) {
throw 'Expansion error';
}
const countAlias = [];
const stubs = (typeof value['stub'] === 'string') ? [value['stub']] : value['stub'];
const __alias = [];
let endsm = '';
if (value.type === 'array') {
statement += `
__ot_temp = __ot_result[${nameTag}];
delete __ot_result[${nameTag}];
{`;
if (hasCountExpand(value)) {
statement += `
let __ot_count;`;
}
const stub = value.stub;
if (typeof stub === 'object' && !(stub instanceof Array)) {
statement += `
const __ot_stub = __ot_temp.stub
let __ot_data;`;
}
if (value.append) {
statement += `
const __ot_append = __ot_temp.append;`;
}
if (value.fixed) {
statement += `
const __ot_fixed = __ot_temp.fixed;`;
}
if (ok) {
statement += `
let __ot_items = __ot_result[${_statement}] = [];`;
} else {
statement += `
let __ot_items = __ot_result[${nameTag}] = [];`;
}
if (typeof stub === 'object' && !(stub instanceof Array)) {
for (let key in stub) {
expandArray(stub[key], name, innerAlias, countAlias, key);
}
} else {
expandArray(value, name, innerAlias, countAlias, '');
}
if (value.append) {
if (value.append instanceof Array) {
statement += `
{
const __ot_result = __ot_append;`;
replaceObject(value.append, innerAlias.concat(countAlias));
statement += `
__ot_items.push(...__ot_result);
}`;
} else if (isPlainObject(value.append)) {
statement += `
{
const __ot_result = __ot_append;`;
replaceObject(value.append, innerAlias.concat(countAlias));
statement += `
__ot_items.push(__ot_result);
}`;
} else if ('string' === typeof value.append && match(value.append)) {
const [_ok, __statement] = getStatement(value.append, '', false, innerAlias.concat(countAlias));
if (_ok) {
statement += `
__ot_items.push(${__statement});`;
// debug('replaceObject %o to statement %s', data, statement);
} else {
value.append = __statement;
statement += `
__ot_items.push(__ot_append);`;
// debug('replaceObject %o to value %o', data, _statement);
}
} else {
statement += `
__ot_items.push(__ot_append);`;
}
}
if (value.fixed) {
if (value.fixed instanceof Array) {
for (let i in value.fixed) {
replaceFixed(value.fixed[i], innerAlias.concat(countAlias), `[${i}]`);
}
} else {
replaceFixed(value.fixed, innerAlias.concat(countAlias));
}
}
statement += `
}`;
} else {
if (value.append) {
statement += `
__ot_temp = __ot_result[${nameTag}];
delete __ot_result[${nameTag}];
{
const __ot_result = {};
const __ot_append = __ot_temp.append;`;
} else {
statement += `
delete __ot_result[${nameTag}];
{
const __ot_result = {};`
}
const stub = value.stub;
if (typeof stub === 'object' && !(stub instanceof Array)) {
for (let key in stub) {
expandObject(stub[key], name, innerAlias, countAlias, key);
}
} else {
expandObject(value, name, innerAlias, countAlias, '');
}
if (value.append) {
if ('object' !== typeof value.append || value.append instanceof Array) {
throw 'Object expansion must expand to object';
}
statement += `
Object.assign(__ot_result, __ot_append);`;
replaceObject(value.append, innerAlias);
}
statement += `
__ot_temp = __ot_result;
}`;
if (ok) {
statement += `
__ot_result[${_statement}] = __ot_temp;`;
} else {
statement += `
__ot_result[${nameTag}] = __ot_temp;`;
}
}
} else if (value instanceof Array || isPlainObject(value) || cloneInstances.includes(value.constructor)) {
if (ok) {
statement += `
__ot_temp = __ot_result[${nameTag}];
delete __ot_result[${nameTag}];
__ot_result[${_statement}] = __ot_temp;
{
const __ot_result = __ot_temp;`;
} else {
statement += `
__ot_temp = __ot_result[${nameTag}];
{
const __ot_result = __ot_temp;`;
}
replaceObject(value, innerAlias);
statement += '}';
} else if ('string' === typeof value && match(value)) {
if (ok) {
const [_ok, __statement] = getStatement(value, '', false, innerAlias);
if (_ok) {
statement += `
delete __ot_result[${nameTag}];
__ot_result[${_statement}] = (${__statement});`;
// debug('replaceObject %o to statement %s', data, statement);
} else {
data[key] = __statement;
statement += `
__ot_temp = __ot_result[${nameTag}];
delete __ot_result[${nameTag}];
__ot_result[${_statement}] = __ot_temp;`;
// debug('replaceObject %o to value %o', data, _statement);
}
} else {
const [_ok, __statement] = getStatement(value, `[${nameTag}]`, data, innerAlias);
if (_ok) {
statement += __statement;
// debug('replaceObject %o to statement %s', data, statement);
} else {
data[key] = __statement;
// debug('replaceObject %o to value %o', data, _statement);
}
}
} else if (ok) {
statement += `
__ot_temp = __ot_result[${nameTag}];
delete __ot_result[${nameTag}];
__ot_result[${_statement}] = __ot_temp;`;
}
if (name !== key) {
data[name] = data[key];
delete data[key];
}
}
}
function replaceString(str, innerAlias = getConstAlias()) {
if (match(str)) {
const [ok, _statement] = getStatement(value, '', undefined, innerAlias);
if (ok) {
statement = _statement;
// debug('replaceString %s to statement %s', str, _statement);
} else {
// debug('replaceString %s to value %o', str, _statement);
return _statement;
}
}
}
if ('string' === typeof template) {
if (!match(template)) {
debug('return common value %s', template);
return function() {return template;};
}
template += replaceString(template);
if ('' !== statement) {
statement = `
const __ot_constants = this.constants;
const __ot_helpers = this.helpers;${statement}`;
debug('return function (%s) {%s}', _arguments.join(','), statement);
return new Function(..._arguments, statement).bind({constants, helpers});
} else {
debug('return value %o', template);
return function() {return template;};
}
} else {
replaceObject(template);
if ('' !== statement) {
statement = `
const __ot_constants = this.constants;
const __ot_helpers = this.helpers;`
+ (cloneInstances.length ?
'const __ot_result = this.clone(this.data, this.instanceClone);\n' :
'const __ot_result = this.clone(this.data);\n')
+ `let __ot_temp;
${statement}
return __ot_result;`;
debug('return function (%s) {%s}', _arguments.join(','), statement);
return new Function(..._arguments, statement).bind({data: template, clone, instanceClone, constants, helpers});
} else {
debug('return value %o', template);
return function() {return template;};
}
}
}
objectTemplates.escape = escape;
/**
* Expose `objectTemplates`
*/
module.exports = objectTemplates;