@voxgig/struct 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/StructUtility.d.ts +2 -8
- package/dist/StructUtility.js +28 -357
- package/dist/StructUtility.js.map +1 -1
- package/package.json +2 -2
- package/src/StructUtility.ts +47 -407
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,22 @@ 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-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 R_BT_ESCAPE = /\$BT/g;
|
|
237
|
-
const R_DS_ESCAPE = /\$DS/g;
|
|
238
|
-
const R_INJECTION_PARTIAL = /`([^`]+)`/g;
|
|
239
|
-
// Default max depth (for walk etc).
|
|
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_BT_ESCAPE = /\$BT/g;
|
|
180
|
+
const R_DS_ESCAPE = /\$DS/g;
|
|
181
|
+
const R_INJECTION_PARTIAL = /`([^`]+)`/g;
|
|
240
182
|
const MAXDEPTH = 32;
|
|
241
183
|
// Return type string for narrowest type.
|
|
242
184
|
function typename(t) {
|
|
@@ -275,7 +217,6 @@ function isempty(val) {
|
|
|
275
217
|
(Array.isArray(val) && 0 === val.length) ||
|
|
276
218
|
(S_object === typeof val && 0 === Object.keys(val).length));
|
|
277
219
|
}
|
|
278
|
-
// Value is a function.
|
|
279
220
|
function isfunc(val) {
|
|
280
221
|
return S_function === typeof val;
|
|
281
222
|
}
|
|
@@ -302,13 +243,6 @@ function size(val) {
|
|
|
302
243
|
return 0;
|
|
303
244
|
}
|
|
304
245
|
}
|
|
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
246
|
function slice(val, start, end, mutate) {
|
|
313
247
|
if (S_number === typeof val) {
|
|
314
248
|
start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start;
|
|
@@ -371,7 +305,6 @@ function slice(val, start, end, mutate) {
|
|
|
371
305
|
}
|
|
372
306
|
return val;
|
|
373
307
|
}
|
|
374
|
-
// String padding.
|
|
375
308
|
function pad(str, padding, padchar) {
|
|
376
309
|
str = S_string === typeof str ? str : stringify(str);
|
|
377
310
|
padding = null == padding ? 44 : padding;
|
|
@@ -522,11 +455,6 @@ function items(val, apply) {
|
|
|
522
455
|
}
|
|
523
456
|
return out;
|
|
524
457
|
}
|
|
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
458
|
function flatten(list, depth) {
|
|
531
459
|
if (!islist(list)) {
|
|
532
460
|
return list;
|
|
@@ -545,7 +473,6 @@ function filter(val, check) {
|
|
|
545
473
|
}
|
|
546
474
|
return out;
|
|
547
475
|
}
|
|
548
|
-
// Escape regular expression.
|
|
549
476
|
function escre(s) {
|
|
550
477
|
return re_replace(R_ESCAPE_REGEXP, s, '\\$&');
|
|
551
478
|
}
|
|
@@ -553,7 +480,6 @@ function escre(s) {
|
|
|
553
480
|
function re_escape(s) {
|
|
554
481
|
return escre(s);
|
|
555
482
|
}
|
|
556
|
-
// Escape URLs.
|
|
557
483
|
function escurl(s) {
|
|
558
484
|
s = null == s ? S_MT : s;
|
|
559
485
|
return encodeURIComponent(s);
|
|
@@ -563,8 +489,6 @@ function escurl(s) {
|
|
|
563
489
|
// dialect is the RE2 subset documented in /REGEX.md. Internal library code
|
|
564
490
|
// uses these helpers instead of host-language regex methods directly.
|
|
565
491
|
// ===========================================================================
|
|
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
492
|
function re_compile(pattern, flags) {
|
|
569
493
|
if (pattern instanceof RegExp) {
|
|
570
494
|
return pattern;
|
|
@@ -615,7 +539,6 @@ function re_replace(pattern, input, replacement) {
|
|
|
615
539
|
}
|
|
616
540
|
return input.replace(re, replacement);
|
|
617
541
|
}
|
|
618
|
-
// Boolean test for any match.
|
|
619
542
|
function re_test(pattern, input) {
|
|
620
543
|
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
|
621
544
|
return re.test(input);
|
|
@@ -662,9 +585,6 @@ function join(arr, sep, url) {
|
|
|
662
585
|
}), (n) => S_MT !== n[1]).join(sepdef);
|
|
663
586
|
return out;
|
|
664
587
|
}
|
|
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
588
|
function jsonify(val, flags) {
|
|
669
589
|
let str = S_null;
|
|
670
590
|
if (null != val) {
|
|
@@ -834,25 +754,10 @@ function jt(...v) {
|
|
|
834
754
|
}
|
|
835
755
|
return a;
|
|
836
756
|
}
|
|
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
757
|
function delprop(parent, key) {
|
|
844
758
|
if (!iskey(key)) {
|
|
845
759
|
return parent;
|
|
846
760
|
}
|
|
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
761
|
if (iscondensed(parent)) {
|
|
857
762
|
throw new Error('struct: condensed structures are immutable - ' + 'expand() first, then modify the copy');
|
|
858
763
|
}
|
|
@@ -861,7 +766,6 @@ function delprop(parent, key) {
|
|
|
861
766
|
delete parent[key];
|
|
862
767
|
}
|
|
863
768
|
else if (islist(parent)) {
|
|
864
|
-
// Ensure key is an integer.
|
|
865
769
|
let keyI = +key;
|
|
866
770
|
if (isNaN(keyI)) {
|
|
867
771
|
return parent;
|
|
@@ -887,15 +791,6 @@ function setprop(parent, key, val) {
|
|
|
887
791
|
if (!iskey(key)) {
|
|
888
792
|
return parent;
|
|
889
793
|
}
|
|
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
794
|
if (iscondensed(parent)) {
|
|
900
795
|
throw new Error('struct: condensed structures are immutable - ' + 'expand() first, then modify the copy');
|
|
901
796
|
}
|
|
@@ -905,41 +800,22 @@ function setprop(parent, key, val) {
|
|
|
905
800
|
pany[key] = val;
|
|
906
801
|
}
|
|
907
802
|
else if (islist(parent)) {
|
|
908
|
-
// Ensure key is an integer.
|
|
909
803
|
let keyI = +key;
|
|
910
804
|
if (isNaN(keyI)) {
|
|
911
805
|
return parent;
|
|
912
806
|
}
|
|
913
807
|
keyI = Math.floor(keyI);
|
|
914
|
-
// TODO: DELETE list element
|
|
915
808
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
916
809
|
if (0 <= keyI) {
|
|
917
810
|
parent[slice(keyI, 0, size(parent) + 1)] = val;
|
|
918
811
|
}
|
|
919
|
-
// Prepend value if keyI is negative
|
|
920
812
|
else {
|
|
921
813
|
parent.unshift(val);
|
|
922
814
|
}
|
|
923
815
|
}
|
|
924
816
|
return parent;
|
|
925
817
|
}
|
|
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) {
|
|
818
|
+
function walk(val, before, after, maxdepth, key, parent, path, pool) {
|
|
943
819
|
if (NONE === pool) {
|
|
944
820
|
pool = [[]];
|
|
945
821
|
}
|
|
@@ -981,12 +857,10 @@ key, parent, path, pool) {
|
|
|
981
857
|
// Merge a list of values into each other. Later values have
|
|
982
858
|
// precedence. Nodes override scalars. Node kinds (list or map)
|
|
983
859
|
// override each other, and do *not* merge. The first element is
|
|
984
|
-
// modified.
|
|
860
|
+
// modified; the others are not.
|
|
985
861
|
function merge(val, maxdepth) {
|
|
986
|
-
// const md: number = null == maxdepth ? MAXDEPTH : maxdepth < 0 ? 0 : maxdepth
|
|
987
862
|
const md = slice(maxdepth ?? MAXDEPTH, 0);
|
|
988
863
|
let out = NONE;
|
|
989
|
-
// Handle edge cases.
|
|
990
864
|
if (!islist(val)) {
|
|
991
865
|
return val;
|
|
992
866
|
}
|
|
@@ -998,12 +872,10 @@ function merge(val, maxdepth) {
|
|
|
998
872
|
else if (1 === lenlist) {
|
|
999
873
|
return list[0];
|
|
1000
874
|
}
|
|
1001
|
-
// Merge a list of values.
|
|
1002
875
|
out = getprop(list, 0, {});
|
|
1003
876
|
for (let oI = 1; oI < lenlist; oI++) {
|
|
1004
877
|
const obj = list[oI];
|
|
1005
878
|
if (!isnode(obj)) {
|
|
1006
|
-
// Nodes win.
|
|
1007
879
|
out = obj;
|
|
1008
880
|
}
|
|
1009
881
|
else {
|
|
@@ -1016,7 +888,6 @@ function merge(val, maxdepth) {
|
|
|
1016
888
|
if (md <= pI) {
|
|
1017
889
|
setprop(cur[pI - 1], key, val);
|
|
1018
890
|
}
|
|
1019
|
-
// Scalars just override directly.
|
|
1020
891
|
else if (!isnode(val)) {
|
|
1021
892
|
cur[pI] = val;
|
|
1022
893
|
}
|
|
@@ -1026,38 +897,35 @@ function merge(val, maxdepth) {
|
|
|
1026
897
|
dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI];
|
|
1027
898
|
const tval = dst[pI];
|
|
1028
899
|
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
900
|
// Matching override and destination so continue with their values.
|
|
1034
|
-
|
|
901
|
+
if (vtype === typify(tval)) {
|
|
1035
902
|
cur[pI] = tval;
|
|
1036
903
|
}
|
|
1037
|
-
//
|
|
904
|
+
// Otherwise the override wins: a plain node is copied, taking
|
|
905
|
+
// nothing from the destination, so no later merge writes into it.
|
|
906
|
+
else if (0 === (T_instance & vtype)) {
|
|
907
|
+
cur[pI] = islist(val) ? [] : {};
|
|
908
|
+
dst[pI] = NONE;
|
|
909
|
+
}
|
|
1038
910
|
else {
|
|
1039
911
|
cur[pI] = val;
|
|
1040
|
-
//
|
|
912
|
+
// A class instance is kept as is, so there is nothing to descend.
|
|
1041
913
|
val = NONE;
|
|
1042
914
|
}
|
|
1043
915
|
}
|
|
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
916
|
return val;
|
|
1048
917
|
}
|
|
1049
|
-
function after(key, _val,
|
|
918
|
+
function after(key, _val, parent, path) {
|
|
1050
919
|
const cI = size(path);
|
|
1051
920
|
const target = cur[cI - 1];
|
|
1052
921
|
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
922
|
setprop(target, key, value);
|
|
1056
|
-
|
|
923
|
+
// walk writes this back into the override, so below the root it is
|
|
924
|
+
// the override's own child (_lookup keeps a null, getprop would not).
|
|
925
|
+
return 0 === cI ? value : _lookup(parent, key);
|
|
1057
926
|
}
|
|
1058
927
|
// Walk overriding node, creating paths in output as needed.
|
|
1059
928
|
out = walk(obj, before, after, maxdepth);
|
|
1060
|
-
// console.log('WALK-DONE', out, obj)
|
|
1061
929
|
}
|
|
1062
930
|
}
|
|
1063
931
|
if (0 === md) {
|
|
@@ -1105,7 +973,6 @@ function setpath(store, path, val, injdef) {
|
|
|
1105
973
|
return parent;
|
|
1106
974
|
}
|
|
1107
975
|
function getpath(store, path, injdef) {
|
|
1108
|
-
// Operate on a string array.
|
|
1109
976
|
const parts = islist(path)
|
|
1110
977
|
? path
|
|
1111
978
|
: 'string' === typeof path
|
|
@@ -1135,7 +1002,6 @@ function getpath(store, path, injdef) {
|
|
|
1135
1002
|
}
|
|
1136
1003
|
store = expand(store);
|
|
1137
1004
|
}
|
|
1138
|
-
// let root = store
|
|
1139
1005
|
let val = store;
|
|
1140
1006
|
const base = getprop(injdef, S_base);
|
|
1141
1007
|
const src = getprop(store, base, store);
|
|
@@ -1146,7 +1012,6 @@ function getpath(store, path, injdef) {
|
|
|
1146
1012
|
val = src;
|
|
1147
1013
|
}
|
|
1148
1014
|
else if (0 < numparts) {
|
|
1149
|
-
// Check for $ACTIONs
|
|
1150
1015
|
if (1 === numparts) {
|
|
1151
1016
|
val = getprop(store, parts[0]);
|
|
1152
1017
|
}
|
|
@@ -1193,7 +1058,6 @@ function getpath(store, path, injdef) {
|
|
|
1193
1058
|
val = dparent;
|
|
1194
1059
|
}
|
|
1195
1060
|
else {
|
|
1196
|
-
// const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
1197
1061
|
const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)]);
|
|
1198
1062
|
if (ascends <= size(dpath)) {
|
|
1199
1063
|
val = getpath(store, fullpath);
|
|
@@ -1220,20 +1084,14 @@ function getpath(store, path, injdef) {
|
|
|
1220
1084
|
const ref = pathify(path);
|
|
1221
1085
|
val = handler(injdef, val, ref, store);
|
|
1222
1086
|
}
|
|
1223
|
-
// console.log('GETPATH', path, val)
|
|
1224
1087
|
return val;
|
|
1225
1088
|
}
|
|
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
1089
|
function inject(val, store, injdef) {
|
|
1231
1090
|
const valtype = typeof val;
|
|
1232
1091
|
let inj = injdef;
|
|
1233
1092
|
// Create state if at root of injection. The input value is placed
|
|
1234
1093
|
// inside a virtual parent holder to simplify edge cases.
|
|
1235
1094
|
if (NONE === injdef || null == injdef.mode) {
|
|
1236
|
-
// Set up state assuming we are starting in the virtual parent.
|
|
1237
1095
|
inj = new Injection(val, { [S_DTOP]: val });
|
|
1238
1096
|
inj.dparent = store;
|
|
1239
1097
|
inj.errs = getprop(store, S_DERRS, []);
|
|
@@ -1248,12 +1106,7 @@ function inject(val, store, injdef) {
|
|
|
1248
1106
|
inj.descend();
|
|
1249
1107
|
// console.log('INJ-START', val, inj.mode, inj.key, inj.val,
|
|
1250
1108
|
// 't=', inj.path, 'P=', inj.parent, 'dp=', inj.dparent, 'ST=', store.$TOP)
|
|
1251
|
-
// Descend into node.
|
|
1252
1109
|
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
1110
|
let nodekeys;
|
|
1258
1111
|
nodekeys = keysof(val);
|
|
1259
1112
|
if (ismap(val)) {
|
|
@@ -1265,10 +1118,6 @@ function inject(val, store, injdef) {
|
|
|
1265
1118
|
else {
|
|
1266
1119
|
nodekeys = keysof(val);
|
|
1267
1120
|
}
|
|
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
1121
|
for (let nkI = 0; nkI < nodekeys.length; nkI++) {
|
|
1273
1122
|
const childinj = inj.child(nkI, nodekeys);
|
|
1274
1123
|
const nodekey = childinj.key;
|
|
@@ -1297,7 +1146,6 @@ function inject(val, store, injdef) {
|
|
|
1297
1146
|
}
|
|
1298
1147
|
}
|
|
1299
1148
|
}
|
|
1300
|
-
// Inject paths into string scalars.
|
|
1301
1149
|
else if (S_string === valtype) {
|
|
1302
1150
|
inj.mode = M_VAL;
|
|
1303
1151
|
val = _injectstr(val, store, inj);
|
|
@@ -1305,26 +1153,20 @@ function inject(val, store, injdef) {
|
|
|
1305
1153
|
inj.setval(val);
|
|
1306
1154
|
}
|
|
1307
1155
|
}
|
|
1308
|
-
// Custom modification.
|
|
1309
1156
|
if (inj.modify && SKIP !== val) {
|
|
1310
1157
|
const mkey = inj.key;
|
|
1311
1158
|
const mparent = inj.parent;
|
|
1312
1159
|
const mval = getprop(mparent, mkey);
|
|
1313
1160
|
inj.modify(mval, mkey, mparent, inj, store);
|
|
1314
1161
|
}
|
|
1315
|
-
// console.log('INJ-VAL', val)
|
|
1316
1162
|
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
1163
|
return _lookup(inj.parent, S_DTOP);
|
|
1320
1164
|
}
|
|
1321
1165
|
// The transform_* functions are special command inject handlers (see Injector).
|
|
1322
|
-
// Delete a key from a map or list.
|
|
1323
1166
|
const transform_DELETE = (inj) => {
|
|
1324
1167
|
inj.setval(NONE);
|
|
1325
1168
|
return NONE;
|
|
1326
1169
|
};
|
|
1327
|
-
// Copy value from source data.
|
|
1328
1170
|
const transform_COPY = (inj, _val) => {
|
|
1329
1171
|
const ijname = 'COPY';
|
|
1330
1172
|
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
@@ -1348,8 +1190,6 @@ const transform_KEY = (inj) => {
|
|
|
1348
1190
|
delprop(parent, S_BKEY);
|
|
1349
1191
|
return getprop(inj.dparent, keyspec);
|
|
1350
1192
|
}
|
|
1351
|
-
// Key is defined within general purpose $META object.
|
|
1352
|
-
// return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1353
1193
|
return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2);
|
|
1354
1194
|
};
|
|
1355
1195
|
// Annotate node. Does nothing itself, just used by
|
|
@@ -1378,8 +1218,6 @@ const transform_MERGE = (inj) => {
|
|
|
1378
1218
|
args = Array.isArray(args) ? args : [args];
|
|
1379
1219
|
// Remove the $MERGE command from a parent map.
|
|
1380
1220
|
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
1221
|
const mergelist = flatten([[parent], args, [clone(parent)]]);
|
|
1384
1222
|
merge(mergelist);
|
|
1385
1223
|
}
|
|
@@ -1394,13 +1232,11 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1394
1232
|
}
|
|
1395
1233
|
// Remove remaining keys to avoid spurious processing.
|
|
1396
1234
|
slice(inj.keys, 0, 1, true);
|
|
1397
|
-
// const [err, srcpath, child] = injectorArgs([T_string, T_any], inj)
|
|
1398
1235
|
const [err, srcpath, child] = injectorArgs([T_string, T_any], slice(inj.parent, 1));
|
|
1399
1236
|
if (NONE !== err) {
|
|
1400
1237
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
1401
1238
|
return NONE;
|
|
1402
1239
|
}
|
|
1403
|
-
// Source data.
|
|
1404
1240
|
const srcstore = getprop(store, inj.base, store);
|
|
1405
1241
|
const src = getpath(srcstore, srcpath, inj);
|
|
1406
1242
|
const srctype = typify(src);
|
|
@@ -1427,7 +1263,6 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1427
1263
|
const ckey = getelem(inj.path, -2);
|
|
1428
1264
|
const tpath = slice(inj.path, -1);
|
|
1429
1265
|
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey]);
|
|
1430
|
-
// Parent structure.
|
|
1431
1266
|
tcur = { [ckey]: tcur };
|
|
1432
1267
|
if (1 < size(tpath)) {
|
|
1433
1268
|
const pkey = getelem(inj.path, -3, S_DTOP);
|
|
@@ -1445,9 +1280,7 @@ const transform_EACH = (inj, _val, _ref, store) => {
|
|
|
1445
1280
|
inject(tval, store, tinj);
|
|
1446
1281
|
rval = tinj.val;
|
|
1447
1282
|
}
|
|
1448
|
-
// _updateAncestors(inj, target, tkey, rval)
|
|
1449
1283
|
setprop(target, tkey, rval);
|
|
1450
|
-
// Prevent callee from damaging first list entry (since we are in `val` mode).
|
|
1451
1284
|
return rval[0];
|
|
1452
1285
|
};
|
|
1453
1286
|
// Convert a node to a map.
|
|
@@ -1458,21 +1291,17 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1458
1291
|
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1459
1292
|
return NONE;
|
|
1460
1293
|
}
|
|
1461
|
-
// Get arguments.
|
|
1462
1294
|
const args = getprop(parent, key);
|
|
1463
1295
|
const [err, srcpath, origchildspec] = injectorArgs([T_string, T_any], args);
|
|
1464
1296
|
if (NONE !== err) {
|
|
1465
1297
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
1466
1298
|
return NONE;
|
|
1467
1299
|
}
|
|
1468
|
-
// Find key and target node.
|
|
1469
1300
|
const tkey = getelem(path, -2);
|
|
1470
1301
|
const pathsize = size(path);
|
|
1471
1302
|
const target = getelem(nodes, pathsize - 2, () => getelem(nodes, pathsize - 1));
|
|
1472
|
-
// Source data
|
|
1473
1303
|
const srcstore = getprop(store, inj.base, store);
|
|
1474
1304
|
let src = getpath(srcstore, srcpath, inj);
|
|
1475
|
-
// Prepare source as a list.
|
|
1476
1305
|
if (!islist(src)) {
|
|
1477
1306
|
if (ismap(src)) {
|
|
1478
1307
|
src = items(src, (item) => {
|
|
@@ -1487,11 +1316,9 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1487
1316
|
if (null == src) {
|
|
1488
1317
|
return NONE;
|
|
1489
1318
|
}
|
|
1490
|
-
// Get keypath.
|
|
1491
1319
|
const keypath = getprop(origchildspec, S_BKEY);
|
|
1492
1320
|
const childspec = delprop(origchildspec, S_BKEY);
|
|
1493
1321
|
const child = getprop(childspec, S_BVAL, childspec);
|
|
1494
|
-
// Build parallel target object.
|
|
1495
1322
|
const tval = {};
|
|
1496
1323
|
items(src, (item) => {
|
|
1497
1324
|
const srckey = item[0];
|
|
@@ -1517,7 +1344,6 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1517
1344
|
});
|
|
1518
1345
|
let rval = {};
|
|
1519
1346
|
if (!isempty(tval)) {
|
|
1520
|
-
// Build parallel source object.
|
|
1521
1347
|
const tsrc = {};
|
|
1522
1348
|
src.reduce((a, n, i) => {
|
|
1523
1349
|
const kn = null == keypath
|
|
@@ -1547,14 +1373,9 @@ const transform_PACK = (inj, _val, _ref, store) => {
|
|
|
1547
1373
|
inject(tval, store, tinj);
|
|
1548
1374
|
rval = tinj.val;
|
|
1549
1375
|
}
|
|
1550
|
-
// _updateAncestors(inj, target, tkey, rval)
|
|
1551
1376
|
setprop(target, tkey, rval);
|
|
1552
|
-
// Drop transform key.
|
|
1553
1377
|
return NONE;
|
|
1554
1378
|
};
|
|
1555
|
-
// TODO: not found ref should removed key (setprop NONE)
|
|
1556
|
-
// Reference original spec (enables recursice transformations)
|
|
1557
|
-
// Format: ['`$REF`', '`spec-path`']
|
|
1558
1379
|
const transform_REF = (inj, val, _ref, store) => {
|
|
1559
1380
|
const { nodes } = inj;
|
|
1560
1381
|
if (M_VAL !== inj.mode) {
|
|
@@ -1563,12 +1384,9 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1563
1384
|
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1564
1385
|
const refpath = _lookup(inj.parent, 1);
|
|
1565
1386
|
inj.keyI = size(inj.keys);
|
|
1566
|
-
// Spec reference.
|
|
1567
1387
|
const spec = getprop(store, S_DSPEC)();
|
|
1568
1388
|
const dpath = slice(inj.path, 1);
|
|
1569
1389
|
const ref = getpath(spec, refpath, {
|
|
1570
|
-
// TODO: test relative refs
|
|
1571
|
-
// dpath: inj.path.slice(1),
|
|
1572
1390
|
dpath,
|
|
1573
1391
|
// dparent: getpath(spec, inj.path.slice(1))
|
|
1574
1392
|
dparent: getpath(spec, dpath),
|
|
@@ -1609,17 +1427,13 @@ const transform_REF = (inj, val, _ref, store) => {
|
|
|
1609
1427
|
return val;
|
|
1610
1428
|
};
|
|
1611
1429
|
const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
1612
|
-
// console.log('FORMAT-START', inj, _val)
|
|
1613
1430
|
// Remove remaining keys to avoid spurious processing.
|
|
1614
1431
|
slice(inj.keys, 0, 1, true);
|
|
1615
1432
|
if (M_VAL !== inj.mode) {
|
|
1616
1433
|
return NONE;
|
|
1617
1434
|
}
|
|
1618
|
-
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1619
|
-
// TODO: EACH and PACK should accept customm functions too
|
|
1620
1435
|
const name = _lookup(inj.parent, 1);
|
|
1621
1436
|
const child = _lookup(inj.parent, 2);
|
|
1622
|
-
// Source data.
|
|
1623
1437
|
const tkey = getelem(inj.path, -2);
|
|
1624
1438
|
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1));
|
|
1625
1439
|
const cinj = injectChild(child, store, inj);
|
|
@@ -1631,7 +1445,6 @@ const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
|
1631
1445
|
}
|
|
1632
1446
|
const out = walk(resolved, formatter);
|
|
1633
1447
|
setprop(target, tkey, out);
|
|
1634
|
-
// _updateAncestors(inj, target, tkey, out)
|
|
1635
1448
|
return out;
|
|
1636
1449
|
};
|
|
1637
1450
|
const FORMATTER = {
|
|
@@ -1672,7 +1485,6 @@ const transform_APPLY = (inj, _val, _ref, store) => {
|
|
|
1672
1485
|
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1673
1486
|
return NONE;
|
|
1674
1487
|
}
|
|
1675
|
-
// const [err, apply, child] = injectorArgs([T_function, T_any], inj)
|
|
1676
1488
|
const [err, apply, child] = injectorArgs([T_function, T_any], slice(inj.parent, 1));
|
|
1677
1489
|
if (NONE !== err) {
|
|
1678
1490
|
inj.errs.push('$' + ijname + ': ' + err);
|
|
@@ -1729,7 +1541,6 @@ injdef) {
|
|
|
1729
1541
|
$FORMAT: transform_FORMAT,
|
|
1730
1542
|
$APPLY: transform_APPLY,
|
|
1731
1543
|
},
|
|
1732
|
-
// Custom extra transforms, if any.
|
|
1733
1544
|
extraTransforms,
|
|
1734
1545
|
{
|
|
1735
1546
|
$ERRS: errs,
|
|
@@ -1770,7 +1581,6 @@ const validate_TYPE = (inj, _val, ref) => {
|
|
|
1770
1581
|
}
|
|
1771
1582
|
return out;
|
|
1772
1583
|
};
|
|
1773
|
-
// Allow any value.
|
|
1774
1584
|
const validate_ANY = (inj) => {
|
|
1775
1585
|
const out = _lookup(inj.dparent, inj.key);
|
|
1776
1586
|
return out;
|
|
@@ -1781,10 +1591,8 @@ const validate_ANY = (inj) => {
|
|
|
1781
1591
|
const validate_CHILD = (inj) => {
|
|
1782
1592
|
const { mode, key, parent, keys, path } = inj;
|
|
1783
1593
|
// Setup data structures for validation by cloning child template.
|
|
1784
|
-
// Map syntax.
|
|
1785
1594
|
if (M_KEYPRE === mode) {
|
|
1786
1595
|
const childtm = getprop(parent, key);
|
|
1787
|
-
// Get corresponding current object.
|
|
1788
1596
|
const pkey = getelem(path, -2);
|
|
1789
1597
|
let tval = getprop(inj.dparent, pkey);
|
|
1790
1598
|
if (NONE == tval) {
|
|
@@ -1800,21 +1608,16 @@ const validate_CHILD = (inj) => {
|
|
|
1800
1608
|
// NOTE: modifying inj! This extends the child value loop in inject.
|
|
1801
1609
|
keys.push(ckey);
|
|
1802
1610
|
}
|
|
1803
|
-
// Remove $CHILD to cleanup ouput.
|
|
1804
1611
|
inj.setval(NONE);
|
|
1805
1612
|
return NONE;
|
|
1806
1613
|
}
|
|
1807
|
-
// List syntax.
|
|
1808
1614
|
if (M_VAL === mode) {
|
|
1809
1615
|
if (!islist(parent)) {
|
|
1810
|
-
// $CHILD was not inside a list.
|
|
1811
1616
|
inj.errs.push('Invalid $CHILD as value');
|
|
1812
1617
|
return NONE;
|
|
1813
1618
|
}
|
|
1814
1619
|
const childtm = _lookup(parent, 1);
|
|
1815
1620
|
if (NONE === inj.dparent) {
|
|
1816
|
-
// Empty list as default.
|
|
1817
|
-
// parent.length = 0
|
|
1818
1621
|
slice(parent, 0, 0, true);
|
|
1819
1622
|
return NONE;
|
|
1820
1623
|
}
|
|
@@ -1844,11 +1647,6 @@ const validate_CHILD = (inj) => {
|
|
|
1844
1647
|
}
|
|
1845
1648
|
return NONE;
|
|
1846
1649
|
};
|
|
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
1650
|
const validate_ONE = (inj, _val, _ref, store) => {
|
|
1853
1651
|
const { mode, parent, keyI } = inj;
|
|
1854
1652
|
// Only operate in val mode, since parent is a list.
|
|
@@ -1862,8 +1660,9 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1862
1660
|
inj.keyI = size(inj.keys);
|
|
1863
1661
|
// Clean up structure, replacing [$ONE, ...] with current
|
|
1864
1662
|
inj.setval(inj.dparent, 2);
|
|
1663
|
+
// Path shortens for the message only: `modify` reads
|
|
1664
|
+
// getprop(inj.parent, inj.key), and inj.parent is still this list.
|
|
1865
1665
|
inj.path = slice(inj.path, -1);
|
|
1866
|
-
inj.key = getelem(inj.path, -1);
|
|
1867
1666
|
const tvals = slice(parent, 1);
|
|
1868
1667
|
if (0 === size(tvals)) {
|
|
1869
1668
|
inj.errs.push('The $ONE validator at field ' +
|
|
@@ -1871,9 +1670,7 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1871
1670
|
' must have at least one argument.');
|
|
1872
1671
|
return;
|
|
1873
1672
|
}
|
|
1874
|
-
// See if we can find a match.
|
|
1875
1673
|
for (const tval of tvals) {
|
|
1876
|
-
// If match, then errs.length = 0
|
|
1877
1674
|
const terrs = [];
|
|
1878
1675
|
const vstore = merge([{}, store], 1);
|
|
1879
1676
|
vstore.$TOP = inj.dparent;
|
|
@@ -1888,7 +1685,6 @@ const validate_ONE = (inj, _val, _ref, store) => {
|
|
|
1888
1685
|
return;
|
|
1889
1686
|
}
|
|
1890
1687
|
}
|
|
1891
|
-
// There was no match.
|
|
1892
1688
|
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1893
1689
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(tvals) ? 'one of ' : '') + valdesc, typify(inj.dparent), inj.dparent, 'V0210'));
|
|
1894
1690
|
}
|
|
@@ -1906,7 +1702,6 @@ const validate_EXACT = (inj) => {
|
|
|
1906
1702
|
inj.keyI = size(inj.keys);
|
|
1907
1703
|
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1908
1704
|
inj.setval(inj.dparent, 2);
|
|
1909
|
-
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
1910
1705
|
inj.path = slice(inj.path, 0, -1);
|
|
1911
1706
|
inj.key = getelem(inj.path, -1);
|
|
1912
1707
|
const tvals = slice(parent, 1);
|
|
@@ -1916,7 +1711,6 @@ const validate_EXACT = (inj) => {
|
|
|
1916
1711
|
' must have at least one argument.');
|
|
1917
1712
|
return;
|
|
1918
1713
|
}
|
|
1919
|
-
// See if we can find an exact value match.
|
|
1920
1714
|
let currentstr = undefined;
|
|
1921
1715
|
for (const tval of tvals) {
|
|
1922
1716
|
let exactmatch = tval === inj.dparent;
|
|
@@ -1929,7 +1723,6 @@ const validate_EXACT = (inj) => {
|
|
|
1929
1723
|
return;
|
|
1930
1724
|
}
|
|
1931
1725
|
}
|
|
1932
|
-
// There was no match.
|
|
1933
1726
|
const valdesc = replace(join(items(tvals, (n) => stringify(n[1])), ', '), R_TRANSFORM_NAME, (_m, p1) => p1.toLowerCase());
|
|
1934
1727
|
inj.errs.push(_invalidTypeMsg(inj.path, (1 < size(inj.path) ? '' : 'value ') +
|
|
1935
1728
|
'exactly equal to ' +
|
|
@@ -1949,9 +1742,7 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1949
1742
|
if (SKIP === pval) {
|
|
1950
1743
|
return;
|
|
1951
1744
|
}
|
|
1952
|
-
// select needs exact matches
|
|
1953
1745
|
const exact = getprop(inj.meta, S_BEXACT, false);
|
|
1954
|
-
// Current val to verify.
|
|
1955
1746
|
const cval = getprop(inj.dparent, key);
|
|
1956
1747
|
if (NONE === inj || (!exact && NONE === cval)) {
|
|
1957
1748
|
return;
|
|
@@ -1962,7 +1753,6 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
1962
1753
|
return;
|
|
1963
1754
|
}
|
|
1964
1755
|
const ctype = typify(cval);
|
|
1965
|
-
// Type mismatch.
|
|
1966
1756
|
if (ptype !== ctype && NONE !== pval) {
|
|
1967
1757
|
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0010'));
|
|
1968
1758
|
return;
|
|
@@ -2016,16 +1806,6 @@ const _validation = (pval, key, parent, inj) => {
|
|
|
2016
1806
|
}
|
|
2017
1807
|
return;
|
|
2018
1808
|
};
|
|
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
1809
|
function validate(data, // Source data to transform into new data (original not mutated)
|
|
2030
1810
|
spec, // Transform specification; output follows this shape
|
|
2031
1811
|
injdef) {
|
|
@@ -2034,7 +1814,6 @@ injdef) {
|
|
|
2034
1814
|
const errs = injdef?.errs || [];
|
|
2035
1815
|
const store = merge([
|
|
2036
1816
|
{
|
|
2037
|
-
// Remove the transform commands.
|
|
2038
1817
|
$DELETE: null,
|
|
2039
1818
|
$COPY: null,
|
|
2040
1819
|
$KEY: null,
|
|
@@ -2151,9 +1930,7 @@ const select_NOT = (inj, _val, _ref, store) => {
|
|
|
2151
1930
|
const select_CMP = (inj, _val, ref, store) => {
|
|
2152
1931
|
if (M_KEYPRE === inj.mode) {
|
|
2153
1932
|
const term = _lookup(inj.parent, inj.key);
|
|
2154
|
-
// const src = getprop(store, inj.base, store)
|
|
2155
1933
|
const gkey = getelem(inj.path, -2);
|
|
2156
|
-
// const tval = getprop(src, gkey)
|
|
2157
1934
|
const ppath = slice(inj.path, -1);
|
|
2158
1935
|
const point = getpath(store, ppath);
|
|
2159
1936
|
let pass = false;
|
|
@@ -2190,10 +1967,6 @@ const select_CMP = (inj, _val, ref, store) => {
|
|
|
2190
1967
|
}
|
|
2191
1968
|
return NONE;
|
|
2192
1969
|
};
|
|
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
1970
|
function select(children, query) {
|
|
2198
1971
|
if (!isnode(children)) {
|
|
2199
1972
|
return [];
|
|
@@ -2308,7 +2081,6 @@ class Injection {
|
|
|
2308
2081
|
}
|
|
2309
2082
|
}
|
|
2310
2083
|
}
|
|
2311
|
-
// TODO: is this needed?
|
|
2312
2084
|
return this.dparent;
|
|
2313
2085
|
}
|
|
2314
2086
|
child(keyI, keys) {
|
|
@@ -2344,17 +2116,11 @@ class Injection {
|
|
|
2344
2116
|
const akey = getelem(this.path, 0 - ancestor);
|
|
2345
2117
|
parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val);
|
|
2346
2118
|
}
|
|
2347
|
-
// console.log('SETVAL', val, this.key, this.parent)
|
|
2348
2119
|
return parent;
|
|
2349
2120
|
}
|
|
2350
2121
|
}
|
|
2351
2122
|
// Internal utilities
|
|
2352
2123
|
// ==================
|
|
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
2124
|
// Build a type validation error message.
|
|
2359
2125
|
function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
2360
2126
|
const vs = null == v ? 'no value' : stringify(v);
|
|
@@ -2373,8 +2139,6 @@ function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
|
2373
2139
|
const _injecthandler = (inj, val, ref, store) => {
|
|
2374
2140
|
let out = val;
|
|
2375
2141
|
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
2142
|
if (iscmd) {
|
|
2379
2143
|
out = val(inj, val, ref, store);
|
|
2380
2144
|
}
|
|
@@ -2403,22 +2167,11 @@ const _validatehandler = (inj, val, ref, store) => {
|
|
|
2403
2167
|
}
|
|
2404
2168
|
return out;
|
|
2405
2169
|
};
|
|
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
2170
|
function _injectstr(val, store, inj) {
|
|
2416
|
-
// Can't inject into non-strings
|
|
2417
2171
|
if (S_string !== typeof val || S_MT === val) {
|
|
2418
2172
|
return S_MT;
|
|
2419
2173
|
}
|
|
2420
2174
|
let out = val;
|
|
2421
|
-
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
2422
2175
|
const m = re_find(R_INJECTION_FULL, val);
|
|
2423
2176
|
// Full string of the val is an injection.
|
|
2424
2177
|
if (m) {
|
|
@@ -2426,17 +2179,14 @@ function _injectstr(val, store, inj) {
|
|
|
2426
2179
|
inj.full = true;
|
|
2427
2180
|
}
|
|
2428
2181
|
let pathref = m[1];
|
|
2429
|
-
// Special escapes inside injection.
|
|
2430
2182
|
if (3 < size(pathref)) {
|
|
2431
2183
|
pathref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, pathref, S_BT), S_DS);
|
|
2432
2184
|
}
|
|
2433
|
-
// Get the extracted path reference.
|
|
2434
2185
|
out = getpath(store, pathref, inj);
|
|
2435
2186
|
}
|
|
2436
2187
|
else {
|
|
2437
2188
|
// Check for injections within the string.
|
|
2438
2189
|
const partial = (_m, ref) => {
|
|
2439
|
-
// Special escapes inside injection.
|
|
2440
2190
|
if (3 < size(ref)) {
|
|
2441
2191
|
ref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, ref, S_BT), S_DS);
|
|
2442
2192
|
}
|
|
@@ -2444,7 +2194,6 @@ function _injectstr(val, store, inj) {
|
|
|
2444
2194
|
inj.full = false;
|
|
2445
2195
|
}
|
|
2446
2196
|
const found = getpath(store, ref, inj);
|
|
2447
|
-
// Ensure inject value is a string.
|
|
2448
2197
|
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found);
|
|
2449
2198
|
};
|
|
2450
2199
|
out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]));
|
|
@@ -2496,13 +2245,11 @@ function checkPlacement(modes, ijname, parentTypes, inj) {
|
|
|
2496
2245
|
}
|
|
2497
2246
|
return true;
|
|
2498
2247
|
}
|
|
2499
|
-
// function injectorArgs(argTypes: number[], inj: Injection): any {
|
|
2500
2248
|
function injectorArgs(argTypes, args) {
|
|
2501
2249
|
const numargs = size(argTypes);
|
|
2502
2250
|
const found = new Array(1 + numargs);
|
|
2503
2251
|
found[0] = NONE;
|
|
2504
2252
|
for (let argI = 0; argI < numargs; argI++) {
|
|
2505
|
-
// const arg = inj.parent[1 + argI]
|
|
2506
2253
|
const arg = args[argI];
|
|
2507
2254
|
const argType = typify(arg);
|
|
2508
2255
|
if (0 === (argTypes[argI] & argType)) {
|
|
@@ -2537,7 +2284,6 @@ function injectChild(child, store, inj) {
|
|
|
2537
2284
|
setprop(cinj.parent, inj.key, child);
|
|
2538
2285
|
}
|
|
2539
2286
|
}
|
|
2540
|
-
// console.log('FORMAT-INJECT-CHILD', child)
|
|
2541
2287
|
inject(child, store, cinj);
|
|
2542
2288
|
return cinj;
|
|
2543
2289
|
}
|
|
@@ -2616,56 +2362,6 @@ class StructUtility {
|
|
|
2616
2362
|
}
|
|
2617
2363
|
}
|
|
2618
2364
|
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
2365
|
const CONDENSE_VERSION = 1;
|
|
2670
2366
|
const CONDENSE_MAP = 0;
|
|
2671
2367
|
const CONDENSE_LIST = 1;
|
|
@@ -2678,18 +2374,6 @@ const S_condense = '$condense';
|
|
|
2678
2374
|
const S_condsym = 'sym';
|
|
2679
2375
|
const S_condnode = 'node';
|
|
2680
2376
|
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
2377
|
function condenseCmp(a, b) {
|
|
2694
2378
|
let i = 0;
|
|
2695
2379
|
let j = 0;
|
|
@@ -2704,12 +2388,6 @@ function condenseCmp(a, b) {
|
|
|
2704
2388
|
}
|
|
2705
2389
|
return a.length - i - (b.length - j);
|
|
2706
2390
|
}
|
|
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
2391
|
function condenseSet(obj, key, val) {
|
|
2714
2392
|
if ('__proto__' === key) {
|
|
2715
2393
|
Object.defineProperty(obj, key, {
|
|
@@ -2907,13 +2585,6 @@ function condenseResolve(cond, idx, path) {
|
|
|
2907
2585
|
}
|
|
2908
2586
|
return at;
|
|
2909
2587
|
}
|
|
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
2588
|
function condenseMaterialise(cond, idx) {
|
|
2918
2589
|
const sym = cond[S_condsym];
|
|
2919
2590
|
const node = cond[S_condnode];
|