@voxgig/struct 0.0.1 → 0.0.3
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 +51 -35
- package/dist/struct.js +678 -301
- package/dist/struct.js.map +1 -1
- package/package.json +3 -3
- package/src/struct.ts +836 -369
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,45 +187,68 @@ 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
|
}
|
|
245
246
|
return val;
|
|
246
247
|
}
|
|
247
248
|
function pad(str, padding, padchar) {
|
|
248
|
-
str = stringify(str);
|
|
249
|
+
str = S_string === typeof str ? 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,35 @@ 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, flags) {
|
|
365
|
+
let str = S_null;
|
|
366
|
+
if (null != val) {
|
|
367
|
+
const indent = getprop(flags, 'indent', 2);
|
|
368
|
+
str = JSON.stringify(val, null, indent);
|
|
369
|
+
if (UNDEF === str) {
|
|
370
|
+
str = S_null;
|
|
371
|
+
}
|
|
372
|
+
const offset = getprop(flags, 'offset', 0);
|
|
373
|
+
if (0 < offset) {
|
|
374
|
+
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
375
|
+
// indented by offset.
|
|
376
|
+
str = '{\n' + str.split('\n').slice(1)
|
|
377
|
+
.map(n => pad(n, 0 - offset - size(n)))
|
|
378
|
+
.join('\n');
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return str;
|
|
358
382
|
}
|
|
359
383
|
// Safely stringify a value for humans (NOT JSON!).
|
|
360
384
|
function stringify(val, maxlen, pretty) {
|
|
@@ -363,30 +387,35 @@ function stringify(val, maxlen, pretty) {
|
|
|
363
387
|
if (UNDEF === val) {
|
|
364
388
|
return pretty ? '<>' : valstr;
|
|
365
389
|
}
|
|
366
|
-
|
|
367
|
-
valstr =
|
|
368
|
-
if (val !== null &&
|
|
369
|
-
typeof val === "object" &&
|
|
370
|
-
!Array.isArray(val)) {
|
|
371
|
-
const sortedObj = {};
|
|
372
|
-
for (const k of Object.keys(val).sort()) {
|
|
373
|
-
sortedObj[k] = val[k];
|
|
374
|
-
}
|
|
375
|
-
return sortedObj;
|
|
376
|
-
}
|
|
377
|
-
return val;
|
|
378
|
-
});
|
|
390
|
+
if (S_string === typeof val) {
|
|
391
|
+
valstr = val;
|
|
379
392
|
}
|
|
380
|
-
|
|
381
|
-
|
|
393
|
+
else {
|
|
394
|
+
try {
|
|
395
|
+
valstr = JSON.stringify(val, function (_key, val) {
|
|
396
|
+
if (val !== null &&
|
|
397
|
+
typeof val === "object" &&
|
|
398
|
+
!Array.isArray(val)) {
|
|
399
|
+
const sortedObj = {};
|
|
400
|
+
for (const k of Object.keys(val).sort()) {
|
|
401
|
+
sortedObj[k] = val[k];
|
|
402
|
+
}
|
|
403
|
+
return sortedObj;
|
|
404
|
+
}
|
|
405
|
+
return val;
|
|
406
|
+
});
|
|
407
|
+
valstr = valstr.replace(R_QUOTES, S_MT);
|
|
408
|
+
}
|
|
409
|
+
catch (err) {
|
|
410
|
+
valstr = S_MT + val;
|
|
411
|
+
}
|
|
382
412
|
}
|
|
383
|
-
valstr = S_string !== typeof valstr ? S_MT + valstr : valstr;
|
|
384
|
-
valstr = valstr.replace(/"/g, '');
|
|
385
413
|
if (null != maxlen && -1 < maxlen) {
|
|
386
414
|
let js = valstr.substring(0, maxlen);
|
|
387
415
|
valstr = maxlen < valstr.length ? (js.substring(0, maxlen - 3) + '...') : valstr;
|
|
388
416
|
}
|
|
389
417
|
if (pretty) {
|
|
418
|
+
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
390
419
|
let c = [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69]
|
|
391
420
|
.map(n => `\x1b[38;5;${n}m`), r = '\x1b[0m', d = 0, o = c[0], t = o;
|
|
392
421
|
for (const ch of valstr) {
|
|
@@ -426,8 +455,8 @@ function pathify(val, startin, endin) {
|
|
|
426
455
|
pathstr = path
|
|
427
456
|
// .filter((p: any, t: any) => (t = typeof p, S_string === t || S_number === t))
|
|
428
457
|
.filter((p) => iskey(p))
|
|
429
|
-
.map((p) =>
|
|
430
|
-
p.replace(
|
|
458
|
+
.map((p) => S_number === typeof p ? S_MT + Math.floor(p) :
|
|
459
|
+
p.replace(R_DOT, S_MT))
|
|
431
460
|
.join(S_DT);
|
|
432
461
|
}
|
|
433
462
|
}
|
|
@@ -443,28 +472,52 @@ function clone(val) {
|
|
|
443
472
|
const replacer = (_k, v) => S_function === typeof v ?
|
|
444
473
|
(refs.push(v), '`$FUNCTION:' + (refs.length - 1) + '`') : v;
|
|
445
474
|
const reviver = (_k, v, m) => S_string === typeof v ?
|
|
446
|
-
(m = v.match(
|
|
475
|
+
(m = v.match(R_FUNCTION_REF), m ? refs[m[1]] : v) : v;
|
|
447
476
|
return UNDEF === val ? UNDEF : JSON.parse(JSON.stringify(val, replacer), reviver);
|
|
448
477
|
}
|
|
478
|
+
// Safely delete a property from an object or array element.
|
|
479
|
+
// Undefined arguments and invalid keys are ignored.
|
|
480
|
+
// Returns the (possibly modified) parent.
|
|
481
|
+
// For objects, the property is deleted using the delete operator.
|
|
482
|
+
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
483
|
+
function delprop(parent, key) {
|
|
484
|
+
if (!iskey(key)) {
|
|
485
|
+
return parent;
|
|
486
|
+
}
|
|
487
|
+
if (ismap(parent)) {
|
|
488
|
+
// key = S_MT + key
|
|
489
|
+
key = strkey(key);
|
|
490
|
+
delete parent[key];
|
|
491
|
+
}
|
|
492
|
+
else if (islist(parent)) {
|
|
493
|
+
// Ensure key is an integer.
|
|
494
|
+
let keyI = +key;
|
|
495
|
+
if (isNaN(keyI)) {
|
|
496
|
+
return parent;
|
|
497
|
+
}
|
|
498
|
+
keyI = Math.floor(keyI);
|
|
499
|
+
// Delete list element at position keyI, shifting later elements down.
|
|
500
|
+
if (0 <= keyI && keyI < parent.length) {
|
|
501
|
+
for (let pI = keyI; pI < parent.length - 1; pI++) {
|
|
502
|
+
parent[pI] = parent[pI + 1];
|
|
503
|
+
}
|
|
504
|
+
parent.length = parent.length - 1;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return parent;
|
|
508
|
+
}
|
|
449
509
|
// Safely set a property. Undefined arguments and invalid keys are ignored.
|
|
450
510
|
// Returns the (possibly modified) parent.
|
|
451
|
-
// If the value is undefined the key will be deleted from the parent.
|
|
452
511
|
// If the parent is a list, and the key is negative, prepend the value.
|
|
453
512
|
// 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
513
|
function setprop(parent, key, val) {
|
|
457
514
|
if (!iskey(key)) {
|
|
458
515
|
return parent;
|
|
459
516
|
}
|
|
460
517
|
if (ismap(parent)) {
|
|
461
518
|
key = S_MT + key;
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
parent[key] = val;
|
|
467
|
-
}
|
|
519
|
+
const pany = parent;
|
|
520
|
+
pany[key] = val;
|
|
468
521
|
}
|
|
469
522
|
else if (islist(parent)) {
|
|
470
523
|
// Ensure key is an integer.
|
|
@@ -473,17 +526,8 @@ function setprop(parent, key, val) {
|
|
|
473
526
|
return parent;
|
|
474
527
|
}
|
|
475
528
|
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
529
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
486
|
-
|
|
530
|
+
if (0 <= keyI) {
|
|
487
531
|
parent[parent.length < keyI ? parent.length : keyI] = val;
|
|
488
532
|
}
|
|
489
533
|
// Prepend value if keyI is negative
|
|
@@ -552,7 +596,7 @@ function merge(val) {
|
|
|
552
596
|
let lenpath = path.length;
|
|
553
597
|
cI = lenpath - 1;
|
|
554
598
|
if (UNDEF === cur[cI]) {
|
|
555
|
-
cur[cI] = getpath(slice(path, 0, lenpath - 1)
|
|
599
|
+
cur[cI] = getpath(out, slice(path, 0, lenpath - 1));
|
|
556
600
|
}
|
|
557
601
|
// Create node if needed.
|
|
558
602
|
if (!isnode(cur[cI])) {
|
|
@@ -577,83 +621,121 @@ function merge(val) {
|
|
|
577
621
|
}
|
|
578
622
|
return out;
|
|
579
623
|
}
|
|
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) {
|
|
624
|
+
function getpath(store, path, injdef) {
|
|
589
625
|
// Operate on a string array.
|
|
590
|
-
const parts = islist(path) ? path :
|
|
626
|
+
const parts = islist(path) ? path :
|
|
627
|
+
'string' === typeof path ? path.split(S_DT) :
|
|
628
|
+
'number' === typeof path ? [strkey(path)] : UNDEF;
|
|
591
629
|
if (UNDEF === parts) {
|
|
592
630
|
return UNDEF;
|
|
593
631
|
}
|
|
594
|
-
let root = store
|
|
632
|
+
// let root = store
|
|
595
633
|
let val = store;
|
|
596
|
-
const base = getprop(
|
|
634
|
+
const base = getprop(injdef, S_base);
|
|
635
|
+
const src = getprop(store, base, store);
|
|
636
|
+
const numparts = size(parts);
|
|
637
|
+
const dparent = getprop(injdef, 'dparent');
|
|
597
638
|
// 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
|
-
|
|
639
|
+
if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
|
|
640
|
+
val = src;
|
|
641
|
+
}
|
|
642
|
+
else if (0 < numparts) {
|
|
643
|
+
// Check for $ACTIONs
|
|
644
|
+
if (1 === numparts) {
|
|
645
|
+
val = getprop(store, parts[0]);
|
|
646
|
+
}
|
|
647
|
+
if (!isfunc(val)) {
|
|
648
|
+
val = src;
|
|
649
|
+
const m = parts[0].match(R_META_PATH);
|
|
650
|
+
if (m && injdef && injdef.meta) {
|
|
651
|
+
val = getprop(injdef.meta, m[1]);
|
|
652
|
+
parts[0] = m[3];
|
|
653
|
+
}
|
|
654
|
+
const dpath = getprop(injdef, 'dpath');
|
|
655
|
+
for (let pI = 0; UNDEF !== val && pI < parts.length; pI++) {
|
|
656
|
+
let part = parts[pI];
|
|
657
|
+
if (injdef && S_DKEY === part) {
|
|
658
|
+
part = getprop(injdef, S_key);
|
|
659
|
+
}
|
|
660
|
+
else if (injdef && part.startsWith('$GET:')) {
|
|
661
|
+
// $GET:path$ -> get store value, use as path part (string)
|
|
662
|
+
part = stringify(getpath(src, part.substring(5, part.length - 1)));
|
|
663
|
+
}
|
|
664
|
+
else if (injdef && part.startsWith('$REF:')) {
|
|
665
|
+
// $REF:refpath$ -> get spec value, use as path part (string)
|
|
666
|
+
part = stringify(getpath(getprop(store, S_DSPEC), part.substring(5, part.length - 1)));
|
|
667
|
+
}
|
|
668
|
+
else if (injdef && part.startsWith('$META:')) {
|
|
669
|
+
// $META:metapath$ -> get meta value, use as path part (string)
|
|
670
|
+
part = stringify(getpath(getprop(injdef, 'meta'), part.substring(6, part.length - 1)));
|
|
671
|
+
}
|
|
672
|
+
// $$ escapes $
|
|
673
|
+
part = part.replace(R_DOUBLE_DOLLAR, '$');
|
|
674
|
+
if (S_MT === part) {
|
|
675
|
+
let ascends = 0;
|
|
676
|
+
while (S_MT === parts[1 + pI]) {
|
|
677
|
+
ascends++;
|
|
678
|
+
pI++;
|
|
679
|
+
}
|
|
680
|
+
if (injdef && 0 < ascends) {
|
|
681
|
+
if (pI === parts.length - 1) {
|
|
682
|
+
ascends--;
|
|
683
|
+
}
|
|
684
|
+
if (0 === ascends) {
|
|
685
|
+
val = dparent;
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1));
|
|
689
|
+
if (ascends <= size(dpath)) {
|
|
690
|
+
val = getpath(store, fullpath);
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
val = UNDEF;
|
|
694
|
+
}
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
else {
|
|
699
|
+
val = dparent;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
val = getprop(val, part);
|
|
704
|
+
}
|
|
620
705
|
}
|
|
621
706
|
}
|
|
622
707
|
}
|
|
623
708
|
// Inj may provide a custom handler to modify found value.
|
|
624
|
-
|
|
709
|
+
const handler = getprop(injdef, 'handler');
|
|
710
|
+
if (null != injdef && isfunc(handler)) {
|
|
625
711
|
const ref = pathify(path);
|
|
626
|
-
val =
|
|
712
|
+
val = handler(injdef, val, ref, store);
|
|
627
713
|
}
|
|
628
714
|
return val;
|
|
629
715
|
}
|
|
630
|
-
// let cI = 0
|
|
631
716
|
// Inject values from a data store into a node recursively, resolving
|
|
632
717
|
// paths against the store, or current if they are local. THe modify
|
|
633
718
|
// argument allows custom modification of the result. The inj
|
|
634
719
|
// (Injection) argument is used to maintain recursive state.
|
|
635
|
-
function inject(val, store,
|
|
720
|
+
function inject(val, store, injdef) {
|
|
636
721
|
const valtype = typeof val;
|
|
722
|
+
let inj = injdef;
|
|
637
723
|
// Create state if at root of injection. The input value is placed
|
|
638
724
|
// inside a virtual parent holder to simplify edge cases.
|
|
639
|
-
if (UNDEF ===
|
|
725
|
+
if (UNDEF === injdef || null == injdef.mode) {
|
|
640
726
|
// 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);
|
|
727
|
+
inj = new Injection(val, { [S_DTOP]: val });
|
|
728
|
+
inj.dparent = store;
|
|
729
|
+
inj.errs = getprop(store, S_DERRS, []);
|
|
730
|
+
inj.meta.__d = 0;
|
|
731
|
+
if (UNDEF !== injdef) {
|
|
732
|
+
inj.modify = null == injdef.modify ? inj.modify : injdef.modify;
|
|
733
|
+
inj.extra = null == injdef.extra ? inj.extra : injdef.extra;
|
|
734
|
+
inj.meta = null == injdef.meta ? inj.meta : injdef.meta;
|
|
735
|
+
inj.handler = null == injdef.handler ? inj.handler : injdef.handler;
|
|
736
|
+
}
|
|
655
737
|
}
|
|
656
|
-
|
|
738
|
+
inj.descend();
|
|
657
739
|
// Descend into node.
|
|
658
740
|
if (isnode(val)) {
|
|
659
741
|
// Keys are sorted alphanumerically to ensure determinism.
|
|
@@ -673,7 +755,7 @@ function inject(val, store, modify, current, inj) {
|
|
|
673
755
|
const nodekey = childinj.key;
|
|
674
756
|
childinj.mode = S_MKEYPRE;
|
|
675
757
|
// Peform the key:pre mode injection on the child key.
|
|
676
|
-
const prekey = _injectstr(nodekey, store,
|
|
758
|
+
const prekey = _injectstr(nodekey, store, childinj);
|
|
677
759
|
// The injection may modify child processing.
|
|
678
760
|
nkI = childinj.keyI;
|
|
679
761
|
nodekeys = childinj.keys;
|
|
@@ -683,13 +765,13 @@ function inject(val, store, modify, current, inj) {
|
|
|
683
765
|
childinj.mode = S_MVAL;
|
|
684
766
|
// Perform the val mode injection on the child value.
|
|
685
767
|
// NOTE: return value is not used.
|
|
686
|
-
inject(childinj.val, store,
|
|
768
|
+
inject(childinj.val, store, childinj);
|
|
687
769
|
// The injection may modify child processing.
|
|
688
770
|
nkI = childinj.keyI;
|
|
689
771
|
nodekeys = childinj.keys;
|
|
690
772
|
// Peform the key:post mode injection on the child key.
|
|
691
773
|
childinj.mode = S_MKEYPOST;
|
|
692
|
-
_injectstr(nodekey, store,
|
|
774
|
+
_injectstr(nodekey, store, childinj);
|
|
693
775
|
// The injection may modify child processing.
|
|
694
776
|
nkI = childinj.keyI;
|
|
695
777
|
nodekeys = childinj.keys;
|
|
@@ -699,15 +781,19 @@ function inject(val, store, modify, current, inj) {
|
|
|
699
781
|
// Inject paths into string scalars.
|
|
700
782
|
else if (S_string === valtype) {
|
|
701
783
|
inj.mode = S_MVAL;
|
|
702
|
-
val = _injectstr(val, store,
|
|
703
|
-
|
|
784
|
+
val = _injectstr(val, store, inj);
|
|
785
|
+
if (SKIP !== val) {
|
|
786
|
+
inj.setval(val);
|
|
787
|
+
}
|
|
704
788
|
}
|
|
789
|
+
// console.log('INJECT-M0 ', val, '' + inj)
|
|
705
790
|
// Custom modification.
|
|
706
|
-
if (modify) {
|
|
791
|
+
if (inj.modify && SKIP !== val) {
|
|
707
792
|
let mkey = inj.key;
|
|
708
793
|
let mparent = inj.parent;
|
|
709
794
|
let mval = getprop(mparent, mkey);
|
|
710
|
-
|
|
795
|
+
// console.log('INJECT-M1 ' + inj)
|
|
796
|
+
inj.modify(mval, mkey, mparent, inj, store);
|
|
711
797
|
}
|
|
712
798
|
inj.val = val;
|
|
713
799
|
// Original val reference may no longer be correct.
|
|
@@ -717,42 +803,41 @@ function inject(val, store, modify, current, inj) {
|
|
|
717
803
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
718
804
|
// Delete a key from a map or list.
|
|
719
805
|
const transform_DELETE = (inj) => {
|
|
720
|
-
// _setparentprop(inj, UNDEF)
|
|
721
806
|
inj.setval(UNDEF);
|
|
722
807
|
return UNDEF;
|
|
723
808
|
};
|
|
724
809
|
// Copy value from source data.
|
|
725
|
-
const transform_COPY = (inj, _val
|
|
810
|
+
const transform_COPY = (inj, _val) => {
|
|
726
811
|
const { mode, key } = inj;
|
|
727
812
|
let out = key;
|
|
728
813
|
if (!mode.startsWith(S_MKEY)) {
|
|
729
|
-
out = getprop(
|
|
814
|
+
out = getprop(inj.dparent, key);
|
|
730
815
|
inj.setval(out);
|
|
731
816
|
}
|
|
732
817
|
return out;
|
|
733
818
|
};
|
|
734
819
|
// As a value, inject the key of the parent node.
|
|
735
820
|
// As a key, defined the name of the key property in the source object.
|
|
736
|
-
const transform_KEY = (inj
|
|
821
|
+
const transform_KEY = (inj) => {
|
|
737
822
|
const { mode, path, parent } = inj;
|
|
738
823
|
// Do nothing in val mode.
|
|
739
824
|
if (S_MVAL !== mode) {
|
|
740
825
|
return UNDEF;
|
|
741
826
|
}
|
|
742
827
|
// Key is defined by $KEY meta property.
|
|
743
|
-
const keyspec = getprop(parent,
|
|
828
|
+
const keyspec = getprop(parent, S_BKEY);
|
|
744
829
|
if (UNDEF !== keyspec) {
|
|
745
|
-
|
|
746
|
-
return getprop(
|
|
830
|
+
delprop(parent, S_BKEY);
|
|
831
|
+
return getprop(inj.dparent, keyspec);
|
|
747
832
|
}
|
|
748
833
|
// Key is defined within general purpose $META object.
|
|
749
|
-
return getprop(getprop(parent,
|
|
834
|
+
return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2));
|
|
750
835
|
};
|
|
751
|
-
//
|
|
836
|
+
// Annotatea node. Does nothing itself, just used by
|
|
752
837
|
// other injectors, and is removed when called.
|
|
753
|
-
const
|
|
838
|
+
const transform_ANNO = (inj) => {
|
|
754
839
|
const { parent } = inj;
|
|
755
|
-
|
|
840
|
+
delprop(parent, S_BANNO);
|
|
756
841
|
return UNDEF;
|
|
757
842
|
};
|
|
758
843
|
// Merge a list of objects into the current object.
|
|
@@ -760,29 +845,31 @@ const transform_META = (inj) => {
|
|
|
760
845
|
// If the value is an array, the elements are first merged using `merge`.
|
|
761
846
|
// If the value is the empty string, merge the top level store.
|
|
762
847
|
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
763
|
-
const transform_MERGE = (inj
|
|
848
|
+
const transform_MERGE = (inj) => {
|
|
764
849
|
const { mode, key, parent } = inj;
|
|
850
|
+
// Ensures $MERGE is removed from parent list (val mode).
|
|
851
|
+
let out = UNDEF;
|
|
765
852
|
if (S_MKEYPRE === mode) {
|
|
766
|
-
|
|
853
|
+
out = key;
|
|
767
854
|
}
|
|
768
855
|
// Operate after child values have been transformed.
|
|
769
|
-
if (S_MKEYPOST === mode) {
|
|
856
|
+
else if (S_MKEYPOST === mode) {
|
|
857
|
+
out = key;
|
|
770
858
|
let args = getprop(parent, key);
|
|
771
|
-
args =
|
|
859
|
+
args = Array.isArray(args) ? args : [args];
|
|
772
860
|
// Remove the $MERGE command from a parent map.
|
|
773
861
|
inj.setval(UNDEF);
|
|
774
862
|
// Literals in the parent have precedence, but we still merge onto
|
|
775
863
|
// the parent object, so that node tree references are not changed.
|
|
776
864
|
const mergelist = [parent, ...args, clone(parent)];
|
|
777
865
|
merge(mergelist);
|
|
778
|
-
return key
|
|
866
|
+
// return key
|
|
779
867
|
}
|
|
780
|
-
|
|
781
|
-
return UNDEF;
|
|
868
|
+
return out;
|
|
782
869
|
};
|
|
783
870
|
// Convert a node to a list.
|
|
784
871
|
// Format: ['`$EACH`', '`source-path-of-node`', child-template]
|
|
785
|
-
const transform_EACH = (inj, _val,
|
|
872
|
+
const transform_EACH = (inj, _val, _ref, store) => {
|
|
786
873
|
// Remove arguments to avoid spurious processing.
|
|
787
874
|
if (null != inj.keys) {
|
|
788
875
|
inj.keys.length = 1;
|
|
@@ -795,7 +882,7 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
795
882
|
const child = clone(getprop(inj.parent, 2));
|
|
796
883
|
// Source data.
|
|
797
884
|
const srcstore = getprop(store, inj.base, store);
|
|
798
|
-
const src = getpath(
|
|
885
|
+
const src = getpath(srcstore, srcpath, inj);
|
|
799
886
|
// Create parallel data structures:
|
|
800
887
|
// source entries :: child templates
|
|
801
888
|
let tcur = [];
|
|
@@ -810,26 +897,33 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
810
897
|
tval = Object.entries(src).map(n => ({
|
|
811
898
|
...clone(child),
|
|
812
899
|
// Make a note of the key for $KEY transforms.
|
|
813
|
-
[
|
|
900
|
+
[S_BANNO]: { KEY: n[0] }
|
|
814
901
|
}));
|
|
815
902
|
}
|
|
816
903
|
let rval = [];
|
|
817
904
|
if (0 < size(tval)) {
|
|
818
905
|
tcur = null == src ? UNDEF : Object.values(src);
|
|
906
|
+
const ckey = getelem(inj.path, -2);
|
|
819
907
|
const tpath = slice(inj.path, -1);
|
|
908
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey];
|
|
820
909
|
// Parent structure.
|
|
821
|
-
const ckey = getelem(
|
|
910
|
+
// const ckey = getelem(cpath, -1)
|
|
822
911
|
tcur = { [ckey]: tcur };
|
|
823
912
|
if (1 < tpath.length) {
|
|
824
913
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
914
|
+
// const pkey = getelem(cpath, -2, S_DTOP)
|
|
825
915
|
tcur = { [pkey]: tcur };
|
|
916
|
+
dpath.push('$:' + pkey);
|
|
826
917
|
}
|
|
827
918
|
const tinj = inj.child(0, [ckey]);
|
|
828
919
|
tinj.path = tpath;
|
|
829
920
|
tinj.nodes = slice(inj.nodes, -1);
|
|
830
|
-
tinj.parent =
|
|
921
|
+
tinj.parent = getelem(tinj.nodes, -1);
|
|
922
|
+
setprop(tinj.parent, ckey, tval);
|
|
831
923
|
tinj.val = tval;
|
|
832
|
-
|
|
924
|
+
tinj.dpath = dpath;
|
|
925
|
+
tinj.dparent = tcur;
|
|
926
|
+
inject(tval, store, tinj);
|
|
833
927
|
rval = tinj.val;
|
|
834
928
|
}
|
|
835
929
|
_updateAncestors(inj, target, tkey, rval);
|
|
@@ -838,7 +932,7 @@ const transform_EACH = (inj, _val, current, _ref, store) => {
|
|
|
838
932
|
};
|
|
839
933
|
// Convert a node to a map.
|
|
840
934
|
// Format: { '`$PACK`':['`source-path`', child-template]}
|
|
841
|
-
const transform_PACK = (inj, _val,
|
|
935
|
+
const transform_PACK = (inj, _val, _ref, store) => {
|
|
842
936
|
const { mode, key, path, parent, nodes } = inj;
|
|
843
937
|
// Defensive context checks.
|
|
844
938
|
if (S_MKEYPRE !== mode || S_string !== typeof key || null == path || null == nodes) {
|
|
@@ -849,31 +943,37 @@ const transform_PACK = (inj, _val, current, _ref, store) => {
|
|
|
849
943
|
const srcpath = args[0]; // Path to source data.
|
|
850
944
|
const child = clone(args[1]); // Child template.
|
|
851
945
|
// Find key and target node.
|
|
852
|
-
const keyprop = child[
|
|
946
|
+
const keyprop = child[S_BKEY];
|
|
853
947
|
const tkey = getelem(path, -2);
|
|
854
948
|
const target = nodes[path.length - 2] || nodes[path.length - 1];
|
|
855
949
|
// Source data
|
|
856
950
|
const srcstore = getprop(store, inj.base, store);
|
|
857
|
-
let src = getpath(
|
|
951
|
+
let src = getpath(srcstore, srcpath, inj);
|
|
858
952
|
// Prepare source as a list.
|
|
859
953
|
src = islist(src) ? src :
|
|
860
954
|
ismap(src) ? Object.entries(src)
|
|
861
|
-
.reduce((a, n) => (n[1][
|
|
955
|
+
.reduce((a, n) => (n[1][S_BANNO] = { KEY: n[0] }, a.push(n[1]), a), []) :
|
|
862
956
|
UNDEF;
|
|
863
957
|
if (null == src) {
|
|
864
958
|
return UNDEF;
|
|
865
959
|
}
|
|
866
960
|
// Get key if specified.
|
|
867
|
-
let childkey = getprop(child,
|
|
961
|
+
let childkey = getprop(child, S_BKEY);
|
|
868
962
|
let keyname = UNDEF === childkey ? keyprop : childkey;
|
|
869
|
-
|
|
963
|
+
delprop(child, S_BKEY);
|
|
870
964
|
// Build parallel target object.
|
|
871
965
|
let tval = {};
|
|
872
966
|
tval = src.reduce((a, n) => {
|
|
873
967
|
let kn = getprop(n, keyname);
|
|
874
968
|
setprop(a, kn, clone(child));
|
|
875
969
|
const nchild = getprop(a, kn);
|
|
876
|
-
|
|
970
|
+
const mval = getprop(n, S_BANNO);
|
|
971
|
+
if (UNDEF === mval) {
|
|
972
|
+
delprop(nchild, S_BANNO);
|
|
973
|
+
}
|
|
974
|
+
else {
|
|
975
|
+
setprop(nchild, S_BANNO, mval);
|
|
976
|
+
}
|
|
877
977
|
return a;
|
|
878
978
|
}, tval);
|
|
879
979
|
let rval = {};
|
|
@@ -887,27 +987,33 @@ const transform_PACK = (inj, _val, current, _ref, store) => {
|
|
|
887
987
|
}, tcur);
|
|
888
988
|
const tpath = slice(inj.path, -1);
|
|
889
989
|
const ckey = getelem(inj.path, -2);
|
|
990
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey];
|
|
890
991
|
tcur = { [ckey]: tcur };
|
|
891
992
|
if (1 < tpath.length) {
|
|
892
993
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
893
994
|
tcur = { [pkey]: tcur };
|
|
995
|
+
dpath.push('$:' + pkey);
|
|
894
996
|
}
|
|
895
997
|
const tinj = inj.child(0, [ckey]);
|
|
896
998
|
tinj.path = tpath;
|
|
897
999
|
tinj.nodes = slice(inj.nodes, -1);
|
|
898
|
-
tinj.parent = tcur
|
|
1000
|
+
// tinj.parent = tcur
|
|
1001
|
+
tinj.parent = getelem(tinj.nodes, -1);
|
|
899
1002
|
tinj.val = tval;
|
|
900
|
-
|
|
1003
|
+
tinj.dpath = dpath;
|
|
1004
|
+
tinj.dparent = tcur;
|
|
1005
|
+
inject(tval, store, tinj);
|
|
901
1006
|
rval = tinj.val;
|
|
902
1007
|
}
|
|
903
1008
|
_updateAncestors(inj, target, tkey, rval);
|
|
904
1009
|
// Drop transform key.
|
|
905
1010
|
return UNDEF;
|
|
906
1011
|
};
|
|
1012
|
+
// TODO: not found ref should removed key (setprop UNDEF)
|
|
907
1013
|
// Reference original spec (enables recursice transformations)
|
|
908
1014
|
// Format: ['`$REF`', '`spec-path`']
|
|
909
|
-
const transform_REF = (inj, val,
|
|
910
|
-
const { nodes
|
|
1015
|
+
const transform_REF = (inj, val, _ref, store) => {
|
|
1016
|
+
const { nodes } = inj;
|
|
911
1017
|
if (S_MVAL !== inj.mode) {
|
|
912
1018
|
return UNDEF;
|
|
913
1019
|
}
|
|
@@ -916,7 +1022,11 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
916
1022
|
inj.keyI = inj.keys.length;
|
|
917
1023
|
// Spec reference.
|
|
918
1024
|
const spec = getprop(store, S_DSPEC)();
|
|
919
|
-
const ref = getpath(refpath,
|
|
1025
|
+
const ref = getpath(spec, refpath, {
|
|
1026
|
+
// TODO: test relative refs
|
|
1027
|
+
dpath: inj.path.slice(1),
|
|
1028
|
+
dparent: getpath(spec, inj.path.slice(1))
|
|
1029
|
+
});
|
|
920
1030
|
let hasSubRef = false;
|
|
921
1031
|
if (isnode(ref)) {
|
|
922
1032
|
walk(ref, (_k, v) => {
|
|
@@ -929,8 +1039,8 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
929
1039
|
let tref = clone(ref);
|
|
930
1040
|
const cpath = slice(inj.path, -3);
|
|
931
1041
|
const tpath = slice(inj.path, -1);
|
|
932
|
-
let tcur = getpath(
|
|
933
|
-
let tval = getpath(
|
|
1042
|
+
let tcur = getpath(store, cpath);
|
|
1043
|
+
let tval = getpath(store, tpath);
|
|
934
1044
|
let rval = UNDEF;
|
|
935
1045
|
if (!hasSubRef || UNDEF !== tval) {
|
|
936
1046
|
const tinj = inj.child(0, [getelem(tpath, -1)]);
|
|
@@ -938,7 +1048,9 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
938
1048
|
tinj.nodes = slice(inj.nodes, -1);
|
|
939
1049
|
tinj.parent = getelem(nodes, -2);
|
|
940
1050
|
tinj.val = tref;
|
|
941
|
-
|
|
1051
|
+
tinj.dpath = [...cpath];
|
|
1052
|
+
tinj.dparent = tcur;
|
|
1053
|
+
inject(tref, store, tinj);
|
|
942
1054
|
rval = tinj.val;
|
|
943
1055
|
}
|
|
944
1056
|
else {
|
|
@@ -955,12 +1067,14 @@ const transform_REF = (inj, val, _current, _ref, store) => {
|
|
|
955
1067
|
// Arrays are treated as if they are objects with indices as keys.
|
|
956
1068
|
function transform(data, // Source data to transform into new data (original not mutated)
|
|
957
1069
|
spec, // Transform specification; output follows this shape
|
|
958
|
-
extra, // Additional store of data and transforms.
|
|
959
|
-
modify // Optionally modify individual values.
|
|
960
|
-
) {
|
|
1070
|
+
// extra?: any, // Additional store of data and transforms.
|
|
1071
|
+
// modify?: Modify // Optionally modify individual values.
|
|
1072
|
+
injdef) {
|
|
961
1073
|
// Clone the spec so that the clone can be modified in place as the transform result.
|
|
962
1074
|
const origspec = spec;
|
|
963
1075
|
spec = clone(origspec);
|
|
1076
|
+
const extra = injdef?.extra;
|
|
1077
|
+
// const modify = injdef?.modify
|
|
964
1078
|
const extraTransforms = {};
|
|
965
1079
|
const extraData = null == extra ? UNDEF : items(extra)
|
|
966
1080
|
.reduce((a, n) => (n[0].startsWith(S_DS) ? extraTransforms[n[0]] = n[1] : (a[n[0]] = n[1]), a), {});
|
|
@@ -984,7 +1098,7 @@ modify // Optionally modify individual values.
|
|
|
984
1098
|
$DELETE: transform_DELETE,
|
|
985
1099
|
$COPY: transform_COPY,
|
|
986
1100
|
$KEY: transform_KEY,
|
|
987
|
-
$
|
|
1101
|
+
$ANNO: transform_ANNO,
|
|
988
1102
|
$MERGE: transform_MERGE,
|
|
989
1103
|
$EACH: transform_EACH,
|
|
990
1104
|
$PACK: transform_PACK,
|
|
@@ -992,12 +1106,13 @@ modify // Optionally modify individual values.
|
|
|
992
1106
|
// Custom extra transforms, if any.
|
|
993
1107
|
...extraTransforms,
|
|
994
1108
|
};
|
|
995
|
-
const out = inject(spec, store, modify,
|
|
1109
|
+
// const out = inject(spec, store, { modify, extra })
|
|
1110
|
+
const out = inject(spec, store, injdef);
|
|
996
1111
|
return out;
|
|
997
1112
|
}
|
|
998
1113
|
// A required string value. NOTE: Rejects empty strings.
|
|
999
|
-
const validate_STRING = (inj
|
|
1000
|
-
let out = getprop(
|
|
1114
|
+
const validate_STRING = (inj) => {
|
|
1115
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1001
1116
|
const t = typify(out);
|
|
1002
1117
|
if (S_string !== t) {
|
|
1003
1118
|
let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010');
|
|
@@ -1012,8 +1127,8 @@ const validate_STRING = (inj, _val, current) => {
|
|
|
1012
1127
|
return out;
|
|
1013
1128
|
};
|
|
1014
1129
|
// A required number value (int or float).
|
|
1015
|
-
const validate_NUMBER = (inj
|
|
1016
|
-
let out = getprop(
|
|
1130
|
+
const validate_NUMBER = (inj) => {
|
|
1131
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1017
1132
|
const t = typify(out);
|
|
1018
1133
|
if (S_number !== t) {
|
|
1019
1134
|
inj.errs.push(_invalidTypeMsg(inj.path, S_number, t, out, 'V1020'));
|
|
@@ -1022,8 +1137,8 @@ const validate_NUMBER = (inj, _val, current) => {
|
|
|
1022
1137
|
return out;
|
|
1023
1138
|
};
|
|
1024
1139
|
// A required boolean value.
|
|
1025
|
-
const validate_BOOLEAN = (inj
|
|
1026
|
-
let out = getprop(
|
|
1140
|
+
const validate_BOOLEAN = (inj) => {
|
|
1141
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1027
1142
|
const t = typify(out);
|
|
1028
1143
|
if (S_boolean !== t) {
|
|
1029
1144
|
inj.errs.push(_invalidTypeMsg(inj.path, S_boolean, t, out, 'V1030'));
|
|
@@ -1032,8 +1147,8 @@ const validate_BOOLEAN = (inj, _val, current) => {
|
|
|
1032
1147
|
return out;
|
|
1033
1148
|
};
|
|
1034
1149
|
// A required object (map) value (contents not validated).
|
|
1035
|
-
const validate_OBJECT = (inj
|
|
1036
|
-
let out = getprop(
|
|
1150
|
+
const validate_OBJECT = (inj) => {
|
|
1151
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1037
1152
|
const t = typify(out);
|
|
1038
1153
|
if (t !== S_object) {
|
|
1039
1154
|
inj.errs.push(_invalidTypeMsg(inj.path, S_object, t, out, 'V1040'));
|
|
@@ -1042,8 +1157,8 @@ const validate_OBJECT = (inj, _val, current) => {
|
|
|
1042
1157
|
return out;
|
|
1043
1158
|
};
|
|
1044
1159
|
// A required array (list) value (contents not validated).
|
|
1045
|
-
const validate_ARRAY = (inj
|
|
1046
|
-
let out = getprop(
|
|
1160
|
+
const validate_ARRAY = (inj) => {
|
|
1161
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1047
1162
|
const t = typify(out);
|
|
1048
1163
|
if (t !== S_array) {
|
|
1049
1164
|
inj.errs.push(_invalidTypeMsg(inj.path, S_array, t, out, 'V1050'));
|
|
@@ -1052,8 +1167,8 @@ const validate_ARRAY = (inj, _val, current) => {
|
|
|
1052
1167
|
return out;
|
|
1053
1168
|
};
|
|
1054
1169
|
// A required function value.
|
|
1055
|
-
const validate_FUNCTION = (inj
|
|
1056
|
-
let out = getprop(
|
|
1170
|
+
const validate_FUNCTION = (inj) => {
|
|
1171
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1057
1172
|
const t = typify(out);
|
|
1058
1173
|
if (S_function !== t) {
|
|
1059
1174
|
inj.errs.push(_invalidTypeMsg(inj.path, S_function, t, out, 'V1060'));
|
|
@@ -1062,13 +1177,14 @@ const validate_FUNCTION = (inj, _val, current) => {
|
|
|
1062
1177
|
return out;
|
|
1063
1178
|
};
|
|
1064
1179
|
// Allow any value.
|
|
1065
|
-
const validate_ANY = (inj
|
|
1066
|
-
|
|
1180
|
+
const validate_ANY = (inj) => {
|
|
1181
|
+
let out = getprop(inj.dparent, inj.key);
|
|
1182
|
+
return out;
|
|
1067
1183
|
};
|
|
1068
1184
|
// Specify child values for map or list.
|
|
1069
1185
|
// Map syntax: {'`$CHILD`': child-template }
|
|
1070
1186
|
// List syntax: ['`$CHILD`', child-template ]
|
|
1071
|
-
const validate_CHILD = (inj
|
|
1187
|
+
const validate_CHILD = (inj) => {
|
|
1072
1188
|
const { mode, key, parent, keys, path } = inj;
|
|
1073
1189
|
// Setup data structures for validation by cloning child template.
|
|
1074
1190
|
// Map syntax.
|
|
@@ -1076,7 +1192,7 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1076
1192
|
const childtm = getprop(parent, key);
|
|
1077
1193
|
// Get corresponding current object.
|
|
1078
1194
|
const pkey = getprop(path, path.length - 2);
|
|
1079
|
-
let tval = getprop(
|
|
1195
|
+
let tval = getprop(inj.dparent, pkey);
|
|
1080
1196
|
if (UNDEF == tval) {
|
|
1081
1197
|
tval = {};
|
|
1082
1198
|
}
|
|
@@ -1091,7 +1207,6 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1091
1207
|
keys.push(ckey);
|
|
1092
1208
|
}
|
|
1093
1209
|
// Remove $CHILD to cleanup ouput.
|
|
1094
|
-
// _setparentprop(inj, UNDEF)
|
|
1095
1210
|
inj.setval(UNDEF);
|
|
1096
1211
|
return UNDEF;
|
|
1097
1212
|
}
|
|
@@ -1103,32 +1218,32 @@ const validate_CHILD = (inj, _val, current) => {
|
|
|
1103
1218
|
return UNDEF;
|
|
1104
1219
|
}
|
|
1105
1220
|
const childtm = getprop(parent, 1);
|
|
1106
|
-
if (UNDEF ===
|
|
1221
|
+
if (UNDEF === inj.dparent) {
|
|
1107
1222
|
// Empty list as default.
|
|
1108
1223
|
parent.length = 0;
|
|
1109
1224
|
return UNDEF;
|
|
1110
1225
|
}
|
|
1111
|
-
if (!islist(
|
|
1112
|
-
const msg = _invalidTypeMsg(slice(inj.path, -1), S_array, typify(
|
|
1226
|
+
if (!islist(inj.dparent)) {
|
|
1227
|
+
const msg = _invalidTypeMsg(slice(inj.path, -1), S_array, typify(inj.dparent), inj.dparent, 'V0230');
|
|
1113
1228
|
inj.errs.push(msg);
|
|
1114
1229
|
inj.keyI = parent.length;
|
|
1115
|
-
return
|
|
1230
|
+
return inj.dparent;
|
|
1116
1231
|
}
|
|
1117
1232
|
// Clone children abd reset inj key index.
|
|
1118
1233
|
// The inject child loop will now iterate over the cloned children,
|
|
1119
1234
|
// validating them againt the current list values.
|
|
1120
|
-
|
|
1121
|
-
parent.length =
|
|
1235
|
+
inj.dparent.map((_n, i) => parent[i] = clone(childtm));
|
|
1236
|
+
parent.length = inj.dparent.length;
|
|
1122
1237
|
inj.keyI = 0;
|
|
1123
|
-
const out = getprop(
|
|
1238
|
+
const out = getprop(inj.dparent, 0);
|
|
1124
1239
|
return out;
|
|
1125
1240
|
}
|
|
1126
1241
|
return UNDEF;
|
|
1127
1242
|
};
|
|
1128
1243
|
// Match at least one of the specified shapes.
|
|
1129
1244
|
// Syntax: ['`$ONE`', alt0, alt1, ...]okI
|
|
1130
|
-
const validate_ONE = (inj, _val,
|
|
1131
|
-
const { mode, parent,
|
|
1245
|
+
const validate_ONE = (inj, _val, _ref, store) => {
|
|
1246
|
+
const { mode, parent, keyI } = inj;
|
|
1132
1247
|
// Only operate in val mode, since parent is a list.
|
|
1133
1248
|
if (S_MVAL === mode) {
|
|
1134
1249
|
if (!islist(parent) || 0 !== keyI) {
|
|
@@ -1139,7 +1254,7 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1139
1254
|
}
|
|
1140
1255
|
inj.keyI = inj.keys.length;
|
|
1141
1256
|
// Clean up structure, replacing [$ONE, ...] with current
|
|
1142
|
-
|
|
1257
|
+
inj.setval(inj.dparent, 2);
|
|
1143
1258
|
inj.path = slice(inj.path, -1);
|
|
1144
1259
|
inj.key = getelem(inj.path, -1);
|
|
1145
1260
|
let tvals = slice(parent, 1);
|
|
@@ -1154,9 +1269,13 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1154
1269
|
// If match, then errs.length = 0
|
|
1155
1270
|
let terrs = [];
|
|
1156
1271
|
const vstore = { ...store };
|
|
1157
|
-
vstore.$TOP =
|
|
1158
|
-
const vcurrent = validate(
|
|
1159
|
-
|
|
1272
|
+
vstore.$TOP = inj.dparent;
|
|
1273
|
+
const vcurrent = validate(inj.dparent, tval, {
|
|
1274
|
+
extra: vstore,
|
|
1275
|
+
errs: terrs,
|
|
1276
|
+
meta: inj.meta,
|
|
1277
|
+
});
|
|
1278
|
+
inj.setval(vcurrent, -2);
|
|
1160
1279
|
// Accept current value if there was a match
|
|
1161
1280
|
if (0 === terrs.length) {
|
|
1162
1281
|
return;
|
|
@@ -1166,12 +1285,12 @@ const validate_ONE = (inj, _val, current, _ref, store) => {
|
|
|
1166
1285
|
const valdesc = tvals
|
|
1167
1286
|
.map((v) => stringify(v))
|
|
1168
1287
|
.join(', ')
|
|
1169
|
-
.replace(
|
|
1170
|
-
inj.errs.push(_invalidTypeMsg(inj.path, (1 < tvals.length ? 'one of ' : '') + valdesc, typify(
|
|
1288
|
+
.replace(R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1289
|
+
inj.errs.push(_invalidTypeMsg(inj.path, (1 < tvals.length ? 'one of ' : '') + valdesc, typify(inj.dparent), inj.dparent, 'V0210'));
|
|
1171
1290
|
}
|
|
1172
1291
|
};
|
|
1173
|
-
const validate_EXACT = (inj
|
|
1174
|
-
const { mode, parent, key, keyI
|
|
1292
|
+
const validate_EXACT = (inj) => {
|
|
1293
|
+
const { mode, parent, key, keyI } = inj;
|
|
1175
1294
|
// Only operate in val mode, since parent is a list.
|
|
1176
1295
|
if (S_MVAL === mode) {
|
|
1177
1296
|
if (!islist(parent) || 0 !== keyI) {
|
|
@@ -1181,8 +1300,8 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1181
1300
|
return;
|
|
1182
1301
|
}
|
|
1183
1302
|
inj.keyI = inj.keys.length;
|
|
1184
|
-
// Clean up structure, replacing [$EXACT, ...] with current
|
|
1185
|
-
inj.setval(
|
|
1303
|
+
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1304
|
+
inj.setval(inj.dparent, 2);
|
|
1186
1305
|
inj.path = slice(inj.path, 0, inj.path.length - 1);
|
|
1187
1306
|
inj.key = getelem(inj.path, -1);
|
|
1188
1307
|
let tvals = slice(parent, 1);
|
|
@@ -1195,9 +1314,9 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1195
1314
|
// See if we can find an exact value match.
|
|
1196
1315
|
let currentstr = undefined;
|
|
1197
1316
|
for (let tval of tvals) {
|
|
1198
|
-
let exactmatch = tval ===
|
|
1317
|
+
let exactmatch = tval === inj.dparent;
|
|
1199
1318
|
if (!exactmatch && isnode(tval)) {
|
|
1200
|
-
currentstr = undefined === currentstr ? stringify(
|
|
1319
|
+
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
|
|
1201
1320
|
const tvalstr = stringify(tval);
|
|
1202
1321
|
exactmatch = tvalstr === currentstr;
|
|
1203
1322
|
}
|
|
@@ -1208,23 +1327,29 @@ const validate_EXACT = (inj, _val, current, _ref, _store) => {
|
|
|
1208
1327
|
const valdesc = tvals
|
|
1209
1328
|
.map((v) => stringify(v))
|
|
1210
1329
|
.join(', ')
|
|
1211
|
-
.replace(
|
|
1330
|
+
.replace(R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1212
1331
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < inj.path.length ? '' : 'value ') +
|
|
1213
|
-
'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc, typify(
|
|
1332
|
+
'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc, typify(inj.dparent), inj.dparent, 'V0110'));
|
|
1214
1333
|
}
|
|
1215
1334
|
else {
|
|
1216
|
-
|
|
1335
|
+
delprop(parent, key);
|
|
1217
1336
|
}
|
|
1218
1337
|
};
|
|
1219
1338
|
// This is the "modify" argument to inject. Use this to perform
|
|
1220
1339
|
// generic validation. Runs *after* any special commands.
|
|
1221
|
-
const _validation = (pval, key, parent, inj
|
|
1340
|
+
const _validation = (pval, key, parent, inj) => {
|
|
1222
1341
|
if (UNDEF === inj) {
|
|
1223
1342
|
return;
|
|
1224
1343
|
}
|
|
1344
|
+
if (SKIP === pval) {
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
// select needs exact matches
|
|
1348
|
+
const exact = getprop(inj.meta, '`$EXACT`');
|
|
1225
1349
|
// Current val to verify.
|
|
1226
|
-
const cval = getprop(
|
|
1227
|
-
if (UNDEF === cval || UNDEF === inj) {
|
|
1350
|
+
const cval = getprop(inj.dparent, key);
|
|
1351
|
+
// if (UNDEF === cval || UNDEF === inj) {
|
|
1352
|
+
if (UNDEF === inj || (!exact && UNDEF === cval)) {
|
|
1228
1353
|
return;
|
|
1229
1354
|
}
|
|
1230
1355
|
const ptype = typify(pval);
|
|
@@ -1233,6 +1358,7 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1233
1358
|
return;
|
|
1234
1359
|
}
|
|
1235
1360
|
const ctype = typify(cval);
|
|
1361
|
+
// console.log('VALID-A', pval, ptype, cval, ctype)
|
|
1236
1362
|
// Type mismatch.
|
|
1237
1363
|
if (ptype !== ctype && UNDEF !== pval) {
|
|
1238
1364
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0010'));
|
|
@@ -1263,7 +1389,7 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1263
1389
|
// Object is open, so merge in extra keys.
|
|
1264
1390
|
merge([pval, cval]);
|
|
1265
1391
|
if (isnode(pval)) {
|
|
1266
|
-
|
|
1392
|
+
delprop(pval, '`$OPEN`');
|
|
1267
1393
|
}
|
|
1268
1394
|
}
|
|
1269
1395
|
}
|
|
@@ -1272,6 +1398,13 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1272
1398
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0030'));
|
|
1273
1399
|
}
|
|
1274
1400
|
}
|
|
1401
|
+
else if (exact) {
|
|
1402
|
+
// else if (inj.meta['`$EXACT`']) {
|
|
1403
|
+
// console.log('VALID-X', cval, pval)
|
|
1404
|
+
if (cval !== pval) {
|
|
1405
|
+
inj.errs.push('Value ' + cval + ' should equal ' + pval);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1275
1408
|
else {
|
|
1276
1409
|
// Spec value was a default, copy over data
|
|
1277
1410
|
setprop(parent, key, cval);
|
|
@@ -1290,10 +1423,10 @@ const _validation = (pval, key, parent, inj, current, _store) => {
|
|
|
1290
1423
|
// is open, and if missing an empty default is inserted.
|
|
1291
1424
|
function validate(data, // Source data to transform into new data (original not mutated)
|
|
1292
1425
|
spec, // Transform specification; output follows this shape
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
const errs =
|
|
1426
|
+
injdef) {
|
|
1427
|
+
const extra = injdef?.extra;
|
|
1428
|
+
const collect = null != injdef?.errs;
|
|
1429
|
+
const errs = injdef?.errs || [];
|
|
1297
1430
|
const store = {
|
|
1298
1431
|
// Remove the transform commands.
|
|
1299
1432
|
$DELETE: null,
|
|
@@ -1318,20 +1451,243 @@ collecterrs) {
|
|
|
1318
1451
|
// NOTE: collecterrs paramter always wins.
|
|
1319
1452
|
$ERRS: errs,
|
|
1320
1453
|
};
|
|
1321
|
-
const out = transform(data, spec,
|
|
1322
|
-
|
|
1454
|
+
const out = transform(data, spec, {
|
|
1455
|
+
meta: injdef?.meta,
|
|
1456
|
+
extra: store,
|
|
1457
|
+
modify: _validation,
|
|
1458
|
+
handler: _validatehandler
|
|
1459
|
+
});
|
|
1460
|
+
const generr = (0 < errs.length && !collect);
|
|
1323
1461
|
if (generr) {
|
|
1324
1462
|
throw new Error('Invalid data: ' + errs.join(' | '));
|
|
1325
1463
|
}
|
|
1326
1464
|
return out;
|
|
1327
1465
|
}
|
|
1466
|
+
const select_AND = (inj, _val, _ref, store) => {
|
|
1467
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1468
|
+
const terms = getprop(inj.parent, inj.key);
|
|
1469
|
+
// const src = getprop(store, inj.base, store)
|
|
1470
|
+
const ppath = slice(inj.path, -1);
|
|
1471
|
+
const point = getpath(store, ppath);
|
|
1472
|
+
const vstore = { ...store };
|
|
1473
|
+
vstore.$TOP = point;
|
|
1474
|
+
for (let term of terms) {
|
|
1475
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1476
|
+
let terrs = [];
|
|
1477
|
+
validate(point, term, {
|
|
1478
|
+
extra: vstore,
|
|
1479
|
+
errs: terrs,
|
|
1480
|
+
meta: inj.meta,
|
|
1481
|
+
});
|
|
1482
|
+
if (0 != terrs.length) {
|
|
1483
|
+
inj.errs.push('AND:' + pathify(ppath) + ': ' + stringify(point) + ' fail:' + stringify(terms));
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
const gkey = getelem(inj.path, -2);
|
|
1487
|
+
const gp = getelem(inj.nodes, -2);
|
|
1488
|
+
setprop(gp, gkey, point);
|
|
1489
|
+
}
|
|
1490
|
+
};
|
|
1491
|
+
const select_OR = (inj, _val, _ref, store) => {
|
|
1492
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1493
|
+
const terms = getprop(inj.parent, inj.key);
|
|
1494
|
+
// const src = getprop(store, inj.base, store)
|
|
1495
|
+
const ppath = slice(inj.path, -1);
|
|
1496
|
+
const point = getpath(store, ppath);
|
|
1497
|
+
// console.log('OR:', ppath, point)
|
|
1498
|
+
const vstore = { ...store };
|
|
1499
|
+
vstore.$TOP = point;
|
|
1500
|
+
for (let term of terms) {
|
|
1501
|
+
// console.log('OR-TERM:', term)
|
|
1502
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1503
|
+
let terrs = [];
|
|
1504
|
+
validate(point, term, {
|
|
1505
|
+
extra: vstore,
|
|
1506
|
+
errs: terrs,
|
|
1507
|
+
meta: inj.meta,
|
|
1508
|
+
});
|
|
1509
|
+
// console.log('OR-ERRS:', terrs)
|
|
1510
|
+
if (0 === terrs.length) {
|
|
1511
|
+
const gkey = getelem(inj.path, -2);
|
|
1512
|
+
const gp = getelem(inj.nodes, -2);
|
|
1513
|
+
setprop(gp, gkey, point);
|
|
1514
|
+
// console.log('OR-NODES:' + inj, inj.nodes)
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
inj.errs.push('OR:' + pathify(ppath) + ': ' + stringify(point) + ' fail:' + stringify(terms));
|
|
1519
|
+
}
|
|
1520
|
+
};
|
|
1521
|
+
const select_CMP = (inj, _val, ref, store) => {
|
|
1522
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1523
|
+
const term = getprop(inj.parent, inj.key);
|
|
1524
|
+
// const src = getprop(store, inj.base, store)
|
|
1525
|
+
const gkey = getelem(inj.path, -2);
|
|
1526
|
+
// const tval = getprop(src, gkey)
|
|
1527
|
+
const ppath = slice(inj.path, -1);
|
|
1528
|
+
const point = getpath(store, ppath);
|
|
1529
|
+
let pass = false;
|
|
1530
|
+
if ('$GT' === ref && point > term) {
|
|
1531
|
+
pass = true;
|
|
1532
|
+
}
|
|
1533
|
+
else if ('$LT' === ref && point < term) {
|
|
1534
|
+
pass = true;
|
|
1535
|
+
}
|
|
1536
|
+
else if ('$GTE' === ref && point >= term) {
|
|
1537
|
+
pass = true;
|
|
1538
|
+
}
|
|
1539
|
+
else if ('$LTE' === ref && point <= term) {
|
|
1540
|
+
pass = true;
|
|
1541
|
+
}
|
|
1542
|
+
if (pass) {
|
|
1543
|
+
// Update spec to match found value so that _validate does not complain.
|
|
1544
|
+
const gp = getelem(inj.nodes, -2);
|
|
1545
|
+
setprop(gp, gkey, point);
|
|
1546
|
+
}
|
|
1547
|
+
else {
|
|
1548
|
+
inj.errs.push('CMP: ' + pathify(ppath) + ': ' + stringify(point) +
|
|
1549
|
+
' fail:' + ref + ' ' + stringify(term));
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
return UNDEF;
|
|
1553
|
+
};
|
|
1554
|
+
// Select children from a top-level object that match a MongoDB-style query.
|
|
1555
|
+
// Supports $and, $or, and equality comparisons.
|
|
1556
|
+
// For arrays, children are elements; for objects, children are values.
|
|
1557
|
+
// TODO: swap arg order for consistency
|
|
1558
|
+
function select(query, children) {
|
|
1559
|
+
if (!isnode(children)) {
|
|
1560
|
+
return [];
|
|
1561
|
+
}
|
|
1562
|
+
if (ismap(children)) {
|
|
1563
|
+
children = items(children).map(n => (n[1][S_DKEY] = n[0], n[1]));
|
|
1564
|
+
}
|
|
1565
|
+
else {
|
|
1566
|
+
children = children.map((n, i) => ((ismap(n) ? n[S_DKEY] = i : null), n));
|
|
1567
|
+
}
|
|
1568
|
+
const results = [];
|
|
1569
|
+
const injdef = {
|
|
1570
|
+
errs: [],
|
|
1571
|
+
meta: { '`$EXACT`': true },
|
|
1572
|
+
extra: {
|
|
1573
|
+
$AND: select_AND,
|
|
1574
|
+
$OR: select_OR,
|
|
1575
|
+
$GT: select_CMP,
|
|
1576
|
+
$LT: select_CMP,
|
|
1577
|
+
$GTE: select_CMP,
|
|
1578
|
+
$LTE: select_CMP,
|
|
1579
|
+
}
|
|
1580
|
+
};
|
|
1581
|
+
const q = clone(query);
|
|
1582
|
+
walk(q, (_k, v) => {
|
|
1583
|
+
if (ismap(v)) {
|
|
1584
|
+
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true));
|
|
1585
|
+
}
|
|
1586
|
+
return v;
|
|
1587
|
+
});
|
|
1588
|
+
for (const child of children) {
|
|
1589
|
+
// console.log('CHILD', child, q)
|
|
1590
|
+
injdef.errs = [];
|
|
1591
|
+
validate(child, clone(q), injdef);
|
|
1592
|
+
// console.log('CHILD-ERRS', injdef.errs)
|
|
1593
|
+
if (0 === size(injdef.errs)) {
|
|
1594
|
+
results.push(child);
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
return results;
|
|
1598
|
+
}
|
|
1599
|
+
// Injection state used for recursive injection into JSON - like data structures.
|
|
1600
|
+
class Injection {
|
|
1601
|
+
constructor(val, parent) {
|
|
1602
|
+
this.val = val;
|
|
1603
|
+
this.parent = parent;
|
|
1604
|
+
this.errs = [];
|
|
1605
|
+
this.dparent = UNDEF;
|
|
1606
|
+
this.dpath = [S_DTOP];
|
|
1607
|
+
this.mode = S_MVAL;
|
|
1608
|
+
this.full = false;
|
|
1609
|
+
this.keyI = 0;
|
|
1610
|
+
this.keys = [S_DTOP];
|
|
1611
|
+
this.key = S_DTOP;
|
|
1612
|
+
this.path = [S_DTOP];
|
|
1613
|
+
this.nodes = [parent];
|
|
1614
|
+
this.handler = _injecthandler;
|
|
1615
|
+
this.base = S_DTOP;
|
|
1616
|
+
this.meta = {};
|
|
1617
|
+
}
|
|
1618
|
+
toString(prefix) {
|
|
1619
|
+
return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
|
|
1620
|
+
pad(pathify(this.path, 1)) +
|
|
1621
|
+
this.mode + (this.full ? '/full' : '') + S_CN +
|
|
1622
|
+
'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
|
|
1623
|
+
' p=' + stringify(this.parent, -1, 1) +
|
|
1624
|
+
' m=' + stringify(this.meta, -1, 1) +
|
|
1625
|
+
' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
|
|
1626
|
+
' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1);
|
|
1627
|
+
}
|
|
1628
|
+
descend() {
|
|
1629
|
+
this.meta.__d++;
|
|
1630
|
+
const parentkey = getelem(this.path, -2);
|
|
1631
|
+
// Resolve current node in store for local paths.
|
|
1632
|
+
if (UNDEF === this.dparent) {
|
|
1633
|
+
// Even if there's no data, dpath should continue to match path, so that
|
|
1634
|
+
// relative paths work properly.
|
|
1635
|
+
if (1 < this.dpath.length) {
|
|
1636
|
+
this.dpath = [...this.dpath, parentkey];
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
else {
|
|
1640
|
+
// this.dparent is the containing node of the current store value.
|
|
1641
|
+
if (null != parentkey) {
|
|
1642
|
+
this.dparent = getprop(this.dparent, parentkey);
|
|
1643
|
+
let lastpart = getelem(this.dpath, -1);
|
|
1644
|
+
if (lastpart === '$:' + parentkey) {
|
|
1645
|
+
this.dpath = slice(this.dpath, -1);
|
|
1646
|
+
}
|
|
1647
|
+
else {
|
|
1648
|
+
this.dpath = [...this.dpath, parentkey];
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
return this.dparent;
|
|
1653
|
+
}
|
|
1654
|
+
child(keyI, keys) {
|
|
1655
|
+
const key = strkey(keys[keyI]);
|
|
1656
|
+
const val = this.val;
|
|
1657
|
+
const cinj = new Injection(getprop(val, key), val);
|
|
1658
|
+
cinj.keyI = keyI;
|
|
1659
|
+
cinj.keys = keys;
|
|
1660
|
+
cinj.key = key;
|
|
1661
|
+
cinj.path = [...(this.path || []), key];
|
|
1662
|
+
cinj.nodes = [...(this.nodes || []), val];
|
|
1663
|
+
cinj.mode = this.mode;
|
|
1664
|
+
cinj.handler = this.handler;
|
|
1665
|
+
cinj.modify = this.modify;
|
|
1666
|
+
cinj.base = this.base;
|
|
1667
|
+
cinj.meta = this.meta;
|
|
1668
|
+
cinj.errs = this.errs;
|
|
1669
|
+
cinj.prior = this;
|
|
1670
|
+
cinj.dpath = [...this.dpath];
|
|
1671
|
+
cinj.dparent = this.dparent;
|
|
1672
|
+
return cinj;
|
|
1673
|
+
}
|
|
1674
|
+
setval(val, ancestor) {
|
|
1675
|
+
if (null == ancestor || ancestor < 2) {
|
|
1676
|
+
return UNDEF === val ?
|
|
1677
|
+
delprop(this.parent, this.key) :
|
|
1678
|
+
setprop(this.parent, this.key, val);
|
|
1679
|
+
}
|
|
1680
|
+
else {
|
|
1681
|
+
const aval = getelem(this.nodes, 0 - ancestor);
|
|
1682
|
+
const akey = getelem(this.path, 0 - ancestor);
|
|
1683
|
+
return UNDEF === val ?
|
|
1684
|
+
delprop(aval, akey) :
|
|
1685
|
+
setprop(aval, akey, val);
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1328
1689
|
// Internal utilities
|
|
1329
1690
|
// ==================
|
|
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
1691
|
// Update all references to target in inj.nodes.
|
|
1336
1692
|
function _updateAncestors(_inj, target, tkey, tval) {
|
|
1337
1693
|
// SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
|
|
@@ -1350,20 +1706,38 @@ function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
|
1350
1706
|
}
|
|
1351
1707
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
1352
1708
|
// call the function passing the injection inj. This is how transforms operate.
|
|
1353
|
-
const _injecthandler = (inj, val,
|
|
1709
|
+
const _injecthandler = (inj, val, ref, store) => {
|
|
1354
1710
|
let out = val;
|
|
1355
1711
|
const iscmd = isfunc(val) && (UNDEF === ref || ref.startsWith(S_DS));
|
|
1356
1712
|
// Only call val function if it is a special command ($NAME format).
|
|
1357
1713
|
if (iscmd) {
|
|
1358
|
-
out = val(inj, val,
|
|
1714
|
+
out = val(inj, val, ref, store);
|
|
1359
1715
|
}
|
|
1360
1716
|
// Update parent with value. Ensures references remain in node tree.
|
|
1361
1717
|
else if (S_MVAL === inj.mode && inj.full) {
|
|
1362
|
-
// _setparentprop(inj, val)
|
|
1363
1718
|
inj.setval(val);
|
|
1364
1719
|
}
|
|
1365
1720
|
return out;
|
|
1366
1721
|
};
|
|
1722
|
+
const _validatehandler = (inj, val, ref, store) => {
|
|
1723
|
+
let out = val;
|
|
1724
|
+
const m = ref.match(R_META_PATH);
|
|
1725
|
+
const ismetapath = null != m;
|
|
1726
|
+
if (ismetapath) {
|
|
1727
|
+
if ('=' === m[2]) {
|
|
1728
|
+
inj.setval(['`$EXACT`', val]);
|
|
1729
|
+
}
|
|
1730
|
+
else {
|
|
1731
|
+
inj.setval(val);
|
|
1732
|
+
}
|
|
1733
|
+
inj.keyI = -1;
|
|
1734
|
+
out = SKIP;
|
|
1735
|
+
}
|
|
1736
|
+
else {
|
|
1737
|
+
out = _injecthandler(inj, val, ref, store);
|
|
1738
|
+
}
|
|
1739
|
+
return out;
|
|
1740
|
+
};
|
|
1367
1741
|
// Inject values from a data store into a string. Not a public utility - used by
|
|
1368
1742
|
// `inject`. Inject are marked with `path` where path is resolved
|
|
1369
1743
|
// with getpath against the store or current (if defined)
|
|
@@ -1373,14 +1747,14 @@ const _injecthandler = (inj, val, current, ref, store) => {
|
|
|
1373
1747
|
// upper case letters only, and 999 is any digits, which are
|
|
1374
1748
|
// discarded. This syntax specifies the name of a transform, and
|
|
1375
1749
|
// optionally allows transforms to be ordered by alphanumeric sorting.
|
|
1376
|
-
function _injectstr(val, store,
|
|
1750
|
+
function _injectstr(val, store, inj) {
|
|
1377
1751
|
// Can't inject into non-strings
|
|
1378
1752
|
if (S_string !== typeof val || S_MT === val) {
|
|
1379
1753
|
return S_MT;
|
|
1380
1754
|
}
|
|
1381
1755
|
let out = val;
|
|
1382
1756
|
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
1383
|
-
const m = val.match(
|
|
1757
|
+
const m = val.match(R_INJECTION_FULL);
|
|
1384
1758
|
// Full string of the val is an injection.
|
|
1385
1759
|
if (m) {
|
|
1386
1760
|
if (null != inj) {
|
|
@@ -1388,37 +1762,38 @@ function _injectstr(val, store, current, inj) {
|
|
|
1388
1762
|
}
|
|
1389
1763
|
let pathref = m[1];
|
|
1390
1764
|
// Special escapes inside injection.
|
|
1391
|
-
pathref =
|
|
1392
|
-
|
|
1765
|
+
pathref = 3 < pathref.length ?
|
|
1766
|
+
pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) :
|
|
1767
|
+
pathref;
|
|
1393
1768
|
// Get the extracted path reference.
|
|
1394
|
-
out = getpath(
|
|
1769
|
+
out = getpath(store, pathref, inj);
|
|
1395
1770
|
}
|
|
1396
1771
|
else {
|
|
1397
1772
|
// Check for injections within the string.
|
|
1398
1773
|
const partial = (_m, ref) => {
|
|
1399
1774
|
// Special escapes inside injection.
|
|
1400
|
-
ref = 3 < ref.length ? ref.replace(
|
|
1775
|
+
ref = 3 < ref.length ? ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) : ref;
|
|
1401
1776
|
if (inj) {
|
|
1402
1777
|
inj.full = false;
|
|
1403
1778
|
}
|
|
1404
|
-
const found = getpath(
|
|
1779
|
+
const found = getpath(store, ref, inj);
|
|
1405
1780
|
// Ensure inject value is a string.
|
|
1406
1781
|
return UNDEF === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
1407
1782
|
};
|
|
1408
|
-
out = val.replace(
|
|
1783
|
+
out = val.replace(R_INJECTION_PARTIAL, partial);
|
|
1409
1784
|
// Also call the inj handler on the entire string, providing the
|
|
1410
1785
|
// option for custom injection.
|
|
1411
1786
|
if (null != inj && isfunc(inj.handler)) {
|
|
1412
1787
|
inj.full = true;
|
|
1413
|
-
out = inj.handler(inj, out,
|
|
1788
|
+
out = inj.handler(inj, out, val, store);
|
|
1414
1789
|
}
|
|
1415
1790
|
}
|
|
1416
1791
|
return out;
|
|
1417
1792
|
}
|
|
1418
|
-
// Define a class to mirror the JavaScript implementation
|
|
1419
1793
|
class StructUtility {
|
|
1420
1794
|
constructor() {
|
|
1421
1795
|
this.clone = clone;
|
|
1796
|
+
this.delprop = delprop;
|
|
1422
1797
|
this.escre = escre;
|
|
1423
1798
|
this.escurl = escurl;
|
|
1424
1799
|
this.getelem = getelem;
|
|
@@ -1434,10 +1809,12 @@ class StructUtility {
|
|
|
1434
1809
|
this.isnode = isnode;
|
|
1435
1810
|
this.items = items;
|
|
1436
1811
|
this.joinurl = joinurl;
|
|
1812
|
+
this.jsonify = jsonify;
|
|
1437
1813
|
this.keysof = keysof;
|
|
1438
1814
|
this.merge = merge;
|
|
1439
1815
|
this.pad = pad;
|
|
1440
1816
|
this.pathify = pathify;
|
|
1817
|
+
this.select = select;
|
|
1441
1818
|
this.setprop = setprop;
|
|
1442
1819
|
this.size = size;
|
|
1443
1820
|
this.slice = slice;
|