@voxgig/struct 0.1.0 → 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 +9 -3
- package/dist/StructUtility.js +333 -177
- package/dist/StructUtility.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -6
- package/src/StructUtility.ts +679 -691
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.
|
|
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.
|
|
@@ -809,7 +910,7 @@ key, parent, path, pool) {
|
|
|
809
910
|
for (let i = 0; i < depth; i++) {
|
|
810
911
|
childPath[i] = path[i];
|
|
811
912
|
}
|
|
812
|
-
for (
|
|
913
|
+
for (const [ckey, child] of items(out)) {
|
|
813
914
|
childPath[depth] = S_MT + ckey;
|
|
814
915
|
setprop(out, ckey, walk(child, before, after, maxdepth, ckey, out, childPath, pool));
|
|
815
916
|
}
|
|
@@ -840,16 +941,16 @@ function merge(val, maxdepth) {
|
|
|
840
941
|
// Merge a list of values.
|
|
841
942
|
out = getprop(list, 0, {});
|
|
842
943
|
for (let oI = 1; oI < lenlist; oI++) {
|
|
843
|
-
|
|
944
|
+
const obj = list[oI];
|
|
844
945
|
if (!isnode(obj)) {
|
|
845
946
|
// Nodes win.
|
|
846
947
|
out = obj;
|
|
847
948
|
}
|
|
848
949
|
else {
|
|
849
950
|
// Current value at path end in overriding node.
|
|
850
|
-
|
|
951
|
+
const cur = [out];
|
|
851
952
|
// Current value at path end in destination node.
|
|
852
|
-
|
|
953
|
+
const dst = [out];
|
|
853
954
|
function before(key, val, _parent, path) {
|
|
854
955
|
const pI = size(path);
|
|
855
956
|
if (md <= pI) {
|
|
@@ -908,9 +1009,13 @@ function merge(val, maxdepth) {
|
|
|
908
1009
|
// String paths create only maps. Use a string list to create list parts.
|
|
909
1010
|
function setpath(store, path, val, injdef) {
|
|
910
1011
|
const pathType = typify(path);
|
|
911
|
-
const parts = 0 < (T_list & pathType)
|
|
912
|
-
|
|
913
|
-
|
|
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;
|
|
914
1019
|
if (NONE === parts) {
|
|
915
1020
|
return NONE;
|
|
916
1021
|
}
|
|
@@ -936,9 +1041,13 @@ function setpath(store, path, val, injdef) {
|
|
|
936
1041
|
}
|
|
937
1042
|
function getpath(store, path, injdef) {
|
|
938
1043
|
// Operate on a string array.
|
|
939
|
-
const parts = islist(path)
|
|
940
|
-
|
|
941
|
-
|
|
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;
|
|
942
1051
|
if (NONE === parts) {
|
|
943
1052
|
return NONE;
|
|
944
1053
|
}
|
|
@@ -959,7 +1068,7 @@ function getpath(store, path, injdef) {
|
|
|
959
1068
|
}
|
|
960
1069
|
if (!isfunc(val)) {
|
|
961
1070
|
val = src;
|
|
962
|
-
const m = parts[0]
|
|
1071
|
+
const m = re_find(R_META_PATH, parts[0]);
|
|
963
1072
|
if (m && injdef && injdef.meta) {
|
|
964
1073
|
val = getprop(injdef.meta, m[1]);
|
|
965
1074
|
parts[0] = m[3];
|
|
@@ -983,7 +1092,7 @@ function getpath(store, path, injdef) {
|
|
|
983
1092
|
part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)));
|
|
984
1093
|
}
|
|
985
1094
|
// $$ escapes $
|
|
986
|
-
part =
|
|
1095
|
+
part = re_replace(R_DOUBLE_DOLLAR, part, '$');
|
|
987
1096
|
if (S_MT === part) {
|
|
988
1097
|
let ascends = 0;
|
|
989
1098
|
while (S_MT === parts[1 + pI]) {
|
|
@@ -1063,8 +1172,8 @@ function inject(val, store, injdef) {
|
|
|
1063
1172
|
nodekeys = keysof(val);
|
|
1064
1173
|
if (ismap(val)) {
|
|
1065
1174
|
nodekeys = flatten([
|
|
1066
|
-
filter(nodekeys, (n => !n[1].includes(S_DS))
|
|
1067
|
-
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)),
|
|
1068
1177
|
]);
|
|
1069
1178
|
}
|
|
1070
1179
|
else {
|
|
@@ -1112,16 +1221,16 @@ function inject(val, store, injdef) {
|
|
|
1112
1221
|
}
|
|
1113
1222
|
// Custom modification.
|
|
1114
1223
|
if (inj.modify && SKIP !== val) {
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1224
|
+
const mkey = inj.key;
|
|
1225
|
+
const mparent = inj.parent;
|
|
1226
|
+
const mval = getprop(mparent, mkey);
|
|
1118
1227
|
inj.modify(mval, mkey, mparent, inj, store);
|
|
1119
1228
|
}
|
|
1120
1229
|
// console.log('INJ-VAL', val)
|
|
1121
1230
|
inj.val = val;
|
|
1122
1231
|
// Original val reference may no longer be correct.
|
|
1123
1232
|
// This return value is only used as the top level result.
|
|
1124
|
-
return
|
|
1233
|
+
return _lookup(inj.parent, S_DTOP);
|
|
1125
1234
|
}
|
|
1126
1235
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
1127
1236
|
// Delete a key from a map or list.
|
|
@@ -1135,7 +1244,7 @@ const transform_COPY = (inj, _val) => {
|
|
|
1135
1244
|
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
1136
1245
|
return NONE;
|
|
1137
1246
|
}
|
|
1138
|
-
|
|
1247
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1139
1248
|
inj.setval(out);
|
|
1140
1249
|
return out;
|
|
1141
1250
|
};
|
|
@@ -1148,14 +1257,14 @@ const transform_KEY = (inj) => {
|
|
|
1148
1257
|
return NONE;
|
|
1149
1258
|
}
|
|
1150
1259
|
// Key is defined by $KEY meta property.
|
|
1151
|
-
const keyspec =
|
|
1260
|
+
const keyspec = _lookup(parent, S_BKEY);
|
|
1152
1261
|
if (NONE !== keyspec) {
|
|
1153
1262
|
delprop(parent, S_BKEY);
|
|
1154
1263
|
return getprop(inj.dparent, keyspec);
|
|
1155
1264
|
}
|
|
1156
1265
|
// Key is defined within general purpose $META object.
|
|
1157
1266
|
// return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1158
|
-
return
|
|
1267
|
+
return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2);
|
|
1159
1268
|
};
|
|
1160
1269
|
// Annotate node. Does nothing itself, just used by
|
|
1161
1270
|
// other injectors, and is removed when called.
|
|
@@ -1164,9 +1273,9 @@ const transform_ANNO = (inj) => {
|
|
|
1164
1273
|
delprop(parent, S_BANNO);
|
|
1165
1274
|
return NONE;
|
|
1166
1275
|
};
|
|
1167
|
-
// Merge a list of objects into the current object.
|
|
1276
|
+
// Merge a list of objects into the current object.
|
|
1168
1277
|
// Must be a key in an object. The value is merged over the current object.
|
|
1169
|
-
// 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`.
|
|
1170
1279
|
// If the value is the empty string, merge the top level store.
|
|
1171
1280
|
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
1172
1281
|
const transform_MERGE = (inj) => {
|
|
@@ -1220,11 +1329,11 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1220
1329
|
tval = items(src, () => clone(child));
|
|
1221
1330
|
}
|
|
1222
1331
|
else if (0 < (T_map & srctype)) {
|
|
1223
|
-
tval = items(src, (n => merge([
|
|
1332
|
+
tval = items(src, (n) => merge([
|
|
1224
1333
|
clone(child),
|
|
1225
1334
|
// Make a note of the key for $KEY transforms.
|
|
1226
|
-
{ [S_BANNO]: { KEY: n[0] } }
|
|
1227
|
-
], 1))
|
|
1335
|
+
{ [S_BANNO]: { KEY: n[0] } },
|
|
1336
|
+
], 1));
|
|
1228
1337
|
}
|
|
1229
1338
|
let rval = [];
|
|
1230
1339
|
if (0 < size(tval)) {
|
|
@@ -1258,7 +1367,7 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1258
1367
|
// Convert a node to a map.
|
|
1259
1368
|
// Format: { '`$PACK`':['source-path', child-template]}
|
|
1260
1369
|
const transform_PACK = (inj, _val, _ref, store) => {
|
|
1261
|
-
const {
|
|
1370
|
+
const { key, path, parent, nodes } = inj;
|
|
1262
1371
|
const ijname = 'EACH';
|
|
1263
1372
|
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1264
1373
|
return NONE;
|
|
@@ -1297,7 +1406,7 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1297
1406
|
const childspec = delprop(origchildspec, S_BKEY);
|
|
1298
1407
|
const child = getprop(childspec, S_BVAL, childspec);
|
|
1299
1408
|
// Build parallel target object.
|
|
1300
|
-
|
|
1409
|
+
const tval = {};
|
|
1301
1410
|
items(src, (item) => {
|
|
1302
1411
|
const srckey = item[0];
|
|
1303
1412
|
const srcnode = item[1];
|
|
@@ -1323,12 +1432,13 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1323
1432
|
let rval = {};
|
|
1324
1433
|
if (!isempty(tval)) {
|
|
1325
1434
|
// Build parallel source object.
|
|
1326
|
-
|
|
1435
|
+
const tsrc = {};
|
|
1327
1436
|
src.reduce((a, n, i) => {
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1437
|
+
const kn = null == keypath
|
|
1438
|
+
? i
|
|
1439
|
+
: keypath.startsWith('`')
|
|
1440
|
+
? inject(keypath, merge([{}, store, { $TOP: n }], 1))
|
|
1441
|
+
: getpath(n, keypath, inj);
|
|
1332
1442
|
setprop(a, kn, n);
|
|
1333
1443
|
return a;
|
|
1334
1444
|
}, tsrc);
|
|
@@ -1365,7 +1475,7 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1365
1475
|
return NONE;
|
|
1366
1476
|
}
|
|
1367
1477
|
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1368
|
-
const refpath =
|
|
1478
|
+
const refpath = _lookup(inj.parent, 1);
|
|
1369
1479
|
inj.keyI = size(inj.keys);
|
|
1370
1480
|
// Spec reference.
|
|
1371
1481
|
const spec = getprop(store, S_DSPEC)();
|
|
@@ -1386,11 +1496,11 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1386
1496
|
return v;
|
|
1387
1497
|
});
|
|
1388
1498
|
}
|
|
1389
|
-
|
|
1499
|
+
const tref = clone(ref);
|
|
1390
1500
|
const cpath = slice(inj.path, -3);
|
|
1391
1501
|
const tpath = slice(inj.path, -1);
|
|
1392
|
-
|
|
1393
|
-
|
|
1502
|
+
const tcur = getpath(store, cpath);
|
|
1503
|
+
const tval = getpath(store, tpath);
|
|
1394
1504
|
let rval = NONE;
|
|
1395
1505
|
if (!hasSubRef || NONE !== tval) {
|
|
1396
1506
|
const tinj = inj.child(0, [getelem(tpath, -1)]);
|
|
@@ -1421,28 +1531,28 @@ const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
|
1421
1531
|
}
|
|
1422
1532
|
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1423
1533
|
// TODO: EACH and PACK should accept customm functions too
|
|
1424
|
-
const name =
|
|
1425
|
-
const child =
|
|
1534
|
+
const name = _lookup(inj.parent, 1);
|
|
1535
|
+
const child = _lookup(inj.parent, 2);
|
|
1426
1536
|
// Source data.
|
|
1427
1537
|
const tkey = getelem(inj.path, -2);
|
|
1428
1538
|
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1));
|
|
1429
1539
|
const cinj = injectChild(child, store, inj);
|
|
1430
1540
|
const resolved = cinj.val;
|
|
1431
|
-
|
|
1541
|
+
const formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name);
|
|
1432
1542
|
if (NONE === formatter) {
|
|
1433
1543
|
inj.errs.push('$FORMAT: unknown format: ' + name + '.');
|
|
1434
1544
|
return NONE;
|
|
1435
1545
|
}
|
|
1436
|
-
|
|
1546
|
+
const out = walk(resolved, formatter);
|
|
1437
1547
|
setprop(target, tkey, out);
|
|
1438
1548
|
// _updateAncestors(inj, target, tkey, out)
|
|
1439
1549
|
return out;
|
|
1440
1550
|
};
|
|
1441
1551
|
const FORMATTER = {
|
|
1442
1552
|
identity: (_k, v) => v,
|
|
1443
|
-
upper: (_k, v) => isnode(v) ? v : ('' + v).toUpperCase(),
|
|
1444
|
-
lower: (_k, v) => isnode(v) ? v : ('' + v).toLowerCase(),
|
|
1445
|
-
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),
|
|
1446
1556
|
number: (_k, v) => {
|
|
1447
1557
|
if (isnode(v)) {
|
|
1448
1558
|
return v;
|
|
@@ -1467,7 +1577,9 @@ const FORMATTER = {
|
|
|
1467
1577
|
return n | 0;
|
|
1468
1578
|
}
|
|
1469
1579
|
},
|
|
1470
|
-
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,
|
|
1471
1583
|
};
|
|
1472
1584
|
const transform_APPLY = (inj, _val, _ref, store) => {
|
|
1473
1585
|
const ijname = 'APPLY';
|
|
@@ -1501,12 +1613,11 @@ injdef) {
|
|
|
1501
1613
|
const collect = null != injdef?.errs;
|
|
1502
1614
|
const errs = injdef?.errs || [];
|
|
1503
1615
|
const extraTransforms = {};
|
|
1504
|
-
const extraData = null == extra
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
]);
|
|
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)]);
|
|
1510
1621
|
// Define a top level store that provides transform operations.
|
|
1511
1622
|
const store = merge([
|
|
1512
1623
|
{
|
|
@@ -1536,10 +1647,10 @@ injdef) {
|
|
|
1536
1647
|
extraTransforms,
|
|
1537
1648
|
{
|
|
1538
1649
|
$ERRS: errs,
|
|
1539
|
-
}
|
|
1650
|
+
},
|
|
1540
1651
|
], 1);
|
|
1541
1652
|
const out = inject(spec, store, injdef);
|
|
1542
|
-
const generr =
|
|
1653
|
+
const generr = 0 < size(errs) && !collect;
|
|
1543
1654
|
if (generr) {
|
|
1544
1655
|
throw new Error(join(errs, ' | '));
|
|
1545
1656
|
}
|
|
@@ -1547,15 +1658,15 @@ injdef) {
|
|
|
1547
1658
|
}
|
|
1548
1659
|
// A required string value. NOTE: Rejects empty strings.
|
|
1549
1660
|
const validate_STRING = (inj) => {
|
|
1550
|
-
|
|
1661
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1551
1662
|
const t = typify(out);
|
|
1552
1663
|
if (0 === (T_string & t)) {
|
|
1553
|
-
|
|
1664
|
+
const msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010');
|
|
1554
1665
|
inj.errs.push(msg);
|
|
1555
1666
|
return NONE;
|
|
1556
1667
|
}
|
|
1557
1668
|
if (S_MT === out) {
|
|
1558
|
-
|
|
1669
|
+
const msg = 'Empty string at ' + pathify(inj.path, 1);
|
|
1559
1670
|
inj.errs.push(msg);
|
|
1560
1671
|
return NONE;
|
|
1561
1672
|
}
|
|
@@ -1564,7 +1675,7 @@ const validate_STRING = (inj) => {
|
|
|
1564
1675
|
const validate_TYPE = (inj, _val, ref) => {
|
|
1565
1676
|
const tname = slice(ref, 1).toLowerCase();
|
|
1566
1677
|
const typev = 1 << (31 - TYPENAME.indexOf(tname));
|
|
1567
|
-
|
|
1678
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1568
1679
|
const t = typify(out);
|
|
1569
1680
|
// console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
|
|
1570
1681
|
if (0 === (t & typev)) {
|
|
@@ -1575,7 +1686,7 @@ const validate_TYPE = (inj, _val, ref) => {
|
|
|
1575
1686
|
};
|
|
1576
1687
|
// Allow any value.
|
|
1577
1688
|
const validate_ANY = (inj) => {
|
|
1578
|
-
|
|
1689
|
+
const out = _lookup(inj.dparent, inj.key);
|
|
1579
1690
|
return out;
|
|
1580
1691
|
};
|
|
1581
1692
|
// Specify child values for map or list.
|
|
@@ -1598,7 +1709,7 @@ const validate_CHILD = (inj) => {
|
|
|
1598
1709
|
return NONE;
|
|
1599
1710
|
}
|
|
1600
1711
|
const ckeys = keysof(tval);
|
|
1601
|
-
for (
|
|
1712
|
+
for (const ckey of ckeys) {
|
|
1602
1713
|
setprop(parent, ckey, clone(childtm));
|
|
1603
1714
|
// NOTE: modifying inj! This extends the child value loop in inject.
|
|
1604
1715
|
keys.push(ckey);
|
|
@@ -1614,7 +1725,7 @@ const validate_CHILD = (inj) => {
|
|
|
1614
1725
|
inj.errs.push('Invalid $CHILD as value');
|
|
1615
1726
|
return NONE;
|
|
1616
1727
|
}
|
|
1617
|
-
const childtm =
|
|
1728
|
+
const childtm = _lookup(parent, 1);
|
|
1618
1729
|
if (NONE === inj.dparent) {
|
|
1619
1730
|
// Empty list as default.
|
|
1620
1731
|
// parent.length = 0
|
|
@@ -1658,7 +1769,7 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1658
1769
|
inj.setval(inj.dparent, 2);
|
|
1659
1770
|
inj.path = slice(inj.path, -1);
|
|
1660
1771
|
inj.key = getelem(inj.path, -1);
|
|
1661
|
-
|
|
1772
|
+
const tvals = slice(parent, 1);
|
|
1662
1773
|
if (0 === size(tvals)) {
|
|
1663
1774
|
inj.errs.push('The $ONE validator at field ' +
|
|
1664
1775
|
pathify(inj.path, 1, 1) +
|
|
@@ -1666,9 +1777,9 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1666
1777
|
return;
|
|
1667
1778
|
}
|
|
1668
1779
|
// See if we can find a match.
|
|
1669
|
-
for (
|
|
1780
|
+
for (const tval of tvals) {
|
|
1670
1781
|
// If match, then errs.length = 0
|
|
1671
|
-
|
|
1782
|
+
const terrs = [];
|
|
1672
1783
|
const vstore = merge([{}, store], 1);
|
|
1673
1784
|
vstore.$TOP = inj.dparent;
|
|
1674
1785
|
const vcurrent = validate(inj.dparent, tval, {
|
|
@@ -1703,7 +1814,7 @@ const validate_EXACT = (inj) => {
|
|
|
1703
1814
|
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
1704
1815
|
inj.path = slice(inj.path, 0, -1);
|
|
1705
1816
|
inj.key = getelem(inj.path, -1);
|
|
1706
|
-
|
|
1817
|
+
const tvals = slice(parent, 1);
|
|
1707
1818
|
if (0 === size(tvals)) {
|
|
1708
1819
|
inj.errs.push('The $EXACT validator at field ' +
|
|
1709
1820
|
pathify(inj.path, 1, 1) +
|
|
@@ -1712,7 +1823,7 @@ const validate_EXACT = (inj) => {
|
|
|
1712
1823
|
}
|
|
1713
1824
|
// See if we can find an exact value match.
|
|
1714
1825
|
let currentstr = undefined;
|
|
1715
|
-
for (
|
|
1826
|
+
for (const tval of tvals) {
|
|
1716
1827
|
let exactmatch = tval === inj.dparent;
|
|
1717
1828
|
if (!exactmatch && isnode(tval)) {
|
|
1718
1829
|
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
|
|
@@ -1726,7 +1837,9 @@ const validate_EXACT = (inj) => {
|
|
|
1726
1837
|
// There was no match.
|
|
1727
1838
|
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1728
1839
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(inj.path) ? '' : 'value ') +
|
|
1729
|
-
'exactly equal to ' +
|
|
1840
|
+
'exactly equal to ' +
|
|
1841
|
+
(1 === size(tvals) ? '' : 'one of ') +
|
|
1842
|
+
valdesc, typify(inj.dparent), inj.dparent, 'V0110'));
|
|
1730
1843
|
}
|
|
1731
1844
|
else {
|
|
1732
1845
|
delprop(parent, key);
|
|
@@ -1769,8 +1882,11 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1769
1882
|
// Empty spec object {} means object can be open (any keys).
|
|
1770
1883
|
if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
|
|
1771
1884
|
const badkeys = [];
|
|
1772
|
-
for (
|
|
1773
|
-
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)) {
|
|
1774
1890
|
badkeys.push(ckey);
|
|
1775
1891
|
}
|
|
1776
1892
|
}
|
|
@@ -1796,8 +1912,7 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1796
1912
|
else if (exact) {
|
|
1797
1913
|
if (cval !== pval) {
|
|
1798
1914
|
const pathmsg = 1 < size(inj.path) ? 'at field ' + pathify(inj.path, 1) + S_VIZ : S_MT;
|
|
1799
|
-
inj.errs.push('Value ' + pathmsg + cval +
|
|
1800
|
-
' should equal ' + pval + S_DT);
|
|
1915
|
+
inj.errs.push('Value ' + pathmsg + cval + ' should equal ' + pval + S_DT);
|
|
1801
1916
|
}
|
|
1802
1917
|
}
|
|
1803
1918
|
else {
|
|
@@ -1853,9 +1968,9 @@ injdef) {
|
|
|
1853
1968
|
// NOTE: collecterrs parameter always wins.
|
|
1854
1969
|
{
|
|
1855
1970
|
$ERRS: errs,
|
|
1856
|
-
}
|
|
1971
|
+
},
|
|
1857
1972
|
], 1);
|
|
1858
|
-
|
|
1973
|
+
const meta = getprop(injdef, 'meta', {});
|
|
1859
1974
|
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false));
|
|
1860
1975
|
const out = transform(data, spec, {
|
|
1861
1976
|
meta,
|
|
@@ -1864,7 +1979,7 @@ injdef) {
|
|
|
1864
1979
|
handler: _validatehandler,
|
|
1865
1980
|
errs,
|
|
1866
1981
|
});
|
|
1867
|
-
const generr =
|
|
1982
|
+
const generr = 0 < size(errs) && !collect;
|
|
1868
1983
|
if (generr) {
|
|
1869
1984
|
throw new Error(join(errs, ' | '));
|
|
1870
1985
|
}
|
|
@@ -1872,13 +1987,13 @@ injdef) {
|
|
|
1872
1987
|
}
|
|
1873
1988
|
const select_AND = (inj, _val, _ref, store) => {
|
|
1874
1989
|
if (M_KEYPRE === inj.mode) {
|
|
1875
|
-
const terms =
|
|
1990
|
+
const terms = _lookup(inj.parent, inj.key);
|
|
1876
1991
|
const ppath = slice(inj.path, -1);
|
|
1877
1992
|
const point = getpath(store, ppath);
|
|
1878
1993
|
const vstore = merge([{}, store], 1);
|
|
1879
1994
|
vstore.$TOP = point;
|
|
1880
|
-
for (
|
|
1881
|
-
|
|
1995
|
+
for (const term of terms) {
|
|
1996
|
+
const terrs = [];
|
|
1882
1997
|
validate(point, term, {
|
|
1883
1998
|
extra: vstore,
|
|
1884
1999
|
errs: terrs,
|
|
@@ -1895,13 +2010,13 @@ const select_AND = (inj, _val, _ref, store) => {
|
|
|
1895
2010
|
};
|
|
1896
2011
|
const select_OR = (inj, _val, _ref, store) => {
|
|
1897
2012
|
if (M_KEYPRE === inj.mode) {
|
|
1898
|
-
const terms =
|
|
2013
|
+
const terms = _lookup(inj.parent, inj.key);
|
|
1899
2014
|
const ppath = slice(inj.path, -1);
|
|
1900
2015
|
const point = getpath(store, ppath);
|
|
1901
2016
|
const vstore = merge([{}, store], 1);
|
|
1902
2017
|
vstore.$TOP = point;
|
|
1903
|
-
for (
|
|
1904
|
-
|
|
2018
|
+
for (const term of terms) {
|
|
2019
|
+
const terrs = [];
|
|
1905
2020
|
validate(point, term, {
|
|
1906
2021
|
extra: vstore,
|
|
1907
2022
|
errs: terrs,
|
|
@@ -1919,12 +2034,12 @@ const select_OR = (inj, _val, _ref, store) => {
|
|
|
1919
2034
|
};
|
|
1920
2035
|
const select_NOT = (inj, _val, _ref, store) => {
|
|
1921
2036
|
if (M_KEYPRE === inj.mode) {
|
|
1922
|
-
const term =
|
|
2037
|
+
const term = _lookup(inj.parent, inj.key);
|
|
1923
2038
|
const ppath = slice(inj.path, -1);
|
|
1924
2039
|
const point = getpath(store, ppath);
|
|
1925
2040
|
const vstore = merge([{}, store], 1);
|
|
1926
2041
|
vstore.$TOP = point;
|
|
1927
|
-
|
|
2042
|
+
const terrs = [];
|
|
1928
2043
|
validate(point, term, {
|
|
1929
2044
|
extra: vstore,
|
|
1930
2045
|
errs: terrs,
|
|
@@ -1940,7 +2055,7 @@ const select_NOT = (inj, _val, _ref, store) => {
|
|
|
1940
2055
|
};
|
|
1941
2056
|
const select_CMP = (inj, _val, ref, store) => {
|
|
1942
2057
|
if (M_KEYPRE === inj.mode) {
|
|
1943
|
-
const term =
|
|
2058
|
+
const term = _lookup(inj.parent, inj.key);
|
|
1944
2059
|
// const src = getprop(store, inj.base, store)
|
|
1945
2060
|
const gkey = getelem(inj.path, -2);
|
|
1946
2061
|
// const tval = getprop(src, gkey)
|
|
@@ -1959,7 +2074,7 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
1959
2074
|
else if ('$LTE' === ref && point <= term) {
|
|
1960
2075
|
pass = true;
|
|
1961
2076
|
}
|
|
1962
|
-
else if ('$LIKE' === ref && stringify(point)
|
|
2077
|
+
else if ('$LIKE' === ref && re_test(re_compile(term), stringify(point))) {
|
|
1963
2078
|
pass = true;
|
|
1964
2079
|
}
|
|
1965
2080
|
if (pass) {
|
|
@@ -1968,8 +2083,14 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
1968
2083
|
setprop(gp, gkey, point);
|
|
1969
2084
|
}
|
|
1970
2085
|
else {
|
|
1971
|
-
inj.errs.push('CMP: ' +
|
|
1972
|
-
|
|
2086
|
+
inj.errs.push('CMP: ' +
|
|
2087
|
+
pathify(ppath) +
|
|
2088
|
+
S_VIZ +
|
|
2089
|
+
stringify(point) +
|
|
2090
|
+
' fail:' +
|
|
2091
|
+
ref +
|
|
2092
|
+
' ' +
|
|
2093
|
+
stringify(term));
|
|
1973
2094
|
}
|
|
1974
2095
|
}
|
|
1975
2096
|
return NONE;
|
|
@@ -1983,7 +2104,7 @@ function select(children, query) {
|
|
|
1983
2104
|
return [];
|
|
1984
2105
|
}
|
|
1985
2106
|
if (ismap(children)) {
|
|
1986
|
-
children = items(children, n => {
|
|
2107
|
+
children = items(children, (n) => {
|
|
1987
2108
|
setprop(n[1], S_DKEY, n[0]);
|
|
1988
2109
|
return n[1];
|
|
1989
2110
|
});
|
|
@@ -2004,7 +2125,7 @@ function select(children, query) {
|
|
|
2004
2125
|
$GTE: select_CMP,
|
|
2005
2126
|
$LTE: select_CMP,
|
|
2006
2127
|
$LIKE: select_CMP,
|
|
2007
|
-
}
|
|
2128
|
+
},
|
|
2008
2129
|
};
|
|
2009
2130
|
const q = clone(query);
|
|
2010
2131
|
walk(q, (_k, v) => {
|
|
@@ -2042,14 +2163,31 @@ class Injection {
|
|
|
2042
2163
|
this.meta = {};
|
|
2043
2164
|
}
|
|
2044
2165
|
toString(prefix) {
|
|
2045
|
-
return 'INJ' +
|
|
2166
|
+
return ('INJ' +
|
|
2167
|
+
(null == prefix ? '' : S_FS + prefix) +
|
|
2168
|
+
S_CN +
|
|
2046
2169
|
pad(pathify(this.path, 1)) +
|
|
2047
|
-
MODENAME[this.mode] +
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
'
|
|
2051
|
-
|
|
2052
|
-
|
|
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));
|
|
2053
2191
|
}
|
|
2054
2192
|
descend() {
|
|
2055
2193
|
this.meta.__d++;
|
|
@@ -2066,7 +2204,7 @@ class Injection {
|
|
|
2066
2204
|
// this.dparent is the containing node of the current store value.
|
|
2067
2205
|
if (null != parentkey) {
|
|
2068
2206
|
this.dparent = getprop(this.dparent, parentkey);
|
|
2069
|
-
|
|
2207
|
+
const lastpart = getelem(this.dpath, -1);
|
|
2070
2208
|
if (lastpart === '$:' + parentkey) {
|
|
2071
2209
|
this.dpath = slice(this.dpath, -1);
|
|
2072
2210
|
}
|
|
@@ -2101,16 +2239,15 @@ class Injection {
|
|
|
2101
2239
|
setval(val, ancestor) {
|
|
2102
2240
|
let parent = NONE;
|
|
2103
2241
|
if (null == ancestor || ancestor < 2) {
|
|
2104
|
-
parent =
|
|
2105
|
-
|
|
2106
|
-
|
|
2242
|
+
parent =
|
|
2243
|
+
NONE === val
|
|
2244
|
+
? (this.parent = delprop(this.parent, this.key))
|
|
2245
|
+
: setprop(this.parent, this.key, val);
|
|
2107
2246
|
}
|
|
2108
2247
|
else {
|
|
2109
2248
|
const aval = getelem(this.nodes, 0 - ancestor);
|
|
2110
2249
|
const akey = getelem(this.path, 0 - ancestor);
|
|
2111
|
-
parent = NONE === val ?
|
|
2112
|
-
delprop(aval, akey) :
|
|
2113
|
-
setprop(aval, akey, val);
|
|
2250
|
+
parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val);
|
|
2114
2251
|
}
|
|
2115
2252
|
// console.log('SETVAL', val, this.key, this.parent)
|
|
2116
2253
|
return parent;
|
|
@@ -2125,14 +2262,16 @@ class Injection {
|
|
|
2125
2262
|
// }
|
|
2126
2263
|
// Build a type validation error message.
|
|
2127
2264
|
function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
2128
|
-
|
|
2129
|
-
return 'Expected ' +
|
|
2130
|
-
(1 < size(path) ?
|
|
2131
|
-
needtype +
|
|
2132
|
-
|
|
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 +
|
|
2133
2272
|
// Uncomment to help debug validation errors.
|
|
2134
2273
|
// ' [' + _whence + ']' +
|
|
2135
|
-
'.';
|
|
2274
|
+
'.');
|
|
2136
2275
|
}
|
|
2137
2276
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
2138
2277
|
// call the function passing the injection inj. This is how transforms operate.
|
|
@@ -2152,7 +2291,7 @@ const _injecthandler = (inj, val, ref, store) => {
|
|
|
2152
2291
|
};
|
|
2153
2292
|
const _validatehandler = (inj, val, ref, store) => {
|
|
2154
2293
|
let out = val;
|
|
2155
|
-
const m =
|
|
2294
|
+
const m = re_find(R_META_PATH, ref);
|
|
2156
2295
|
const ismetapath = null != m;
|
|
2157
2296
|
if (ismetapath) {
|
|
2158
2297
|
if ('=' === m[2]) {
|
|
@@ -2185,7 +2324,7 @@ function _injectstr(val, store, inj) {
|
|
|
2185
2324
|
}
|
|
2186
2325
|
let out = val;
|
|
2187
2326
|
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
2188
|
-
const m =
|
|
2327
|
+
const m = re_find(R_INJECTION_FULL, val);
|
|
2189
2328
|
// Full string of the val is an injection.
|
|
2190
2329
|
if (m) {
|
|
2191
2330
|
if (null != inj) {
|
|
@@ -2194,7 +2333,7 @@ function _injectstr(val, store, inj) {
|
|
|
2194
2333
|
let pathref = m[1];
|
|
2195
2334
|
// Special escapes inside injection.
|
|
2196
2335
|
if (3 < size(pathref)) {
|
|
2197
|
-
pathref =
|
|
2336
|
+
pathref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, pathref, S_BT), S_DS);
|
|
2198
2337
|
}
|
|
2199
2338
|
// Get the extracted path reference.
|
|
2200
2339
|
out = getpath(store, pathref, inj);
|
|
@@ -2204,7 +2343,7 @@ function _injectstr(val, store, inj) {
|
|
|
2204
2343
|
const partial = (_m, ref) => {
|
|
2205
2344
|
// Special escapes inside injection.
|
|
2206
2345
|
if (3 < size(ref)) {
|
|
2207
|
-
ref =
|
|
2346
|
+
ref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, ref, S_BT), S_DS);
|
|
2208
2347
|
}
|
|
2209
2348
|
if (inj) {
|
|
2210
2349
|
inj.full = false;
|
|
@@ -2213,7 +2352,7 @@ function _injectstr(val, store, inj) {
|
|
|
2213
2352
|
// Ensure inject value is a string.
|
|
2214
2353
|
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
2215
2354
|
};
|
|
2216
|
-
out =
|
|
2355
|
+
out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]));
|
|
2217
2356
|
// Also call the inj handler on the entire string, providing the
|
|
2218
2357
|
// option for custom injection.
|
|
2219
2358
|
if (null != inj && isfunc(inj.handler)) {
|
|
@@ -2238,15 +2377,25 @@ const PLACEMENT = {
|
|
|
2238
2377
|
};
|
|
2239
2378
|
function checkPlacement(modes, ijname, parentTypes, inj) {
|
|
2240
2379
|
if (0 === (modes & inj.mode)) {
|
|
2241
|
-
inj.errs.push('$' +
|
|
2242
|
-
|
|
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
|
+
'.');
|
|
2243
2387
|
return false;
|
|
2244
2388
|
}
|
|
2245
2389
|
if (!isempty(parentTypes)) {
|
|
2246
2390
|
const ptype = typify(inj.parent);
|
|
2247
2391
|
if (0 === (parentTypes & ptype)) {
|
|
2248
|
-
inj.errs.push('$' +
|
|
2249
|
-
|
|
2392
|
+
inj.errs.push('$' +
|
|
2393
|
+
ijname +
|
|
2394
|
+
': invalid placement in parent ' +
|
|
2395
|
+
typename(ptype) +
|
|
2396
|
+
', expected: ' +
|
|
2397
|
+
typename(parentTypes) +
|
|
2398
|
+
'.');
|
|
2250
2399
|
return false;
|
|
2251
2400
|
}
|
|
2252
2401
|
}
|
|
@@ -2262,9 +2411,16 @@ function injectorArgs(argTypes, args) {
|
|
|
2262
2411
|
const arg = args[argI];
|
|
2263
2412
|
const argType = typify(arg);
|
|
2264
2413
|
if (0 === (argTypes[argI] & argType)) {
|
|
2265
|
-
found[0] =
|
|
2266
|
-
'
|
|
2267
|
-
|
|
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
|
+
'.';
|
|
2268
2424
|
break;
|
|
2269
2425
|
}
|
|
2270
2426
|
found[1 + argI] = arg;
|