@voxgig/struct 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/struct.d.ts +48 -35
- package/dist/struct.js +626 -284
- package/dist/struct.js.map +1 -1
- package/package.json +2 -2
- package/src/struct.ts +770 -349
package/dist/struct.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.StructUtility = void 0;
|
|
5
5
|
exports.clone = clone;
|
|
6
|
+
exports.delprop = delprop;
|
|
6
7
|
exports.escre = escre;
|
|
7
8
|
exports.escurl = escurl;
|
|
8
9
|
exports.getelem = getelem;
|
|
@@ -18,10 +19,12 @@ exports.ismap = ismap;
|
|
|
18
19
|
exports.isnode = isnode;
|
|
19
20
|
exports.items = items;
|
|
20
21
|
exports.joinurl = joinurl;
|
|
22
|
+
exports.jsonify = jsonify;
|
|
21
23
|
exports.keysof = keysof;
|
|
22
24
|
exports.merge = merge;
|
|
23
25
|
exports.pad = pad;
|
|
24
26
|
exports.pathify = pathify;
|
|
27
|
+
exports.select = select;
|
|
25
28
|
exports.setprop = setprop;
|
|
26
29
|
exports.size = size;
|
|
27
30
|
exports.slice = slice;
|
|
@@ -86,8 +89,9 @@ const S_MKEYPOST = 'key:post';
|
|
|
86
89
|
const S_MVAL = 'val';
|
|
87
90
|
const S_MKEY = 'key';
|
|
88
91
|
// Special keys.
|
|
89
|
-
const
|
|
90
|
-
const
|
|
92
|
+
const S_BKEY = '`$KEY`';
|
|
93
|
+
const S_BANNO = '`$ANNO`';
|
|
94
|
+
const S_DKEY = '$KEY';
|
|
91
95
|
const S_DTOP = '$TOP';
|
|
92
96
|
const S_DERRS = '$ERRS';
|
|
93
97
|
const S_DSPEC = '$SPEC';
|
|
@@ -100,68 +104,40 @@ const S_number = 'number';
|
|
|
100
104
|
const S_object = 'object';
|
|
101
105
|
const S_string = 'string';
|
|
102
106
|
const S_null = 'null';
|
|
107
|
+
const S_key = 'key';
|
|
103
108
|
const S_MT = '';
|
|
104
109
|
const S_BT = '`';
|
|
105
110
|
const S_DS = '$';
|
|
106
111
|
const S_DT = '.';
|
|
107
112
|
const S_CN = ':';
|
|
108
113
|
const S_FS = '/';
|
|
114
|
+
const S_OS = '[';
|
|
115
|
+
const S_CS = ']';
|
|
116
|
+
const S_SP = ' ';
|
|
109
117
|
const S_KEY = 'KEY';
|
|
110
118
|
// The standard undefined value for this language.
|
|
111
119
|
const UNDEF = undefined;
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
toString(prefix) {
|
|
131
|
-
return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
|
|
132
|
-
pad(pathify(this.path)) +
|
|
133
|
-
this.mode + (this.full ? '/full' : '') + S_CN +
|
|
134
|
-
'key=' + this.keyI + S_FS + this.key + S_FS + this.keys +
|
|
135
|
-
' p=' + stringify(this.parent, -1, 1) +
|
|
136
|
-
' m=' + stringify(this.meta, -1, 1) +
|
|
137
|
-
' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1);
|
|
138
|
-
}
|
|
139
|
-
child(keyI, keys) {
|
|
140
|
-
const key = strkey(keys[keyI]);
|
|
141
|
-
const val = this.val;
|
|
142
|
-
const cinj = new Injection(getprop(val, key), val, this.modify, this.errs);
|
|
143
|
-
cinj.keyI = keyI;
|
|
144
|
-
cinj.keys = keys;
|
|
145
|
-
cinj.key = key;
|
|
146
|
-
cinj.path = [...(this.path || []), key];
|
|
147
|
-
cinj.nodes = [...(this.nodes || []), val];
|
|
148
|
-
cinj.mode = this.mode;
|
|
149
|
-
cinj.handler = this.handler;
|
|
150
|
-
cinj.base = this.base;
|
|
151
|
-
cinj.meta = this.meta;
|
|
152
|
-
cinj.prior = this;
|
|
153
|
-
return cinj;
|
|
154
|
-
}
|
|
155
|
-
setval(val, ancestor) {
|
|
156
|
-
if (null == ancestor || ancestor < 2) {
|
|
157
|
-
return setprop(this.parent, this.key, val);
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
return setprop(getelem(this.nodes, 0 - ancestor), getelem(this.path, 0 - ancestor), val);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
120
|
+
// Private marker to indicate a skippable value.
|
|
121
|
+
const SKIP = { '`$SKIP`': true };
|
|
122
|
+
// Regular expression constants
|
|
123
|
+
const R_INTEGER_KEY = /^[-0-9]+$/; // Match integer keys (including <0).
|
|
124
|
+
const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g; // Chars that need escaping in regexp.
|
|
125
|
+
const R_TRAILING_SLASH = /\/+$/; // Trailing slashes in URLs.
|
|
126
|
+
const R_LEADING_TRAILING_SLASH = /([^\/])\/+/; // Multiple slashes in URL middle.
|
|
127
|
+
const R_LEADING_SLASH = /^\/+/; // Leading slashes in URLs.
|
|
128
|
+
const R_QUOTES = /"/g; // Double quotes for removal.
|
|
129
|
+
const R_DOT = /\./g; // Dots in path strings.
|
|
130
|
+
const R_FUNCTION_REF = /^`\$FUNCTION:([0-9]+)`$/; // Function reference in clone.
|
|
131
|
+
const R_META_PATH = /^([^$]+)\$([=~])(.+)$/; // Meta path syntax.
|
|
132
|
+
const R_DOUBLE_DOLLAR = /\$\$/g; // Double dollar escape sequence.
|
|
133
|
+
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g; // Transform command names.
|
|
134
|
+
const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/; // Full string injection pattern.
|
|
135
|
+
const R_BT_ESCAPE = /\$BT/g; // Backtick escape sequence.
|
|
136
|
+
const R_DS_ESCAPE = /\$DS/g; // Dollar sign escape sequence.
|
|
137
|
+
const R_INJECTION_PARTIAL = /`([^`]+)`/g; // Partial string injection pattern.
|
|
164
138
|
// Value is a node - defined, and a map (hash) or list (array).
|
|
139
|
+
// NOTE: typescript
|
|
140
|
+
// things
|
|
165
141
|
function isnode(val) {
|
|
166
142
|
return null != val && S_object == typeof val;
|
|
167
143
|
}
|
|
@@ -188,6 +164,8 @@ function isempty(val) {
|
|
|
188
164
|
function isfunc(val) {
|
|
189
165
|
return S_function === typeof val;
|
|
190
166
|
}
|
|
167
|
+
// The integer size of the value. For arrays and strings, the length,
|
|
168
|
+
// for numbers, the integer part, for boolean, true is 1 and falso 0, for all other values, 0.
|
|
191
169
|
function size(val) {
|
|
192
170
|
if (islist(val)) {
|
|
193
171
|
return val.length;
|
|
@@ -209,36 +187,59 @@ function size(val) {
|
|
|
209
187
|
return 0;
|
|
210
188
|
}
|
|
211
189
|
}
|
|
190
|
+
// Extract part of an array or string into a new value, from the start point to the end point.
|
|
191
|
+
// If no end is specified, extract to the full length of the value. Negative arguments count
|
|
192
|
+
// from the end of the value. For numbers, perform min and max bounding, where start is
|
|
193
|
+
// inclusive, and end is *exclusive*.
|
|
212
194
|
function slice(val, start, end) {
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
195
|
+
if (S_number === typeof val) {
|
|
196
|
+
start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start;
|
|
197
|
+
end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1;
|
|
198
|
+
return Math.min(Math.max(val, start), end);
|
|
199
|
+
}
|
|
200
|
+
const vlen = size(val);
|
|
201
|
+
if (null != end && null == start) {
|
|
202
|
+
start = 0;
|
|
203
|
+
}
|
|
204
|
+
if (null != start) {
|
|
205
|
+
if (start < 0) {
|
|
206
|
+
end = vlen + start;
|
|
207
|
+
if (end < 0) {
|
|
208
|
+
end = 0;
|
|
209
|
+
}
|
|
210
|
+
start = 0;
|
|
211
|
+
}
|
|
212
|
+
else if (null != end) {
|
|
213
|
+
if (end < 0) {
|
|
214
|
+
end = vlen + end;
|
|
218
215
|
if (end < 0) {
|
|
219
216
|
end = 0;
|
|
220
217
|
}
|
|
221
|
-
start = 0;
|
|
222
218
|
}
|
|
223
|
-
else if (
|
|
224
|
-
|
|
225
|
-
end = vlen + end;
|
|
226
|
-
if (end < 0) {
|
|
227
|
-
end = 0;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
else if (vlen < end) {
|
|
231
|
-
end = val.length;
|
|
232
|
-
}
|
|
219
|
+
else if (vlen < end) {
|
|
220
|
+
end = vlen;
|
|
233
221
|
}
|
|
234
|
-
|
|
235
|
-
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
end = vlen;
|
|
225
|
+
}
|
|
226
|
+
if (vlen < start) {
|
|
227
|
+
start = vlen;
|
|
228
|
+
}
|
|
229
|
+
if (-1 < start && start <= end && end <= vlen) {
|
|
230
|
+
if (islist(val)) {
|
|
231
|
+
val = val.slice(start, end);
|
|
236
232
|
}
|
|
237
|
-
if (
|
|
238
|
-
|
|
233
|
+
else if (S_string === typeof val) {
|
|
234
|
+
val = val.substring(start, end);
|
|
239
235
|
}
|
|
240
|
-
|
|
241
|
-
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
if (islist(val)) {
|
|
239
|
+
val = [];
|
|
240
|
+
}
|
|
241
|
+
else if (S_string === typeof val) {
|
|
242
|
+
val = S_MT;
|
|
242
243
|
}
|
|
243
244
|
}
|
|
244
245
|
}
|
|
@@ -247,7 +248,7 @@ function slice(val, start, end) {
|
|
|
247
248
|
function pad(str, padding, padchar) {
|
|
248
249
|
str = stringify(str);
|
|
249
250
|
padding = null == padding ? 44 : padding;
|
|
250
|
-
padchar = null == padchar ?
|
|
251
|
+
padchar = null == padchar ? S_SP : ((padchar + S_SP)[0]);
|
|
251
252
|
return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar);
|
|
252
253
|
}
|
|
253
254
|
// Determine the type of a value as a string.
|
|
@@ -275,7 +276,7 @@ function getelem(val, key, alt) {
|
|
|
275
276
|
}
|
|
276
277
|
if (islist(val)) {
|
|
277
278
|
let nkey = parseInt(key);
|
|
278
|
-
if (Number.isInteger(nkey) && ('' + key).match(
|
|
279
|
+
if (Number.isInteger(nkey) && ('' + key).match(R_INTEGER_KEY)) {
|
|
279
280
|
if (nkey < 0) {
|
|
280
281
|
key = val.length + nkey;
|
|
281
282
|
}
|
|
@@ -289,7 +290,6 @@ function getelem(val, key, alt) {
|
|
|
289
290
|
}
|
|
290
291
|
// Safely get a property of a node. Undefined arguments return undefined.
|
|
291
292
|
// If the key is not found, return the alternative value, if any.
|
|
292
|
-
// TODO: for arrays. make -i work, such that -1 => last element
|
|
293
293
|
function getprop(val, key, alt) {
|
|
294
294
|
let out = alt;
|
|
295
295
|
if (UNDEF === val || UNDEF === key) {
|
|
@@ -326,7 +326,7 @@ function strkey(key = UNDEF) {
|
|
|
326
326
|
// Sorted keys of a map, or indexes of a list.
|
|
327
327
|
function keysof(val) {
|
|
328
328
|
return !isnode(val) ? [] :
|
|
329
|
-
ismap(val) ? Object.keys(val).sort() : val.map((_n, i) =>
|
|
329
|
+
ismap(val) ? Object.keys(val).sort() : val.map((_n, i) => S_MT + i);
|
|
330
330
|
}
|
|
331
331
|
// Value of property with name key in node val is defined.
|
|
332
332
|
function haskey(val, key) {
|
|
@@ -340,7 +340,7 @@ function items(val) {
|
|
|
340
340
|
// Escape regular expression.
|
|
341
341
|
function escre(s) {
|
|
342
342
|
s = null == s ? S_MT : s;
|
|
343
|
-
return s.replace(
|
|
343
|
+
return s.replace(R_ESCAPE_REGEXP, '\\$&');
|
|
344
344
|
}
|
|
345
345
|
// Escape URLs.
|
|
346
346
|
function escurl(s) {
|
|
@@ -350,11 +350,26 @@ function escurl(s) {
|
|
|
350
350
|
// Concatenate url part strings, merging forward slashes as needed.
|
|
351
351
|
function joinurl(sarr) {
|
|
352
352
|
return sarr
|
|
353
|
-
.filter(s => null != s &&
|
|
354
|
-
.map((s, i) => 0 === i ? s.replace(
|
|
355
|
-
s.replace(
|
|
356
|
-
|
|
357
|
-
|
|
353
|
+
.filter(s => null != s && S_MT !== s)
|
|
354
|
+
.map((s, i) => 0 === i ? s.replace(R_TRAILING_SLASH, S_MT) :
|
|
355
|
+
s.replace(R_LEADING_TRAILING_SLASH, '$1/')
|
|
356
|
+
.replace(R_LEADING_SLASH, S_MT)
|
|
357
|
+
.replace(R_TRAILING_SLASH, S_MT))
|
|
358
|
+
.filter(s => S_MT !== s)
|
|
359
|
+
.join(S_FS);
|
|
360
|
+
}
|
|
361
|
+
// Output JSON in a "standard" format, with 2 space indents, each property on a new line,
|
|
362
|
+
// and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
|
|
363
|
+
// In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
|
|
364
|
+
function jsonify(val) {
|
|
365
|
+
let str = S_null;
|
|
366
|
+
if (null != val) {
|
|
367
|
+
str = JSON.stringify(val, null, 2);
|
|
368
|
+
if (UNDEF === str) {
|
|
369
|
+
str = S_null;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return str;
|
|
358
373
|
}
|
|
359
374
|
// Safely stringify a value for humans (NOT JSON!).
|
|
360
375
|
function stringify(val, maxlen, pretty) {
|
|
@@ -381,12 +396,13 @@ function stringify(val, maxlen, pretty) {
|
|
|
381
396
|
valstr = S_MT + val;
|
|
382
397
|
}
|
|
383
398
|
valstr = S_string !== typeof valstr ? S_MT + valstr : valstr;
|
|
384
|
-
valstr = valstr.replace(
|
|
399
|
+
valstr = valstr.replace(R_QUOTES, S_MT);
|
|
385
400
|
if (null != maxlen && -1 < maxlen) {
|
|
386
401
|
let js = valstr.substring(0, maxlen);
|
|
387
402
|
valstr = maxlen < valstr.length ? (js.substring(0, maxlen - 3) + '...') : valstr;
|
|
388
403
|
}
|
|
389
404
|
if (pretty) {
|
|
405
|
+
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
390
406
|
let c = [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69]
|
|
391
407
|
.map(n => `\x1b[38;5;${n}m`), r = '\x1b[0m', d = 0, o = c[0], t = o;
|
|
392
408
|
for (const ch of valstr) {
|
|
@@ -426,8 +442,8 @@ function pathify(val, startin, endin) {
|
|
|
426
442
|
pathstr = path
|
|
427
443
|
// .filter((p: any, t: any) => (t = typeof p, S_string === t || S_number === t))
|
|
428
444
|
.filter((p) => iskey(p))
|
|
429
|
-
.map((p) =>
|
|
430
|
-
p.replace(
|
|
445
|
+
.map((p) => S_number === typeof p ? S_MT + Math.floor(p) :
|
|
446
|
+
p.replace(R_DOT, S_MT))
|
|
431
447
|
.join(S_DT);
|
|
432
448
|
}
|
|
433
449
|
}
|
|
@@ -443,28 +459,52 @@ function clone(val) {
|
|
|
443
459
|
const replacer = (_k, v) => S_function === typeof v ?
|
|
444
460
|
(refs.push(v), '`$FUNCTION:' + (refs.length - 1) + '`') : v;
|
|
445
461
|
const reviver = (_k, v, m) => S_string === typeof v ?
|
|
446
|
-
(m = v.match(
|
|
462
|
+
(m = v.match(R_FUNCTION_REF), m ? refs[m[1]] : v) : v;
|
|
447
463
|
return UNDEF === val ? UNDEF : JSON.parse(JSON.stringify(val, replacer), reviver);
|
|
448
464
|
}
|
|
465
|
+
// Safely delete a property from an object or array element.
|
|
466
|
+
// Undefined arguments and invalid keys are ignored.
|
|
467
|
+
// Returns the (possibly modified) parent.
|
|
468
|
+
// For objects, the property is deleted using the delete operator.
|
|
469
|
+
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
470
|
+
function delprop(parent, key) {
|
|
471
|
+
if (!iskey(key)) {
|
|
472
|
+
return parent;
|
|
473
|
+
}
|
|
474
|
+
if (ismap(parent)) {
|
|
475
|
+
// key = S_MT + key
|
|
476
|
+
key = strkey(key);
|
|
477
|
+
delete parent[key];
|
|
478
|
+
}
|
|
479
|
+
else if (islist(parent)) {
|
|
480
|
+
// Ensure key is an integer.
|
|
481
|
+
let keyI = +key;
|
|
482
|
+
if (isNaN(keyI)) {
|
|
483
|
+
return parent;
|
|
484
|
+
}
|
|
485
|
+
keyI = Math.floor(keyI);
|
|
486
|
+
// Delete list element at position keyI, shifting later elements down.
|
|
487
|
+
if (0 <= keyI && keyI < parent.length) {
|
|
488
|
+
for (let pI = keyI; pI < parent.length - 1; pI++) {
|
|
489
|
+
parent[pI] = parent[pI + 1];
|
|
490
|
+
}
|
|
491
|
+
parent.length = parent.length - 1;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return parent;
|
|
495
|
+
}
|
|
449
496
|
// Safely set a property. Undefined arguments and invalid keys are ignored.
|
|
450
497
|
// Returns the (possibly modified) parent.
|
|
451
|
-
// If the value is undefined the key will be deleted from the parent.
|
|
452
498
|
// If the parent is a list, and the key is negative, prepend the value.
|
|
453
499
|
// NOTE: If the key is above the list size, append the value; below, prepend.
|
|
454
|
-
// If the value is undefined, remove the list element at index key, and shift the
|
|
455
|
-
// remaining elements down. These rules avoid "holes" in the list.
|
|
456
500
|
function setprop(parent, key, val) {
|
|
457
501
|
if (!iskey(key)) {
|
|
458
502
|
return parent;
|
|
459
503
|
}
|
|
460
504
|
if (ismap(parent)) {
|
|
461
505
|
key = S_MT + key;
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
parent[key] = val;
|
|
467
|
-
}
|
|
506
|
+
const pany = parent;
|
|
507
|
+
pany[key] = val;
|
|
468
508
|
}
|
|
469
509
|
else if (islist(parent)) {
|
|
470
510
|
// Ensure key is an integer.
|
|
@@ -473,17 +513,8 @@ function setprop(parent, key, val) {
|
|
|
473
513
|
return parent;
|
|
474
514
|
}
|
|
475
515
|
keyI = Math.floor(keyI);
|
|
476
|
-
// Delete list element at position keyI, shifting later elements down.
|
|
477
|
-
if (UNDEF === val) {
|
|
478
|
-
if (0 <= keyI && keyI < parent.length) {
|
|
479
|
-
for (let pI = keyI; pI < parent.length - 1; pI++) {
|
|
480
|
-
parent[pI] = parent[pI + 1];
|
|
481
|
-
}
|
|
482
|
-
parent.length = parent.length - 1;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
516
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
486
|
-
|
|
517
|
+
if (0 <= keyI) {
|
|
487
518
|
parent[parent.length < keyI ? parent.length : keyI] = val;
|
|
488
519
|
}
|
|
489
520
|
// Prepend value if keyI is negative
|
|
@@ -552,7 +583,7 @@ function merge(val) {
|
|
|
552
583
|
let lenpath = path.length;
|
|
553
584
|
cI = lenpath - 1;
|
|
554
585
|
if (UNDEF === cur[cI]) {
|
|
555
|
-
cur[cI] = getpath(slice(path, 0, lenpath - 1)
|
|
586
|
+
cur[cI] = getpath(out, slice(path, 0, lenpath - 1));
|
|
556
587
|
}
|
|
557
588
|
// Create node if needed.
|
|
558
589
|
if (!isnode(cur[cI])) {
|
|
@@ -577,83 +608,121 @@ function merge(val) {
|
|
|
577
608
|
}
|
|
578
609
|
return out;
|
|
579
610
|
}
|
|
580
|
-
|
|
581
|
-
// path `a.b` gets the value 1 from {a:{b:1}}. The path can specified
|
|
582
|
-
// as a dotted string, or a string array. If the path starts with a
|
|
583
|
-
// dot (or the first element is ''), the path is considered local, and
|
|
584
|
-
// resolved against the `current` argument, if defined. Integer path
|
|
585
|
-
// parts are used as array indexes. The inj argument allows for
|
|
586
|
-
// custom handling when called from `inject` or `transform`.
|
|
587
|
-
// TODO: change sig to be consistent with getprop
|
|
588
|
-
function getpath(path, store, current, inj) {
|
|
611
|
+
function getpath(store, path, injdef) {
|
|
589
612
|
// Operate on a string array.
|
|
590
|
-
const parts = islist(path) ? path :
|
|
613
|
+
const parts = islist(path) ? path :
|
|
614
|
+
'string' === typeof path ? path.split(S_DT) :
|
|
615
|
+
'number' === typeof path ? [strkey(path)] : UNDEF;
|
|
591
616
|
if (UNDEF === parts) {
|
|
592
617
|
return UNDEF;
|
|
593
618
|
}
|
|
594
|
-
let root = store
|
|
619
|
+
// let root = store
|
|
595
620
|
let val = store;
|
|
596
|
-
const base = getprop(
|
|
621
|
+
const base = getprop(injdef, S_base);
|
|
622
|
+
const src = getprop(store, base, store);
|
|
623
|
+
const numparts = size(parts);
|
|
624
|
+
const dparent = getprop(injdef, 'dparent');
|
|
597
625
|
// An empty path (incl empty string) just finds the store.
|
|
598
|
-
if (null == path || null == store || (1 ===
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
getprop(
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
626
|
+
if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
|
|
627
|
+
val = src;
|
|
628
|
+
}
|
|
629
|
+
else if (0 < numparts) {
|
|
630
|
+
// Check for $ACTIONs
|
|
631
|
+
if (1 === numparts) {
|
|
632
|
+
val = getprop(store, parts[0]);
|
|
633
|
+
}
|
|
634
|
+
if (!isfunc(val)) {
|
|
635
|
+
val = src;
|
|
636
|
+
const m = parts[0].match(R_META_PATH);
|
|
637
|
+
if (m && injdef && injdef.meta) {
|
|
638
|
+
val = getprop(injdef.meta, m[1]);
|
|
639
|
+
parts[0] = m[3];
|
|
640
|
+
}
|
|
641
|
+
const dpath = getprop(injdef, 'dpath');
|
|
642
|
+
for (let pI = 0; UNDEF !== val && pI < parts.length; pI++) {
|
|
643
|
+
let part = parts[pI];
|
|
644
|
+
if (injdef && S_DKEY === part) {
|
|
645
|
+
part = getprop(injdef, S_key);
|
|
646
|
+
}
|
|
647
|
+
else if (injdef && part.startsWith('$GET:')) {
|
|
648
|
+
// $GET:path$ -> get store value, use as path part (string)
|
|
649
|
+
part = stringify(getpath(src, part.substring(5, part.length - 1)));
|
|
650
|
+
}
|
|
651
|
+
else if (injdef && part.startsWith('$REF:')) {
|
|
652
|
+
// $REF:refpath$ -> get spec value, use as path part (string)
|
|
653
|
+
part = stringify(getpath(getprop(store, S_DSPEC), part.substring(5, part.length - 1)));
|
|
654
|
+
}
|
|
655
|
+
else if (injdef && part.startsWith('$META:')) {
|
|
656
|
+
// $META:metapath$ -> get meta value, use as path part (string)
|
|
657
|
+
part = stringify(getpath(getprop(injdef, 'meta'), part.substring(6, part.length - 1)));
|
|
658
|
+
}
|
|
659
|
+
// $$ escapes $
|
|
660
|
+
part = part.replace(R_DOUBLE_DOLLAR, '$');
|
|
661
|
+
if (S_MT === part) {
|
|
662
|
+
let ascends = 0;
|
|
663
|
+
while (S_MT === parts[1 + pI]) {
|
|
664
|
+
ascends++;
|
|
665
|
+
pI++;
|
|
666
|
+
}
|
|
667
|
+
if (injdef && 0 < ascends) {
|
|
668
|
+
if (pI === parts.length - 1) {
|
|
669
|
+
ascends--;
|
|
670
|
+
}
|
|
671
|
+
if (0 === ascends) {
|
|
672
|
+
val = dparent;
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1));
|
|
676
|
+
if (ascends <= size(dpath)) {
|
|
677
|
+
val = getpath(store, fullpath);
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
val = UNDEF;
|
|
681
|
+
}
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
else {
|
|
686
|
+
val = dparent;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
val = getprop(val, part);
|
|
691
|
+
}
|
|
620
692
|
}
|
|
621
693
|
}
|
|
622
694
|
}
|
|
623
695
|
// Inj may provide a custom handler to modify found value.
|
|
624
|
-
|
|
696
|
+
const handler = getprop(injdef, 'handler');
|
|
697
|
+
if (null != injdef && isfunc(handler)) {
|
|
625
698
|
const ref = pathify(path);
|
|
626
|
-
val =
|
|
699
|
+
val = handler(injdef, val, ref, store);
|
|
627
700
|
}
|
|
628
701
|
return val;
|
|
629
702
|
}
|
|
630
|
-
// let cI = 0
|
|
631
703
|
// Inject values from a data store into a node recursively, resolving
|
|
632
704
|
// paths against the store, or current if they are local. THe modify
|
|
633
705
|
// argument allows custom modification of the result. The inj
|
|
634
706
|
// (Injection) argument is used to maintain recursive state.
|
|
635
|
-
function inject(val, store,
|
|
707
|
+
function inject(val, store, injdef) {
|
|
636
708
|
const valtype = typeof val;
|
|
709
|
+
let inj = injdef;
|
|
637
710
|
// Create state if at root of injection. The input value is placed
|
|
638
711
|
// inside a virtual parent holder to simplify edge cases.
|
|
639
|
-
if (UNDEF ===
|
|
712
|
+
if (UNDEF === injdef || null == injdef.mode) {
|
|
640
713
|
// Set up state assuming we are starting in the virtual parent.
|
|
641
|
-
inj = new Injection(val, { [S_DTOP]: val }
|
|
642
|
-
inj.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
// A provided current is expected to be the containing node of the associated
|
|
652
|
-
// store value.
|
|
653
|
-
const parentkey = getelem(inj.path, -2);
|
|
654
|
-
current = null == parentkey ? current : getprop(current, parentkey);
|
|
714
|
+
inj = new Injection(val, { [S_DTOP]: val });
|
|
715
|
+
inj.dparent = store;
|
|
716
|
+
inj.errs = getprop(store, S_DERRS, []);
|
|
717
|
+
inj.meta.__d = 0;
|
|
718
|
+
if (UNDEF !== injdef) {
|
|
719
|
+
inj.modify = null == injdef.modify ? inj.modify : injdef.modify;
|
|
720
|
+
inj.extra = null == injdef.extra ? inj.extra : injdef.extra;
|
|
721
|
+
inj.meta = null == injdef.meta ? inj.meta : injdef.meta;
|
|
722
|
+
inj.handler = null == injdef.handler ? inj.handler : injdef.handler;
|
|
723
|
+
}
|
|
655
724
|
}
|
|
656
|
-
|
|
725
|
+
inj.descend();
|
|
657
726
|
// Descend into node.
|
|
658
727
|
if (isnode(val)) {
|
|
659
728
|
// Keys are sorted alphanumerically to ensure determinism.
|
|
@@ -673,7 +742,7 @@ function inject(val, store, modify, current, inj) {
|
|
|
673
742
|
const nodekey = childinj.key;
|
|
674
743
|
childinj.mode = S_MKEYPRE;
|
|
675
744
|
// Peform the key:pre mode injection on the child key.
|
|
676
|
-
const prekey = _injectstr(nodekey, store,
|
|
745
|
+
const prekey = _injectstr(nodekey, store, childinj);
|
|
677
746
|
// The injection may modify child processing.
|
|
678
747
|
nkI = childinj.keyI;
|
|
679
748
|
nodekeys = childinj.keys;
|
|
@@ -683,13 +752,13 @@ function inject(val, store, modify, current, inj) {
|
|
|
683
752
|
childinj.mode = S_MVAL;
|
|
684
753
|
// Perform the val mode injection on the child value.
|
|
685
754
|
// NOTE: return value is not used.
|
|
686
|
-
inject(childinj.val, store,
|
|
755
|
+
inject(childinj.val, store, childinj);
|
|
687
756
|
// The injection may modify child processing.
|
|
688
757
|
nkI = childinj.keyI;
|
|
689
758
|
nodekeys = childinj.keys;
|
|
690
759
|
// Peform the key:post mode injection on the child key.
|
|
691
760
|
childinj.mode = S_MKEYPOST;
|
|
692
|
-
_injectstr(nodekey, store,
|
|
761
|
+
_injectstr(nodekey, store, childinj);
|
|
693
762
|
// The injection may modify child processing.
|
|
694
763
|
nkI = childinj.keyI;
|
|
695
764
|
nodekeys = childinj.keys;
|
|
@@ -699,15 +768,19 @@ function inject(val, store, modify, current, inj) {
|
|
|
699
768
|
// Inject paths into string scalars.
|
|
700
769
|
else if (S_string === valtype) {
|
|
701
770
|
inj.mode = S_MVAL;
|
|
702
|
-
val = _injectstr(val, store,
|
|
703
|
-
|
|
771
|
+
val = _injectstr(val, store, inj);
|
|
772
|
+
if (SKIP !== val) {
|
|
773
|
+
inj.setval(val);
|
|
774
|
+
}
|
|
704
775
|
}
|
|
776
|
+
// console.log('INJECT-M0 ', val, '' + inj)
|
|
705
777
|
// Custom modification.
|
|
706
|
-
if (modify) {
|
|
778
|
+
if (inj.modify && SKIP !== val) {
|
|
707
779
|
let mkey = inj.key;
|
|
708
780
|
let mparent = inj.parent;
|
|
709
781
|
let mval = getprop(mparent, mkey);
|
|
710
|
-
|
|
782
|
+
// console.log('INJECT-M1 ' + inj)
|
|
783
|
+
inj.modify(mval, mkey, mparent, inj, store);
|
|
711
784
|
}
|
|
712
785
|
inj.val = val;
|
|
713
786
|
// Original val reference may no longer be correct.
|
|
@@ -717,42 +790,41 @@ function inject(val, store, modify, current, inj) {
|
|
|
717
790
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
718
791
|
// Delete a key from a map or list.
|
|
719
792
|
const transform_DELETE = (inj) => {
|
|
720
|
-
// _setparentprop(inj, UNDEF)
|
|
721
793
|
inj.setval(UNDEF);
|
|
722
794
|
return UNDEF;
|
|
723
795
|
};
|
|
724
796
|
// Copy value from source data.
|
|
725
|
-
const transform_COPY = (inj, _val
|
|
797
|
+
const transform_COPY = (inj, _val) => {
|
|
726
798
|
const { mode, key } = inj;
|
|
727
799
|
let out = key;
|
|
728
800
|
if (!mode.startsWith(S_MKEY)) {
|
|
729
|
-
out = getprop(
|
|
801
|
+
out = getprop(inj.dparent, key);
|
|
730
802
|
inj.setval(out);
|
|
731
803
|
}
|
|
732
804
|
return out;
|
|
733
805
|
};
|
|
734
806
|
// As a value, inject the key of the parent node.
|
|
735
807
|
// As a key, defined the name of the key property in the source object.
|
|
736
|
-
const transform_KEY = (inj
|
|
808
|
+
const transform_KEY = (inj) => {
|
|
737
809
|
const { mode, path, parent } = inj;
|
|
738
810
|
// Do nothing in val mode.
|
|
739
811
|
if (S_MVAL !== mode) {
|
|
740
812
|
return UNDEF;
|
|
741
813
|
}
|
|
742
814
|
// Key is defined by $KEY meta property.
|
|
743
|
-
const keyspec = getprop(parent,
|
|
815
|
+
const keyspec = getprop(parent, S_BKEY);
|
|
744
816
|
if (UNDEF !== keyspec) {
|
|
745
|
-
|
|
746
|
-
return getprop(
|
|
817
|
+
delprop(parent, S_BKEY);
|
|
818
|
+
return getprop(inj.dparent, keyspec);
|
|
747
819
|
}
|
|
748
820
|
// Key is defined within general purpose $META object.
|
|
749
|
-
return getprop(getprop(parent,
|
|
821
|
+
return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2));
|
|
750
822
|
};
|
|
751
|
-
//
|
|
823
|
+
// Annotatea node. Does nothing itself, just used by
|
|
752
824
|
// other injectors, and is removed when called.
|
|
753
|
-
const
|
|
825
|
+
const transform_ANNO = (inj) => {
|
|
754
826
|
const { parent } = inj;
|
|
755
|
-
|
|
827
|
+
delprop(parent, S_BANNO);
|
|
756
828
|
return UNDEF;
|
|
757
829
|
};
|
|
758
830
|
// Merge a list of objects into the current object.
|
|
@@ -760,29 +832,31 @@ const transform_META = (inj) => {
|
|
|
760
832
|
// If the value is an array, the elements are first merged using `merge`.
|
|
761
833
|
// If the value is the empty string, merge the top level store.
|
|
762
834
|
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
763
|
-
const transform_MERGE = (inj
|
|
835
|
+
const transform_MERGE = (inj) => {
|
|
764
836
|
const { mode, key, parent } = inj;
|
|
837
|
+
// Ensures $MERGE is removed from parent list (val mode).
|
|
838
|
+
let out = UNDEF;
|
|
765
839
|
if (S_MKEYPRE === mode) {
|
|
766
|
-
|
|
840
|
+
out = key;
|
|
767
841
|
}
|
|
768
842
|
// Operate after child values have been transformed.
|
|
769
|
-
if (S_MKEYPOST === mode) {
|
|
843
|
+
else if (S_MKEYPOST === mode) {
|
|
844
|
+
out = key;
|
|
770
845
|
let args = getprop(parent, key);
|
|
771
|
-
args =
|
|
846
|
+
args = Array.isArray(args) ? args : [args];
|
|
772
847
|
// Remove the $MERGE command from a parent map.
|
|
773
848
|
inj.setval(UNDEF);
|
|
774
849
|
// Literals in the parent have precedence, but we still merge onto
|
|
775
850
|
// the parent object, so that node tree references are not changed.
|
|
776
851
|
const mergelist = [parent, ...args, clone(parent)];
|
|
777
852
|
merge(mergelist);
|
|
778
|
-
return key
|
|
853
|
+
// return key
|
|
779
854
|
}
|
|
780
|
-
|
|
781
|
-
return UNDEF;
|
|
855
|
+
return out;
|
|
782
856
|
};
|
|
783
857
|
// Convert a node to a list.
|
|
784
858
|
// Format: ['`$EACH`', '`source-path-of-node`', child-template]
|
|
785
|
-
const transform_EACH = (inj, _val,
|
|
859
|
+
const transform_EACH = (inj, _val, _ref, store) => {
|
|
786
860
|
// Remove arguments to avoid spurious processing.
|
|
787
861
|
if (null != inj.keys) {
|
|
788
862
|
inj.keys.length = 1;
|
|
@@ -795,7 +869,7 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
795
869
|
const child = clone(getprop(inj.parent, 2));
|
|
796
870
|
// Source data.
|
|
797
871
|
const srcstore = getprop(store, inj.base, store);
|
|
798
|
-
const src = getpath(
|
|
872
|
+
const src = getpath(srcstore, srcpath, inj);
|
|
799
873
|
// Create parallel data structures:
|
|
800
874
|
// source entries :: child templates
|
|
801
875
|
let tcur = [];
|
|
@@ -810,26 +884,33 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
810
884
|
tval = Object.entries(src).map(n => ({
|
|
811
885
|
...clone(child),
|
|
812
886
|
// Make a note of the key for $KEY transforms.
|
|
813
|
-
[
|
|
887
|
+
[S_BANNO]: { KEY: n[0] }
|
|
814
888
|
}));
|
|
815
889
|
}
|
|
816
890
|
let rval = [];
|
|
817
891
|
if (0 < size(tval)) {
|
|
818
892
|
tcur = null == src ? UNDEF : Object.values(src);
|
|
893
|
+
const ckey = getelem(inj.path, -2);
|
|
819
894
|
const tpath = slice(inj.path, -1);
|
|
895
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey];
|
|
820
896
|
// Parent structure.
|
|
821
|
-
const ckey = getelem(
|
|
897
|
+
// const ckey = getelem(cpath, -1)
|
|
822
898
|
tcur = { [ckey]: tcur };
|
|
823
899
|
if (1 < tpath.length) {
|
|
824
900
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
901
|
+
// const pkey = getelem(cpath, -2, S_DTOP)
|
|
825
902
|
tcur = { [pkey]: tcur };
|
|
903
|
+
dpath.push('$:' + pkey);
|
|
826
904
|
}
|
|
827
905
|
const tinj = inj.child(0, [ckey]);
|
|
828
906
|
tinj.path = tpath;
|
|
829
907
|
tinj.nodes = slice(inj.nodes, -1);
|
|
830
|
-
tinj.parent =
|
|
908
|
+
tinj.parent = getelem(tinj.nodes, -1);
|
|
909
|
+
setprop(tinj.parent, ckey, tval);
|
|
831
910
|
tinj.val = tval;
|
|
832
|
-
|
|
911
|
+
tinj.dpath = dpath;
|
|
912
|
+
tinj.dparent = tcur;
|
|
913
|
+
inject(tval, store, tinj);
|
|
833
914
|
rval = tinj.val;
|
|
834
915
|
}
|
|
835
916
|
_updateAncestors(inj, target, tkey, rval);
|
|
@@ -838,7 +919,7 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
838
919
|
};
|
|
839
920
|
// Convert a node to a map.
|
|
840
921
|
// Format: { '`$PACK`':['`source-path`', child-template]}
|
|
841
|
-
const transform_PACK = (inj, _val,
|
|
922
|
+
const transform_PACK = (inj, _val, _ref, store) => {
|
|
842
923
|
const { mode, key, path, parent, nodes } = inj;
|
|
843
924
|
// Defensive context checks.
|
|
844
925
|
if (S_MKEYPRE !== mode || S_string !== typeof key || null == path || null == nodes) {
|
|
@@ -849,31 +930,37 @@ const transform_PACK = (inj, _val, current, _ref, store) => {
|
|
|
849
930
|
const srcpath = args[0]; // Path to source data.
|
|
850
931
|
const child = clone(args[1]); // Child template.
|
|
851
932
|
// Find key and target node.
|
|
852
|
-
const keyprop = child[
|
|
933
|
+
const keyprop = child[S_BKEY];
|
|
853
934
|
const tkey = getelem(path, -2);
|
|
854
935
|
const target = nodes[path.length - 2] || nodes[path.length - 1];
|
|
855
936
|
// Source data
|
|
856
937
|
const srcstore = getprop(store, inj.base, store);
|
|
857
|
-
let src = getpath(
|
|
938
|
+
let src = getpath(srcstore, srcpath, inj);
|
|
858
939
|
// Prepare source as a list.
|
|
859
940
|
src = islist(src) ? src :
|
|
860
941
|
ismap(src) ? Object.entries(src)
|
|
861
|
-
.reduce((a, n) => (n[1][
|
|
942
|
+
.reduce((a, n) => (n[1][S_BANNO] = { KEY: n[0] }, a.push(n[1]), a), []) :
|
|
862
943
|
UNDEF;
|
|
863
944
|
if (null == src) {
|
|
864
945
|
return UNDEF;
|
|
865
946
|
}
|
|
866
947
|
// Get key if specified.
|
|
867
|
-
let childkey = getprop(child,
|
|
948
|
+
let childkey = getprop(child, S_BKEY);
|
|
868
949
|
let keyname = UNDEF === childkey ? keyprop : childkey;
|
|
869
|
-
|
|
950
|
+
delprop(child, S_BKEY);
|
|
870
951
|
// Build parallel target object.
|
|
871
952
|
let tval = {};
|
|
872
953
|
tval = src.reduce((a, n) => {
|
|
873
954
|
let kn = getprop(n, keyname);
|
|
874
955
|
setprop(a, kn, clone(child));
|
|
875
956
|
const nchild = getprop(a, kn);
|
|
876
|
-
|
|
957
|
+
const mval = getprop(n, S_BANNO);
|
|
958
|
+
if (UNDEF === mval) {
|
|
959
|
+
delprop(nchild, S_BANNO);
|
|
960
|
+
}
|
|
961
|
+
else {
|
|
962
|
+
setprop(nchild, S_BANNO, mval);
|
|
963
|
+
}
|
|
877
964
|
return a;
|
|
878
965
|
}, tval);
|
|
879
966
|
let rval = {};
|
|
@@ -887,27 +974,33 @@ const transform_PACK = (inj, _val, current, _ref, store) => {
|
|
|
887
974
|
}, tcur);
|
|
888
975
|
const tpath = slice(inj.path, -1);
|
|
889
976
|
const ckey = getelem(inj.path, -2);
|
|
977
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey];
|
|
890
978
|
tcur = { [ckey]: tcur };
|
|
891
979
|
if (1 < tpath.length) {
|
|
892
980
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
893
981
|
tcur = { [pkey]: tcur };
|
|
982
|
+
dpath.push('$:' + pkey);
|
|
894
983
|
}
|
|
895
984
|
const tinj = inj.child(0, [ckey]);
|
|
896
985
|
tinj.path = tpath;
|
|
897
986
|
tinj.nodes = slice(inj.nodes, -1);
|
|
898
|
-
tinj.parent = tcur
|
|
987
|
+
// tinj.parent = tcur
|
|
988
|
+
tinj.parent = getelem(tinj.nodes, -1);
|
|
899
989
|
tinj.val = tval;
|
|
900
|
-
|
|
990
|
+
tinj.dpath = dpath;
|
|
991
|
+
tinj.dparent = tcur;
|
|
992
|
+
inject(tval, store, tinj);
|
|
901
993
|
rval = tinj.val;
|
|
902
994
|
}
|
|
903
995
|
_updateAncestors(inj, target, tkey, rval);
|
|
904
996
|
// Drop transform key.
|
|
905
997
|
return UNDEF;
|
|
906
998
|
};
|
|
999
|
+
// TODO: not found ref should removed key (setprop UNDEF)
|
|
907
1000
|
// Reference original spec (enables recursice transformations)
|
|
908
1001
|
// Format: ['`$REF`', '`spec-path`']
|
|
909
|
-
const transform_REF = (inj, val,
|
|
910
|
-
const { nodes
|
|
1002
|
+
const transform_REF = (inj, val, _ref, store) => {
|
|
1003
|
+
const { nodes } = inj;
|
|
911
1004
|
if (S_MVAL !== inj.mode) {
|
|
912
1005
|
return UNDEF;
|
|
913
1006
|
}
|
|
@@ -916,7 +1009,11 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
916
1009
|
inj.keyI = inj.keys.length;
|
|
917
1010
|
// Spec reference.
|
|
918
1011
|
const spec = getprop(store, S_DSPEC)();
|
|
919
|
-
const ref = getpath(refpath,
|
|
1012
|
+
const ref = getpath(spec, refpath, {
|
|
1013
|
+
// TODO: test relative refs
|
|
1014
|
+
dpath: inj.path.slice(1),
|
|
1015
|
+
dparent: getpath(spec, inj.path.slice(1))
|
|
1016
|
+
});
|
|
920
1017
|
let hasSubRef = false;
|
|
921
1018
|
if (isnode(ref)) {
|
|
922
1019
|
walk(ref, (_k, v) => {
|
|
@@ -929,8 +1026,8 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
929
1026
|
let tref = clone(ref);
|
|
930
1027
|
const cpath = slice(inj.path, -3);
|
|
931
1028
|
const tpath = slice(inj.path, -1);
|
|
932
|
-
let tcur = getpath(
|
|
933
|
-
let tval = getpath(
|
|
1029
|
+
let tcur = getpath(store, cpath);
|
|
1030
|
+
let tval = getpath(store, tpath);
|
|
934
1031
|
let rval = UNDEF;
|
|
935
1032
|
if (!hasSubRef || UNDEF !== tval) {
|
|
936
1033
|
const tinj = inj.child(0, [getelem(tpath, -1)]);
|
|
@@ -938,7 +1035,9 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
938
1035
|
tinj.nodes = slice(inj.nodes, -1);
|
|
939
1036
|
tinj.parent = getelem(nodes, -2);
|
|
940
1037
|
tinj.val = tref;
|
|
941
|
-
|
|
1038
|
+
tinj.dpath = [...cpath];
|
|
1039
|
+
tinj.dparent = tcur;
|
|
1040
|
+
inject(tref, store, tinj);
|
|
942
1041
|
rval = tinj.val;
|
|
943
1042
|
}
|
|
944
1043
|
else {
|
|
@@ -955,12 +1054,14 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
955
1054
|
// Arrays are treated as if they are objects with indices as keys.
|
|
956
1055
|
function transform(data, // Source data to transform into new data (original not mutated)
|
|
957
1056
|
spec, // Transform specification; output follows this shape
|
|
958
|
-
extra, // Additional store of data and transforms.
|
|
959
|
-
modify // Optionally modify individual values.
|
|
960
|
-
) {
|
|
1057
|
+
// extra?: any, // Additional store of data and transforms.
|
|
1058
|
+
// modify?: Modify // Optionally modify individual values.
|
|
1059
|
+
injdef) {
|
|
961
1060
|
// Clone the spec so that the clone can be modified in place as the transform result.
|
|
962
1061
|
const origspec = spec;
|
|
963
1062
|
spec = clone(origspec);
|
|
1063
|
+
const extra = injdef?.extra;
|
|
1064
|
+
// const modify = injdef?.modify
|
|
964
1065
|
const extraTransforms = {};
|
|
965
1066
|
const extraData = null == extra ? UNDEF : items(extra)
|
|
966
1067
|
.reduce((a, n) => (n[0].startsWith(S_DS) ? extraTransforms[n[0]] = n[1] : (a[n[0]] = n[1]), a), {});
|
|
@@ -984,7 +1085,7 @@ modify // Optionally modify individual values.
|
|
|
984
1085
|
$DELETE: transform_DELETE,
|
|
985
1086
|
$COPY: transform_COPY,
|
|
986
1087
|
$KEY: transform_KEY,
|
|
987
|
-
$
|
|
1088
|
+
$ANNO: transform_ANNO,
|
|
988
1089
|
$MERGE: transform_MERGE,
|
|
989
1090
|
$EACH: transform_EACH,
|
|
990
1091
|
$PACK: transform_PACK,
|
|
@@ -992,12 +1093,13 @@ modify // Optionally modify individual values.
|
|
|
992
1093
|
// Custom extra transforms, if any.
|
|
993
1094
|
...extraTransforms,
|
|
994
1095
|
};
|
|
995
|
-
const out = inject(spec, store, modify,
|
|
1096
|
+
// const out = inject(spec, store, { modify, extra })
|
|
1097
|
+
const out = inject(spec, store, injdef);
|
|
996
1098
|
return out;
|
|
997
1099
|
}
|
|
998
1100
|
// A required string value. NOTE: Rejects empty strings.
|
|
999
|
-
const validate_STRING = (inj
|
|
1000
|
-
let out = getprop(
|
|
1101
|
+
const validate_STRING = (inj) => {
|
|
1102
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1001
1103
|
const t = typify(out);
|
|
1002
1104
|
if (S_string !== t) {
|
|
1003
1105
|
let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010');
|
|
@@ -1012,8 +1114,8 @@ const validate_STRING = (inj, _val, current) => {
|
|
|
1012
1114
|
return out;
|
|
1013
1115
|
};
|
|
1014
1116
|
// A required number value (int or float).
|
|
1015
|
-
const validate_NUMBER = (inj
|
|
1016
|
-
let out = getprop(
|
|
1117
|
+
const validate_NUMBER = (inj) => {
|
|
1118
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1017
1119
|
const t = typify(out);
|
|
1018
1120
|
if (S_number !== t) {
|
|
1019
1121
|
inj.errs.push(_invalidTypeMsg(inj.path, S_number, t, out, 'V1020'));
|
|
@@ -1022,8 +1124,8 @@ const validate_NUMBER = (inj, _val, current) => {
|
|
|
1022
1124
|
return out;
|
|
1023
1125
|
};
|
|
1024
1126
|
// A required boolean value.
|
|
1025
|
-
const validate_BOOLEAN = (inj
|
|
1026
|
-
let out = getprop(
|
|
1127
|
+
const validate_BOOLEAN = (inj) => {
|
|
1128
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1027
1129
|
const t = typify(out);
|
|
1028
1130
|
if (S_boolean !== t) {
|
|
1029
1131
|
inj.errs.push(_invalidTypeMsg(inj.path, S_boolean, t, out, 'V1030'));
|
|
@@ -1032,8 +1134,8 @@ const validate_BOOLEAN = (inj, _val, current) => {
|
|
|
1032
1134
|
return out;
|
|
1033
1135
|
};
|
|
1034
1136
|
// A required object (map) value (contents not validated).
|
|
1035
|
-
const validate_OBJECT = (inj
|
|
1036
|
-
let out = getprop(
|
|
1137
|
+
const validate_OBJECT = (inj) => {
|
|
1138
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1037
1139
|
const t = typify(out);
|
|
1038
1140
|
if (t !== S_object) {
|
|
1039
1141
|
inj.errs.push(_invalidTypeMsg(inj.path, S_object, t, out, 'V1040'));
|
|
@@ -1042,8 +1144,8 @@ const validate_OBJECT = (inj, _val, current) => {
|
|
|
1042
1144
|
return out;
|
|
1043
1145
|
};
|
|
1044
1146
|
// A required array (list) value (contents not validated).
|
|
1045
|
-
const validate_ARRAY = (inj
|
|
1046
|
-
let out = getprop(
|
|
1147
|
+
const validate_ARRAY = (inj) => {
|
|
1148
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1047
1149
|
const t = typify(out);
|
|
1048
1150
|
if (t !== S_array) {
|
|
1049
1151
|
inj.errs.push(_invalidTypeMsg(inj.path, S_array, t, out, 'V1050'));
|
|
@@ -1052,8 +1154,8 @@ const validate_ARRAY = (inj, _val, current) => {
|
|
|
1052
1154
|
return out;
|
|
1053
1155
|
};
|
|
1054
1156
|
// A required function value.
|
|
1055
|
-
const validate_FUNCTION = (inj
|
|
1056
|
-
let out = getprop(
|
|
1157
|
+
const validate_FUNCTION = (inj) => {
|
|
1158
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1057
1159
|
const t = typify(out);
|
|
1058
1160
|
if (S_function !== t) {
|
|
1059
1161
|
inj.errs.push(_invalidTypeMsg(inj.path, S_function, t, out, 'V1060'));
|
|
@@ -1062,13 +1164,14 @@ const validate_FUNCTION = (inj, _val, current) => {
|
|
|
1062
1164
|
return out;
|
|
1063
1165
|
};
|
|
1064
1166
|
// Allow any value.
|
|
1065
|
-
const validate_ANY = (inj
|
|
1066
|
-
|
|
1167
|
+
const validate_ANY = (inj) => {
|
|
1168
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1169
|
+
return out;
|
|
1067
1170
|
};
|
|
1068
1171
|
// Specify child values for map or list.
|
|
1069
1172
|
// Map syntax: {'`$CHILD`': child-template }
|
|
1070
1173
|
// List syntax: ['`$CHILD`', child-template ]
|
|
1071
|
-
const validate_CHILD = (inj
|
|
1174
|
+
const validate_CHILD = (inj) => {
|
|
1072
1175
|
const { mode, key, parent, keys, path } = inj;
|
|
1073
1176
|
// Setup data structures for validation by cloning child template.
|
|
1074
1177
|
// Map syntax.
|
|
@@ -1076,7 +1179,7 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1076
1179
|
const childtm = getprop(parent, key);
|
|
1077
1180
|
// Get corresponding current object.
|
|
1078
1181
|
const pkey = getprop(path, path.length - 2);
|
|
1079
|
-
let tval = getprop(
|
|
1182
|
+
let tval = getprop(inj.dparent, pkey);
|
|
1080
1183
|
if (UNDEF == tval) {
|
|
1081
1184
|
tval = {};
|
|
1082
1185
|
}
|
|
@@ -1091,7 +1194,6 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1091
1194
|
keys.push(ckey);
|
|
1092
1195
|
}
|
|
1093
1196
|
// Remove $CHILD to cleanup ouput.
|
|
1094
|
-
// _setparentprop(inj, UNDEF)
|
|
1095
1197
|
inj.setval(UNDEF);
|
|
1096
1198
|
return UNDEF;
|
|
1097
1199
|
}
|
|
@@ -1103,32 +1205,32 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1103
1205
|
return UNDEF;
|
|
1104
1206
|
}
|
|
1105
1207
|
const childtm = getprop(parent, 1);
|
|
1106
|
-
if (UNDEF ===
|
|
1208
|
+
if (UNDEF === inj.dparent) {
|
|
1107
1209
|
// Empty list as default.
|
|
1108
1210
|
parent.length = 0;
|
|
1109
1211
|
return UNDEF;
|
|
1110
1212
|
}
|
|
1111
|
-
if (!islist(
|
|
1112
|
-
const msg = _invalidTypeMsg(slice(inj.path, -1), S_array, typify(
|
|
1213
|
+
if (!islist(inj.dparent)) {
|
|
1214
|
+
const msg = _invalidTypeMsg(slice(inj.path, -1), S_array, typify(inj.dparent), inj.dparent, 'V0230');
|
|
1113
1215
|
inj.errs.push(msg);
|
|
1114
1216
|
inj.keyI = parent.length;
|
|
1115
|
-
return
|
|
1217
|
+
return inj.dparent;
|
|
1116
1218
|
}
|
|
1117
1219
|
// Clone children abd reset inj key index.
|
|
1118
1220
|
// The inject child loop will now iterate over the cloned children,
|
|
1119
1221
|
// validating them againt the current list values.
|
|
1120
|
-
|
|
1121
|
-
parent.length =
|
|
1222
|
+
inj.dparent.map((_n, i) => parent[i] = clone(childtm));
|
|
1223
|
+
parent.length = inj.dparent.length;
|
|
1122
1224
|
inj.keyI = 0;
|
|
1123
|
-
const out = getprop(
|
|
1225
|
+
const out = getprop(inj.dparent, 0);
|
|
1124
1226
|
return out;
|
|
1125
1227
|
}
|
|
1126
1228
|
return UNDEF;
|
|
1127
1229
|
};
|
|
1128
1230
|
// Match at least one of the specified shapes.
|
|
1129
1231
|
// Syntax: ['`$ONE`', alt0, alt1, ...]okI
|
|
1130
|
-
const validate_ONE = (inj, _val,
|
|
1131
|
-
const { mode, parent,
|
|
1232
|
+
const validate_ONE = (inj, _val, _ref, store) => {
|
|
1233
|
+
const { mode, parent, keyI } = inj;
|
|
1132
1234
|
// Only operate in val mode, since parent is a list.
|
|
1133
1235
|
if (S_MVAL === mode) {
|
|
1134
1236
|
if (!islist(parent) || 0 !== keyI) {
|
|
@@ -1139,7 +1241,7 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1139
1241
|
}
|
|
1140
1242
|
inj.keyI = inj.keys.length;
|
|
1141
1243
|
// Clean up structure, replacing [$ONE, ...] with current
|
|
1142
|
-
|
|
1244
|
+
inj.setval(inj.dparent, 2);
|
|
1143
1245
|
inj.path = slice(inj.path, -1);
|
|
1144
1246
|
inj.key = getelem(inj.path, -1);
|
|
1145
1247
|
let tvals = slice(parent, 1);
|
|
@@ -1154,9 +1256,13 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1154
1256
|
// If match, then errs.length = 0
|
|
1155
1257
|
let terrs = [];
|
|
1156
1258
|
const vstore = { ...store };
|
|
1157
|
-
vstore.$TOP =
|
|
1158
|
-
const vcurrent = validate(
|
|
1159
|
-
|
|
1259
|
+
vstore.$TOP = inj.dparent;
|
|
1260
|
+
const vcurrent = validate(inj.dparent, tval, {
|
|
1261
|
+
extra: vstore,
|
|
1262
|
+
errs: terrs,
|
|
1263
|
+
meta: inj.meta,
|
|
1264
|
+
});
|
|
1265
|
+
inj.setval(vcurrent, -2);
|
|
1160
1266
|
// Accept current value if there was a match
|
|
1161
1267
|
if (0 === terrs.length) {
|
|
1162
1268
|
return;
|
|
@@ -1166,12 +1272,12 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1166
1272
|
const valdesc = tvals
|
|
1167
1273
|
.map((v) => stringify(v))
|
|
1168
1274
|
.join(', ')
|
|
1169
|
-
.replace(
|
|
1170
|
-
inj.errs.push(_invalidTypeMsg(inj.path, (1 < tvals.length ? 'one of ' : '') + valdesc, typify(
|
|
1275
|
+
.replace(R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1276
|
+
inj.errs.push(_invalidTypeMsg(inj.path, (1 < tvals.length ? 'one of ' : '') + valdesc, typify(inj.dparent), inj.dparent, 'V0210'));
|
|
1171
1277
|
}
|
|
1172
1278
|
};
|
|
1173
|
-
const validate_EXACT = (inj
|
|
1174
|
-
const { mode, parent, key, keyI
|
|
1279
|
+
const validate_EXACT = (inj) => {
|
|
1280
|
+
const { mode, parent, key, keyI } = inj;
|
|
1175
1281
|
// Only operate in val mode, since parent is a list.
|
|
1176
1282
|
if (S_MVAL === mode) {
|
|
1177
1283
|
if (!islist(parent) || 0 !== keyI) {
|
|
@@ -1181,8 +1287,8 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1181
1287
|
return;
|
|
1182
1288
|
}
|
|
1183
1289
|
inj.keyI = inj.keys.length;
|
|
1184
|
-
// Clean up structure, replacing [$EXACT, ...] with current
|
|
1185
|
-
inj.setval(
|
|
1290
|
+
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1291
|
+
inj.setval(inj.dparent, 2);
|
|
1186
1292
|
inj.path = slice(inj.path, 0, inj.path.length - 1);
|
|
1187
1293
|
inj.key = getelem(inj.path, -1);
|
|
1188
1294
|
let tvals = slice(parent, 1);
|
|
@@ -1195,9 +1301,9 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1195
1301
|
// See if we can find an exact value match.
|
|
1196
1302
|
let currentstr = undefined;
|
|
1197
1303
|
for (let tval of tvals) {
|
|
1198
|
-
let exactmatch = tval ===
|
|
1304
|
+
let exactmatch = tval === inj.dparent;
|
|
1199
1305
|
if (!exactmatch && isnode(tval)) {
|
|
1200
|
-
currentstr = undefined === currentstr ? stringify(
|
|
1306
|
+
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
|
|
1201
1307
|
const tvalstr = stringify(tval);
|
|
1202
1308
|
exactmatch = tvalstr === currentstr;
|
|
1203
1309
|
}
|
|
@@ -1208,23 +1314,29 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1208
1314
|
const valdesc = tvals
|
|
1209
1315
|
.map((v) => stringify(v))
|
|
1210
1316
|
.join(', ')
|
|
1211
|
-
.replace(
|
|
1317
|
+
.replace(R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1212
1318
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < inj.path.length ? '' : 'value ') +
|
|
1213
|
-
'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc, typify(
|
|
1319
|
+
'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc, typify(inj.dparent), inj.dparent, 'V0110'));
|
|
1214
1320
|
}
|
|
1215
1321
|
else {
|
|
1216
|
-
|
|
1322
|
+
delprop(parent, key);
|
|
1217
1323
|
}
|
|
1218
1324
|
};
|
|
1219
1325
|
// This is the "modify" argument to inject. Use this to perform
|
|
1220
1326
|
// generic validation. Runs *after* any special commands.
|
|
1221
|
-
const _validation = (pval, key, parent, inj
|
|
1327
|
+
const _validation = (pval, key, parent, inj) => {
|
|
1222
1328
|
if (UNDEF === inj) {
|
|
1223
1329
|
return;
|
|
1224
1330
|
}
|
|
1331
|
+
if (SKIP === pval) {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
// select needs exact matches
|
|
1335
|
+
const exact = getprop(inj.meta, '`$EXACT`');
|
|
1225
1336
|
// Current val to verify.
|
|
1226
|
-
const cval = getprop(
|
|
1227
|
-
if (UNDEF === cval || UNDEF === inj) {
|
|
1337
|
+
const cval = getprop(inj.dparent, key);
|
|
1338
|
+
// if (UNDEF === cval || UNDEF === inj) {
|
|
1339
|
+
if (UNDEF === inj || (!exact && UNDEF === cval)) {
|
|
1228
1340
|
return;
|
|
1229
1341
|
}
|
|
1230
1342
|
const ptype = typify(pval);
|
|
@@ -1233,6 +1345,7 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1233
1345
|
return;
|
|
1234
1346
|
}
|
|
1235
1347
|
const ctype = typify(cval);
|
|
1348
|
+
// console.log('VALID-A', pval, ptype, cval, ctype)
|
|
1236
1349
|
// Type mismatch.
|
|
1237
1350
|
if (ptype !== ctype && UNDEF !== pval) {
|
|
1238
1351
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0010'));
|
|
@@ -1263,7 +1376,7 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1263
1376
|
// Object is open, so merge in extra keys.
|
|
1264
1377
|
merge([pval, cval]);
|
|
1265
1378
|
if (isnode(pval)) {
|
|
1266
|
-
|
|
1379
|
+
delprop(pval, '`$OPEN`');
|
|
1267
1380
|
}
|
|
1268
1381
|
}
|
|
1269
1382
|
}
|
|
@@ -1272,6 +1385,13 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1272
1385
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0030'));
|
|
1273
1386
|
}
|
|
1274
1387
|
}
|
|
1388
|
+
else if (exact) {
|
|
1389
|
+
// else if (inj.meta['`$EXACT`']) {
|
|
1390
|
+
// console.log('VALID-X', cval, pval)
|
|
1391
|
+
if (cval !== pval) {
|
|
1392
|
+
inj.errs.push('Value ' + cval + ' should equal ' + pval);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1275
1395
|
else {
|
|
1276
1396
|
// Spec value was a default, copy over data
|
|
1277
1397
|
setprop(parent, key, cval);
|
|
@@ -1290,10 +1410,10 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1290
1410
|
// is open, and if missing an empty default is inserted.
|
|
1291
1411
|
function validate(data, // Source data to transform into new data (original not mutated)
|
|
1292
1412
|
spec, // Transform specification; output follows this shape
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
const errs =
|
|
1413
|
+
injdef) {
|
|
1414
|
+
const extra = injdef?.extra;
|
|
1415
|
+
const collect = null != injdef?.errs;
|
|
1416
|
+
const errs = injdef?.errs || [];
|
|
1297
1417
|
const store = {
|
|
1298
1418
|
// Remove the transform commands.
|
|
1299
1419
|
$DELETE: null,
|
|
@@ -1318,20 +1438,221 @@ collecterrs) {
|
|
|
1318
1438
|
// NOTE: collecterrs paramter always wins.
|
|
1319
1439
|
$ERRS: errs,
|
|
1320
1440
|
};
|
|
1321
|
-
const out = transform(data, spec,
|
|
1322
|
-
|
|
1441
|
+
const out = transform(data, spec, {
|
|
1442
|
+
meta: injdef?.meta,
|
|
1443
|
+
extra: store,
|
|
1444
|
+
modify: _validation,
|
|
1445
|
+
handler: _validatehandler
|
|
1446
|
+
});
|
|
1447
|
+
const generr = (0 < errs.length && !collect);
|
|
1323
1448
|
if (generr) {
|
|
1324
1449
|
throw new Error('Invalid data: ' + errs.join(' | '));
|
|
1325
1450
|
}
|
|
1326
1451
|
return out;
|
|
1327
1452
|
}
|
|
1453
|
+
const select_AND = (inj, val, _ref, store) => {
|
|
1454
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1455
|
+
const terms = getprop(inj.parent, inj.key);
|
|
1456
|
+
const src = getprop(store, inj.base, store);
|
|
1457
|
+
for (let term of terms) {
|
|
1458
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1459
|
+
let terrs = [];
|
|
1460
|
+
validate(src, term, {
|
|
1461
|
+
extra: store,
|
|
1462
|
+
errs: terrs,
|
|
1463
|
+
meta: inj.meta,
|
|
1464
|
+
});
|
|
1465
|
+
if (0 != terrs.length) {
|
|
1466
|
+
inj.errs.push('AND:' + stringify(val) + ' fail:' + stringify(term));
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
const select_OR = (inj, val, _ref, store) => {
|
|
1472
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1473
|
+
const terms = getprop(inj.parent, inj.key);
|
|
1474
|
+
const src = getprop(store, inj.base, store);
|
|
1475
|
+
for (let term of terms) {
|
|
1476
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1477
|
+
let terrs = [];
|
|
1478
|
+
validate(src, term, {
|
|
1479
|
+
extra: store,
|
|
1480
|
+
errs: terrs,
|
|
1481
|
+
meta: inj.meta,
|
|
1482
|
+
});
|
|
1483
|
+
if (0 === terrs.length) {
|
|
1484
|
+
return;
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
inj.errs.push('OR:' + stringify(val) + ' fail:' + stringify(terms));
|
|
1488
|
+
}
|
|
1489
|
+
};
|
|
1490
|
+
const select_CMP = (inj, _val, ref, store) => {
|
|
1491
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1492
|
+
const term = getprop(inj.parent, inj.key);
|
|
1493
|
+
const src = getprop(store, inj.base, store);
|
|
1494
|
+
const gkey = getelem(inj.path, -2);
|
|
1495
|
+
const tval = getprop(src, gkey);
|
|
1496
|
+
let pass = false;
|
|
1497
|
+
if ('$GT' === ref && tval > term) {
|
|
1498
|
+
pass = true;
|
|
1499
|
+
}
|
|
1500
|
+
else if ('$LT' === ref && tval < term) {
|
|
1501
|
+
pass = true;
|
|
1502
|
+
}
|
|
1503
|
+
else if ('$GTE' === ref && tval >= term) {
|
|
1504
|
+
pass = true;
|
|
1505
|
+
}
|
|
1506
|
+
else if ('$LTE' === ref && tval <= term) {
|
|
1507
|
+
pass = true;
|
|
1508
|
+
}
|
|
1509
|
+
if (pass) {
|
|
1510
|
+
// Update spec to match found value so that _validate does not complain.
|
|
1511
|
+
const gp = getelem(inj.nodes, -2);
|
|
1512
|
+
setprop(gp, gkey, tval);
|
|
1513
|
+
}
|
|
1514
|
+
else {
|
|
1515
|
+
inj.errs.push('CMP: fail:' + ref + ' ' + stringify(term));
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
return UNDEF;
|
|
1519
|
+
};
|
|
1520
|
+
// Select children from a top-level object that match a MongoDB-style query.
|
|
1521
|
+
// Supports $and, $or, and equality comparisons.
|
|
1522
|
+
// For arrays, children are elements; for objects, children are values.
|
|
1523
|
+
// TODO: swap arg order for consistency
|
|
1524
|
+
function select(query, children) {
|
|
1525
|
+
if (!isnode(children)) {
|
|
1526
|
+
return [];
|
|
1527
|
+
}
|
|
1528
|
+
if (ismap(children)) {
|
|
1529
|
+
children = items(children).map(n => (n[1][S_DKEY] = n[0], n[1]));
|
|
1530
|
+
}
|
|
1531
|
+
else {
|
|
1532
|
+
children = children.map((n, i) => ((ismap(n) ? n[S_DKEY] = i : null), n));
|
|
1533
|
+
}
|
|
1534
|
+
const results = [];
|
|
1535
|
+
const injdef = {
|
|
1536
|
+
errs: [],
|
|
1537
|
+
meta: { '`$EXACT`': true },
|
|
1538
|
+
extra: {
|
|
1539
|
+
$AND: select_AND,
|
|
1540
|
+
$OR: select_OR,
|
|
1541
|
+
$GT: select_CMP,
|
|
1542
|
+
$LT: select_CMP,
|
|
1543
|
+
$GTE: select_CMP,
|
|
1544
|
+
$LTE: select_CMP,
|
|
1545
|
+
}
|
|
1546
|
+
};
|
|
1547
|
+
const q = clone(query);
|
|
1548
|
+
walk(q, (_k, v) => {
|
|
1549
|
+
if (ismap(v)) {
|
|
1550
|
+
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true));
|
|
1551
|
+
}
|
|
1552
|
+
return v;
|
|
1553
|
+
});
|
|
1554
|
+
for (const child of children) {
|
|
1555
|
+
// console.log('CHILD', child, q)
|
|
1556
|
+
injdef.errs = [];
|
|
1557
|
+
validate(child, clone(q), injdef);
|
|
1558
|
+
if (0 === size(injdef.errs)) {
|
|
1559
|
+
results.push(child);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
return results;
|
|
1563
|
+
}
|
|
1564
|
+
// Injection state used for recursive injection into JSON - like data structures.
|
|
1565
|
+
class Injection {
|
|
1566
|
+
constructor(val, parent) {
|
|
1567
|
+
this.val = val;
|
|
1568
|
+
this.parent = parent;
|
|
1569
|
+
this.errs = [];
|
|
1570
|
+
this.dparent = UNDEF;
|
|
1571
|
+
this.dpath = [S_DTOP];
|
|
1572
|
+
this.mode = S_MVAL;
|
|
1573
|
+
this.full = false;
|
|
1574
|
+
this.keyI = 0;
|
|
1575
|
+
this.keys = [S_DTOP];
|
|
1576
|
+
this.key = S_DTOP;
|
|
1577
|
+
this.path = [S_DTOP];
|
|
1578
|
+
this.nodes = [parent];
|
|
1579
|
+
this.handler = _injecthandler;
|
|
1580
|
+
this.base = S_DTOP;
|
|
1581
|
+
this.meta = {};
|
|
1582
|
+
}
|
|
1583
|
+
toString(prefix) {
|
|
1584
|
+
return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
|
|
1585
|
+
pad(pathify(this.path, 1)) +
|
|
1586
|
+
this.mode + (this.full ? '/full' : '') + S_CN +
|
|
1587
|
+
'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
|
|
1588
|
+
' p=' + stringify(this.parent, -1, 1) +
|
|
1589
|
+
' m=' + stringify(this.meta, -1, 1) +
|
|
1590
|
+
' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
|
|
1591
|
+
' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1);
|
|
1592
|
+
}
|
|
1593
|
+
descend() {
|
|
1594
|
+
this.meta.__d++;
|
|
1595
|
+
const parentkey = getelem(this.path, -2);
|
|
1596
|
+
// Resolve current node in store for local paths.
|
|
1597
|
+
if (UNDEF === this.dparent) {
|
|
1598
|
+
// Even if there's no data, dpath should continue to match path, so that
|
|
1599
|
+
// relative paths work properly.
|
|
1600
|
+
if (1 < this.dpath.length) {
|
|
1601
|
+
this.dpath = [...this.dpath, parentkey];
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
else {
|
|
1605
|
+
// this.dparent is the containing node of the current store value.
|
|
1606
|
+
if (null != parentkey) {
|
|
1607
|
+
this.dparent = getprop(this.dparent, parentkey);
|
|
1608
|
+
let lastpart = getelem(this.dpath, -1);
|
|
1609
|
+
if (lastpart === '$:' + parentkey) {
|
|
1610
|
+
this.dpath = slice(this.dpath, -1);
|
|
1611
|
+
}
|
|
1612
|
+
else {
|
|
1613
|
+
this.dpath = [...this.dpath, parentkey];
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
return this.dparent;
|
|
1618
|
+
}
|
|
1619
|
+
child(keyI, keys) {
|
|
1620
|
+
const key = strkey(keys[keyI]);
|
|
1621
|
+
const val = this.val;
|
|
1622
|
+
const cinj = new Injection(getprop(val, key), val);
|
|
1623
|
+
cinj.keyI = keyI;
|
|
1624
|
+
cinj.keys = keys;
|
|
1625
|
+
cinj.key = key;
|
|
1626
|
+
cinj.path = [...(this.path || []), key];
|
|
1627
|
+
cinj.nodes = [...(this.nodes || []), val];
|
|
1628
|
+
cinj.mode = this.mode;
|
|
1629
|
+
cinj.handler = this.handler;
|
|
1630
|
+
cinj.modify = this.modify;
|
|
1631
|
+
cinj.base = this.base;
|
|
1632
|
+
cinj.meta = this.meta;
|
|
1633
|
+
cinj.errs = this.errs;
|
|
1634
|
+
cinj.prior = this;
|
|
1635
|
+
cinj.dpath = [...this.dpath];
|
|
1636
|
+
cinj.dparent = this.dparent;
|
|
1637
|
+
return cinj;
|
|
1638
|
+
}
|
|
1639
|
+
setval(val, ancestor) {
|
|
1640
|
+
if (null == ancestor || ancestor < 2) {
|
|
1641
|
+
return UNDEF === val ?
|
|
1642
|
+
delprop(this.parent, this.key) :
|
|
1643
|
+
setprop(this.parent, this.key, val);
|
|
1644
|
+
}
|
|
1645
|
+
else {
|
|
1646
|
+
const aval = getelem(this.nodes, 0 - ancestor);
|
|
1647
|
+
const akey = getelem(this.path, 0 - ancestor);
|
|
1648
|
+
return UNDEF === val ?
|
|
1649
|
+
delprop(aval, akey) :
|
|
1650
|
+
setprop(aval, akey, val);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1328
1654
|
// Internal utilities
|
|
1329
1655
|
// ==================
|
|
1330
|
-
// Set inj.key property of inj.parent node, ensuring reference consistency
|
|
1331
|
-
// when needed by implementation language.
|
|
1332
|
-
// function _setparentprop(inj: Injection, val: any) {
|
|
1333
|
-
// setprop(inj.parent, inj.key, val)
|
|
1334
|
-
// }
|
|
1335
1656
|
// Update all references to target in inj.nodes.
|
|
1336
1657
|
function _updateAncestors(_inj, target, tkey, tval) {
|
|
1337
1658
|
// SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
|
|
@@ -1350,20 +1671,38 @@ function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
|
1350
1671
|
}
|
|
1351
1672
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
1352
1673
|
// call the function passing the injection inj. This is how transforms operate.
|
|
1353
|
-
const _injecthandler = (inj, val,
|
|
1674
|
+
const _injecthandler = (inj, val, ref, store) => {
|
|
1354
1675
|
let out = val;
|
|
1355
1676
|
const iscmd = isfunc(val) && (UNDEF === ref || ref.startsWith(S_DS));
|
|
1356
1677
|
// Only call val function if it is a special command ($NAME format).
|
|
1357
1678
|
if (iscmd) {
|
|
1358
|
-
out = val(inj, val,
|
|
1679
|
+
out = val(inj, val, ref, store);
|
|
1359
1680
|
}
|
|
1360
1681
|
// Update parent with value. Ensures references remain in node tree.
|
|
1361
1682
|
else if (S_MVAL === inj.mode && inj.full) {
|
|
1362
|
-
// _setparentprop(inj, val)
|
|
1363
1683
|
inj.setval(val);
|
|
1364
1684
|
}
|
|
1365
1685
|
return out;
|
|
1366
1686
|
};
|
|
1687
|
+
const _validatehandler = (inj, val, ref, store) => {
|
|
1688
|
+
let out = val;
|
|
1689
|
+
const m = ref.match(R_META_PATH);
|
|
1690
|
+
const ismetapath = null != m;
|
|
1691
|
+
if (ismetapath) {
|
|
1692
|
+
if ('=' === m[2]) {
|
|
1693
|
+
inj.setval(['`$EXACT`', val]);
|
|
1694
|
+
}
|
|
1695
|
+
else {
|
|
1696
|
+
inj.setval(val);
|
|
1697
|
+
}
|
|
1698
|
+
inj.keyI = -1;
|
|
1699
|
+
out = SKIP;
|
|
1700
|
+
}
|
|
1701
|
+
else {
|
|
1702
|
+
out = _injecthandler(inj, val, ref, store);
|
|
1703
|
+
}
|
|
1704
|
+
return out;
|
|
1705
|
+
};
|
|
1367
1706
|
// Inject values from a data store into a string. Not a public utility - used by
|
|
1368
1707
|
// `inject`. Inject are marked with `path` where path is resolved
|
|
1369
1708
|
// with getpath against the store or current (if defined)
|
|
@@ -1373,14 +1712,14 @@ const _injecthandler = (inj, val, current, ref, store) => {
|
|
|
1373
1712
|
// upper case letters only, and 999 is any digits, which are
|
|
1374
1713
|
// discarded. This syntax specifies the name of a transform, and
|
|
1375
1714
|
// optionally allows transforms to be ordered by alphanumeric sorting.
|
|
1376
|
-
function _injectstr(val, store,
|
|
1715
|
+
function _injectstr(val, store, inj) {
|
|
1377
1716
|
// Can't inject into non-strings
|
|
1378
1717
|
if (S_string !== typeof val || S_MT === val) {
|
|
1379
1718
|
return S_MT;
|
|
1380
1719
|
}
|
|
1381
1720
|
let out = val;
|
|
1382
1721
|
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
1383
|
-
const m = val.match(
|
|
1722
|
+
const m = val.match(R_INJECTION_FULL);
|
|
1384
1723
|
// Full string of the val is an injection.
|
|
1385
1724
|
if (m) {
|
|
1386
1725
|
if (null != inj) {
|
|
@@ -1388,37 +1727,38 @@ function _injectstr(val, store, current, inj) {
|
|
|
1388
1727
|
}
|
|
1389
1728
|
let pathref = m[1];
|
|
1390
1729
|
// Special escapes inside injection.
|
|
1391
|
-
pathref =
|
|
1392
|
-
|
|
1730
|
+
pathref = 3 < pathref.length ?
|
|
1731
|
+
pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) :
|
|
1732
|
+
pathref;
|
|
1393
1733
|
// Get the extracted path reference.
|
|
1394
|
-
out = getpath(
|
|
1734
|
+
out = getpath(store, pathref, inj);
|
|
1395
1735
|
}
|
|
1396
1736
|
else {
|
|
1397
1737
|
// Check for injections within the string.
|
|
1398
1738
|
const partial = (_m, ref) => {
|
|
1399
1739
|
// Special escapes inside injection.
|
|
1400
|
-
ref = 3 < ref.length ? ref.replace(
|
|
1740
|
+
ref = 3 < ref.length ? ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) : ref;
|
|
1401
1741
|
if (inj) {
|
|
1402
1742
|
inj.full = false;
|
|
1403
1743
|
}
|
|
1404
|
-
const found = getpath(
|
|
1744
|
+
const found = getpath(store, ref, inj);
|
|
1405
1745
|
// Ensure inject value is a string.
|
|
1406
1746
|
return UNDEF === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
1407
1747
|
};
|
|
1408
|
-
out = val.replace(
|
|
1748
|
+
out = val.replace(R_INJECTION_PARTIAL, partial);
|
|
1409
1749
|
// Also call the inj handler on the entire string, providing the
|
|
1410
1750
|
// option for custom injection.
|
|
1411
1751
|
if (null != inj && isfunc(inj.handler)) {
|
|
1412
1752
|
inj.full = true;
|
|
1413
|
-
out = inj.handler(inj, out,
|
|
1753
|
+
out = inj.handler(inj, out, val, store);
|
|
1414
1754
|
}
|
|
1415
1755
|
}
|
|
1416
1756
|
return out;
|
|
1417
1757
|
}
|
|
1418
|
-
// Define a class to mirror the JavaScript implementation
|
|
1419
1758
|
class StructUtility {
|
|
1420
1759
|
constructor() {
|
|
1421
1760
|
this.clone = clone;
|
|
1761
|
+
this.delprop = delprop;
|
|
1422
1762
|
this.escre = escre;
|
|
1423
1763
|
this.escurl = escurl;
|
|
1424
1764
|
this.getelem = getelem;
|
|
@@ -1434,10 +1774,12 @@ class StructUtility {
|
|
|
1434
1774
|
this.isnode = isnode;
|
|
1435
1775
|
this.items = items;
|
|
1436
1776
|
this.joinurl = joinurl;
|
|
1777
|
+
this.jsonify = jsonify;
|
|
1437
1778
|
this.keysof = keysof;
|
|
1438
1779
|
this.merge = merge;
|
|
1439
1780
|
this.pad = pad;
|
|
1440
1781
|
this.pathify = pathify;
|
|
1782
|
+
this.select = select;
|
|
1441
1783
|
this.setprop = setprop;
|
|
1442
1784
|
this.size = size;
|
|
1443
1785
|
this.slice = slice;
|