@voxgig/struct 0.0.10 → 0.2.0
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/README.md +888 -0
- package/dist/StructUtility.d.ts +10 -4
- package/dist/StructUtility.js +360 -182
- package/dist/StructUtility.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -6
- package/src/StructUtility.ts +712 -696
package/dist/StructUtility.js
CHANGED
|
@@ -39,12 +39,18 @@ exports.typify = typify;
|
|
|
39
39
|
exports.typename = typename;
|
|
40
40
|
exports.validate = validate;
|
|
41
41
|
exports.walk = walk;
|
|
42
|
+
exports.re_compile = re_compile;
|
|
43
|
+
exports.re_find = re_find;
|
|
44
|
+
exports.re_find_all = re_find_all;
|
|
45
|
+
exports.re_replace = re_replace;
|
|
46
|
+
exports.re_test = re_test;
|
|
47
|
+
exports.re_escape = re_escape;
|
|
42
48
|
exports.jm = jm;
|
|
43
49
|
exports.jt = jt;
|
|
44
50
|
exports.checkPlacement = checkPlacement;
|
|
45
51
|
exports.injectorArgs = injectorArgs;
|
|
46
52
|
exports.injectChild = injectChild;
|
|
47
|
-
// VERSION: @voxgig/struct 0.0
|
|
53
|
+
// VERSION: @voxgig/struct 0.2.0
|
|
48
54
|
/* Voxgig Struct
|
|
49
55
|
* =============
|
|
50
56
|
*
|
|
@@ -189,12 +195,20 @@ const TYPENAME = [
|
|
|
189
195
|
S_function,
|
|
190
196
|
S_symbol,
|
|
191
197
|
S_null,
|
|
192
|
-
'',
|
|
193
|
-
'',
|
|
198
|
+
'',
|
|
199
|
+
'',
|
|
200
|
+
'',
|
|
201
|
+
'',
|
|
202
|
+
'',
|
|
203
|
+
'',
|
|
204
|
+
'',
|
|
194
205
|
S_list,
|
|
195
206
|
S_map,
|
|
196
207
|
S_instance,
|
|
197
|
-
'',
|
|
208
|
+
'',
|
|
209
|
+
'',
|
|
210
|
+
'',
|
|
211
|
+
'',
|
|
198
212
|
S_scalar,
|
|
199
213
|
S_node,
|
|
200
214
|
];
|
|
@@ -208,9 +222,6 @@ exports.DELETE = DELETE;
|
|
|
208
222
|
// Regular expression constants
|
|
209
223
|
const R_INTEGER_KEY = /^[-0-9]+$/; // Match integer keys (including <0).
|
|
210
224
|
const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g; // Chars that need escaping in regexp.
|
|
211
|
-
const R_TRAILING_SLASH = /\/+$/; // Trailing slashes in URLs.
|
|
212
|
-
const R_LEADING_TRAILING_SLASH = /([^\/])\/+/; // Multiple slashes in URL middle.
|
|
213
|
-
const R_LEADING_SLASH = /^\/+/; // Leading slashes in URLs.
|
|
214
225
|
const R_QUOTES = /"/g; // Double quotes for removal.
|
|
215
226
|
const R_DOT = /\./g; // Dots in path strings.
|
|
216
227
|
const R_CLONE_REF = /^`\$REF:([0-9]+)`$/; // Copy reference in cloning.
|
|
@@ -255,9 +266,10 @@ function iskey(key) {
|
|
|
255
266
|
}
|
|
256
267
|
// Check for an "empty" value - undefined, empty string, array, object.
|
|
257
268
|
function isempty(val) {
|
|
258
|
-
return null == val ||
|
|
269
|
+
return (null == val ||
|
|
270
|
+
S_MT === val ||
|
|
259
271
|
(Array.isArray(val) && 0 === val.length) ||
|
|
260
|
-
(S_object === typeof val && 0 === Object.keys(val).length);
|
|
272
|
+
(S_object === typeof val && 0 === Object.keys(val).length));
|
|
261
273
|
}
|
|
262
274
|
// Value is a function.
|
|
263
275
|
function isfunc(val) {
|
|
@@ -334,7 +346,7 @@ function slice(val, start, end, mutate) {
|
|
|
334
346
|
for (let i = 0, j = start; j < end; i++, j++) {
|
|
335
347
|
val[i] = val[j];
|
|
336
348
|
}
|
|
337
|
-
val.length =
|
|
349
|
+
val.length = end - start;
|
|
338
350
|
}
|
|
339
351
|
else {
|
|
340
352
|
val = val.slice(start, end);
|
|
@@ -359,7 +371,7 @@ function slice(val, start, end, mutate) {
|
|
|
359
371
|
function pad(str, padding, padchar) {
|
|
360
372
|
str = S_string === typeof str ? str : stringify(str);
|
|
361
373
|
padding = null == padding ? 44 : padding;
|
|
362
|
-
padchar = null == padchar ? S_SP : (
|
|
374
|
+
padchar = null == padchar ? S_SP : (padchar + S_SP)[0];
|
|
363
375
|
return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar);
|
|
364
376
|
}
|
|
365
377
|
// Determine the type of a value as a bit code.
|
|
@@ -400,7 +412,7 @@ function typify(value) {
|
|
|
400
412
|
}
|
|
401
413
|
else if (S_object === typestr) {
|
|
402
414
|
if (value.constructor instanceof Function) {
|
|
403
|
-
|
|
415
|
+
const cname = value.constructor.name;
|
|
404
416
|
if ('Object' !== cname && 'Array' !== cname) {
|
|
405
417
|
return T_node | T_instance;
|
|
406
418
|
}
|
|
@@ -418,15 +430,16 @@ function getelem(val, key, alt) {
|
|
|
418
430
|
return alt;
|
|
419
431
|
}
|
|
420
432
|
if (islist(val)) {
|
|
421
|
-
|
|
422
|
-
if (Number.isInteger(nkey) && ('' + key)
|
|
433
|
+
const nkey = parseInt(key);
|
|
434
|
+
if (Number.isInteger(nkey) && re_test(R_INTEGER_KEY, '' + key)) {
|
|
423
435
|
if (nkey < 0) {
|
|
424
436
|
key = val.length + nkey;
|
|
425
437
|
}
|
|
426
438
|
out = val[key];
|
|
427
439
|
}
|
|
428
440
|
}
|
|
429
|
-
|
|
441
|
+
// null at a slot counts as "no value" — same Group A rule as getprop.
|
|
442
|
+
if (null == out) {
|
|
430
443
|
return 0 < (T_function & typify(alt)) ? alt() : alt;
|
|
431
444
|
}
|
|
432
445
|
return out;
|
|
@@ -441,11 +454,27 @@ function getprop(val, key, alt) {
|
|
|
441
454
|
if (isnode(val)) {
|
|
442
455
|
out = val[key];
|
|
443
456
|
}
|
|
444
|
-
|
|
457
|
+
// JSON null at a key is treated as "no value" for the default-substitution
|
|
458
|
+
// rule. This unifies cross-port behaviour: every host language conflates
|
|
459
|
+
// null and absent at the value level either always or via positional
|
|
460
|
+
// checks; the library follows that constraint on observation.
|
|
461
|
+
if (null == out) {
|
|
445
462
|
return alt;
|
|
446
463
|
}
|
|
447
464
|
return out;
|
|
448
465
|
}
|
|
466
|
+
// Internal: literal value lookup that preserves stored JSON null. Group B
|
|
467
|
+
// callers (validate / transform commands / builders / inject internals) use
|
|
468
|
+
// this when they need to inspect the raw stored value at a slot regardless
|
|
469
|
+
// of whether it is null. The public getprop / getelem / haskey APIs treat
|
|
470
|
+
// null as absent (Group A) per UNDEF_SPEC.md.
|
|
471
|
+
function _lookup(val, key) {
|
|
472
|
+
if (NONE === val || NONE === key)
|
|
473
|
+
return NONE;
|
|
474
|
+
if (isnode(val))
|
|
475
|
+
return val[key];
|
|
476
|
+
return NONE;
|
|
477
|
+
}
|
|
449
478
|
// Convert different types of keys to string representation.
|
|
450
479
|
// String keys are returned as is.
|
|
451
480
|
// Number keys are converted to strings.
|
|
@@ -470,13 +499,17 @@ function strkey(key = NONE) {
|
|
|
470
499
|
// Sorted keys of a map, or indexes (as strings) of a list.
|
|
471
500
|
// Root utility - only uses language facilities.
|
|
472
501
|
function keysof(val) {
|
|
473
|
-
return !isnode(val)
|
|
474
|
-
|
|
502
|
+
return !isnode(val)
|
|
503
|
+
? []
|
|
504
|
+
: ismap(val)
|
|
505
|
+
? Object.keys(val).sort()
|
|
506
|
+
: val.map((_n, i) => S_MT + i);
|
|
475
507
|
}
|
|
476
508
|
// Value of property with name key in node val is defined.
|
|
477
509
|
// Root utility - only uses language facilities.
|
|
478
510
|
function haskey(val, key) {
|
|
479
|
-
|
|
511
|
+
// null at a key counts as "no value" — same rule as getprop.
|
|
512
|
+
return null != getprop(val, key);
|
|
480
513
|
}
|
|
481
514
|
function items(val, apply) {
|
|
482
515
|
let out = keysof(val).map((k) => [k, val[k]]);
|
|
@@ -498,9 +531,9 @@ function flatten(list, depth) {
|
|
|
498
531
|
}
|
|
499
532
|
// Filter item values using check function.
|
|
500
533
|
function filter(val, check) {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
534
|
+
const all = items(val);
|
|
535
|
+
const numall = size(all);
|
|
536
|
+
const out = [];
|
|
504
537
|
for (let i = 0; i < numall; i++) {
|
|
505
538
|
if (check(all[i])) {
|
|
506
539
|
out.push(all[i][1]);
|
|
@@ -510,18 +543,83 @@ function filter(val, check) {
|
|
|
510
543
|
}
|
|
511
544
|
// Escape regular expression.
|
|
512
545
|
function escre(s) {
|
|
513
|
-
|
|
514
|
-
|
|
546
|
+
return re_replace(R_ESCAPE_REGEXP, s, '\\$&');
|
|
547
|
+
}
|
|
548
|
+
// Alias so call sites read naturally next to the other re_* helpers.
|
|
549
|
+
function re_escape(s) {
|
|
550
|
+
return escre(s);
|
|
515
551
|
}
|
|
516
552
|
// Escape URLs.
|
|
517
553
|
function escurl(s) {
|
|
518
554
|
s = null == s ? S_MT : s;
|
|
519
555
|
return encodeURIComponent(s);
|
|
520
556
|
}
|
|
557
|
+
// ===========================================================================
|
|
558
|
+
// Regex utility — uniform API. Every port exposes these same names. The
|
|
559
|
+
// dialect is the RE2 subset documented in /REGEX.md. Internal library code
|
|
560
|
+
// uses these helpers instead of host-language regex methods directly.
|
|
561
|
+
// ===========================================================================
|
|
562
|
+
// Compile a regex (or return as-is if already compiled). Cached behaviour is
|
|
563
|
+
// up to the port; in TS we just construct a RegExp.
|
|
564
|
+
function re_compile(pattern, flags) {
|
|
565
|
+
if (pattern instanceof RegExp) {
|
|
566
|
+
return pattern;
|
|
567
|
+
}
|
|
568
|
+
return new RegExp(pattern, flags);
|
|
569
|
+
}
|
|
570
|
+
// First match. Returns [whole, capture1, ...] or null.
|
|
571
|
+
function re_find(pattern, input) {
|
|
572
|
+
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
|
573
|
+
return input.match(re);
|
|
574
|
+
}
|
|
575
|
+
// All non-overlapping matches, left to right. Each element is the same shape
|
|
576
|
+
// as re_find's array.
|
|
577
|
+
function re_find_all(pattern, input) {
|
|
578
|
+
let re;
|
|
579
|
+
if (pattern instanceof RegExp) {
|
|
580
|
+
re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g');
|
|
581
|
+
}
|
|
582
|
+
else {
|
|
583
|
+
re = new RegExp(pattern, 'g');
|
|
584
|
+
}
|
|
585
|
+
const out = [];
|
|
586
|
+
let m;
|
|
587
|
+
while ((m = re.exec(input)) !== null) {
|
|
588
|
+
out.push(m);
|
|
589
|
+
if (m[0] === '')
|
|
590
|
+
re.lastIndex++; // avoid infinite loop on empty match
|
|
591
|
+
}
|
|
592
|
+
return out;
|
|
593
|
+
}
|
|
594
|
+
// Replace every match. `replacement` is either a string (with $& and $1..$9)
|
|
595
|
+
// or a callback receiving the same array as re_find returns.
|
|
596
|
+
function re_replace(pattern, input, replacement) {
|
|
597
|
+
let re;
|
|
598
|
+
if (pattern instanceof RegExp) {
|
|
599
|
+
re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g');
|
|
600
|
+
}
|
|
601
|
+
else {
|
|
602
|
+
re = new RegExp(pattern, 'g');
|
|
603
|
+
}
|
|
604
|
+
if (typeof replacement === 'function') {
|
|
605
|
+
return input.replace(re, (...args) => {
|
|
606
|
+
// Strip the trailing offset + full string args String.prototype.replace passes.
|
|
607
|
+
// The callback below receives just the capture groups (whole at [0]).
|
|
608
|
+
const groups = args.slice(0, -2);
|
|
609
|
+
return replacement(groups);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
return input.replace(re, replacement);
|
|
613
|
+
}
|
|
614
|
+
// Boolean test for any match.
|
|
615
|
+
function re_test(pattern, input) {
|
|
616
|
+
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
|
617
|
+
return re.test(input);
|
|
618
|
+
}
|
|
521
619
|
// Replace a search string (all), or a regexp, in a source string.
|
|
522
620
|
function replace(s, from, to) {
|
|
523
621
|
let rs = s;
|
|
524
|
-
|
|
622
|
+
const ts = typify(s);
|
|
525
623
|
if (0 === (T_string & ts)) {
|
|
526
624
|
rs = stringify(s);
|
|
527
625
|
}
|
|
@@ -540,25 +638,24 @@ function join(arr, sep, url) {
|
|
|
540
638
|
const sepre = 1 === size(sepdef) ? escre(sepdef) : NONE;
|
|
541
639
|
const out = filter(items(
|
|
542
640
|
// filter(arr, (n) => null != n[1] && S_MT !== n[1]),
|
|
543
|
-
filter(arr, (n) =>
|
|
544
|
-
|
|
641
|
+
filter(arr, (n) => 0 < (T_string & typify(n[1])) && S_MT !== n[1]), (n) => {
|
|
642
|
+
const i = +n[0];
|
|
545
643
|
let s = n[1];
|
|
546
644
|
if (NONE !== sepre && S_MT !== sepre) {
|
|
547
645
|
if (url && 0 === i) {
|
|
548
|
-
s =
|
|
646
|
+
s = re_replace(re_compile(sepre + '+$'), s, S_MT);
|
|
549
647
|
return s;
|
|
550
648
|
}
|
|
551
649
|
if (0 < i) {
|
|
552
|
-
s =
|
|
650
|
+
s = re_replace(re_compile('^' + sepre + '+'), s, S_MT);
|
|
553
651
|
}
|
|
554
652
|
if (i < sarr - 1 || !url) {
|
|
555
|
-
s =
|
|
653
|
+
s = re_replace(re_compile(sepre + '+$'), s, S_MT);
|
|
556
654
|
}
|
|
557
|
-
s =
|
|
655
|
+
s = re_replace(re_compile('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'), s, '$1' + sepdef + '$2');
|
|
558
656
|
}
|
|
559
657
|
return s;
|
|
560
|
-
}), (n) => S_MT !== n[1])
|
|
561
|
-
.join(sepdef);
|
|
658
|
+
}), (n) => S_MT !== n[1]).join(sepdef);
|
|
562
659
|
return out;
|
|
563
660
|
}
|
|
564
661
|
// Output JSON in a "standard" format, with 2 space indents, each property on a new line,
|
|
@@ -577,11 +674,12 @@ function jsonify(val, flags) {
|
|
|
577
674
|
if (0 < offset) {
|
|
578
675
|
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
579
676
|
// indented by offset. Assume first brace is on line with asignment, so not offset.
|
|
580
|
-
str =
|
|
581
|
-
|
|
677
|
+
str =
|
|
678
|
+
'{\n' +
|
|
679
|
+
join(items(slice(str.split('\n'), 1), (n) => pad(n[1], 0 - offset - size(n[1]))), '\n');
|
|
582
680
|
}
|
|
583
681
|
}
|
|
584
|
-
catch
|
|
682
|
+
catch {
|
|
585
683
|
str = '__JSONIFY_FAILED__';
|
|
586
684
|
}
|
|
587
685
|
}
|
|
@@ -600,9 +698,7 @@ function stringify(val, maxlen, pretty) {
|
|
|
600
698
|
else {
|
|
601
699
|
try {
|
|
602
700
|
valstr = JSON.stringify(val, function (_key, val) {
|
|
603
|
-
if (val !== null &&
|
|
604
|
-
typeof val === "object" &&
|
|
605
|
-
!Array.isArray(val)) {
|
|
701
|
+
if (val !== null && typeof val === 'object' && !Array.isArray(val)) {
|
|
606
702
|
const sortedObj = {};
|
|
607
703
|
items(val, (n) => {
|
|
608
704
|
sortedObj[n[0]] = val[n[0]];
|
|
@@ -611,19 +707,21 @@ function stringify(val, maxlen, pretty) {
|
|
|
611
707
|
}
|
|
612
708
|
return val;
|
|
613
709
|
});
|
|
614
|
-
valstr =
|
|
710
|
+
valstr = re_replace(R_QUOTES, valstr, S_MT);
|
|
615
711
|
}
|
|
616
|
-
catch
|
|
712
|
+
catch {
|
|
617
713
|
valstr = '__STRINGIFY_FAILED__';
|
|
618
714
|
}
|
|
619
715
|
}
|
|
620
716
|
if (null != maxlen && -1 < maxlen) {
|
|
621
|
-
|
|
622
|
-
valstr = maxlen < valstr.length ?
|
|
717
|
+
const js = valstr.substring(0, maxlen);
|
|
718
|
+
valstr = maxlen < valstr.length ? js.substring(0, maxlen - 3) + '...' : valstr;
|
|
623
719
|
}
|
|
624
720
|
if (pretty) {
|
|
625
721
|
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
626
|
-
|
|
722
|
+
const c = items([81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69], (n) => '\x1b[38;5;' + n[1] + 'm');
|
|
723
|
+
const r = '\x1b[0m';
|
|
724
|
+
let d = 0, o = c[0], t = o;
|
|
627
725
|
for (const ch of valstr) {
|
|
628
726
|
if (ch === '{' || ch === '[') {
|
|
629
727
|
d++;
|
|
@@ -646,10 +744,13 @@ function stringify(val, maxlen, pretty) {
|
|
|
646
744
|
// Build a human friendly path string.
|
|
647
745
|
function pathify(val, startin, endin) {
|
|
648
746
|
let pathstr = NONE;
|
|
649
|
-
let path = islist(val)
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
747
|
+
let path = islist(val)
|
|
748
|
+
? val
|
|
749
|
+
: S_string == typeof val
|
|
750
|
+
? [val]
|
|
751
|
+
: S_number == typeof val
|
|
752
|
+
? [val]
|
|
753
|
+
: NONE;
|
|
653
754
|
const start = null == startin ? 0 : -1 < startin ? startin : 0;
|
|
654
755
|
const end = null == endin ? 0 : -1 < endin ? endin : 0;
|
|
655
756
|
if (NONE != path && 0 <= start) {
|
|
@@ -659,9 +760,8 @@ function pathify(val, startin, endin) {
|
|
|
659
760
|
}
|
|
660
761
|
else {
|
|
661
762
|
pathstr = join(items(filter(path, (n) => iskey(n[1])), (n) => {
|
|
662
|
-
|
|
663
|
-
return S_number === typeof p ? S_MT + Math.floor(p) :
|
|
664
|
-
p.replace(R_DOT, S_MT);
|
|
763
|
+
const p = n[1];
|
|
764
|
+
return S_number === typeof p ? S_MT + Math.floor(p) : re_replace(R_DOT, p, S_MT);
|
|
665
765
|
}), S_DT);
|
|
666
766
|
}
|
|
667
767
|
}
|
|
@@ -675,10 +775,8 @@ function pathify(val, startin, endin) {
|
|
|
675
775
|
function clone(val) {
|
|
676
776
|
const refs = [];
|
|
677
777
|
const reftype = T_function | T_instance;
|
|
678
|
-
const replacer = (_k, v) => 0 < (reftype & typify(v)) ?
|
|
679
|
-
|
|
680
|
-
const reviver = (_k, v, m) => S_string === typeof v ?
|
|
681
|
-
(m = v.match(R_CLONE_REF), m ? refs[m[1]] : v) : v;
|
|
778
|
+
const replacer = (_k, v) => 0 < (reftype & typify(v)) ? (refs.push(v), '`$REF:' + (refs.length - 1) + '`') : v;
|
|
779
|
+
const reviver = (_k, v, m) => S_string === typeof v ? ((m = re_find(R_CLONE_REF, v)), m ? refs[m[1]] : v) : v;
|
|
682
780
|
const out = NONE === val ? NONE : JSON.parse(JSON.stringify(val, replacer), reviver);
|
|
683
781
|
return out;
|
|
684
782
|
}
|
|
@@ -687,9 +785,12 @@ function jm(...kv) {
|
|
|
687
785
|
const kvsize = size(kv);
|
|
688
786
|
const o = {};
|
|
689
787
|
for (let i = 0; i < kvsize; i += 2) {
|
|
690
|
-
|
|
788
|
+
// Builders are Group B (value processing): preserve literal stored
|
|
789
|
+
// values, including null. Direct array index distinguishes "slot exists
|
|
790
|
+
// with value null" from "slot beyond end of kv".
|
|
791
|
+
let k = i < kv.length ? kv[i] : '$KEY' + i;
|
|
691
792
|
k = 'string' === typeof k ? k : stringify(k);
|
|
692
|
-
o[k] =
|
|
793
|
+
o[k] = i + 1 < kv.length ? kv[i + 1] : null;
|
|
693
794
|
}
|
|
694
795
|
return o;
|
|
695
796
|
}
|
|
@@ -702,7 +803,7 @@ function jt(...v) {
|
|
|
702
803
|
}
|
|
703
804
|
return a;
|
|
704
805
|
}
|
|
705
|
-
// Safely delete a property from an object or array element.
|
|
806
|
+
// Safely delete a property from an object or array element.
|
|
706
807
|
// Undefined arguments and invalid keys are ignored.
|
|
707
808
|
// Returns the (possibly modified) parent.
|
|
708
809
|
// For objects, the property is deleted using the delete operator.
|
|
@@ -768,6 +869,11 @@ function setprop(parent, key, val) {
|
|
|
768
869
|
return parent;
|
|
769
870
|
}
|
|
770
871
|
// Walk a data structure depth first, applying a function to each value.
|
|
872
|
+
// The `path` argument passed to the before/after callbacks is a single
|
|
873
|
+
// mutable array per depth, shared across all callback invocations for the
|
|
874
|
+
// lifetime of this top-level walk call. Callbacks that need to store the
|
|
875
|
+
// path MUST clone it (e.g. `path.slice()`); the contents will otherwise
|
|
876
|
+
// be overwritten by subsequent visits.
|
|
771
877
|
function walk(
|
|
772
878
|
// These arguments are the public interface.
|
|
773
879
|
val,
|
|
@@ -778,18 +884,35 @@ after,
|
|
|
778
884
|
// Maximum recursive depth, default: 32. Use null for infinite depth.
|
|
779
885
|
maxdepth,
|
|
780
886
|
// These areguments are used for recursive state.
|
|
781
|
-
key, parent, path) {
|
|
887
|
+
key, parent, path, pool) {
|
|
888
|
+
if (NONE === pool) {
|
|
889
|
+
pool = [[]];
|
|
890
|
+
}
|
|
782
891
|
if (NONE === path) {
|
|
783
|
-
path = [];
|
|
892
|
+
path = pool[0];
|
|
784
893
|
}
|
|
894
|
+
const depth = path.length;
|
|
785
895
|
let out = null == before ? val : before(key, val, parent, path);
|
|
786
896
|
maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : MAXDEPTH;
|
|
787
|
-
if (0 === maxdepth || (
|
|
897
|
+
if (0 === maxdepth || (0 < maxdepth && maxdepth <= depth)) {
|
|
788
898
|
return out;
|
|
789
899
|
}
|
|
790
900
|
if (isnode(out)) {
|
|
791
|
-
|
|
792
|
-
|
|
901
|
+
const childDepth = depth + 1;
|
|
902
|
+
let childPath = pool[childDepth];
|
|
903
|
+
if (NONE === childPath) {
|
|
904
|
+
childPath = new Array(childDepth);
|
|
905
|
+
pool[childDepth] = childPath;
|
|
906
|
+
}
|
|
907
|
+
// Sync prefix [0..depth-1] from the current path. Only needed once per
|
|
908
|
+
// parent: siblings share the same prefix and will each overwrite slot
|
|
909
|
+
// [depth] below.
|
|
910
|
+
for (let i = 0; i < depth; i++) {
|
|
911
|
+
childPath[i] = path[i];
|
|
912
|
+
}
|
|
913
|
+
for (const [ckey, child] of items(out)) {
|
|
914
|
+
childPath[depth] = S_MT + ckey;
|
|
915
|
+
setprop(out, ckey, walk(child, before, after, maxdepth, ckey, out, childPath, pool));
|
|
793
916
|
}
|
|
794
917
|
}
|
|
795
918
|
out = null == after ? out : after(key, out, parent, path);
|
|
@@ -818,16 +941,16 @@ function merge(val, maxdepth) {
|
|
|
818
941
|
// Merge a list of values.
|
|
819
942
|
out = getprop(list, 0, {});
|
|
820
943
|
for (let oI = 1; oI < lenlist; oI++) {
|
|
821
|
-
|
|
944
|
+
const obj = list[oI];
|
|
822
945
|
if (!isnode(obj)) {
|
|
823
946
|
// Nodes win.
|
|
824
947
|
out = obj;
|
|
825
948
|
}
|
|
826
949
|
else {
|
|
827
950
|
// Current value at path end in overriding node.
|
|
828
|
-
|
|
951
|
+
const cur = [out];
|
|
829
952
|
// Current value at path end in destination node.
|
|
830
|
-
|
|
953
|
+
const dst = [out];
|
|
831
954
|
function before(key, val, _parent, path) {
|
|
832
955
|
const pI = size(path);
|
|
833
956
|
if (md <= pI) {
|
|
@@ -886,9 +1009,13 @@ function merge(val, maxdepth) {
|
|
|
886
1009
|
// String paths create only maps. Use a string list to create list parts.
|
|
887
1010
|
function setpath(store, path, val, injdef) {
|
|
888
1011
|
const pathType = typify(path);
|
|
889
|
-
const parts = 0 < (T_list & pathType)
|
|
890
|
-
|
|
891
|
-
|
|
1012
|
+
const parts = 0 < (T_list & pathType)
|
|
1013
|
+
? path
|
|
1014
|
+
: 0 < (T_string & pathType)
|
|
1015
|
+
? path.split(S_DT)
|
|
1016
|
+
: 0 < (T_number & pathType)
|
|
1017
|
+
? [path]
|
|
1018
|
+
: NONE;
|
|
892
1019
|
if (NONE === parts) {
|
|
893
1020
|
return NONE;
|
|
894
1021
|
}
|
|
@@ -914,9 +1041,13 @@ function setpath(store, path, val, injdef) {
|
|
|
914
1041
|
}
|
|
915
1042
|
function getpath(store, path, injdef) {
|
|
916
1043
|
// Operate on a string array.
|
|
917
|
-
const parts = islist(path)
|
|
918
|
-
|
|
919
|
-
|
|
1044
|
+
const parts = islist(path)
|
|
1045
|
+
? path
|
|
1046
|
+
: 'string' === typeof path
|
|
1047
|
+
? path.split(S_DT)
|
|
1048
|
+
: 'number' === typeof path
|
|
1049
|
+
? [strkey(path)]
|
|
1050
|
+
: NONE;
|
|
920
1051
|
if (NONE === parts) {
|
|
921
1052
|
return NONE;
|
|
922
1053
|
}
|
|
@@ -937,7 +1068,7 @@ function getpath(store, path, injdef) {
|
|
|
937
1068
|
}
|
|
938
1069
|
if (!isfunc(val)) {
|
|
939
1070
|
val = src;
|
|
940
|
-
const m = parts[0]
|
|
1071
|
+
const m = re_find(R_META_PATH, parts[0]);
|
|
941
1072
|
if (m && injdef && injdef.meta) {
|
|
942
1073
|
val = getprop(injdef.meta, m[1]);
|
|
943
1074
|
parts[0] = m[3];
|
|
@@ -961,7 +1092,7 @@ function getpath(store, path, injdef) {
|
|
|
961
1092
|
part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)));
|
|
962
1093
|
}
|
|
963
1094
|
// $$ escapes $
|
|
964
|
-
part =
|
|
1095
|
+
part = re_replace(R_DOUBLE_DOLLAR, part, '$');
|
|
965
1096
|
if (S_MT === part) {
|
|
966
1097
|
let ascends = 0;
|
|
967
1098
|
while (S_MT === parts[1 + pI]) {
|
|
@@ -1041,8 +1172,8 @@ function inject(val, store, injdef) {
|
|
|
1041
1172
|
nodekeys = keysof(val);
|
|
1042
1173
|
if (ismap(val)) {
|
|
1043
1174
|
nodekeys = flatten([
|
|
1044
|
-
filter(nodekeys, (n => !n[1].includes(S_DS))
|
|
1045
|
-
filter(nodekeys, (n => n[1].includes(S_DS))
|
|
1175
|
+
filter(nodekeys, (n) => !n[1].includes(S_DS)),
|
|
1176
|
+
filter(nodekeys, (n) => n[1].includes(S_DS)),
|
|
1046
1177
|
]);
|
|
1047
1178
|
}
|
|
1048
1179
|
else {
|
|
@@ -1090,16 +1221,16 @@ function inject(val, store, injdef) {
|
|
|
1090
1221
|
}
|
|
1091
1222
|
// Custom modification.
|
|
1092
1223
|
if (inj.modify && SKIP !== val) {
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1224
|
+
const mkey = inj.key;
|
|
1225
|
+
const mparent = inj.parent;
|
|
1226
|
+
const mval = getprop(mparent, mkey);
|
|
1096
1227
|
inj.modify(mval, mkey, mparent, inj, store);
|
|
1097
1228
|
}
|
|
1098
1229
|
// console.log('INJ-VAL', val)
|
|
1099
1230
|
inj.val = val;
|
|
1100
1231
|
// Original val reference may no longer be correct.
|
|
1101
1232
|
// This return value is only used as the top level result.
|
|
1102
|
-
return
|
|
1233
|
+
return _lookup(inj.parent, S_DTOP);
|
|
1103
1234
|
}
|
|
1104
1235
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
1105
1236
|
// Delete a key from a map or list.
|
|
@@ -1113,7 +1244,7 @@ const transform_COPY = (inj, _val) => {
|
|
|
1113
1244
|
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
1114
1245
|
return NONE;
|
|
1115
1246
|
}
|
|
1116
|
-
|
|
1247
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1117
1248
|
inj.setval(out);
|
|
1118
1249
|
return out;
|
|
1119
1250
|
};
|
|
@@ -1126,14 +1257,14 @@ const transform_KEY = (inj) => {
|
|
|
1126
1257
|
return NONE;
|
|
1127
1258
|
}
|
|
1128
1259
|
// Key is defined by $KEY meta property.
|
|
1129
|
-
const keyspec =
|
|
1260
|
+
const keyspec = _lookup(parent, S_BKEY);
|
|
1130
1261
|
if (NONE !== keyspec) {
|
|
1131
1262
|
delprop(parent, S_BKEY);
|
|
1132
1263
|
return getprop(inj.dparent, keyspec);
|
|
1133
1264
|
}
|
|
1134
1265
|
// Key is defined within general purpose $META object.
|
|
1135
1266
|
// return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1136
|
-
return
|
|
1267
|
+
return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2);
|
|
1137
1268
|
};
|
|
1138
1269
|
// Annotate node. Does nothing itself, just used by
|
|
1139
1270
|
// other injectors, and is removed when called.
|
|
@@ -1142,9 +1273,9 @@ const transform_ANNO = (inj) => {
|
|
|
1142
1273
|
delprop(parent, S_BANNO);
|
|
1143
1274
|
return NONE;
|
|
1144
1275
|
};
|
|
1145
|
-
// Merge a list of objects into the current object.
|
|
1276
|
+
// Merge a list of objects into the current object.
|
|
1146
1277
|
// Must be a key in an object. The value is merged over the current object.
|
|
1147
|
-
// If the value is an array, the elements are first merged using `merge`.
|
|
1278
|
+
// If the value is an array, the elements are first merged using `merge`.
|
|
1148
1279
|
// If the value is the empty string, merge the top level store.
|
|
1149
1280
|
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
1150
1281
|
const transform_MERGE = (inj) => {
|
|
@@ -1198,15 +1329,15 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1198
1329
|
tval = items(src, () => clone(child));
|
|
1199
1330
|
}
|
|
1200
1331
|
else if (0 < (T_map & srctype)) {
|
|
1201
|
-
tval = items(src, (n => merge([
|
|
1332
|
+
tval = items(src, (n) => merge([
|
|
1202
1333
|
clone(child),
|
|
1203
1334
|
// Make a note of the key for $KEY transforms.
|
|
1204
|
-
{ [S_BANNO]: { KEY: n[0] } }
|
|
1205
|
-
], 1))
|
|
1335
|
+
{ [S_BANNO]: { KEY: n[0] } },
|
|
1336
|
+
], 1));
|
|
1206
1337
|
}
|
|
1207
1338
|
let rval = [];
|
|
1208
1339
|
if (0 < size(tval)) {
|
|
1209
|
-
tcur = null == src ? NONE :
|
|
1340
|
+
tcur = null == src ? NONE : items(src, (n) => n[1]);
|
|
1210
1341
|
const ckey = getelem(inj.path, -2);
|
|
1211
1342
|
const tpath = slice(inj.path, -1);
|
|
1212
1343
|
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey]);
|
|
@@ -1236,7 +1367,7 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1236
1367
|
// Convert a node to a map.
|
|
1237
1368
|
// Format: { '`$PACK`':['source-path', child-template]}
|
|
1238
1369
|
const transform_PACK = (inj, _val, _ref, store) => {
|
|
1239
|
-
const {
|
|
1370
|
+
const { key, path, parent, nodes } = inj;
|
|
1240
1371
|
const ijname = 'EACH';
|
|
1241
1372
|
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1242
1373
|
return NONE;
|
|
@@ -1275,7 +1406,7 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1275
1406
|
const childspec = delprop(origchildspec, S_BKEY);
|
|
1276
1407
|
const child = getprop(childspec, S_BVAL, childspec);
|
|
1277
1408
|
// Build parallel target object.
|
|
1278
|
-
|
|
1409
|
+
const tval = {};
|
|
1279
1410
|
items(src, (item) => {
|
|
1280
1411
|
const srckey = item[0];
|
|
1281
1412
|
const srcnode = item[1];
|
|
@@ -1301,12 +1432,13 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1301
1432
|
let rval = {};
|
|
1302
1433
|
if (!isempty(tval)) {
|
|
1303
1434
|
// Build parallel source object.
|
|
1304
|
-
|
|
1435
|
+
const tsrc = {};
|
|
1305
1436
|
src.reduce((a, n, i) => {
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1437
|
+
const kn = null == keypath
|
|
1438
|
+
? i
|
|
1439
|
+
: keypath.startsWith('`')
|
|
1440
|
+
? inject(keypath, merge([{}, store, { $TOP: n }], 1))
|
|
1441
|
+
: getpath(n, keypath, inj);
|
|
1310
1442
|
setprop(a, kn, n);
|
|
1311
1443
|
return a;
|
|
1312
1444
|
}, tsrc);
|
|
@@ -1343,7 +1475,7 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1343
1475
|
return NONE;
|
|
1344
1476
|
}
|
|
1345
1477
|
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1346
|
-
const refpath =
|
|
1478
|
+
const refpath = _lookup(inj.parent, 1);
|
|
1347
1479
|
inj.keyI = size(inj.keys);
|
|
1348
1480
|
// Spec reference.
|
|
1349
1481
|
const spec = getprop(store, S_DSPEC)();
|
|
@@ -1364,11 +1496,11 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1364
1496
|
return v;
|
|
1365
1497
|
});
|
|
1366
1498
|
}
|
|
1367
|
-
|
|
1499
|
+
const tref = clone(ref);
|
|
1368
1500
|
const cpath = slice(inj.path, -3);
|
|
1369
1501
|
const tpath = slice(inj.path, -1);
|
|
1370
|
-
|
|
1371
|
-
|
|
1502
|
+
const tcur = getpath(store, cpath);
|
|
1503
|
+
const tval = getpath(store, tpath);
|
|
1372
1504
|
let rval = NONE;
|
|
1373
1505
|
if (!hasSubRef || NONE !== tval) {
|
|
1374
1506
|
const tinj = inj.child(0, [getelem(tpath, -1)]);
|
|
@@ -1399,28 +1531,28 @@ const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
|
1399
1531
|
}
|
|
1400
1532
|
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1401
1533
|
// TODO: EACH and PACK should accept customm functions too
|
|
1402
|
-
const name =
|
|
1403
|
-
const child =
|
|
1534
|
+
const name = _lookup(inj.parent, 1);
|
|
1535
|
+
const child = _lookup(inj.parent, 2);
|
|
1404
1536
|
// Source data.
|
|
1405
1537
|
const tkey = getelem(inj.path, -2);
|
|
1406
1538
|
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1));
|
|
1407
1539
|
const cinj = injectChild(child, store, inj);
|
|
1408
1540
|
const resolved = cinj.val;
|
|
1409
|
-
|
|
1541
|
+
const formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name);
|
|
1410
1542
|
if (NONE === formatter) {
|
|
1411
1543
|
inj.errs.push('$FORMAT: unknown format: ' + name + '.');
|
|
1412
1544
|
return NONE;
|
|
1413
1545
|
}
|
|
1414
|
-
|
|
1546
|
+
const out = walk(resolved, formatter);
|
|
1415
1547
|
setprop(target, tkey, out);
|
|
1416
1548
|
// _updateAncestors(inj, target, tkey, out)
|
|
1417
1549
|
return out;
|
|
1418
1550
|
};
|
|
1419
1551
|
const FORMATTER = {
|
|
1420
1552
|
identity: (_k, v) => v,
|
|
1421
|
-
upper: (_k, v) => isnode(v) ? v : ('' + v).toUpperCase(),
|
|
1422
|
-
lower: (_k, v) => isnode(v) ? v : ('' + v).toLowerCase(),
|
|
1423
|
-
string: (_k, v) => isnode(v) ? v :
|
|
1553
|
+
upper: (_k, v) => (isnode(v) ? v : ('' + v).toUpperCase()),
|
|
1554
|
+
lower: (_k, v) => (isnode(v) ? v : ('' + v).toLowerCase()),
|
|
1555
|
+
string: (_k, v) => (isnode(v) ? v : '' + v),
|
|
1424
1556
|
number: (_k, v) => {
|
|
1425
1557
|
if (isnode(v)) {
|
|
1426
1558
|
return v;
|
|
@@ -1445,7 +1577,9 @@ const FORMATTER = {
|
|
|
1445
1577
|
return n | 0;
|
|
1446
1578
|
}
|
|
1447
1579
|
},
|
|
1448
|
-
concat: (k, v) => null == k && islist(v)
|
|
1580
|
+
concat: (k, v) => null == k && islist(v)
|
|
1581
|
+
? join(items(v, (n) => (isnode(n[1]) ? S_MT : S_MT + n[1])), S_MT)
|
|
1582
|
+
: v,
|
|
1449
1583
|
};
|
|
1450
1584
|
const transform_APPLY = (inj, _val, _ref, store) => {
|
|
1451
1585
|
const ijname = 'APPLY';
|
|
@@ -1479,12 +1613,11 @@ injdef) {
|
|
|
1479
1613
|
const collect = null != injdef?.errs;
|
|
1480
1614
|
const errs = injdef?.errs || [];
|
|
1481
1615
|
const extraTransforms = {};
|
|
1482
|
-
const extraData = null == extra
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
]);
|
|
1616
|
+
const extraData = null == extra
|
|
1617
|
+
? NONE
|
|
1618
|
+
: items(extra).reduce((a, n) => (n[0].startsWith(S_DS) ? (extraTransforms[n[0]] = n[1]) : (a[n[0]] = n[1]),
|
|
1619
|
+
a), {});
|
|
1620
|
+
const dataClone = merge([isempty(extraData) ? NONE : clone(extraData), clone(data)]);
|
|
1488
1621
|
// Define a top level store that provides transform operations.
|
|
1489
1622
|
const store = merge([
|
|
1490
1623
|
{
|
|
@@ -1514,10 +1647,10 @@ injdef) {
|
|
|
1514
1647
|
extraTransforms,
|
|
1515
1648
|
{
|
|
1516
1649
|
$ERRS: errs,
|
|
1517
|
-
}
|
|
1650
|
+
},
|
|
1518
1651
|
], 1);
|
|
1519
1652
|
const out = inject(spec, store, injdef);
|
|
1520
|
-
const generr =
|
|
1653
|
+
const generr = 0 < size(errs) && !collect;
|
|
1521
1654
|
if (generr) {
|
|
1522
1655
|
throw new Error(join(errs, ' | '));
|
|
1523
1656
|
}
|
|
@@ -1525,15 +1658,15 @@ injdef) {
|
|
|
1525
1658
|
}
|
|
1526
1659
|
// A required string value. NOTE: Rejects empty strings.
|
|
1527
1660
|
const validate_STRING = (inj) => {
|
|
1528
|
-
|
|
1661
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1529
1662
|
const t = typify(out);
|
|
1530
1663
|
if (0 === (T_string & t)) {
|
|
1531
|
-
|
|
1664
|
+
const msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010');
|
|
1532
1665
|
inj.errs.push(msg);
|
|
1533
1666
|
return NONE;
|
|
1534
1667
|
}
|
|
1535
1668
|
if (S_MT === out) {
|
|
1536
|
-
|
|
1669
|
+
const msg = 'Empty string at ' + pathify(inj.path, 1);
|
|
1537
1670
|
inj.errs.push(msg);
|
|
1538
1671
|
return NONE;
|
|
1539
1672
|
}
|
|
@@ -1542,7 +1675,7 @@ const validate_STRING = (inj) => {
|
|
|
1542
1675
|
const validate_TYPE = (inj, _val, ref) => {
|
|
1543
1676
|
const tname = slice(ref, 1).toLowerCase();
|
|
1544
1677
|
const typev = 1 << (31 - TYPENAME.indexOf(tname));
|
|
1545
|
-
|
|
1678
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1546
1679
|
const t = typify(out);
|
|
1547
1680
|
// console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
|
|
1548
1681
|
if (0 === (t & typev)) {
|
|
@@ -1553,7 +1686,7 @@ const validate_TYPE = (inj, _val, ref) => {
|
|
|
1553
1686
|
};
|
|
1554
1687
|
// Allow any value.
|
|
1555
1688
|
const validate_ANY = (inj) => {
|
|
1556
|
-
|
|
1689
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1557
1690
|
return out;
|
|
1558
1691
|
};
|
|
1559
1692
|
// Specify child values for map or list.
|
|
@@ -1576,7 +1709,7 @@ const validate_CHILD = (inj) => {
|
|
|
1576
1709
|
return NONE;
|
|
1577
1710
|
}
|
|
1578
1711
|
const ckeys = keysof(tval);
|
|
1579
|
-
for (
|
|
1712
|
+
for (const ckey of ckeys) {
|
|
1580
1713
|
setprop(parent, ckey, clone(childtm));
|
|
1581
1714
|
// NOTE: modifying inj! This extends the child value loop in inject.
|
|
1582
1715
|
keys.push(ckey);
|
|
@@ -1592,7 +1725,7 @@ const validate_CHILD = (inj) => {
|
|
|
1592
1725
|
inj.errs.push('Invalid $CHILD as value');
|
|
1593
1726
|
return NONE;
|
|
1594
1727
|
}
|
|
1595
|
-
const childtm =
|
|
1728
|
+
const childtm = _lookup(parent, 1);
|
|
1596
1729
|
if (NONE === inj.dparent) {
|
|
1597
1730
|
// Empty list as default.
|
|
1598
1731
|
// parent.length = 0
|
|
@@ -1636,7 +1769,7 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1636
1769
|
inj.setval(inj.dparent, 2);
|
|
1637
1770
|
inj.path = slice(inj.path, -1);
|
|
1638
1771
|
inj.key = getelem(inj.path, -1);
|
|
1639
|
-
|
|
1772
|
+
const tvals = slice(parent, 1);
|
|
1640
1773
|
if (0 === size(tvals)) {
|
|
1641
1774
|
inj.errs.push('The $ONE validator at field ' +
|
|
1642
1775
|
pathify(inj.path, 1, 1) +
|
|
@@ -1644,9 +1777,9 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1644
1777
|
return;
|
|
1645
1778
|
}
|
|
1646
1779
|
// See if we can find a match.
|
|
1647
|
-
for (
|
|
1780
|
+
for (const tval of tvals) {
|
|
1648
1781
|
// If match, then errs.length = 0
|
|
1649
|
-
|
|
1782
|
+
const terrs = [];
|
|
1650
1783
|
const vstore = merge([{}, store], 1);
|
|
1651
1784
|
vstore.$TOP = inj.dparent;
|
|
1652
1785
|
const vcurrent = validate(inj.dparent, tval, {
|
|
@@ -1681,7 +1814,7 @@ const validate_EXACT = (inj) => {
|
|
|
1681
1814
|
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
1682
1815
|
inj.path = slice(inj.path, 0, -1);
|
|
1683
1816
|
inj.key = getelem(inj.path, -1);
|
|
1684
|
-
|
|
1817
|
+
const tvals = slice(parent, 1);
|
|
1685
1818
|
if (0 === size(tvals)) {
|
|
1686
1819
|
inj.errs.push('The $EXACT validator at field ' +
|
|
1687
1820
|
pathify(inj.path, 1, 1) +
|
|
@@ -1690,7 +1823,7 @@ const validate_EXACT = (inj) => {
|
|
|
1690
1823
|
}
|
|
1691
1824
|
// See if we can find an exact value match.
|
|
1692
1825
|
let currentstr = undefined;
|
|
1693
|
-
for (
|
|
1826
|
+
for (const tval of tvals) {
|
|
1694
1827
|
let exactmatch = tval === inj.dparent;
|
|
1695
1828
|
if (!exactmatch && isnode(tval)) {
|
|
1696
1829
|
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
|
|
@@ -1704,7 +1837,9 @@ const validate_EXACT = (inj) => {
|
|
|
1704
1837
|
// There was no match.
|
|
1705
1838
|
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1706
1839
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(inj.path) ? '' : 'value ') +
|
|
1707
|
-
'exactly equal to ' +
|
|
1840
|
+
'exactly equal to ' +
|
|
1841
|
+
(1 === size(tvals) ? '' : 'one of ') +
|
|
1842
|
+
valdesc, typify(inj.dparent), inj.dparent, 'V0110'));
|
|
1708
1843
|
}
|
|
1709
1844
|
else {
|
|
1710
1845
|
delprop(parent, key);
|
|
@@ -1747,8 +1882,11 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1747
1882
|
// Empty spec object {} means object can be open (any keys).
|
|
1748
1883
|
if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
|
|
1749
1884
|
const badkeys = [];
|
|
1750
|
-
for (
|
|
1751
|
-
if
|
|
1885
|
+
for (const ckey of ckeys) {
|
|
1886
|
+
// Literal presence: _validation needs to know if the SHAPE declares
|
|
1887
|
+
// this key, regardless of whether the validator stored null in that
|
|
1888
|
+
// slot. The Group A haskey would miss null-valued slots.
|
|
1889
|
+
if (NONE === _lookup(pval, ckey)) {
|
|
1752
1890
|
badkeys.push(ckey);
|
|
1753
1891
|
}
|
|
1754
1892
|
}
|
|
@@ -1774,8 +1912,7 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1774
1912
|
else if (exact) {
|
|
1775
1913
|
if (cval !== pval) {
|
|
1776
1914
|
const pathmsg = 1 < size(inj.path) ? 'at field ' + pathify(inj.path, 1) + S_VIZ : S_MT;
|
|
1777
|
-
inj.errs.push('Value ' + pathmsg + cval +
|
|
1778
|
-
' should equal ' + pval + S_DT);
|
|
1915
|
+
inj.errs.push('Value ' + pathmsg + cval + ' should equal ' + pval + S_DT);
|
|
1779
1916
|
}
|
|
1780
1917
|
}
|
|
1781
1918
|
else {
|
|
@@ -1831,9 +1968,9 @@ injdef) {
|
|
|
1831
1968
|
// NOTE: collecterrs parameter always wins.
|
|
1832
1969
|
{
|
|
1833
1970
|
$ERRS: errs,
|
|
1834
|
-
}
|
|
1971
|
+
},
|
|
1835
1972
|
], 1);
|
|
1836
|
-
|
|
1973
|
+
const meta = getprop(injdef, 'meta', {});
|
|
1837
1974
|
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false));
|
|
1838
1975
|
const out = transform(data, spec, {
|
|
1839
1976
|
meta,
|
|
@@ -1842,7 +1979,7 @@ injdef) {
|
|
|
1842
1979
|
handler: _validatehandler,
|
|
1843
1980
|
errs,
|
|
1844
1981
|
});
|
|
1845
|
-
const generr =
|
|
1982
|
+
const generr = 0 < size(errs) && !collect;
|
|
1846
1983
|
if (generr) {
|
|
1847
1984
|
throw new Error(join(errs, ' | '));
|
|
1848
1985
|
}
|
|
@@ -1850,13 +1987,13 @@ injdef) {
|
|
|
1850
1987
|
}
|
|
1851
1988
|
const select_AND = (inj, _val, _ref, store) => {
|
|
1852
1989
|
if (M_KEYPRE === inj.mode) {
|
|
1853
|
-
const terms =
|
|
1990
|
+
const terms = _lookup(inj.parent, inj.key);
|
|
1854
1991
|
const ppath = slice(inj.path, -1);
|
|
1855
1992
|
const point = getpath(store, ppath);
|
|
1856
1993
|
const vstore = merge([{}, store], 1);
|
|
1857
1994
|
vstore.$TOP = point;
|
|
1858
|
-
for (
|
|
1859
|
-
|
|
1995
|
+
for (const term of terms) {
|
|
1996
|
+
const terrs = [];
|
|
1860
1997
|
validate(point, term, {
|
|
1861
1998
|
extra: vstore,
|
|
1862
1999
|
errs: terrs,
|
|
@@ -1873,13 +2010,13 @@ const select_AND = (inj, _val, _ref, store) => {
|
|
|
1873
2010
|
};
|
|
1874
2011
|
const select_OR = (inj, _val, _ref, store) => {
|
|
1875
2012
|
if (M_KEYPRE === inj.mode) {
|
|
1876
|
-
const terms =
|
|
2013
|
+
const terms = _lookup(inj.parent, inj.key);
|
|
1877
2014
|
const ppath = slice(inj.path, -1);
|
|
1878
2015
|
const point = getpath(store, ppath);
|
|
1879
2016
|
const vstore = merge([{}, store], 1);
|
|
1880
2017
|
vstore.$TOP = point;
|
|
1881
|
-
for (
|
|
1882
|
-
|
|
2018
|
+
for (const term of terms) {
|
|
2019
|
+
const terrs = [];
|
|
1883
2020
|
validate(point, term, {
|
|
1884
2021
|
extra: vstore,
|
|
1885
2022
|
errs: terrs,
|
|
@@ -1897,12 +2034,12 @@ const select_OR = (inj, _val, _ref, store) => {
|
|
|
1897
2034
|
};
|
|
1898
2035
|
const select_NOT = (inj, _val, _ref, store) => {
|
|
1899
2036
|
if (M_KEYPRE === inj.mode) {
|
|
1900
|
-
const term =
|
|
2037
|
+
const term = _lookup(inj.parent, inj.key);
|
|
1901
2038
|
const ppath = slice(inj.path, -1);
|
|
1902
2039
|
const point = getpath(store, ppath);
|
|
1903
2040
|
const vstore = merge([{}, store], 1);
|
|
1904
2041
|
vstore.$TOP = point;
|
|
1905
|
-
|
|
2042
|
+
const terrs = [];
|
|
1906
2043
|
validate(point, term, {
|
|
1907
2044
|
extra: vstore,
|
|
1908
2045
|
errs: terrs,
|
|
@@ -1918,7 +2055,7 @@ const select_NOT = (inj, _val, _ref, store) => {
|
|
|
1918
2055
|
};
|
|
1919
2056
|
const select_CMP = (inj, _val, ref, store) => {
|
|
1920
2057
|
if (M_KEYPRE === inj.mode) {
|
|
1921
|
-
const term =
|
|
2058
|
+
const term = _lookup(inj.parent, inj.key);
|
|
1922
2059
|
// const src = getprop(store, inj.base, store)
|
|
1923
2060
|
const gkey = getelem(inj.path, -2);
|
|
1924
2061
|
// const tval = getprop(src, gkey)
|
|
@@ -1937,7 +2074,7 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
1937
2074
|
else if ('$LTE' === ref && point <= term) {
|
|
1938
2075
|
pass = true;
|
|
1939
2076
|
}
|
|
1940
|
-
else if ('$LIKE' === ref && stringify(point)
|
|
2077
|
+
else if ('$LIKE' === ref && re_test(re_compile(term), stringify(point))) {
|
|
1941
2078
|
pass = true;
|
|
1942
2079
|
}
|
|
1943
2080
|
if (pass) {
|
|
@@ -1946,8 +2083,14 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
1946
2083
|
setprop(gp, gkey, point);
|
|
1947
2084
|
}
|
|
1948
2085
|
else {
|
|
1949
|
-
inj.errs.push('CMP: ' +
|
|
1950
|
-
|
|
2086
|
+
inj.errs.push('CMP: ' +
|
|
2087
|
+
pathify(ppath) +
|
|
2088
|
+
S_VIZ +
|
|
2089
|
+
stringify(point) +
|
|
2090
|
+
' fail:' +
|
|
2091
|
+
ref +
|
|
2092
|
+
' ' +
|
|
2093
|
+
stringify(term));
|
|
1951
2094
|
}
|
|
1952
2095
|
}
|
|
1953
2096
|
return NONE;
|
|
@@ -1961,7 +2104,7 @@ function select(children, query) {
|
|
|
1961
2104
|
return [];
|
|
1962
2105
|
}
|
|
1963
2106
|
if (ismap(children)) {
|
|
1964
|
-
children = items(children, n => {
|
|
2107
|
+
children = items(children, (n) => {
|
|
1965
2108
|
setprop(n[1], S_DKEY, n[0]);
|
|
1966
2109
|
return n[1];
|
|
1967
2110
|
});
|
|
@@ -1982,7 +2125,7 @@ function select(children, query) {
|
|
|
1982
2125
|
$GTE: select_CMP,
|
|
1983
2126
|
$LTE: select_CMP,
|
|
1984
2127
|
$LIKE: select_CMP,
|
|
1985
|
-
}
|
|
2128
|
+
},
|
|
1986
2129
|
};
|
|
1987
2130
|
const q = clone(query);
|
|
1988
2131
|
walk(q, (_k, v) => {
|
|
@@ -2020,14 +2163,31 @@ class Injection {
|
|
|
2020
2163
|
this.meta = {};
|
|
2021
2164
|
}
|
|
2022
2165
|
toString(prefix) {
|
|
2023
|
-
return 'INJ' +
|
|
2166
|
+
return ('INJ' +
|
|
2167
|
+
(null == prefix ? '' : S_FS + prefix) +
|
|
2168
|
+
S_CN +
|
|
2024
2169
|
pad(pathify(this.path, 1)) +
|
|
2025
|
-
MODENAME[this.mode] +
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
'
|
|
2029
|
-
|
|
2030
|
-
|
|
2170
|
+
MODENAME[this.mode] +
|
|
2171
|
+
(this.full ? '/full' : '') +
|
|
2172
|
+
S_CN +
|
|
2173
|
+
'key=' +
|
|
2174
|
+
this.keyI +
|
|
2175
|
+
S_FS +
|
|
2176
|
+
this.key +
|
|
2177
|
+
S_FS +
|
|
2178
|
+
S_OS +
|
|
2179
|
+
this.keys +
|
|
2180
|
+
S_CS +
|
|
2181
|
+
' p=' +
|
|
2182
|
+
stringify(this.parent, -1, 1) +
|
|
2183
|
+
' m=' +
|
|
2184
|
+
stringify(this.meta, -1, 1) +
|
|
2185
|
+
' d/' +
|
|
2186
|
+
pathify(this.dpath, 1) +
|
|
2187
|
+
'=' +
|
|
2188
|
+
stringify(this.dparent, -1, 1) +
|
|
2189
|
+
' r=' +
|
|
2190
|
+
stringify(this.nodes[0]?.[S_DTOP], -1, 1));
|
|
2031
2191
|
}
|
|
2032
2192
|
descend() {
|
|
2033
2193
|
this.meta.__d++;
|
|
@@ -2044,7 +2204,7 @@ class Injection {
|
|
|
2044
2204
|
// this.dparent is the containing node of the current store value.
|
|
2045
2205
|
if (null != parentkey) {
|
|
2046
2206
|
this.dparent = getprop(this.dparent, parentkey);
|
|
2047
|
-
|
|
2207
|
+
const lastpart = getelem(this.dpath, -1);
|
|
2048
2208
|
if (lastpart === '$:' + parentkey) {
|
|
2049
2209
|
this.dpath = slice(this.dpath, -1);
|
|
2050
2210
|
}
|
|
@@ -2079,16 +2239,15 @@ class Injection {
|
|
|
2079
2239
|
setval(val, ancestor) {
|
|
2080
2240
|
let parent = NONE;
|
|
2081
2241
|
if (null == ancestor || ancestor < 2) {
|
|
2082
|
-
parent =
|
|
2083
|
-
|
|
2084
|
-
|
|
2242
|
+
parent =
|
|
2243
|
+
NONE === val
|
|
2244
|
+
? (this.parent = delprop(this.parent, this.key))
|
|
2245
|
+
: setprop(this.parent, this.key, val);
|
|
2085
2246
|
}
|
|
2086
2247
|
else {
|
|
2087
2248
|
const aval = getelem(this.nodes, 0 - ancestor);
|
|
2088
2249
|
const akey = getelem(this.path, 0 - ancestor);
|
|
2089
|
-
parent = NONE === val ?
|
|
2090
|
-
delprop(aval, akey) :
|
|
2091
|
-
setprop(aval, akey, val);
|
|
2250
|
+
parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val);
|
|
2092
2251
|
}
|
|
2093
2252
|
// console.log('SETVAL', val, this.key, this.parent)
|
|
2094
2253
|
return parent;
|
|
@@ -2103,14 +2262,16 @@ class Injection {
|
|
|
2103
2262
|
// }
|
|
2104
2263
|
// Build a type validation error message.
|
|
2105
2264
|
function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
2106
|
-
|
|
2107
|
-
return 'Expected ' +
|
|
2108
|
-
(1 < size(path) ?
|
|
2109
|
-
needtype +
|
|
2110
|
-
|
|
2265
|
+
const vs = null == v ? 'no value' : stringify(v);
|
|
2266
|
+
return ('Expected ' +
|
|
2267
|
+
(1 < size(path) ? 'field ' + pathify(path, 1) + ' to be ' : '') +
|
|
2268
|
+
needtype +
|
|
2269
|
+
', but found ' +
|
|
2270
|
+
(null != v ? typename(vt) + S_VIZ : '') +
|
|
2271
|
+
vs +
|
|
2111
2272
|
// Uncomment to help debug validation errors.
|
|
2112
2273
|
// ' [' + _whence + ']' +
|
|
2113
|
-
'.';
|
|
2274
|
+
'.');
|
|
2114
2275
|
}
|
|
2115
2276
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
2116
2277
|
// call the function passing the injection inj. This is how transforms operate.
|
|
@@ -2130,7 +2291,7 @@ const _injecthandler = (inj, val, ref, store) => {
|
|
|
2130
2291
|
};
|
|
2131
2292
|
const _validatehandler = (inj, val, ref, store) => {
|
|
2132
2293
|
let out = val;
|
|
2133
|
-
const m =
|
|
2294
|
+
const m = re_find(R_META_PATH, ref);
|
|
2134
2295
|
const ismetapath = null != m;
|
|
2135
2296
|
if (ismetapath) {
|
|
2136
2297
|
if ('=' === m[2]) {
|
|
@@ -2163,7 +2324,7 @@ function _injectstr(val, store, inj) {
|
|
|
2163
2324
|
}
|
|
2164
2325
|
let out = val;
|
|
2165
2326
|
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
2166
|
-
const m =
|
|
2327
|
+
const m = re_find(R_INJECTION_FULL, val);
|
|
2167
2328
|
// Full string of the val is an injection.
|
|
2168
2329
|
if (m) {
|
|
2169
2330
|
if (null != inj) {
|
|
@@ -2172,7 +2333,7 @@ function _injectstr(val, store, inj) {
|
|
|
2172
2333
|
let pathref = m[1];
|
|
2173
2334
|
// Special escapes inside injection.
|
|
2174
2335
|
if (3 < size(pathref)) {
|
|
2175
|
-
pathref =
|
|
2336
|
+
pathref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, pathref, S_BT), S_DS);
|
|
2176
2337
|
}
|
|
2177
2338
|
// Get the extracted path reference.
|
|
2178
2339
|
out = getpath(store, pathref, inj);
|
|
@@ -2182,7 +2343,7 @@ function _injectstr(val, store, inj) {
|
|
|
2182
2343
|
const partial = (_m, ref) => {
|
|
2183
2344
|
// Special escapes inside injection.
|
|
2184
2345
|
if (3 < size(ref)) {
|
|
2185
|
-
ref =
|
|
2346
|
+
ref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, ref, S_BT), S_DS);
|
|
2186
2347
|
}
|
|
2187
2348
|
if (inj) {
|
|
2188
2349
|
inj.full = false;
|
|
@@ -2191,7 +2352,7 @@ function _injectstr(val, store, inj) {
|
|
|
2191
2352
|
// Ensure inject value is a string.
|
|
2192
2353
|
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
2193
2354
|
};
|
|
2194
|
-
out =
|
|
2355
|
+
out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]));
|
|
2195
2356
|
// Also call the inj handler on the entire string, providing the
|
|
2196
2357
|
// option for custom injection.
|
|
2197
2358
|
if (null != inj && isfunc(inj.handler)) {
|
|
@@ -2216,15 +2377,25 @@ const PLACEMENT = {
|
|
|
2216
2377
|
};
|
|
2217
2378
|
function checkPlacement(modes, ijname, parentTypes, inj) {
|
|
2218
2379
|
if (0 === (modes & inj.mode)) {
|
|
2219
|
-
inj.errs.push('$' +
|
|
2220
|
-
|
|
2380
|
+
inj.errs.push('$' +
|
|
2381
|
+
ijname +
|
|
2382
|
+
': invalid placement as ' +
|
|
2383
|
+
PLACEMENT[inj.mode] +
|
|
2384
|
+
', expected: ' +
|
|
2385
|
+
join(items([M_KEYPRE, M_KEYPOST, M_VAL].filter((m) => modes & m), (n) => PLACEMENT[n[1]]), ',') +
|
|
2386
|
+
'.');
|
|
2221
2387
|
return false;
|
|
2222
2388
|
}
|
|
2223
2389
|
if (!isempty(parentTypes)) {
|
|
2224
2390
|
const ptype = typify(inj.parent);
|
|
2225
2391
|
if (0 === (parentTypes & ptype)) {
|
|
2226
|
-
inj.errs.push('$' +
|
|
2227
|
-
|
|
2392
|
+
inj.errs.push('$' +
|
|
2393
|
+
ijname +
|
|
2394
|
+
': invalid placement in parent ' +
|
|
2395
|
+
typename(ptype) +
|
|
2396
|
+
', expected: ' +
|
|
2397
|
+
typename(parentTypes) +
|
|
2398
|
+
'.');
|
|
2228
2399
|
return false;
|
|
2229
2400
|
}
|
|
2230
2401
|
}
|
|
@@ -2240,9 +2411,16 @@ function injectorArgs(argTypes, args) {
|
|
|
2240
2411
|
const arg = args[argI];
|
|
2241
2412
|
const argType = typify(arg);
|
|
2242
2413
|
if (0 === (argTypes[argI] & argType)) {
|
|
2243
|
-
found[0] =
|
|
2244
|
-
'
|
|
2245
|
-
|
|
2414
|
+
found[0] =
|
|
2415
|
+
'invalid argument: ' +
|
|
2416
|
+
stringify(arg, 22) +
|
|
2417
|
+
' (' +
|
|
2418
|
+
typename(argType) +
|
|
2419
|
+
' at position ' +
|
|
2420
|
+
(1 + argI) +
|
|
2421
|
+
') is not of type: ' +
|
|
2422
|
+
typename(argTypes[argI]) +
|
|
2423
|
+
'.';
|
|
2246
2424
|
break;
|
|
2247
2425
|
}
|
|
2248
2426
|
found[1 + argI] = arg;
|