@voxgig/struct 0.3.6 → 0.4.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 +19 -1
- package/dist/StructUtility.d.ts +2 -8
- package/dist/StructUtility.js +494 -497
- package/dist/StructUtility.js.map +1 -1
- package/package.json +2 -2
- package/src/StructUtility.ts +617 -584
package/dist/StructUtility.js
CHANGED
|
@@ -54,57 +54,6 @@ exports.jt = jt;
|
|
|
54
54
|
exports.checkPlacement = checkPlacement;
|
|
55
55
|
exports.injectorArgs = injectorArgs;
|
|
56
56
|
exports.injectChild = injectChild;
|
|
57
|
-
// VERSION: @voxgig/struct 0.3.6
|
|
58
|
-
/* Voxgig Struct
|
|
59
|
-
* =============
|
|
60
|
-
*
|
|
61
|
-
* Utility functions to manipulate in-memory JSON-like data
|
|
62
|
-
* structures. These structures assumed to be composed of nested
|
|
63
|
-
* "nodes", where a node is a list or map, and has named or indexed
|
|
64
|
-
* fields. The general design principle is "by-example". Transform
|
|
65
|
-
* specifications mirror the desired output. This implementation is
|
|
66
|
-
* designed for porting to multiple language, and to be tolerant of
|
|
67
|
-
* undefined values.
|
|
68
|
-
*
|
|
69
|
-
* Main utilities
|
|
70
|
-
* - getpath: get the value at a key path deep inside an object.
|
|
71
|
-
* - merge: merge multiple nodes, overriding values in earlier nodes.
|
|
72
|
-
* - walk: walk a node tree, applying a function at each node and leaf.
|
|
73
|
-
* - inject: inject values from a data store into a new data structure.
|
|
74
|
-
* - transform: transform a data structure to an example structure.
|
|
75
|
-
* - validate: valiate a data structure against a shape specification.
|
|
76
|
-
*
|
|
77
|
-
* Minor utilities
|
|
78
|
-
* - isnode, islist, ismap, iskey, isfunc: identify value kinds.
|
|
79
|
-
* - isempty: undefined values, or empty nodes.
|
|
80
|
-
* - keysof: sorted list of node keys (ascending).
|
|
81
|
-
* - haskey: true if key value is defined.
|
|
82
|
-
* - clone: create a copy of a JSON-like data structure.
|
|
83
|
-
* - items: list entries of a map or list as [key, value] pairs.
|
|
84
|
-
* - getprop: safely get a property value by key.
|
|
85
|
-
* - setprop: safely set a property value by key.
|
|
86
|
-
* - stringify: human-friendly string version of a value.
|
|
87
|
-
* - escre: escape a regular expresion string.
|
|
88
|
-
* - escurl: escape a url.
|
|
89
|
-
* - join: join parts of a url, merging forward slashes.
|
|
90
|
-
*
|
|
91
|
-
* This set of functions and supporting utilities is designed to work
|
|
92
|
-
* uniformly across many languages, meaning that some code that may be
|
|
93
|
-
* functionally redundant in specific languages is still retained to
|
|
94
|
-
* keep the code human comparable.
|
|
95
|
-
*
|
|
96
|
-
* NOTE: Lists are assumed to be mutable and reference stable.
|
|
97
|
-
*
|
|
98
|
-
* NOTE: In this code JSON nulls are in general *not* considered the
|
|
99
|
-
* same as the undefined value in the given language. However most
|
|
100
|
-
* JSON parsers do use the undefined value to represent JSON
|
|
101
|
-
* null. This is ambiguous as JSON null is a separate value, not an
|
|
102
|
-
* undefined value. You should convert such values to a special value
|
|
103
|
-
* to represent JSON null, if this ambiguity creates issues
|
|
104
|
-
* (thankfully in most APIs, JSON nulls are not used). For example,
|
|
105
|
-
* the unit tests use the string "__NULL__" where necessary.
|
|
106
|
-
*
|
|
107
|
-
*/
|
|
108
57
|
// String constants are explicitly defined.
|
|
109
58
|
// Mode value for inject step (bitfield).
|
|
110
59
|
const M_KEYPRE = 1;
|
|
@@ -113,7 +62,6 @@ const M_KEYPOST = 2;
|
|
|
113
62
|
exports.M_KEYPOST = M_KEYPOST;
|
|
114
63
|
const M_VAL = 4;
|
|
115
64
|
exports.M_VAL = M_VAL;
|
|
116
|
-
// Special strings.
|
|
117
65
|
const S_BKEY = '`$KEY`';
|
|
118
66
|
const S_BANNO = '`$ANNO`';
|
|
119
67
|
const S_BEXACT = '`$EXACT`';
|
|
@@ -122,7 +70,6 @@ const S_DKEY = '$KEY';
|
|
|
122
70
|
const S_DTOP = '$TOP';
|
|
123
71
|
const S_DERRS = '$ERRS';
|
|
124
72
|
const S_DSPEC = '$SPEC';
|
|
125
|
-
// General strings.
|
|
126
73
|
const S_list = 'list';
|
|
127
74
|
const S_base = 'base';
|
|
128
75
|
const S_boolean = 'boolean';
|
|
@@ -141,7 +88,6 @@ const S_integer = 'integer';
|
|
|
141
88
|
const S_map = 'map';
|
|
142
89
|
const S_scalar = 'scalar';
|
|
143
90
|
const S_node = 'node';
|
|
144
|
-
// Character strings.
|
|
145
91
|
const S_BT = '`';
|
|
146
92
|
const S_CN = ':';
|
|
147
93
|
const S_CS = ']';
|
|
@@ -154,7 +100,6 @@ const S_OS = '[';
|
|
|
154
100
|
const S_SP = ' ';
|
|
155
101
|
const S_CM = ',';
|
|
156
102
|
const S_VIZ = ': ';
|
|
157
|
-
// Types
|
|
158
103
|
let t = 31;
|
|
159
104
|
const T_any = (1 << t--) - 1;
|
|
160
105
|
exports.T_any = T_any;
|
|
@@ -174,7 +119,7 @@ const T_function = 1 << t--;
|
|
|
174
119
|
exports.T_function = T_function;
|
|
175
120
|
const T_symbol = 1 << t--;
|
|
176
121
|
exports.T_symbol = T_symbol;
|
|
177
|
-
const T_null = 1 << t--;
|
|
122
|
+
const T_null = 1 << t--;
|
|
178
123
|
exports.T_null = T_null;
|
|
179
124
|
t -= 7;
|
|
180
125
|
const T_list = 1 << t--;
|
|
@@ -218,25 +163,23 @@ const TYPENAME = [
|
|
|
218
163
|
];
|
|
219
164
|
// The standard undefined value for this language.
|
|
220
165
|
const NONE = undefined;
|
|
221
|
-
// Private markers
|
|
222
166
|
const SKIP = { '`$SKIP`': true };
|
|
223
167
|
exports.SKIP = SKIP;
|
|
224
168
|
const DELETE = { '`$DELETE`': true };
|
|
225
169
|
exports.DELETE = DELETE;
|
|
226
|
-
|
|
227
|
-
const R_INTEGER_KEY = /^[-0-9]+$/; // Match integer keys (including <0).
|
|
170
|
+
const R_INTEGER_KEY = /^(0|-?[1-9][0-9]*)$/;
|
|
228
171
|
const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g; // Chars that need escaping in regexp.
|
|
229
|
-
const R_QUOTES = /"/g;
|
|
230
|
-
const R_DOT = /\./g;
|
|
231
|
-
const R_CLONE_REF = /^`\$REF:([0-9]+)`$/;
|
|
232
|
-
const R_META_PATH = /^([^$]+)\$([=~])(.+)$/;
|
|
233
|
-
const R_DOUBLE_DOLLAR = /\$\$/g;
|
|
234
|
-
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g;
|
|
235
|
-
const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/;
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
const
|
|
239
|
-
|
|
172
|
+
const R_QUOTES = /"/g;
|
|
173
|
+
const R_DOT = /\./g;
|
|
174
|
+
const R_CLONE_REF = /^`\$REF:([0-9]+)`$/;
|
|
175
|
+
const R_META_PATH = /^([^$]+)\$([=~])(.+)$/;
|
|
176
|
+
const R_DOUBLE_DOLLAR = /\$\$/g;
|
|
177
|
+
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g;
|
|
178
|
+
const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/;
|
|
179
|
+
const R_CHECKER_NAME = /^\$[A-Z]+$/;
|
|
180
|
+
const R_BT_ESCAPE = /\$BT/g;
|
|
181
|
+
const R_DS_ESCAPE = /\$DS/g;
|
|
182
|
+
const R_INJECTION_PARTIAL = /`([^`]+)`/g;
|
|
240
183
|
const MAXDEPTH = 32;
|
|
241
184
|
// Return type string for narrowest type.
|
|
242
185
|
function typename(t) {
|
|
@@ -275,7 +218,6 @@ function isempty(val) {
|
|
|
275
218
|
(Array.isArray(val) && 0 === val.length) ||
|
|
276
219
|
(S_object === typeof val && 0 === Object.keys(val).length));
|
|
277
220
|
}
|
|
278
|
-
// Value is a function.
|
|
279
221
|
function isfunc(val) {
|
|
280
222
|
return S_function === typeof val;
|
|
281
223
|
}
|
|
@@ -302,13 +244,6 @@ function size(val) {
|
|
|
302
244
|
return 0;
|
|
303
245
|
}
|
|
304
246
|
}
|
|
305
|
-
// Extract part of an array or string into a new value, from the start
|
|
306
|
-
// point to the end point. If no end is specified, extract to the
|
|
307
|
-
// full length of the value. Negative arguments count from the end of
|
|
308
|
-
// the value. For numbers, perform min and max bounding, where start
|
|
309
|
-
// is inclusive, and end is *exclusive*.
|
|
310
|
-
// NOTE: input lists are not mutated by default. Use the mutate
|
|
311
|
-
// argument to mutate lists in place.
|
|
312
247
|
function slice(val, start, end, mutate) {
|
|
313
248
|
if (S_number === typeof val) {
|
|
314
249
|
start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start;
|
|
@@ -371,7 +306,6 @@ function slice(val, start, end, mutate) {
|
|
|
371
306
|
}
|
|
372
307
|
return val;
|
|
373
308
|
}
|
|
374
|
-
// String padding.
|
|
375
309
|
function pad(str, padding, padchar) {
|
|
376
310
|
str = S_string === typeof str ? str : stringify(str);
|
|
377
311
|
padding = null == padding ? 44 : padding;
|
|
@@ -426,20 +360,53 @@ function typify(value) {
|
|
|
426
360
|
// Anything else (e.g. bigint) is considered T_any
|
|
427
361
|
return T_any;
|
|
428
362
|
}
|
|
429
|
-
//
|
|
430
|
-
|
|
363
|
+
// A list key's index: an integral number, or the string String(n) prints, else NaN.
|
|
364
|
+
function _listkey(key) {
|
|
365
|
+
if (S_number === typeof key) {
|
|
366
|
+
return Number.isInteger(key) ? key + 0 : NaN;
|
|
367
|
+
}
|
|
368
|
+
return S_string === typeof key && R_INTEGER_KEY.test(key) ? +key : NaN;
|
|
369
|
+
}
|
|
370
|
+
// The stored value; a plain map yields own properties only, never __proto__.
|
|
371
|
+
function _readprop(val, key) {
|
|
372
|
+
if (islist(val)) {
|
|
373
|
+
const keyI = _listkey(key);
|
|
374
|
+
return 0 <= keyI ? val[keyI] : NONE;
|
|
375
|
+
}
|
|
376
|
+
if (S_string !== typeof key && S_number !== typeof key) {
|
|
377
|
+
return NONE;
|
|
378
|
+
}
|
|
379
|
+
if (0 < (T_instance & typify(val))) {
|
|
380
|
+
return val[key];
|
|
381
|
+
}
|
|
382
|
+
return Object.prototype.hasOwnProperty.call(val, key) ? val[key] : NONE;
|
|
383
|
+
}
|
|
384
|
+
function _setown(obj, key, val) {
|
|
385
|
+
if ('__proto__' === key) {
|
|
386
|
+
Object.defineProperty(obj, key, {
|
|
387
|
+
value: val,
|
|
388
|
+
enumerable: true,
|
|
389
|
+
writable: true,
|
|
390
|
+
configurable: true,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
obj[key] = val;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
// Get a list element; a negative integer key counts from the end.
|
|
431
398
|
function getelem(val, key, alt) {
|
|
432
399
|
let out = NONE;
|
|
433
400
|
if (NONE === val || NONE === key) {
|
|
434
401
|
return alt;
|
|
435
402
|
}
|
|
436
403
|
if (islist(val)) {
|
|
437
|
-
|
|
438
|
-
if (
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
out = val[
|
|
404
|
+
let keyI = _listkey(key);
|
|
405
|
+
if (keyI < 0) {
|
|
406
|
+
keyI = val.length + keyI;
|
|
407
|
+
}
|
|
408
|
+
if (0 <= keyI) {
|
|
409
|
+
out = val[keyI];
|
|
443
410
|
}
|
|
444
411
|
}
|
|
445
412
|
// null at a slot counts as "no value" — same Group A rule as getprop.
|
|
@@ -456,7 +423,7 @@ function getprop(val, key, alt) {
|
|
|
456
423
|
return alt;
|
|
457
424
|
}
|
|
458
425
|
if (isnode(val)) {
|
|
459
|
-
out = val
|
|
426
|
+
out = _readprop(val, key);
|
|
460
427
|
}
|
|
461
428
|
// JSON null at a key is treated as "no value" for the default-substitution
|
|
462
429
|
// rule. This unifies cross-port behaviour: every host language conflates
|
|
@@ -476,7 +443,7 @@ function _lookup(val, key) {
|
|
|
476
443
|
if (NONE === val || NONE === key)
|
|
477
444
|
return NONE;
|
|
478
445
|
if (isnode(val))
|
|
479
|
-
return val
|
|
446
|
+
return _readprop(val, key);
|
|
480
447
|
return NONE;
|
|
481
448
|
}
|
|
482
449
|
// Convert different types of keys to string representation.
|
|
@@ -522,11 +489,6 @@ function items(val, apply) {
|
|
|
522
489
|
}
|
|
523
490
|
return out;
|
|
524
491
|
}
|
|
525
|
-
// To replicate the array spread operator:
|
|
526
|
-
// a=1, b=[2,3], c=[4,5]
|
|
527
|
-
// [a,...b,c] -> [1,2,3,[4,5]]
|
|
528
|
-
// flatten([a,b,[c]]) -> [1,2,3,[4,5]]
|
|
529
|
-
// NOTE: [c] ensures c is not expanded
|
|
530
492
|
function flatten(list, depth) {
|
|
531
493
|
if (!islist(list)) {
|
|
532
494
|
return list;
|
|
@@ -545,7 +507,6 @@ function filter(val, check) {
|
|
|
545
507
|
}
|
|
546
508
|
return out;
|
|
547
509
|
}
|
|
548
|
-
// Escape regular expression.
|
|
549
510
|
function escre(s) {
|
|
550
511
|
return re_replace(R_ESCAPE_REGEXP, s, '\\$&');
|
|
551
512
|
}
|
|
@@ -553,7 +514,6 @@ function escre(s) {
|
|
|
553
514
|
function re_escape(s) {
|
|
554
515
|
return escre(s);
|
|
555
516
|
}
|
|
556
|
-
// Escape URLs.
|
|
557
517
|
function escurl(s) {
|
|
558
518
|
s = null == s ? S_MT : s;
|
|
559
519
|
return encodeURIComponent(s);
|
|
@@ -563,8 +523,6 @@ function escurl(s) {
|
|
|
563
523
|
// dialect is the RE2 subset documented in /REGEX.md. Internal library code
|
|
564
524
|
// uses these helpers instead of host-language regex methods directly.
|
|
565
525
|
// ===========================================================================
|
|
566
|
-
// Compile a regex (or return as-is if already compiled). Cached behaviour is
|
|
567
|
-
// up to the port; in TS we just construct a RegExp.
|
|
568
526
|
function re_compile(pattern, flags) {
|
|
569
527
|
if (pattern instanceof RegExp) {
|
|
570
528
|
return pattern;
|
|
@@ -615,7 +573,6 @@ function re_replace(pattern, input, replacement) {
|
|
|
615
573
|
}
|
|
616
574
|
return input.replace(re, replacement);
|
|
617
575
|
}
|
|
618
|
-
// Boolean test for any match.
|
|
619
576
|
function re_test(pattern, input) {
|
|
620
577
|
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
|
621
578
|
return re.test(input);
|
|
@@ -662,9 +619,6 @@ function join(arr, sep, url) {
|
|
|
662
619
|
}), (n) => S_MT !== n[1]).join(sepdef);
|
|
663
620
|
return out;
|
|
664
621
|
}
|
|
665
|
-
// Output JSON in a "standard" format, with 2 space indents, each property on a new line,
|
|
666
|
-
// and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
|
|
667
|
-
// In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
|
|
668
622
|
function jsonify(val, flags) {
|
|
669
623
|
let str = S_null;
|
|
670
624
|
if (null != val) {
|
|
@@ -821,7 +775,7 @@ function jm(...kv) {
|
|
|
821
775
|
// with value null" from "slot beyond end of kv".
|
|
822
776
|
let k = i < kv.length ? kv[i] : '$KEY' + i;
|
|
823
777
|
k = 'string' === typeof k ? k : stringify(k);
|
|
824
|
-
o
|
|
778
|
+
_setown(o, k, i + 1 < kv.length ? kv[i + 1] : null);
|
|
825
779
|
}
|
|
826
780
|
return o;
|
|
827
781
|
}
|
|
@@ -834,25 +788,10 @@ function jt(...v) {
|
|
|
834
788
|
}
|
|
835
789
|
return a;
|
|
836
790
|
}
|
|
837
|
-
// Safely delete a property from an object or array element.
|
|
838
|
-
// Undefined arguments and invalid keys are ignored.
|
|
839
|
-
// Returns the (possibly modified) parent.
|
|
840
|
-
// For objects, the property is deleted using the delete operator.
|
|
841
|
-
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
842
|
-
// NOTE: parent list may be new list, thus update references.
|
|
843
791
|
function delprop(parent, key) {
|
|
844
792
|
if (!iskey(key)) {
|
|
845
793
|
return parent;
|
|
846
794
|
}
|
|
847
|
-
// A condensed structure is a DAG: one subtree can be reached by many
|
|
848
|
-
// references, so an in-place write would be visible through all of them.
|
|
849
|
-
// Raising is the decided contract (design Q1) - it can be relaxed to
|
|
850
|
-
// copy-on-write later without breaking any caller, where the reverse is not
|
|
851
|
-
// true. expand() gives an ordinary mutable copy.
|
|
852
|
-
//
|
|
853
|
-
// Checked AFTER the key validation: both helpers define an invalid key as
|
|
854
|
-
// ignored, so a call that cannot write must not start throwing merely
|
|
855
|
-
// because the store is condensed.
|
|
856
795
|
if (iscondensed(parent)) {
|
|
857
796
|
throw new Error('struct: condensed structures are immutable - ' + 'expand() first, then modify the copy');
|
|
858
797
|
}
|
|
@@ -861,12 +800,10 @@ function delprop(parent, key) {
|
|
|
861
800
|
delete parent[key];
|
|
862
801
|
}
|
|
863
802
|
else if (islist(parent)) {
|
|
864
|
-
|
|
865
|
-
let keyI = +key;
|
|
803
|
+
const keyI = _listkey(key);
|
|
866
804
|
if (isNaN(keyI)) {
|
|
867
805
|
return parent;
|
|
868
806
|
}
|
|
869
|
-
keyI = Math.floor(keyI);
|
|
870
807
|
// Delete list element at position keyI, shifting later elements down.
|
|
871
808
|
const psize = size(parent);
|
|
872
809
|
if (0 <= keyI && keyI < psize) {
|
|
@@ -887,59 +824,28 @@ function setprop(parent, key, val) {
|
|
|
887
824
|
if (!iskey(key)) {
|
|
888
825
|
return parent;
|
|
889
826
|
}
|
|
890
|
-
// A condensed structure is a DAG: one subtree can be reached by many
|
|
891
|
-
// references, so an in-place write would be visible through all of them.
|
|
892
|
-
// Raising is the decided contract (design Q1) - it can be relaxed to
|
|
893
|
-
// copy-on-write later without breaking any caller, where the reverse is not
|
|
894
|
-
// true. expand() gives an ordinary mutable copy.
|
|
895
|
-
//
|
|
896
|
-
// Checked AFTER the key validation: both helpers define an invalid key as
|
|
897
|
-
// ignored, so a call that cannot write must not start throwing merely
|
|
898
|
-
// because the store is condensed.
|
|
899
827
|
if (iscondensed(parent)) {
|
|
900
828
|
throw new Error('struct: condensed structures are immutable - ' + 'expand() first, then modify the copy');
|
|
901
829
|
}
|
|
902
830
|
if (ismap(parent)) {
|
|
903
|
-
|
|
904
|
-
const pany = parent;
|
|
905
|
-
pany[key] = val;
|
|
831
|
+
_setown(parent, S_MT + key, val);
|
|
906
832
|
}
|
|
907
833
|
else if (islist(parent)) {
|
|
908
|
-
|
|
909
|
-
let keyI = +key;
|
|
834
|
+
const keyI = _listkey(key);
|
|
910
835
|
if (isNaN(keyI)) {
|
|
911
836
|
return parent;
|
|
912
837
|
}
|
|
913
|
-
keyI = Math.floor(keyI);
|
|
914
|
-
// TODO: DELETE list element
|
|
915
838
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
916
839
|
if (0 <= keyI) {
|
|
917
840
|
parent[slice(keyI, 0, size(parent) + 1)] = val;
|
|
918
841
|
}
|
|
919
|
-
// Prepend value if keyI is negative
|
|
920
842
|
else {
|
|
921
843
|
parent.unshift(val);
|
|
922
844
|
}
|
|
923
845
|
}
|
|
924
846
|
return parent;
|
|
925
847
|
}
|
|
926
|
-
|
|
927
|
-
// The `path` argument passed to the before/after callbacks is a single
|
|
928
|
-
// mutable array per depth, shared across all callback invocations for the
|
|
929
|
-
// lifetime of this top-level walk call. Callbacks that need to store the
|
|
930
|
-
// path MUST clone it (e.g. `path.slice()`); the contents will otherwise
|
|
931
|
-
// be overwritten by subsequent visits.
|
|
932
|
-
function walk(
|
|
933
|
-
// These arguments are the public interface.
|
|
934
|
-
val,
|
|
935
|
-
// Before descending into a node.
|
|
936
|
-
before,
|
|
937
|
-
// After descending into a node.
|
|
938
|
-
after,
|
|
939
|
-
// Maximum recursive depth, default: 32. Use null for infinite depth.
|
|
940
|
-
maxdepth,
|
|
941
|
-
// These areguments are used for recursive state.
|
|
942
|
-
key, parent, path, pool) {
|
|
848
|
+
function walk(val, before, after, maxdepth, key, parent, path, pool) {
|
|
943
849
|
if (NONE === pool) {
|
|
944
850
|
pool = [[]];
|
|
945
851
|
}
|
|
@@ -981,12 +887,10 @@ key, parent, path, pool) {
|
|
|
981
887
|
// Merge a list of values into each other. Later values have
|
|
982
888
|
// precedence. Nodes override scalars. Node kinds (list or map)
|
|
983
889
|
// override each other, and do *not* merge. The first element is
|
|
984
|
-
// modified.
|
|
890
|
+
// modified; the others are not.
|
|
985
891
|
function merge(val, maxdepth) {
|
|
986
|
-
// const md: number = null == maxdepth ? MAXDEPTH : maxdepth < 0 ? 0 : maxdepth
|
|
987
892
|
const md = slice(maxdepth ?? MAXDEPTH, 0);
|
|
988
893
|
let out = NONE;
|
|
989
|
-
// Handle edge cases.
|
|
990
894
|
if (!islist(val)) {
|
|
991
895
|
return val;
|
|
992
896
|
}
|
|
@@ -998,12 +902,10 @@ function merge(val, maxdepth) {
|
|
|
998
902
|
else if (1 === lenlist) {
|
|
999
903
|
return list[0];
|
|
1000
904
|
}
|
|
1001
|
-
// Merge a list of values.
|
|
1002
905
|
out = getprop(list, 0, {});
|
|
1003
906
|
for (let oI = 1; oI < lenlist; oI++) {
|
|
1004
907
|
const obj = list[oI];
|
|
1005
908
|
if (!isnode(obj)) {
|
|
1006
|
-
// Nodes win.
|
|
1007
909
|
out = obj;
|
|
1008
910
|
}
|
|
1009
911
|
else {
|
|
@@ -1016,7 +918,6 @@ function merge(val, maxdepth) {
|
|
|
1016
918
|
if (md <= pI) {
|
|
1017
919
|
setprop(cur[pI - 1], key, val);
|
|
1018
920
|
}
|
|
1019
|
-
// Scalars just override directly.
|
|
1020
921
|
else if (!isnode(val)) {
|
|
1021
922
|
cur[pI] = val;
|
|
1022
923
|
}
|
|
@@ -1026,38 +927,35 @@ function merge(val, maxdepth) {
|
|
|
1026
927
|
dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI];
|
|
1027
928
|
const tval = dst[pI];
|
|
1028
929
|
const vtype = typify(val);
|
|
1029
|
-
// Destination empty, so create node (unless override is class instance).
|
|
1030
|
-
if (NONE === tval && 0 === (T_instance & vtype)) {
|
|
1031
|
-
cur[pI] = islist(val) ? [] : {};
|
|
1032
|
-
}
|
|
1033
930
|
// Matching override and destination so continue with their values.
|
|
1034
|
-
|
|
931
|
+
if (vtype === typify(tval)) {
|
|
1035
932
|
cur[pI] = tval;
|
|
1036
933
|
}
|
|
1037
|
-
//
|
|
934
|
+
// Otherwise the override wins: a plain node is copied, taking
|
|
935
|
+
// nothing from the destination, so no later merge writes into it.
|
|
936
|
+
else if (0 === (T_instance & vtype)) {
|
|
937
|
+
cur[pI] = islist(val) ? [] : {};
|
|
938
|
+
dst[pI] = NONE;
|
|
939
|
+
}
|
|
1038
940
|
else {
|
|
1039
941
|
cur[pI] = val;
|
|
1040
|
-
//
|
|
942
|
+
// A class instance is kept as is, so there is nothing to descend.
|
|
1041
943
|
val = NONE;
|
|
1042
944
|
}
|
|
1043
945
|
}
|
|
1044
|
-
// console.log('BEFORE-END', pathify(path), '@', pI, key,
|
|
1045
|
-
// stringify(val, -1, 1), stringify(parent, -1, 1),
|
|
1046
|
-
// 'CUR=', stringify(cur, -1, 1), 'DST=', stringify(dst, -1, 1))
|
|
1047
946
|
return val;
|
|
1048
947
|
}
|
|
1049
|
-
function after(key, _val,
|
|
948
|
+
function after(key, _val, parent, path) {
|
|
1050
949
|
const cI = size(path);
|
|
1051
950
|
const target = cur[cI - 1];
|
|
1052
951
|
const value = cur[cI];
|
|
1053
|
-
// console.log('AFTER-PREP', pathify(path), '@', cI, cur, '|',
|
|
1054
|
-
// stringify(key, -1, 1), stringify(value, -1, 1), 'T=', stringify(target, -1, 1))
|
|
1055
952
|
setprop(target, key, value);
|
|
1056
|
-
|
|
953
|
+
// walk writes this back into the override, so below the root it is
|
|
954
|
+
// the override's own child (_lookup keeps a null, getprop would not).
|
|
955
|
+
return 0 === cI ? value : _lookup(parent, key);
|
|
1057
956
|
}
|
|
1058
957
|
// Walk overriding node, creating paths in output as needed.
|
|
1059
958
|
out = walk(obj, before, after, maxdepth);
|
|
1060
|
-
// console.log('WALK-DONE', out, obj)
|
|
1061
959
|
}
|
|
1062
960
|
}
|
|
1063
961
|
if (0 === md) {
|
|
@@ -1105,14 +1003,27 @@ function setpath(store, path, val, injdef) {
|
|
|
1105
1003
|
return parent;
|
|
1106
1004
|
}
|
|
1107
1005
|
function getpath(store, path, injdef) {
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1006
|
+
let parts = NONE;
|
|
1007
|
+
if (islist(path)) {
|
|
1008
|
+
parts = [];
|
|
1009
|
+
for (const part of path) {
|
|
1010
|
+
if (S_number === typeof part) {
|
|
1011
|
+
parts.push(S_MT + part);
|
|
1012
|
+
}
|
|
1013
|
+
else if (S_string === typeof part) {
|
|
1014
|
+
parts.push(part);
|
|
1015
|
+
}
|
|
1016
|
+
else {
|
|
1017
|
+
return NONE;
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
else if (S_string === typeof path) {
|
|
1022
|
+
parts = path.split(S_DT);
|
|
1023
|
+
}
|
|
1024
|
+
else if (S_number === typeof path) {
|
|
1025
|
+
parts = [S_MT + path];
|
|
1026
|
+
}
|
|
1116
1027
|
if (NONE === parts) {
|
|
1117
1028
|
return NONE;
|
|
1118
1029
|
}
|
|
@@ -1135,7 +1046,6 @@ function getpath(store, path, injdef) {
|
|
|
1135
1046
|
}
|
|
1136
1047
|
store = expand(store);
|
|
1137
1048
|
}
|
|
1138
|
-
// let root = store
|
|
1139
1049
|
let val = store;
|
|
1140
1050
|
const base = getprop(injdef, S_base);
|
|
1141
1051
|
const src = getprop(store, base, store);
|
|
@@ -1146,7 +1056,6 @@ function getpath(store, path, injdef) {
|
|
|
1146
1056
|
val = src;
|
|
1147
1057
|
}
|
|
1148
1058
|
else if (0 < numparts) {
|
|
1149
|
-
// Check for $ACTIONs
|
|
1150
1059
|
if (1 === numparts) {
|
|
1151
1060
|
val = getprop(store, parts[0]);
|
|
1152
1061
|
}
|
|
@@ -1193,7 +1102,6 @@ function getpath(store, path, injdef) {
|
|
|
1193
1102
|
val = dparent;
|
|
1194
1103
|
}
|
|
1195
1104
|
else {
|
|
1196
|
-
// const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
1197
1105
|
const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)]);
|
|
1198
1106
|
if (ascends <= size(dpath)) {
|
|
1199
1107
|
val = getpath(store, fullpath);
|
|
@@ -1220,20 +1128,14 @@ function getpath(store, path, injdef) {
|
|
|
1220
1128
|
const ref = pathify(path);
|
|
1221
1129
|
val = handler(injdef, val, ref, store);
|
|
1222
1130
|
}
|
|
1223
|
-
// console.log('GETPATH', path, val)
|
|
1224
1131
|
return val;
|
|
1225
1132
|
}
|
|
1226
|
-
// Inject values from a data store into a node recursively, resolving
|
|
1227
|
-
// paths against the store, or current if they are local. The modify
|
|
1228
|
-
// argument allows custom modification of the result. The inj
|
|
1229
|
-
// (Injection) argument is used to maintain recursive state.
|
|
1230
1133
|
function inject(val, store, injdef) {
|
|
1231
1134
|
const valtype = typeof val;
|
|
1232
1135
|
let inj = injdef;
|
|
1233
1136
|
// Create state if at root of injection. The input value is placed
|
|
1234
1137
|
// inside a virtual parent holder to simplify edge cases.
|
|
1235
1138
|
if (NONE === injdef || null == injdef.mode) {
|
|
1236
|
-
// Set up state assuming we are starting in the virtual parent.
|
|
1237
1139
|
inj = new Injection(val, { [S_DTOP]: val });
|
|
1238
1140
|
inj.dparent = store;
|
|
1239
1141
|
inj.errs = getprop(store, S_DERRS, []);
|
|
@@ -1248,12 +1150,7 @@ function inject(val, store, injdef) {
|
|
|
1248
1150
|
inj.descend();
|
|
1249
1151
|
// console.log('INJ-START', val, inj.mode, inj.key, inj.val,
|
|
1250
1152
|
// 't=', inj.path, 'P=', inj.parent, 'dp=', inj.dparent, 'ST=', store.$TOP)
|
|
1251
|
-
// Descend into node.
|
|
1252
1153
|
if (isnode(val)) {
|
|
1253
|
-
// Keys are sorted alphanumerically to ensure determinism.
|
|
1254
|
-
// Injection transforms ($FOO) are processed *after* other keys.
|
|
1255
|
-
// NOTE: the optional digits suffix of the transform can thus be
|
|
1256
|
-
// used to order the transforms.
|
|
1257
1154
|
let nodekeys;
|
|
1258
1155
|
nodekeys = keysof(val);
|
|
1259
1156
|
if (ismap(val)) {
|
|
@@ -1265,10 +1162,6 @@ function inject(val, store, injdef) {
|
|
|
1265
1162
|
else {
|
|
1266
1163
|
nodekeys = keysof(val);
|
|
1267
1164
|
}
|
|
1268
|
-
// Each child key-value pair is processed in three injection phases:
|
|
1269
|
-
// 1. inj.mode=M_KEYPRE - Key string is injected, returning a possibly altered key.
|
|
1270
|
-
// 2. inj.mode=M_VAL - The child value is injected.
|
|
1271
|
-
// 3. inj.mode=M_KEYPOST - Key string is injected again, allowing child mutation.
|
|
1272
1165
|
for (let nkI = 0; nkI < nodekeys.length; nkI++) {
|
|
1273
1166
|
const childinj = inj.child(nkI, nodekeys);
|
|
1274
1167
|
const nodekey = childinj.key;
|
|
@@ -1297,7 +1190,6 @@ function inject(val, store, injdef) {
|
|
|
1297
1190
|
}
|
|
1298
1191
|
}
|
|
1299
1192
|
}
|
|
1300
|
-
// Inject paths into string scalars.
|
|
1301
1193
|
else if (S_string === valtype) {
|
|
1302
1194
|
inj.mode = M_VAL;
|
|
1303
1195
|
val = _injectstr(val, store, inj);
|
|
@@ -1305,26 +1197,20 @@ function inject(val, store, injdef) {
|
|
|
1305
1197
|
inj.setval(val);
|
|
1306
1198
|
}
|
|
1307
1199
|
}
|
|
1308
|
-
// Custom modification.
|
|
1309
1200
|
if (inj.modify && SKIP !== val) {
|
|
1310
1201
|
const mkey = inj.key;
|
|
1311
1202
|
const mparent = inj.parent;
|
|
1312
1203
|
const mval = getprop(mparent, mkey);
|
|
1313
1204
|
inj.modify(mval, mkey, mparent, inj, store);
|
|
1314
1205
|
}
|
|
1315
|
-
// console.log('INJ-VAL', val)
|
|
1316
1206
|
inj.val = val;
|
|
1317
|
-
// Original val reference may no longer be correct.
|
|
1318
|
-
// This return value is only used as the top level result.
|
|
1319
1207
|
return _lookup(inj.parent, S_DTOP);
|
|
1320
1208
|
}
|
|
1321
1209
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
1322
|
-
// Delete a key from a map or list.
|
|
1323
1210
|
const transform_DELETE = (inj) => {
|
|
1324
1211
|
inj.setval(NONE);
|
|
1325
1212
|
return NONE;
|
|
1326
1213
|
};
|
|
1327
|
-
// Copy value from source data.
|
|
1328
1214
|
const transform_COPY = (inj, _val) => {
|
|
1329
1215
|
const ijname = 'COPY';
|
|
1330
1216
|
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
@@ -1348,8 +1234,6 @@ const transform_KEY = (inj) => {
|
|
|
1348
1234
|
delprop(parent, S_BKEY);
|
|
1349
1235
|
return getprop(inj.dparent, keyspec);
|
|
1350
1236
|
}
|
|
1351
|
-
// Key is defined within general purpose $META object.
|
|
1352
|
-
// return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1353
1237
|
return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2);
|
|
1354
1238
|
};
|
|
1355
1239
|
// Annotate node. Does nothing itself, just used by
|
|
@@ -1378,8 +1262,6 @@ const transform_MERGE = (inj) => {
|
|
|
1378
1262
|
args = Array.isArray(args) ? args : [args];
|
|
1379
1263
|
// Remove the $MERGE command from a parent map.
|
|
1380
1264
|
inj.setval(NONE);
|
|
1381
|
-
// Literals in the parent have precedence, but we still merge onto
|
|
1382
|
-
// the parent object, so that node tree references are not changed.
|
|
1383
1265
|
const mergelist = flatten([[parent], args, [clone(parent)]]);
|
|
1384
1266
|
merge(mergelist);
|
|
1385
1267
|
}
|
|
@@ -1394,13 +1276,11 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1394
1276
|
}
|
|
1395
1277
|
// Remove remaining keys to avoid spurious processing.
|
|
1396
1278
|
slice(inj.keys, 0, 1, true);
|
|
1397
|
-
// const [err, srcpath, child] = injectorArgs([T_string, T_any], inj)
|
|
1398
1279
|
const [err, srcpath, child] = injectorArgs([T_string, T_any], slice(inj.parent, 1));
|
|
1399
1280
|
if (NONE !== err) {
|
|
1400
1281
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
1401
1282
|
return NONE;
|
|
1402
1283
|
}
|
|
1403
|
-
// Source data.
|
|
1404
1284
|
const srcstore = getprop(store, inj.base, store);
|
|
1405
1285
|
const src = getpath(srcstore, srcpath, inj);
|
|
1406
1286
|
const srctype = typify(src);
|
|
@@ -1427,7 +1307,6 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1427
1307
|
const ckey = getelem(inj.path, -2);
|
|
1428
1308
|
const tpath = slice(inj.path, -1);
|
|
1429
1309
|
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey]);
|
|
1430
|
-
// Parent structure.
|
|
1431
1310
|
tcur = { [ckey]: tcur };
|
|
1432
1311
|
if (1 < size(tpath)) {
|
|
1433
1312
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
@@ -1445,9 +1324,7 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1445
1324
|
inject(tval, store, tinj);
|
|
1446
1325
|
rval = tinj.val;
|
|
1447
1326
|
}
|
|
1448
|
-
// _updateAncestors(inj, target, tkey, rval)
|
|
1449
1327
|
setprop(target, tkey, rval);
|
|
1450
|
-
// Prevent callee from damaging first list entry (since we are in `val` mode).
|
|
1451
1328
|
return rval[0];
|
|
1452
1329
|
};
|
|
1453
1330
|
// Convert a node to a map.
|
|
@@ -1458,21 +1335,17 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1458
1335
|
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1459
1336
|
return NONE;
|
|
1460
1337
|
}
|
|
1461
|
-
// Get arguments.
|
|
1462
1338
|
const args = getprop(parent, key);
|
|
1463
1339
|
const [err, srcpath, origchildspec] = injectorArgs([T_string, T_any], args);
|
|
1464
1340
|
if (NONE !== err) {
|
|
1465
1341
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
1466
1342
|
return NONE;
|
|
1467
1343
|
}
|
|
1468
|
-
// Find key and target node.
|
|
1469
1344
|
const tkey = getelem(path, -2);
|
|
1470
1345
|
const pathsize = size(path);
|
|
1471
1346
|
const target = getelem(nodes, pathsize - 2, () => getelem(nodes, pathsize - 1));
|
|
1472
|
-
// Source data
|
|
1473
1347
|
const srcstore = getprop(store, inj.base, store);
|
|
1474
1348
|
let src = getpath(srcstore, srcpath, inj);
|
|
1475
|
-
// Prepare source as a list.
|
|
1476
1349
|
if (!islist(src)) {
|
|
1477
1350
|
if (ismap(src)) {
|
|
1478
1351
|
src = items(src, (item) => {
|
|
@@ -1487,11 +1360,9 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1487
1360
|
if (null == src) {
|
|
1488
1361
|
return NONE;
|
|
1489
1362
|
}
|
|
1490
|
-
// Get keypath.
|
|
1491
1363
|
const keypath = getprop(origchildspec, S_BKEY);
|
|
1492
1364
|
const childspec = delprop(origchildspec, S_BKEY);
|
|
1493
1365
|
const child = getprop(childspec, S_BVAL, childspec);
|
|
1494
|
-
// Build parallel target object.
|
|
1495
1366
|
const tval = {};
|
|
1496
1367
|
items(src, (item) => {
|
|
1497
1368
|
const srckey = item[0];
|
|
@@ -1517,7 +1388,6 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1517
1388
|
});
|
|
1518
1389
|
let rval = {};
|
|
1519
1390
|
if (!isempty(tval)) {
|
|
1520
|
-
// Build parallel source object.
|
|
1521
1391
|
const tsrc = {};
|
|
1522
1392
|
src.reduce((a, n, i) => {
|
|
1523
1393
|
const kn = null == keypath
|
|
@@ -1547,30 +1417,20 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1547
1417
|
inject(tval, store, tinj);
|
|
1548
1418
|
rval = tinj.val;
|
|
1549
1419
|
}
|
|
1550
|
-
// _updateAncestors(inj, target, tkey, rval)
|
|
1551
1420
|
setprop(target, tkey, rval);
|
|
1552
|
-
// Drop transform key.
|
|
1553
1421
|
return NONE;
|
|
1554
1422
|
};
|
|
1555
|
-
|
|
1556
|
-
// Reference original spec (enables recursice transformations)
|
|
1557
|
-
// Format: ['`$REF`', '`spec-path`']
|
|
1558
|
-
const transform_REF = (inj, val, _ref, store) => {
|
|
1559
|
-
const { nodes } = inj;
|
|
1423
|
+
const transform_REF = (inj, _val, _ref, store) => {
|
|
1560
1424
|
if (M_VAL !== inj.mode) {
|
|
1561
1425
|
return NONE;
|
|
1562
1426
|
}
|
|
1563
1427
|
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1564
1428
|
const refpath = _lookup(inj.parent, 1);
|
|
1565
1429
|
inj.keyI = size(inj.keys);
|
|
1566
|
-
// Spec reference.
|
|
1567
1430
|
const spec = getprop(store, S_DSPEC)();
|
|
1568
1431
|
const dpath = slice(inj.path, 1);
|
|
1569
1432
|
const ref = getpath(spec, refpath, {
|
|
1570
|
-
// TODO: test relative refs
|
|
1571
|
-
// dpath: inj.path.slice(1),
|
|
1572
1433
|
dpath,
|
|
1573
|
-
// dparent: getpath(spec, inj.path.slice(1))
|
|
1574
1434
|
dparent: getpath(spec, dpath),
|
|
1575
1435
|
});
|
|
1576
1436
|
let hasSubRef = false;
|
|
@@ -1582,44 +1442,29 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1582
1442
|
return v;
|
|
1583
1443
|
});
|
|
1584
1444
|
}
|
|
1585
|
-
|
|
1586
|
-
const
|
|
1587
|
-
const
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
let rval = NONE;
|
|
1591
|
-
if (!hasSubRef || NONE !== tval) {
|
|
1592
|
-
const tinj = inj.child(0, [getelem(tpath, -1)]);
|
|
1593
|
-
tinj.path = tpath;
|
|
1594
|
-
tinj.nodes = slice(inj.nodes, -1);
|
|
1595
|
-
tinj.parent = getelem(nodes, -2);
|
|
1596
|
-
tinj.val = tref;
|
|
1597
|
-
tinj.dpath = flatten([cpath]);
|
|
1598
|
-
tinj.dparent = tcur;
|
|
1599
|
-
inject(tref, store, tinj);
|
|
1600
|
-
rval = tinj.val;
|
|
1601
|
-
}
|
|
1602
|
-
else {
|
|
1603
|
-
rval = NONE;
|
|
1604
|
-
}
|
|
1445
|
+
// A recursive reference stops where the data does.
|
|
1446
|
+
const tval = getpath(store, slice(inj.path, -1));
|
|
1447
|
+
const rval = !hasSubRef || NONE !== tval ? clone(ref) : NONE;
|
|
1448
|
+
// The enclosing loop injects the referenced spec in this list's place, so
|
|
1449
|
+
// neither this pass nor this element leaves a value for modify to check.
|
|
1605
1450
|
const grandparent = inj.setval(rval, 2);
|
|
1606
|
-
|
|
1607
|
-
|
|
1451
|
+
const prior = inj.prior;
|
|
1452
|
+
if (prior) {
|
|
1453
|
+
prior.modify = NONE;
|
|
1454
|
+
if (islist(grandparent) || NONE !== rval) {
|
|
1455
|
+
prior.keyI--;
|
|
1456
|
+
}
|
|
1608
1457
|
}
|
|
1609
|
-
return
|
|
1458
|
+
return SKIP;
|
|
1610
1459
|
};
|
|
1611
1460
|
const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
1612
|
-
// console.log('FORMAT-START', inj, _val)
|
|
1613
1461
|
// Remove remaining keys to avoid spurious processing.
|
|
1614
1462
|
slice(inj.keys, 0, 1, true);
|
|
1615
1463
|
if (M_VAL !== inj.mode) {
|
|
1616
1464
|
return NONE;
|
|
1617
1465
|
}
|
|
1618
|
-
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1619
|
-
// TODO: EACH and PACK should accept customm functions too
|
|
1620
1466
|
const name = _lookup(inj.parent, 1);
|
|
1621
1467
|
const child = _lookup(inj.parent, 2);
|
|
1622
|
-
// Source data.
|
|
1623
1468
|
const tkey = getelem(inj.path, -2);
|
|
1624
1469
|
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1));
|
|
1625
1470
|
const cinj = injectChild(child, store, inj);
|
|
@@ -1631,7 +1476,6 @@ const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
|
1631
1476
|
}
|
|
1632
1477
|
const out = walk(resolved, formatter);
|
|
1633
1478
|
setprop(target, tkey, out);
|
|
1634
|
-
// _updateAncestors(inj, target, tkey, out)
|
|
1635
1479
|
return out;
|
|
1636
1480
|
};
|
|
1637
1481
|
const FORMATTER = {
|
|
@@ -1672,7 +1516,6 @@ const transform_APPLY = (inj, _val, _ref, store) => {
|
|
|
1672
1516
|
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1673
1517
|
return NONE;
|
|
1674
1518
|
}
|
|
1675
|
-
// const [err, apply, child] = injectorArgs([T_function, T_any], inj)
|
|
1676
1519
|
const [err, apply, child] = injectorArgs([T_function, T_any], slice(inj.parent, 1));
|
|
1677
1520
|
if (NONE !== err) {
|
|
1678
1521
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
@@ -1729,7 +1572,6 @@ injdef) {
|
|
|
1729
1572
|
$FORMAT: transform_FORMAT,
|
|
1730
1573
|
$APPLY: transform_APPLY,
|
|
1731
1574
|
},
|
|
1732
|
-
// Custom extra transforms, if any.
|
|
1733
1575
|
extraTransforms,
|
|
1734
1576
|
{
|
|
1735
1577
|
$ERRS: errs,
|
|
@@ -1760,7 +1602,8 @@ const validate_STRING = (inj) => {
|
|
|
1760
1602
|
};
|
|
1761
1603
|
const validate_TYPE = (inj, _val, ref) => {
|
|
1762
1604
|
const tname = slice(ref, 1).toLowerCase();
|
|
1763
|
-
|
|
1605
|
+
// $NIL is absent or null.
|
|
1606
|
+
const typev = (1 << (31 - TYPENAME.indexOf(tname))) | (S_nil === tname ? T_null : 0);
|
|
1764
1607
|
const out = _lookup(inj.dparent, inj.key);
|
|
1765
1608
|
const t = typify(out);
|
|
1766
1609
|
// console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
|
|
@@ -1770,7 +1613,6 @@ const validate_TYPE = (inj, _val, ref) => {
|
|
|
1770
1613
|
}
|
|
1771
1614
|
return out;
|
|
1772
1615
|
};
|
|
1773
|
-
// Allow any value.
|
|
1774
1616
|
const validate_ANY = (inj) => {
|
|
1775
1617
|
const out = _lookup(inj.dparent, inj.key);
|
|
1776
1618
|
return out;
|
|
@@ -1781,17 +1623,15 @@ const validate_ANY = (inj) => {
|
|
|
1781
1623
|
const validate_CHILD = (inj) => {
|
|
1782
1624
|
const { mode, key, parent, keys, path } = inj;
|
|
1783
1625
|
// Setup data structures for validation by cloning child template.
|
|
1784
|
-
// Map syntax.
|
|
1785
1626
|
if (M_KEYPRE === mode) {
|
|
1786
1627
|
const childtm = getprop(parent, key);
|
|
1787
|
-
// Get corresponding current object.
|
|
1788
1628
|
const pkey = getelem(path, -2);
|
|
1789
1629
|
let tval = getprop(inj.dparent, pkey);
|
|
1790
1630
|
if (NONE == tval) {
|
|
1791
1631
|
tval = {};
|
|
1792
1632
|
}
|
|
1793
1633
|
else if (!ismap(tval)) {
|
|
1794
|
-
|
|
1634
|
+
// _validation reports the mismatch, once.
|
|
1795
1635
|
return NONE;
|
|
1796
1636
|
}
|
|
1797
1637
|
const ckeys = keysof(tval);
|
|
@@ -1800,27 +1640,20 @@ const validate_CHILD = (inj) => {
|
|
|
1800
1640
|
// NOTE: modifying inj! This extends the child value loop in inject.
|
|
1801
1641
|
keys.push(ckey);
|
|
1802
1642
|
}
|
|
1803
|
-
// Remove $CHILD to cleanup ouput.
|
|
1804
1643
|
inj.setval(NONE);
|
|
1805
1644
|
return NONE;
|
|
1806
1645
|
}
|
|
1807
|
-
// List syntax.
|
|
1808
1646
|
if (M_VAL === mode) {
|
|
1809
1647
|
if (!islist(parent)) {
|
|
1810
|
-
// $CHILD was not inside a list.
|
|
1811
1648
|
inj.errs.push('Invalid $CHILD as value');
|
|
1812
1649
|
return NONE;
|
|
1813
1650
|
}
|
|
1814
1651
|
const childtm = _lookup(parent, 1);
|
|
1815
1652
|
if (NONE === inj.dparent) {
|
|
1816
|
-
// Empty list as default.
|
|
1817
|
-
// parent.length = 0
|
|
1818
1653
|
slice(parent, 0, 0, true);
|
|
1819
1654
|
return NONE;
|
|
1820
1655
|
}
|
|
1821
1656
|
if (!islist(inj.dparent)) {
|
|
1822
|
-
const msg = _invalidTypeMsg(slice(inj.path, -1), S_list, typify(inj.dparent), inj.dparent, 'V0230');
|
|
1823
|
-
inj.errs.push(msg);
|
|
1824
1657
|
inj.keyI = size(parent);
|
|
1825
1658
|
return inj.dparent;
|
|
1826
1659
|
}
|
|
@@ -1844,55 +1677,6 @@ const validate_CHILD = (inj) => {
|
|
|
1844
1677
|
}
|
|
1845
1678
|
return NONE;
|
|
1846
1679
|
};
|
|
1847
|
-
// TODO: implement SOME, ALL
|
|
1848
|
-
// FIX: ONE should mean exactly one, not at least one (=SOME)
|
|
1849
|
-
// TODO: implement a generate validate_ALT to do all of these
|
|
1850
|
-
// Match at least one of the specified shapes.
|
|
1851
|
-
// Syntax: ['`$ONE`', alt0, alt1, ...]
|
|
1852
|
-
const validate_ONE = (inj, _val, _ref, store) => {
|
|
1853
|
-
const { mode, parent, keyI } = inj;
|
|
1854
|
-
// Only operate in val mode, since parent is a list.
|
|
1855
|
-
if (M_VAL === mode) {
|
|
1856
|
-
if (!islist(parent) || 0 !== keyI) {
|
|
1857
|
-
inj.errs.push('The $ONE validator at field ' +
|
|
1858
|
-
pathify(inj.path, 1, 1) +
|
|
1859
|
-
' must be the first element of an array.');
|
|
1860
|
-
return;
|
|
1861
|
-
}
|
|
1862
|
-
inj.keyI = size(inj.keys);
|
|
1863
|
-
// Clean up structure, replacing [$ONE, ...] with current
|
|
1864
|
-
inj.setval(inj.dparent, 2);
|
|
1865
|
-
inj.path = slice(inj.path, -1);
|
|
1866
|
-
inj.key = getelem(inj.path, -1);
|
|
1867
|
-
const tvals = slice(parent, 1);
|
|
1868
|
-
if (0 === size(tvals)) {
|
|
1869
|
-
inj.errs.push('The $ONE validator at field ' +
|
|
1870
|
-
pathify(inj.path, 1, 1) +
|
|
1871
|
-
' must have at least one argument.');
|
|
1872
|
-
return;
|
|
1873
|
-
}
|
|
1874
|
-
// See if we can find a match.
|
|
1875
|
-
for (const tval of tvals) {
|
|
1876
|
-
// If match, then errs.length = 0
|
|
1877
|
-
const terrs = [];
|
|
1878
|
-
const vstore = merge([{}, store], 1);
|
|
1879
|
-
vstore.$TOP = inj.dparent;
|
|
1880
|
-
const vcurrent = validate(inj.dparent, tval, {
|
|
1881
|
-
extra: vstore,
|
|
1882
|
-
errs: terrs,
|
|
1883
|
-
meta: inj.meta,
|
|
1884
|
-
});
|
|
1885
|
-
inj.setval(vcurrent, -2);
|
|
1886
|
-
// Accept current value if there was a match
|
|
1887
|
-
if (0 === size(terrs)) {
|
|
1888
|
-
return;
|
|
1889
|
-
}
|
|
1890
|
-
}
|
|
1891
|
-
// There was no match.
|
|
1892
|
-
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1893
|
-
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(tvals) ? 'one of ' : '') + valdesc, typify(inj.dparent), inj.dparent, 'V0210'));
|
|
1894
|
-
}
|
|
1895
|
-
};
|
|
1896
1680
|
const validate_EXACT = (inj) => {
|
|
1897
1681
|
const { mode, parent, key, keyI } = inj;
|
|
1898
1682
|
// Only operate in val mode, since parent is a list.
|
|
@@ -1903,43 +1687,231 @@ const validate_EXACT = (inj) => {
|
|
|
1903
1687
|
' must be the first element of an array.');
|
|
1904
1688
|
return;
|
|
1905
1689
|
}
|
|
1690
|
+
const cval = _slotval(inj);
|
|
1906
1691
|
inj.keyI = size(inj.keys);
|
|
1907
|
-
//
|
|
1908
|
-
inj.setval(
|
|
1909
|
-
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
1692
|
+
// Replace [$EXACT, ...] with the data value; SKIP keeps inject off the detached list.
|
|
1693
|
+
inj.setval(cval, 2);
|
|
1910
1694
|
inj.path = slice(inj.path, 0, -1);
|
|
1911
|
-
inj.key = getelem(inj.path, -1);
|
|
1912
1695
|
const tvals = slice(parent, 1);
|
|
1913
1696
|
if (0 === size(tvals)) {
|
|
1914
1697
|
inj.errs.push('The $EXACT validator at field ' +
|
|
1915
1698
|
pathify(inj.path, 1, 1) +
|
|
1916
1699
|
' must have at least one argument.');
|
|
1917
|
-
return;
|
|
1700
|
+
return SKIP;
|
|
1918
1701
|
}
|
|
1919
|
-
// See if we can find an exact value match.
|
|
1920
|
-
let currentstr = undefined;
|
|
1921
1702
|
for (const tval of tvals) {
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr;
|
|
1925
|
-
const tvalstr = stringify(tval);
|
|
1926
|
-
exactmatch = tvalstr === currentstr;
|
|
1927
|
-
}
|
|
1928
|
-
if (exactmatch) {
|
|
1929
|
-
return;
|
|
1703
|
+
if (_deepequal(tval, cval)) {
|
|
1704
|
+
return SKIP;
|
|
1930
1705
|
}
|
|
1931
1706
|
}
|
|
1932
|
-
// There was no match.
|
|
1933
1707
|
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1934
1708
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(inj.path) ? '' : 'value ') +
|
|
1935
1709
|
'exactly equal to ' +
|
|
1936
1710
|
(1 === size(tvals) ? '' : 'one of ') +
|
|
1937
|
-
valdesc, typify(
|
|
1711
|
+
valdesc, typify(cval), cval, 'V0110'));
|
|
1712
|
+
return SKIP;
|
|
1938
1713
|
}
|
|
1939
1714
|
else {
|
|
1940
1715
|
delprop(parent, key);
|
|
1941
1716
|
}
|
|
1942
1717
|
};
|
|
1718
|
+
// Count the specs that admit the value: ['`$NOF`', count, spec, ...], where
|
|
1719
|
+
// count is an integer, or a map holding min, max or both.
|
|
1720
|
+
const validate_NOF = (inj, _val, _ref, store) => _validatecount(inj, store, '$NOF', true);
|
|
1721
|
+
// Every spec admits the value: ['`$ALL`', spec, ...].
|
|
1722
|
+
const validate_ALL = (inj, _val, _ref, store) => _validatecount(inj, store, '$ALL', false);
|
|
1723
|
+
// The counting list directives. Without a count, every spec has to admit.
|
|
1724
|
+
function _validatecount(inj, store, name, counted) {
|
|
1725
|
+
const { mode, parent, keyI } = inj;
|
|
1726
|
+
if (M_VAL !== mode) {
|
|
1727
|
+
return NONE;
|
|
1728
|
+
}
|
|
1729
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
1730
|
+
inj.errs.push('The ' +
|
|
1731
|
+
name +
|
|
1732
|
+
' validator at field ' +
|
|
1733
|
+
pathify(inj.path, 1, 1) +
|
|
1734
|
+
' must be the first element of an array.');
|
|
1735
|
+
return NONE;
|
|
1736
|
+
}
|
|
1737
|
+
const cval = _slotval(inj);
|
|
1738
|
+
inj.keyI = size(inj.keys);
|
|
1739
|
+
// Replace the directive's list with the data value. Each return is SKIP, so
|
|
1740
|
+
// that inject leaves the detached list alone and validates nothing in it.
|
|
1741
|
+
inj.setval(cval, 2);
|
|
1742
|
+
inj.path = slice(inj.path, -1);
|
|
1743
|
+
const count = counted ? _lookup(parent, 1) : NONE;
|
|
1744
|
+
const tvals = slice(parent, counted ? 2 : 1);
|
|
1745
|
+
const numtvals = size(tvals);
|
|
1746
|
+
const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1);
|
|
1747
|
+
if (0 === numtvals) {
|
|
1748
|
+
inj.errs.push(field + (counted ? ' must have a count and' : ' must have') + ' at least one argument.');
|
|
1749
|
+
return SKIP;
|
|
1750
|
+
}
|
|
1751
|
+
const [lo, hi, countdesc] = counted ? _nofcount(count, numtvals) : [numtvals, numtvals, 'all'];
|
|
1752
|
+
if (NONE === countdesc) {
|
|
1753
|
+
inj.errs.push(field + ' has an invalid count: ' + stringify(count) + '.');
|
|
1754
|
+
return SKIP;
|
|
1755
|
+
}
|
|
1756
|
+
let admitted = 0;
|
|
1757
|
+
for (let tI = 0; tI < numtvals; tI++) {
|
|
1758
|
+
const left = numtvals - tI;
|
|
1759
|
+
// Stop once the specs left cannot change the verdict.
|
|
1760
|
+
if (hi < admitted || admitted + left < lo || (lo <= admitted && admitted + left <= hi)) {
|
|
1761
|
+
break;
|
|
1762
|
+
}
|
|
1763
|
+
if (_admits(cval, tvals[tI], inj, store)) {
|
|
1764
|
+
admitted++;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
if (admitted < lo || hi < admitted) {
|
|
1768
|
+
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1769
|
+
inj.errs.push(_invalidTypeMsg(inj.path, countdesc + ' of ' + valdesc, typify(cval), cval, 'V0310'));
|
|
1770
|
+
}
|
|
1771
|
+
return SKIP;
|
|
1772
|
+
}
|
|
1773
|
+
// The value's length is within a bound: ['`$LEN`', bound, spec?], where the
|
|
1774
|
+
// bound is as for a $NOF count. A string's length is in code points.
|
|
1775
|
+
const validate_LEN = (inj, _val, _ref, store) => _validatebound(inj, store, '$LEN');
|
|
1776
|
+
// The value is a number within a bound: ['`$RANGE`', bound, spec?], where the
|
|
1777
|
+
// bound holds min or gt, max or lt, or one of each.
|
|
1778
|
+
const validate_RANGE = (inj, _val, _ref, store) => _validatebound(inj, store, '$RANGE');
|
|
1779
|
+
const validate_LIKE = (inj, _val, _ref, store) => _validatebound(inj, store, '$LIKE');
|
|
1780
|
+
// The bounding list directives: a bound or pattern, then at most one spec that also admits.
|
|
1781
|
+
function _validatebound(inj, store, name) {
|
|
1782
|
+
const { mode, parent, keyI } = inj;
|
|
1783
|
+
if (M_VAL !== mode) {
|
|
1784
|
+
return NONE;
|
|
1785
|
+
}
|
|
1786
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
1787
|
+
inj.errs.push('The ' +
|
|
1788
|
+
name +
|
|
1789
|
+
' validator at field ' +
|
|
1790
|
+
pathify(inj.path, 1, 1) +
|
|
1791
|
+
' must be the first element of an array.');
|
|
1792
|
+
return NONE;
|
|
1793
|
+
}
|
|
1794
|
+
const cval = _slotval(inj);
|
|
1795
|
+
inj.keyI = size(inj.keys);
|
|
1796
|
+
// As for the counting directives: SKIP keeps inject off the detached list.
|
|
1797
|
+
inj.setval(cval, 2);
|
|
1798
|
+
inj.path = slice(inj.path, -1);
|
|
1799
|
+
const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1);
|
|
1800
|
+
const nargs = size(parent) - 1;
|
|
1801
|
+
const islen = '$LEN' === name;
|
|
1802
|
+
const islike = '$LIKE' === name;
|
|
1803
|
+
const argname = islike ? 'pattern' : 'bound';
|
|
1804
|
+
if (nargs < 1 || 2 < nargs) {
|
|
1805
|
+
inj.errs.push(field + ' must have a ' + argname + ' and at most one spec.');
|
|
1806
|
+
return SKIP;
|
|
1807
|
+
}
|
|
1808
|
+
const bound = _lookup(parent, 1);
|
|
1809
|
+
const like = islike ? _likere(bound) : NONE;
|
|
1810
|
+
const [lo, hi, desc] = islen
|
|
1811
|
+
? _lenbound(bound)
|
|
1812
|
+
: [0, 0, islike ? (NONE === like ? NONE : 'like /' + bound + '/') : _rangedesc(bound)];
|
|
1813
|
+
if (NONE === desc) {
|
|
1814
|
+
inj.errs.push(field + ' has an invalid ' + argname + ': ' + stringify(bound) + '.');
|
|
1815
|
+
return SKIP;
|
|
1816
|
+
}
|
|
1817
|
+
const ctype = typify(cval);
|
|
1818
|
+
if (2 === nargs && !_admits(cval, _lookup(parent, 2), inj, store)) {
|
|
1819
|
+
const specdesc = replace(stringify(_lookup(parent, 2)), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1820
|
+
inj.errs.push(_invalidTypeMsg(inj.path, specdesc, ctype, cval, 'V0410'));
|
|
1821
|
+
return SKIP;
|
|
1822
|
+
}
|
|
1823
|
+
if (0 === ((islen ? T_string | T_list | T_map : islike ? T_string : T_number) & ctype)) {
|
|
1824
|
+
const needtype = islen ? 'string, list or map' : islike ? S_string : S_number;
|
|
1825
|
+
inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0420'));
|
|
1826
|
+
return SKIP;
|
|
1827
|
+
}
|
|
1828
|
+
const len = islen
|
|
1829
|
+
? S_string === typeof cval
|
|
1830
|
+
? Array.from(cval).length
|
|
1831
|
+
: size(cval)
|
|
1832
|
+
: 0;
|
|
1833
|
+
const inbound = islen
|
|
1834
|
+
? lo <= len && (-1 === hi || len <= hi)
|
|
1835
|
+
: islike
|
|
1836
|
+
? re_test(like, cval)
|
|
1837
|
+
: _inrange(bound, cval);
|
|
1838
|
+
if (!inbound) {
|
|
1839
|
+
const needtype = (1 < size(inj.path) ? '' : 'value ') + desc;
|
|
1840
|
+
inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0430'));
|
|
1841
|
+
}
|
|
1842
|
+
return SKIP;
|
|
1843
|
+
}
|
|
1844
|
+
// The bounds of a $LEN bound, with -1 for no maximum, and the words for it,
|
|
1845
|
+
// which are absent when the bound is invalid.
|
|
1846
|
+
function _lenbound(bound) {
|
|
1847
|
+
const [lo, hi, countdesc] = _nofcount(bound, -1);
|
|
1848
|
+
if (NONE === countdesc) {
|
|
1849
|
+
return [0, 0, NONE];
|
|
1850
|
+
}
|
|
1851
|
+
const desc = 'of length ' +
|
|
1852
|
+
(lo === hi ? lo : -1 === hi ? 'at least ' + lo : 0 === lo ? 'at most ' + hi : lo + ' to ' + hi);
|
|
1853
|
+
return [lo, hi, desc];
|
|
1854
|
+
}
|
|
1855
|
+
// A $LIKE pattern compiled by the port's own regex engine; absent unless it compiles.
|
|
1856
|
+
function _likere(pattern) {
|
|
1857
|
+
if (S_string !== typeof pattern) {
|
|
1858
|
+
return NONE;
|
|
1859
|
+
}
|
|
1860
|
+
try {
|
|
1861
|
+
return re_compile(pattern);
|
|
1862
|
+
}
|
|
1863
|
+
catch {
|
|
1864
|
+
return NONE;
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
// The words for a $RANGE bound, which are absent when the bound is invalid.
|
|
1868
|
+
function _rangedesc(bound) {
|
|
1869
|
+
if (!ismap(bound)) {
|
|
1870
|
+
return NONE;
|
|
1871
|
+
}
|
|
1872
|
+
const min = _lookup(bound, 'min');
|
|
1873
|
+
const max = _lookup(bound, 'max');
|
|
1874
|
+
const gt = _lookup(bound, 'gt');
|
|
1875
|
+
const lt = _lookup(bound, 'lt');
|
|
1876
|
+
const lower = NONE === min ? gt : min;
|
|
1877
|
+
const upper = NONE === max ? lt : max;
|
|
1878
|
+
const isnum = (n) => NONE === n || 0 < (T_number & typify(n));
|
|
1879
|
+
const valid = 0 < size(bound) &&
|
|
1880
|
+
size(bound) === [min, max, gt, lt].filter((n) => NONE !== n).length &&
|
|
1881
|
+
(NONE === min || NONE === gt) &&
|
|
1882
|
+
(NONE === max || NONE === lt) &&
|
|
1883
|
+
isnum(min) &&
|
|
1884
|
+
isnum(max) &&
|
|
1885
|
+
isnum(gt) &&
|
|
1886
|
+
isnum(lt) &&
|
|
1887
|
+
(NONE === lower || NONE === upper || lower <= upper);
|
|
1888
|
+
if (!valid) {
|
|
1889
|
+
return NONE;
|
|
1890
|
+
}
|
|
1891
|
+
return join([
|
|
1892
|
+
NONE !== min
|
|
1893
|
+
? 'at least ' + stringify(min)
|
|
1894
|
+
: NONE !== gt
|
|
1895
|
+
? 'greater than ' + stringify(gt)
|
|
1896
|
+
: NONE,
|
|
1897
|
+
NONE !== max
|
|
1898
|
+
? 'at most ' + stringify(max)
|
|
1899
|
+
: NONE !== lt
|
|
1900
|
+
? 'less than ' + stringify(lt)
|
|
1901
|
+
: NONE,
|
|
1902
|
+
], ' and ');
|
|
1903
|
+
}
|
|
1904
|
+
// A number within a valid $RANGE bound.
|
|
1905
|
+
function _inrange(bound, val) {
|
|
1906
|
+
const min = _lookup(bound, 'min');
|
|
1907
|
+
const max = _lookup(bound, 'max');
|
|
1908
|
+
const gt = _lookup(bound, 'gt');
|
|
1909
|
+
const lt = _lookup(bound, 'lt');
|
|
1910
|
+
return ((NONE === min || min <= val) &&
|
|
1911
|
+
(NONE === max || val <= max) &&
|
|
1912
|
+
(NONE === gt || gt < val) &&
|
|
1913
|
+
(NONE === lt || val < lt));
|
|
1914
|
+
}
|
|
1943
1915
|
// This is the "modify" argument to inject. Use this to perform
|
|
1944
1916
|
// generic validation. Runs *after* any special commands.
|
|
1945
1917
|
const _validation = (pval, key, parent, inj) => {
|
|
@@ -1949,9 +1921,7 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1949
1921
|
if (SKIP === pval) {
|
|
1950
1922
|
return;
|
|
1951
1923
|
}
|
|
1952
|
-
// select needs exact matches
|
|
1953
1924
|
const exact = getprop(inj.meta, S_BEXACT, false);
|
|
1954
|
-
// Current val to verify.
|
|
1955
1925
|
const cval = getprop(inj.dparent, key);
|
|
1956
1926
|
if (NONE === inj || (!exact && NONE === cval)) {
|
|
1957
1927
|
return;
|
|
@@ -1962,7 +1932,6 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1962
1932
|
return;
|
|
1963
1933
|
}
|
|
1964
1934
|
const ctype = typify(cval);
|
|
1965
|
-
// Type mismatch.
|
|
1966
1935
|
if (ptype !== ctype && NONE !== pval) {
|
|
1967
1936
|
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0010'));
|
|
1968
1937
|
return;
|
|
@@ -2016,16 +1985,6 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
2016
1985
|
}
|
|
2017
1986
|
return;
|
|
2018
1987
|
};
|
|
2019
|
-
// Validate a data structure against a shape specification. The shape
|
|
2020
|
-
// specification follows the "by example" principle. Plain data in
|
|
2021
|
-
// teh shape is treated as default values that also specify the
|
|
2022
|
-
// required type. Thus shape {a:1} validates {a:2}, since the types
|
|
2023
|
-
// (number) match, but not {a:'A'}. Shape {a;1} against data {}
|
|
2024
|
-
// returns {a:1} as a=1 is the default value of the a key. Special
|
|
2025
|
-
// validation commands (in the same syntax as transform ) are also
|
|
2026
|
-
// provided to specify required values. Thus shape {a:'`$STRING`'}
|
|
2027
|
-
// validates {a:'A'} but not {a:1}. Empty map or list means the node
|
|
2028
|
-
// is open, and if missing an empty default is inserted.
|
|
2029
1988
|
function validate(data, // Source data to transform into new data (original not mutated)
|
|
2030
1989
|
spec, // Transform specification; output follows this shape
|
|
2031
1990
|
injdef) {
|
|
@@ -2034,7 +1993,6 @@ injdef) {
|
|
|
2034
1993
|
const errs = injdef?.errs || [];
|
|
2035
1994
|
const store = merge([
|
|
2036
1995
|
{
|
|
2037
|
-
// Remove the transform commands.
|
|
2038
1996
|
$DELETE: null,
|
|
2039
1997
|
$COPY: null,
|
|
2040
1998
|
$KEY: null,
|
|
@@ -2055,8 +2013,12 @@ injdef) {
|
|
|
2055
2013
|
$INSTANCE: validate_TYPE,
|
|
2056
2014
|
$ANY: validate_ANY,
|
|
2057
2015
|
$CHILD: validate_CHILD,
|
|
2058
|
-
$ONE: validate_ONE,
|
|
2059
2016
|
$EXACT: validate_EXACT,
|
|
2017
|
+
$NOF: validate_NOF,
|
|
2018
|
+
$ALL: validate_ALL,
|
|
2019
|
+
$LEN: validate_LEN,
|
|
2020
|
+
$RANGE: validate_RANGE,
|
|
2021
|
+
$LIKE: validate_LIKE,
|
|
2060
2022
|
},
|
|
2061
2023
|
getdef(extra, {}),
|
|
2062
2024
|
// A special top level value to collect errors.
|
|
@@ -2067,13 +2029,18 @@ injdef) {
|
|
|
2067
2029
|
], 1);
|
|
2068
2030
|
const meta = getprop(injdef, 'meta', {});
|
|
2069
2031
|
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false));
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2032
|
+
// A spec that names an unknown checker validates nothing.
|
|
2033
|
+
const numerrs = size(errs);
|
|
2034
|
+
_checknames(spec, store, [], errs);
|
|
2035
|
+
const out = numerrs < size(errs)
|
|
2036
|
+
? clone(data)
|
|
2037
|
+
: transform(data, spec, {
|
|
2038
|
+
meta,
|
|
2039
|
+
extra: store,
|
|
2040
|
+
modify: _validation,
|
|
2041
|
+
handler: _validatehandler,
|
|
2042
|
+
errs,
|
|
2043
|
+
});
|
|
2077
2044
|
const generr = 0 < size(errs) && !collect;
|
|
2078
2045
|
if (generr) {
|
|
2079
2046
|
throw new Error(join(errs, ' | '));
|
|
@@ -2149,11 +2116,13 @@ const select_NOT = (inj, _val, _ref, store) => {
|
|
|
2149
2116
|
}
|
|
2150
2117
|
};
|
|
2151
2118
|
const select_CMP = (inj, _val, ref, store) => {
|
|
2119
|
+
// The list form of $LIKE is validate's checker; the key form is this operator.
|
|
2120
|
+
if (M_VAL === inj.mode && '$LIKE' === ref) {
|
|
2121
|
+
return validate_LIKE(inj, _val, ref, store);
|
|
2122
|
+
}
|
|
2152
2123
|
if (M_KEYPRE === inj.mode) {
|
|
2153
2124
|
const term = _lookup(inj.parent, inj.key);
|
|
2154
|
-
// const src = getprop(store, inj.base, store)
|
|
2155
2125
|
const gkey = getelem(inj.path, -2);
|
|
2156
|
-
// const tval = getprop(src, gkey)
|
|
2157
2126
|
const ppath = slice(inj.path, -1);
|
|
2158
2127
|
const point = getpath(store, ppath);
|
|
2159
2128
|
let pass = false;
|
|
@@ -2190,10 +2159,6 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
2190
2159
|
}
|
|
2191
2160
|
return NONE;
|
|
2192
2161
|
};
|
|
2193
|
-
// Select children from a top-level object that match a MongoDB-style query.
|
|
2194
|
-
// Supports $and, $or, and equality comparisons.
|
|
2195
|
-
// For arrays, children are elements; for objects, children are values.
|
|
2196
|
-
// TODO: swap arg order for consistency
|
|
2197
2162
|
function select(children, query) {
|
|
2198
2163
|
if (!isnode(children)) {
|
|
2199
2164
|
return [];
|
|
@@ -2223,12 +2188,7 @@ function select(children, query) {
|
|
|
2223
2188
|
},
|
|
2224
2189
|
};
|
|
2225
2190
|
const q = clone(query);
|
|
2226
|
-
|
|
2227
|
-
if (ismap(v)) {
|
|
2228
|
-
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true));
|
|
2229
|
-
}
|
|
2230
|
-
return v;
|
|
2231
|
-
});
|
|
2191
|
+
_openquery(q);
|
|
2232
2192
|
for (const child of children) {
|
|
2233
2193
|
injdef.errs = [];
|
|
2234
2194
|
validate(child, clone(q), injdef);
|
|
@@ -2238,6 +2198,27 @@ function select(children, query) {
|
|
|
2238
2198
|
}
|
|
2239
2199
|
return results;
|
|
2240
2200
|
}
|
|
2201
|
+
// Open every map in a select query, so a child may hold keys the query does
|
|
2202
|
+
// not name, but leave a directive's literals as written: what $EXACT
|
|
2203
|
+
// compares, and the count or bound of $NOF, $LEN and $RANGE.
|
|
2204
|
+
function _openquery(q) {
|
|
2205
|
+
if (ismap(q)) {
|
|
2206
|
+
setprop(q, '`$OPEN`', getprop(q, '`$OPEN`', true));
|
|
2207
|
+
for (const key of keysof(q)) {
|
|
2208
|
+
_openquery(q[key]);
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
else if (islist(q)) {
|
|
2212
|
+
const head = q[0];
|
|
2213
|
+
const exact = '`$EXACT`' === head;
|
|
2214
|
+
const bounded = '`$NOF`' === head || '`$LEN`' === head || '`$RANGE`' === head;
|
|
2215
|
+
for (let i = 0; i < size(q); i++) {
|
|
2216
|
+
if (0 === i || !(exact || (bounded && 1 === i))) {
|
|
2217
|
+
_openquery(q[i]);
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2241
2222
|
// Injection state used for recursive injection into JSON - like data structures.
|
|
2242
2223
|
class Injection {
|
|
2243
2224
|
constructor(val, parent) {
|
|
@@ -2308,7 +2289,6 @@ class Injection {
|
|
|
2308
2289
|
}
|
|
2309
2290
|
}
|
|
2310
2291
|
}
|
|
2311
|
-
// TODO: is this needed?
|
|
2312
2292
|
return this.dparent;
|
|
2313
2293
|
}
|
|
2314
2294
|
child(keyI, keys) {
|
|
@@ -2344,17 +2324,11 @@ class Injection {
|
|
|
2344
2324
|
const akey = getelem(this.path, 0 - ancestor);
|
|
2345
2325
|
parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val);
|
|
2346
2326
|
}
|
|
2347
|
-
// console.log('SETVAL', val, this.key, this.parent)
|
|
2348
2327
|
return parent;
|
|
2349
2328
|
}
|
|
2350
2329
|
}
|
|
2351
2330
|
// Internal utilities
|
|
2352
2331
|
// ==================
|
|
2353
|
-
// // Update all references to target in inj.nodes.
|
|
2354
|
-
// function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
|
|
2355
|
-
// // SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
|
|
2356
|
-
// setprop(target, tkey, tval)
|
|
2357
|
-
// }
|
|
2358
2332
|
// Build a type validation error message.
|
|
2359
2333
|
function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
2360
2334
|
const vs = null == v ? 'no value' : stringify(v);
|
|
@@ -2368,13 +2342,142 @@ function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
|
2368
2342
|
// ' [' + _whence + ']' +
|
|
2369
2343
|
'.');
|
|
2370
2344
|
}
|
|
2345
|
+
// Names a validate spec may use besides its checkers: transform's that it keeps, and $OPEN.
|
|
2346
|
+
const VALIDATE_NAMES = [
|
|
2347
|
+
'$TOP',
|
|
2348
|
+
'$SPEC',
|
|
2349
|
+
'$BT',
|
|
2350
|
+
'$DS',
|
|
2351
|
+
'$WHEN',
|
|
2352
|
+
'$ANNO',
|
|
2353
|
+
'$REF',
|
|
2354
|
+
'$FORMAT',
|
|
2355
|
+
'$APPLY',
|
|
2356
|
+
'$OPEN',
|
|
2357
|
+
];
|
|
2358
|
+
// Report each checker the spec names that validate lacks; $EXACT's arguments are literals.
|
|
2359
|
+
function _checknames(spec, store, path, errs) {
|
|
2360
|
+
const name = _refname(spec);
|
|
2361
|
+
if (NONE !== name) {
|
|
2362
|
+
if (_unknownname(name, store)) {
|
|
2363
|
+
errs.push('Unknown validator ' + name + ' at field ' + pathify(path) + '.');
|
|
2364
|
+
}
|
|
2365
|
+
return;
|
|
2366
|
+
}
|
|
2367
|
+
if (islist(spec) && '$EXACT' === _refname(spec[0])) {
|
|
2368
|
+
return;
|
|
2369
|
+
}
|
|
2370
|
+
for (const key of keysof(spec)) {
|
|
2371
|
+
if (ismap(spec)) {
|
|
2372
|
+
_checknames(key, store, path, errs);
|
|
2373
|
+
}
|
|
2374
|
+
_checknames(spec[key], store, flatten([path, key]), errs);
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
// The name a whole-string reference gives, when it is a checker name in any case.
|
|
2378
|
+
function _refname(val) {
|
|
2379
|
+
const m = S_string === typeof val ? re_find(R_INJECTION_FULL, val) : null;
|
|
2380
|
+
return null != m && re_test(R_CHECKER_NAME, m[1].toUpperCase()) ? m[1] : NONE;
|
|
2381
|
+
}
|
|
2382
|
+
// Unknown: undefined and upper case, or a defined name in another case. Null disables a name.
|
|
2383
|
+
function _unknownname(name, store) {
|
|
2384
|
+
const known = (n) => VALIDATE_NAMES.includes(n) || null != _lookup(store, n);
|
|
2385
|
+
const upper = name.toUpperCase();
|
|
2386
|
+
return !known(name) && (upper === name || known(upper));
|
|
2387
|
+
}
|
|
2388
|
+
// The data value at a list directive's slot. The directive's own dparent was
|
|
2389
|
+
// read with getprop, which reports a stored null as absent.
|
|
2390
|
+
function _slotval(inj) {
|
|
2391
|
+
const prior = inj.prior;
|
|
2392
|
+
return NONE === prior ? inj.dparent : _lookup(prior.dparent, prior.key);
|
|
2393
|
+
}
|
|
2394
|
+
// JSON equality: numbers by value, lists in order, maps by key in any order.
|
|
2395
|
+
function _deepequal(a, b) {
|
|
2396
|
+
if (a === b) {
|
|
2397
|
+
return true;
|
|
2398
|
+
}
|
|
2399
|
+
const atype = typify(a);
|
|
2400
|
+
if (atype !== typify(b) || 0 === ((T_list | T_map) & atype)) {
|
|
2401
|
+
return false;
|
|
2402
|
+
}
|
|
2403
|
+
const akeys = keysof(a);
|
|
2404
|
+
const bkeys = keysof(b);
|
|
2405
|
+
if (size(akeys) !== size(bkeys)) {
|
|
2406
|
+
return false;
|
|
2407
|
+
}
|
|
2408
|
+
for (let kI = 0; kI < size(akeys); kI++) {
|
|
2409
|
+
const k = akeys[kI];
|
|
2410
|
+
if (k !== bkeys[kI] || !_deepequal(a[k], b[k])) {
|
|
2411
|
+
return false;
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
return true;
|
|
2415
|
+
}
|
|
2416
|
+
// Every member val has is in out, unchanged. A member only out has is a
|
|
2417
|
+
// default the spec supplied, and is ignored.
|
|
2418
|
+
function _keeps(val, out) {
|
|
2419
|
+
if (NONE === val) {
|
|
2420
|
+
return true;
|
|
2421
|
+
}
|
|
2422
|
+
if (0 === ((T_list | T_map) & typify(val)) || islist(val) !== islist(out) || !isnode(out)) {
|
|
2423
|
+
return _deepequal(val, out);
|
|
2424
|
+
}
|
|
2425
|
+
for (const k of keysof(val)) {
|
|
2426
|
+
if (!_keeps(val[k], _lookup(out, k))) {
|
|
2427
|
+
return false;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
return true;
|
|
2431
|
+
}
|
|
2432
|
+
// A spec admits a value when validating the value against it finds no error
|
|
2433
|
+
// and gives back the value as it is.
|
|
2434
|
+
function _admits(val, spec, inj, store) {
|
|
2435
|
+
const terrs = [];
|
|
2436
|
+
const vstore = merge([{}, store], 1);
|
|
2437
|
+
vstore.$TOP = val;
|
|
2438
|
+
const out = validate(val, spec, {
|
|
2439
|
+
extra: vstore,
|
|
2440
|
+
errs: terrs,
|
|
2441
|
+
meta: inj.meta,
|
|
2442
|
+
});
|
|
2443
|
+
return 0 === size(terrs) && _keeps(val, out);
|
|
2444
|
+
}
|
|
2445
|
+
// The bounds of a $NOF count, and the words for it, which are absent when
|
|
2446
|
+
// the count is invalid. Without a max, the bound is the number of specs.
|
|
2447
|
+
function _nofcount(count, numtvals) {
|
|
2448
|
+
const isnat = (n) => 0 < (T_integer & typify(n)) && 0 <= n;
|
|
2449
|
+
if (isnat(count)) {
|
|
2450
|
+
return [count, count, 0 === count ? 'none' : 'exactly ' + count];
|
|
2451
|
+
}
|
|
2452
|
+
const min = _lookup(count, 'min');
|
|
2453
|
+
const max = _lookup(count, 'max');
|
|
2454
|
+
const valid = ismap(count) &&
|
|
2455
|
+
0 < size(count) &&
|
|
2456
|
+
size(count) === (NONE === min ? 0 : 1) + (NONE === max ? 0 : 1) &&
|
|
2457
|
+
(NONE === min || isnat(min)) &&
|
|
2458
|
+
(NONE === max || isnat(max)) &&
|
|
2459
|
+
(NONE === min || NONE === max || min <= max);
|
|
2460
|
+
if (!valid) {
|
|
2461
|
+
return [0, 0, NONE];
|
|
2462
|
+
}
|
|
2463
|
+
const lo = NONE === min ? 0 : min;
|
|
2464
|
+
const hi = NONE === max ? numtvals : max;
|
|
2465
|
+
const desc = NONE === max
|
|
2466
|
+
? 'at least ' + lo
|
|
2467
|
+
: 0 === hi
|
|
2468
|
+
? 'none'
|
|
2469
|
+
: NONE === min
|
|
2470
|
+
? 'at most ' + hi
|
|
2471
|
+
: lo === hi
|
|
2472
|
+
? 'exactly ' + lo
|
|
2473
|
+
: lo + ' to ' + hi;
|
|
2474
|
+
return [lo, hi, desc];
|
|
2475
|
+
}
|
|
2371
2476
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
2372
2477
|
// call the function passing the injection inj. This is how transforms operate.
|
|
2373
2478
|
const _injecthandler = (inj, val, ref, store) => {
|
|
2374
2479
|
let out = val;
|
|
2375
2480
|
const iscmd = isfunc(val) && (NONE === ref || ref.startsWith(S_DS));
|
|
2376
|
-
// Only call val function if it is a special command ($NAME format).
|
|
2377
|
-
// TODO: OR if meta.'$CALL'
|
|
2378
2481
|
if (iscmd) {
|
|
2379
2482
|
out = val(inj, val, ref, store);
|
|
2380
2483
|
}
|
|
@@ -2403,22 +2506,11 @@ const _validatehandler = (inj, val, ref, store) => {
|
|
|
2403
2506
|
}
|
|
2404
2507
|
return out;
|
|
2405
2508
|
};
|
|
2406
|
-
// Inject values from a data store into a string. Not a public utility - used by
|
|
2407
|
-
// `inject`. Inject are marked with `path` where path is resolved
|
|
2408
|
-
// with getpath against the store or current (if defined)
|
|
2409
|
-
// arguments. See `getpath`. Custom injection handling can be
|
|
2410
|
-
// provided by inj.handler (this is used for transform functions).
|
|
2411
|
-
// The path can also have the special syntax $NAME999 where NAME is
|
|
2412
|
-
// upper case letters only, and 999 is any digits, which are
|
|
2413
|
-
// discarded. This syntax specifies the name of a transform, and
|
|
2414
|
-
// optionally allows transforms to be ordered by alphanumeric sorting.
|
|
2415
2509
|
function _injectstr(val, store, inj) {
|
|
2416
|
-
// Can't inject into non-strings
|
|
2417
2510
|
if (S_string !== typeof val || S_MT === val) {
|
|
2418
2511
|
return S_MT;
|
|
2419
2512
|
}
|
|
2420
2513
|
let out = val;
|
|
2421
|
-
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
2422
2514
|
const m = re_find(R_INJECTION_FULL, val);
|
|
2423
2515
|
// Full string of the val is an injection.
|
|
2424
2516
|
if (m) {
|
|
@@ -2426,17 +2518,14 @@ function _injectstr(val, store, inj) {
|
|
|
2426
2518
|
inj.full = true;
|
|
2427
2519
|
}
|
|
2428
2520
|
let pathref = m[1];
|
|
2429
|
-
// Special escapes inside injection.
|
|
2430
2521
|
if (3 < size(pathref)) {
|
|
2431
2522
|
pathref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, pathref, S_BT), S_DS);
|
|
2432
2523
|
}
|
|
2433
|
-
// Get the extracted path reference.
|
|
2434
2524
|
out = getpath(store, pathref, inj);
|
|
2435
2525
|
}
|
|
2436
2526
|
else {
|
|
2437
2527
|
// Check for injections within the string.
|
|
2438
2528
|
const partial = (_m, ref) => {
|
|
2439
|
-
// Special escapes inside injection.
|
|
2440
2529
|
if (3 < size(ref)) {
|
|
2441
2530
|
ref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, ref, S_BT), S_DS);
|
|
2442
2531
|
}
|
|
@@ -2444,7 +2533,6 @@ function _injectstr(val, store, inj) {
|
|
|
2444
2533
|
inj.full = false;
|
|
2445
2534
|
}
|
|
2446
2535
|
const found = getpath(store, ref, inj);
|
|
2447
|
-
// Ensure inject value is a string.
|
|
2448
2536
|
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
2449
2537
|
};
|
|
2450
2538
|
out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]));
|
|
@@ -2496,13 +2584,11 @@ function checkPlacement(modes, ijname, parentTypes, inj) {
|
|
|
2496
2584
|
}
|
|
2497
2585
|
return true;
|
|
2498
2586
|
}
|
|
2499
|
-
// function injectorArgs(argTypes: number[], inj: Injection): any {
|
|
2500
2587
|
function injectorArgs(argTypes, args) {
|
|
2501
2588
|
const numargs = size(argTypes);
|
|
2502
2589
|
const found = new Array(1 + numargs);
|
|
2503
2590
|
found[0] = NONE;
|
|
2504
2591
|
for (let argI = 0; argI < numargs; argI++) {
|
|
2505
|
-
// const arg = inj.parent[1 + argI]
|
|
2506
2592
|
const arg = args[argI];
|
|
2507
2593
|
const argType = typify(arg);
|
|
2508
2594
|
if (0 === (argTypes[argI] & argType)) {
|
|
@@ -2537,7 +2623,6 @@ function injectChild(child, store, inj) {
|
|
|
2537
2623
|
setprop(cinj.parent, inj.key, child);
|
|
2538
2624
|
}
|
|
2539
2625
|
}
|
|
2540
|
-
// console.log('FORMAT-INJECT-CHILD', child)
|
|
2541
2626
|
inject(child, store, cinj);
|
|
2542
2627
|
return cinj;
|
|
2543
2628
|
}
|
|
@@ -2616,56 +2701,6 @@ class StructUtility {
|
|
|
2616
2701
|
}
|
|
2617
2702
|
}
|
|
2618
2703
|
exports.StructUtility = StructUtility;
|
|
2619
|
-
// ---------------------------------------------------------------------------
|
|
2620
|
-
// CONDENSED STRUCTURES
|
|
2621
|
-
//
|
|
2622
|
-
// A condensed structure holds the same information as a JSON-shaped node in a
|
|
2623
|
-
// fraction of the space, and can be read WITHOUT materialising it. Three
|
|
2624
|
-
// mechanisms, all in one format:
|
|
2625
|
-
//
|
|
2626
|
-
// intern a symbol table holds each distinct map key and string value once
|
|
2627
|
-
// share identical subtrees become ONE node with many references (a DAG)
|
|
2628
|
-
// lazy a reader resolves a path by walking integer indexes, touching
|
|
2629
|
-
// only the nodes on that path
|
|
2630
|
-
//
|
|
2631
|
-
// The shape:
|
|
2632
|
-
//
|
|
2633
|
-
// { "$condense": 1, format version
|
|
2634
|
-
// "sym": [ ... ], distinct strings, sorted (so lookup can bisect)
|
|
2635
|
-
// "node": [ ... ], integer-encoded nodes, children before parents
|
|
2636
|
-
// "root": 12 } index into node[]
|
|
2637
|
-
//
|
|
2638
|
-
// Each node is one of:
|
|
2639
|
-
//
|
|
2640
|
-
// map [0, k0, v0, k1, v1, ...] k ascending symbol ids, v node refs
|
|
2641
|
-
// list [1, v0, v1, ...] node refs
|
|
2642
|
-
// string [2, symId]
|
|
2643
|
-
// number [3, value]
|
|
2644
|
-
// true [4] false [5] null [6]
|
|
2645
|
-
//
|
|
2646
|
-
// Two properties are load-bearing rather than incidental:
|
|
2647
|
-
//
|
|
2648
|
-
// Map keys ascend, because keysof() sorts. That makes output byte-stable
|
|
2649
|
-
// (generators depend on it) and lets a key lookup bisect rather than scan.
|
|
2650
|
-
//
|
|
2651
|
-
// References are integer indexes into a post-order table, so every ref
|
|
2652
|
-
// points BACKWARDS. Sharing is therefore free — two identical subtrees
|
|
2653
|
-
// intern to one node and two refs, with no dedup pass at read time — and a
|
|
2654
|
-
// reader can never loop.
|
|
2655
|
-
//
|
|
2656
|
-
// The transport is JSON arrays of integers on purpose. The FORMAT is defined
|
|
2657
|
-
// here, but TOKENIZING is left to each language's own JSON parser: mature,
|
|
2658
|
-
// fast, memory-safe, already present, and nothing to keep in parity across
|
|
2659
|
-
// ports. A binary profile ($condense: 2) is a future option, deliberately
|
|
2660
|
-
// deferred - it is worth perhaps 2-3x and costs a hand-written decoder per
|
|
2661
|
-
// language.
|
|
2662
|
-
//
|
|
2663
|
-
// IMMUTABILITY. Sharing makes a condensed structure a DAG, so a mutation
|
|
2664
|
-
// through one reference would be visible through every other. Rather than
|
|
2665
|
-
// make that hazard the caller's problem, it is designed out: mutating
|
|
2666
|
-
// helpers RAISE on a condensed node, and expand()/value() always return an
|
|
2667
|
-
// independent copy. ref() is the one opt-in escape hatch and is named to be
|
|
2668
|
-
// alarming.
|
|
2669
2704
|
const CONDENSE_VERSION = 1;
|
|
2670
2705
|
const CONDENSE_MAP = 0;
|
|
2671
2706
|
const CONDENSE_LIST = 1;
|
|
@@ -2678,18 +2713,6 @@ const S_condense = '$condense';
|
|
|
2678
2713
|
const S_condsym = 'sym';
|
|
2679
2714
|
const S_condnode = 'node';
|
|
2680
2715
|
const S_condroot = 'root';
|
|
2681
|
-
// Is this a condensed structure? Checked structurally rather than by a marker
|
|
2682
|
-
// alone, so an ordinary map that happens to carry a `$condense` key is not
|
|
2683
|
-
// mistaken for one.
|
|
2684
|
-
// Compare by UNICODE CODE POINT, not by UTF-16 code unit.
|
|
2685
|
-
//
|
|
2686
|
-
// JavaScript's default string sort compares UTF-16 code units, which orders
|
|
2687
|
-
// an astral character BEFORE a high-BMP one; Python, Go and Rust all compare
|
|
2688
|
-
// by code point (equivalently, by UTF-8 bytes). A symbol table sorted the
|
|
2689
|
-
// JavaScript way would make two conforming ports emit different bytes for the
|
|
2690
|
-
// same input, which is exactly the byte-stability the format promises. Code
|
|
2691
|
-
// point order is the portable choice, so it is specified rather than
|
|
2692
|
-
// inherited.
|
|
2693
2716
|
function condenseCmp(a, b) {
|
|
2694
2717
|
let i = 0;
|
|
2695
2718
|
let j = 0;
|
|
@@ -2704,25 +2727,6 @@ function condenseCmp(a, b) {
|
|
|
2704
2727
|
}
|
|
2705
2728
|
return a.length - i - (b.length - j);
|
|
2706
2729
|
}
|
|
2707
|
-
// Assign a key that may be `__proto__`.
|
|
2708
|
-
//
|
|
2709
|
-
// Plain assignment to `__proto__` invokes the inherited setter instead of
|
|
2710
|
-
// creating an own property, so a model carrying that key or string value
|
|
2711
|
-
// would be silently corrupted - it is valid JSON and Seneca-adjacent data
|
|
2712
|
-
// really does use `$`-ish and `__`-ish names.
|
|
2713
|
-
function condenseSet(obj, key, val) {
|
|
2714
|
-
if ('__proto__' === key) {
|
|
2715
|
-
Object.defineProperty(obj, key, {
|
|
2716
|
-
value: val,
|
|
2717
|
-
enumerable: true,
|
|
2718
|
-
writable: true,
|
|
2719
|
-
configurable: true,
|
|
2720
|
-
});
|
|
2721
|
-
}
|
|
2722
|
-
else {
|
|
2723
|
-
obj[key] = val;
|
|
2724
|
-
}
|
|
2725
|
-
}
|
|
2726
2730
|
function iscondensed(val) {
|
|
2727
2731
|
// The version must MATCH, not merely be numeric: a future profile would
|
|
2728
2732
|
// otherwise be decoded under this one's schema.
|
|
@@ -2781,8 +2785,8 @@ function condense(val) {
|
|
|
2781
2785
|
const build = (n) => {
|
|
2782
2786
|
if (ismap(n)) {
|
|
2783
2787
|
const enc = [CONDENSE_MAP];
|
|
2784
|
-
// keysof
|
|
2785
|
-
const keys = keysof(n);
|
|
2788
|
+
// Code-point order, not keysof's code-unit order, so the key ids ascend for bisection.
|
|
2789
|
+
const keys = keysof(n).sort(condenseCmp);
|
|
2786
2790
|
for (let i = 0; i < keys.length; i++) {
|
|
2787
2791
|
enc.push(symid[keys[i]]);
|
|
2788
2792
|
enc.push(build(n[keys[i]]));
|
|
@@ -2842,7 +2846,7 @@ function condenseSymId(cond, key) {
|
|
|
2842
2846
|
// The node index of `key` inside node `idx`, or -1. Touches only this node.
|
|
2843
2847
|
function condenseChild(cond, idx, key) {
|
|
2844
2848
|
const enc = cond[S_condnode][idx];
|
|
2845
|
-
if (null == enc) {
|
|
2849
|
+
if (null == enc || (S_string !== typeof key && S_number !== typeof key)) {
|
|
2846
2850
|
return -1;
|
|
2847
2851
|
}
|
|
2848
2852
|
if (CONDENSE_MAP === enc[0]) {
|
|
@@ -2907,13 +2911,6 @@ function condenseResolve(cond, idx, path) {
|
|
|
2907
2911
|
}
|
|
2908
2912
|
return at;
|
|
2909
2913
|
}
|
|
2910
|
-
// Materialise node `idx` as an ordinary JSON-shaped value.
|
|
2911
|
-
//
|
|
2912
|
-
// Containers are rebuilt on every call and never memoised: sharing is an
|
|
2913
|
-
// implementation detail, and handing the same object out twice would let a
|
|
2914
|
-
// caller mutating one reference corrupt every other. The cost is that
|
|
2915
|
-
// expansion reproduces the original tree - which is exactly the size it was
|
|
2916
|
-
// before condensing, so it is bounded by the input.
|
|
2917
2914
|
function condenseMaterialise(cond, idx) {
|
|
2918
2915
|
const sym = cond[S_condsym];
|
|
2919
2916
|
const node = cond[S_condnode];
|
|
@@ -2926,7 +2923,7 @@ function condenseMaterialise(cond, idx) {
|
|
|
2926
2923
|
if (CONDENSE_MAP === kind) {
|
|
2927
2924
|
const out = {};
|
|
2928
2925
|
for (let j = 1; j < enc.length; j += 2) {
|
|
2929
|
-
|
|
2926
|
+
_setown(out, sym[enc[j]], rebuild(enc[j + 1]));
|
|
2930
2927
|
}
|
|
2931
2928
|
return out;
|
|
2932
2929
|
}
|