@voxgig/struct-js 0.1.2
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/LICENSE +21 -0
- package/README.md +696 -0
- package/package.json +50 -0
- package/src/struct.js +2580 -0
package/src/struct.js
ADDED
|
@@ -0,0 +1,2580 @@
|
|
|
1
|
+
/* Copyright (c) 2025-2026 Voxgig Ltd. MIT LICENSE. */
|
|
2
|
+
// VERSION: @voxgig/struct-js 0.1.2
|
|
3
|
+
/* Voxgig Struct
|
|
4
|
+
* =============
|
|
5
|
+
*
|
|
6
|
+
* Utility functions to manipulate in-memory JSON-like data
|
|
7
|
+
* structures. These structures assumed to be composed of nested
|
|
8
|
+
* "nodes", where a node is a list or map, and has named or indexed
|
|
9
|
+
* fields. The general design principle is "by-example". Transform
|
|
10
|
+
* specifications mirror the desired output. This implementation is
|
|
11
|
+
* designed for porting to multiple language, and to be tolerant of
|
|
12
|
+
* undefined values.
|
|
13
|
+
*
|
|
14
|
+
* Main utilities
|
|
15
|
+
* - getpath: get the value at a key path deep inside an object.
|
|
16
|
+
* - merge: merge multiple nodes, overriding values in earlier nodes.
|
|
17
|
+
* - walk: walk a node tree, applying a function at each node and leaf.
|
|
18
|
+
* - inject: inject values from a data store into a new data structure.
|
|
19
|
+
* - transform: transform a data structure to an example structure.
|
|
20
|
+
* - validate: valiate a data structure against a shape specification.
|
|
21
|
+
*
|
|
22
|
+
* Minor utilities
|
|
23
|
+
* - isnode, islist, ismap, iskey, isfunc: identify value kinds.
|
|
24
|
+
* - isempty: undefined values, or empty nodes.
|
|
25
|
+
* - keysof: sorted list of node keys (ascending).
|
|
26
|
+
* - haskey: true if key value is defined.
|
|
27
|
+
* - clone: create a copy of a JSON-like data structure.
|
|
28
|
+
* - items: list entries of a map or list as [key, value] pairs.
|
|
29
|
+
* - getprop: safely get a property value by key.
|
|
30
|
+
* - setprop: safely set a property value by key.
|
|
31
|
+
* - stringify: human-friendly string version of a value.
|
|
32
|
+
* - escre: escape a regular expresion string.
|
|
33
|
+
* - escurl: escape a url.
|
|
34
|
+
* - join: join parts of a url, merging forward slashes.
|
|
35
|
+
*
|
|
36
|
+
* This set of functions and supporting utilities is designed to work
|
|
37
|
+
* uniformly across many languages, meaning that some code that may be
|
|
38
|
+
* functionally redundant in specific languages is still retained to
|
|
39
|
+
* keep the code human comparable.
|
|
40
|
+
*
|
|
41
|
+
* NOTE: Lists are assumed to be mutable and reference stable.
|
|
42
|
+
*
|
|
43
|
+
* NOTE: In this code JSON nulls are in general *not* considered the
|
|
44
|
+
* same as the undefined value in the given language. However most
|
|
45
|
+
* JSON parsers do use the undefined value to represent JSON
|
|
46
|
+
* null. This is ambiguous as JSON null is a separate value, not an
|
|
47
|
+
* undefined value. You should convert such values to a special value
|
|
48
|
+
* to represent JSON null, if this ambiguity creates issues
|
|
49
|
+
* (thankfully in most APIs, JSON nulls are not used). For example,
|
|
50
|
+
* the unit tests use the string "__NULL__" where necessary.
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
// String constants are explicitly defined.
|
|
54
|
+
// Mode value for inject step (bitfield).
|
|
55
|
+
const M_KEYPRE = 1
|
|
56
|
+
const M_KEYPOST = 2
|
|
57
|
+
const M_VAL = 4
|
|
58
|
+
// Special strings.
|
|
59
|
+
const S_BKEY = '`$KEY`'
|
|
60
|
+
const S_BANNO = '`$ANNO`'
|
|
61
|
+
const S_BEXACT = '`$EXACT`'
|
|
62
|
+
const S_BVAL = '`$VAL`'
|
|
63
|
+
const S_DKEY = '$KEY'
|
|
64
|
+
const S_DTOP = '$TOP'
|
|
65
|
+
const S_DERRS = '$ERRS'
|
|
66
|
+
const S_DSPEC = '$SPEC'
|
|
67
|
+
// General strings.
|
|
68
|
+
const S_list = 'list'
|
|
69
|
+
const S_base = 'base'
|
|
70
|
+
const S_boolean = 'boolean'
|
|
71
|
+
const S_function = 'function'
|
|
72
|
+
const S_symbol = 'symbol'
|
|
73
|
+
const S_instance = 'instance'
|
|
74
|
+
const S_key = 'key'
|
|
75
|
+
const S_any = 'any'
|
|
76
|
+
const S_nil = 'nil'
|
|
77
|
+
const S_null = 'null'
|
|
78
|
+
const S_number = 'number'
|
|
79
|
+
const S_object = 'object'
|
|
80
|
+
const S_string = 'string'
|
|
81
|
+
const S_decimal = 'decimal'
|
|
82
|
+
const S_integer = 'integer'
|
|
83
|
+
const S_map = 'map'
|
|
84
|
+
const S_scalar = 'scalar'
|
|
85
|
+
const S_node = 'node'
|
|
86
|
+
// Character strings.
|
|
87
|
+
const S_BT = '`'
|
|
88
|
+
const S_CN = ':'
|
|
89
|
+
const S_CS = ']'
|
|
90
|
+
const S_DS = '$'
|
|
91
|
+
const S_DT = '.'
|
|
92
|
+
const S_FS = '/'
|
|
93
|
+
const S_KEY = 'KEY'
|
|
94
|
+
const S_MT = ''
|
|
95
|
+
const S_OS = '['
|
|
96
|
+
const S_SP = ' '
|
|
97
|
+
const S_CM = ','
|
|
98
|
+
const S_VIZ = ': '
|
|
99
|
+
// Types
|
|
100
|
+
let t = 31
|
|
101
|
+
const T_any = (1 << t--) - 1
|
|
102
|
+
const T_noval = 1 << t-- // Means property absent, undefined. Also NOT a scalar!
|
|
103
|
+
const T_boolean = 1 << t--
|
|
104
|
+
const T_decimal = 1 << t--
|
|
105
|
+
const T_integer = 1 << t--
|
|
106
|
+
const T_number = 1 << t--
|
|
107
|
+
const T_string = 1 << t--
|
|
108
|
+
const T_function = 1 << t--
|
|
109
|
+
const T_symbol = 1 << t--
|
|
110
|
+
const T_null = 1 << t-- // The actual JSON null value.
|
|
111
|
+
t -= 7
|
|
112
|
+
const T_list = 1 << t--
|
|
113
|
+
const T_map = 1 << t--
|
|
114
|
+
const T_instance = 1 << t--
|
|
115
|
+
t -= 4
|
|
116
|
+
const T_scalar = 1 << t--
|
|
117
|
+
const T_node = 1 << t--
|
|
118
|
+
const TYPENAME = [
|
|
119
|
+
S_any,
|
|
120
|
+
S_nil,
|
|
121
|
+
S_boolean,
|
|
122
|
+
S_decimal,
|
|
123
|
+
S_integer,
|
|
124
|
+
S_number,
|
|
125
|
+
S_string,
|
|
126
|
+
S_function,
|
|
127
|
+
S_symbol,
|
|
128
|
+
S_null,
|
|
129
|
+
'',
|
|
130
|
+
'',
|
|
131
|
+
'',
|
|
132
|
+
'',
|
|
133
|
+
'',
|
|
134
|
+
'',
|
|
135
|
+
'',
|
|
136
|
+
S_list,
|
|
137
|
+
S_map,
|
|
138
|
+
S_instance,
|
|
139
|
+
'',
|
|
140
|
+
'',
|
|
141
|
+
'',
|
|
142
|
+
'',
|
|
143
|
+
S_scalar,
|
|
144
|
+
S_node,
|
|
145
|
+
]
|
|
146
|
+
// The standard undefined value for this language.
|
|
147
|
+
const NONE = undefined
|
|
148
|
+
// Private markers
|
|
149
|
+
const SKIP = { '`$SKIP`': true }
|
|
150
|
+
const DELETE = { '`$DELETE`': true }
|
|
151
|
+
// Regular expression constants
|
|
152
|
+
const R_INTEGER_KEY = /^[-0-9]+$/ // Match integer keys (including <0).
|
|
153
|
+
const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g // Chars that need escaping in regexp.
|
|
154
|
+
const R_QUOTES = /"/g // Double quotes for removal.
|
|
155
|
+
const R_DOT = /\./g // Dots in path strings.
|
|
156
|
+
const R_CLONE_REF = /^`\$REF:([0-9]+)`$/ // Copy reference in cloning.
|
|
157
|
+
const R_META_PATH = /^([^$]+)\$([=~])(.+)$/ // Meta path syntax.
|
|
158
|
+
const R_DOUBLE_DOLLAR = /\$\$/g // Double dollar escape sequence.
|
|
159
|
+
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g // Transform command names.
|
|
160
|
+
const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/ // Full string injection pattern.
|
|
161
|
+
const R_BT_ESCAPE = /\$BT/g // Backtick escape sequence.
|
|
162
|
+
const R_DS_ESCAPE = /\$DS/g // Dollar sign escape sequence.
|
|
163
|
+
const R_INJECTION_PARTIAL = /`([^`]+)`/g // Partial string injection pattern.
|
|
164
|
+
// Default max depth (for walk etc).
|
|
165
|
+
const MAXDEPTH = 32
|
|
166
|
+
// Return type string for narrowest type.
|
|
167
|
+
function typename(t) {
|
|
168
|
+
return getelem(TYPENAME, Math.clz32(t), TYPENAME[0])
|
|
169
|
+
}
|
|
170
|
+
// Get a defined value. Returns alt if val is undefined.
|
|
171
|
+
function getdef(val, alt) {
|
|
172
|
+
if (NONE === val) {
|
|
173
|
+
return alt
|
|
174
|
+
}
|
|
175
|
+
return val
|
|
176
|
+
}
|
|
177
|
+
// Value is a node - defined, and a map (hash) or list (array).
|
|
178
|
+
// NOTE: typescript
|
|
179
|
+
// things
|
|
180
|
+
function isnode(val) {
|
|
181
|
+
return null != val && S_object == typeof val
|
|
182
|
+
}
|
|
183
|
+
// Value is a defined map (hash) with string keys.
|
|
184
|
+
function ismap(val) {
|
|
185
|
+
return null != val && S_object == typeof val && !Array.isArray(val)
|
|
186
|
+
}
|
|
187
|
+
// Value is a defined list (array) with integer keys (indexes).
|
|
188
|
+
function islist(val) {
|
|
189
|
+
return Array.isArray(val)
|
|
190
|
+
}
|
|
191
|
+
// Value is a defined string (non-empty) or integer key.
|
|
192
|
+
function iskey(key) {
|
|
193
|
+
const keytype = typeof key
|
|
194
|
+
return (S_string === keytype && S_MT !== key) || S_number === keytype
|
|
195
|
+
}
|
|
196
|
+
// Check for an "empty" value - undefined, empty string, array, object.
|
|
197
|
+
function isempty(val) {
|
|
198
|
+
return (
|
|
199
|
+
null == val ||
|
|
200
|
+
S_MT === val ||
|
|
201
|
+
(Array.isArray(val) && 0 === val.length) ||
|
|
202
|
+
(S_object === typeof val && 0 === Object.keys(val).length)
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
// Value is a function.
|
|
206
|
+
function isfunc(val) {
|
|
207
|
+
return S_function === typeof val
|
|
208
|
+
}
|
|
209
|
+
// The integer size of the value. For arrays and strings, the length,
|
|
210
|
+
// for numbers, the integer part, for boolean, true is 1 and falso 0, for all other values, 0.
|
|
211
|
+
function size(val) {
|
|
212
|
+
if (islist(val)) {
|
|
213
|
+
return val.length
|
|
214
|
+
} else if (ismap(val)) {
|
|
215
|
+
return Object.keys(val).length
|
|
216
|
+
}
|
|
217
|
+
const valtype = typeof val
|
|
218
|
+
if (S_string == valtype) {
|
|
219
|
+
return val.length
|
|
220
|
+
} else if (S_number == typeof val) {
|
|
221
|
+
return Math.floor(val)
|
|
222
|
+
} else if (S_boolean == typeof val) {
|
|
223
|
+
return true === val ? 1 : 0
|
|
224
|
+
} else {
|
|
225
|
+
return 0
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Extract part of an array or string into a new value, from the start
|
|
229
|
+
// point to the end point. If no end is specified, extract to the
|
|
230
|
+
// full length of the value. Negative arguments count from the end of
|
|
231
|
+
// the value. For numbers, perform min and max bounding, where start
|
|
232
|
+
// is inclusive, and end is *exclusive*.
|
|
233
|
+
// NOTE: input lists are not mutated by default. Use the mutate
|
|
234
|
+
// argument to mutate lists in place.
|
|
235
|
+
function slice(val, start, end, mutate) {
|
|
236
|
+
if (S_number === typeof val) {
|
|
237
|
+
start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start
|
|
238
|
+
end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1
|
|
239
|
+
return Math.min(Math.max(val, start), end)
|
|
240
|
+
}
|
|
241
|
+
const vlen = size(val)
|
|
242
|
+
if (null != end && null == start) {
|
|
243
|
+
start = 0
|
|
244
|
+
}
|
|
245
|
+
if (null != start) {
|
|
246
|
+
if (start < 0) {
|
|
247
|
+
end = vlen + start
|
|
248
|
+
if (end < 0) {
|
|
249
|
+
end = 0
|
|
250
|
+
}
|
|
251
|
+
start = 0
|
|
252
|
+
} else if (null != end) {
|
|
253
|
+
if (end < 0) {
|
|
254
|
+
end = vlen + end
|
|
255
|
+
if (end < 0) {
|
|
256
|
+
end = 0
|
|
257
|
+
}
|
|
258
|
+
} else if (vlen < end) {
|
|
259
|
+
end = vlen
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
end = vlen
|
|
263
|
+
}
|
|
264
|
+
if (vlen < start) {
|
|
265
|
+
start = vlen
|
|
266
|
+
}
|
|
267
|
+
if (-1 < start && start <= end && end <= vlen) {
|
|
268
|
+
if (islist(val)) {
|
|
269
|
+
if (mutate) {
|
|
270
|
+
for (let i = 0, j = start; j < end; i++, j++) {
|
|
271
|
+
val[i] = val[j]
|
|
272
|
+
}
|
|
273
|
+
val.length = end - start
|
|
274
|
+
} else {
|
|
275
|
+
val = val.slice(start, end)
|
|
276
|
+
}
|
|
277
|
+
} else if (S_string === typeof val) {
|
|
278
|
+
val = val.substring(start, end)
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
if (islist(val)) {
|
|
282
|
+
val = []
|
|
283
|
+
} else if (S_string === typeof val) {
|
|
284
|
+
val = S_MT
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return val
|
|
289
|
+
}
|
|
290
|
+
// String padding.
|
|
291
|
+
function pad(str, padding, padchar) {
|
|
292
|
+
str = S_string === typeof str ? str : stringify(str)
|
|
293
|
+
padding = null == padding ? 44 : padding
|
|
294
|
+
padchar = null == padchar ? S_SP : (padchar + S_SP)[0]
|
|
295
|
+
return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar)
|
|
296
|
+
}
|
|
297
|
+
// Determine the type of a value as a bit code.
|
|
298
|
+
function typify(value) {
|
|
299
|
+
if (undefined === value) {
|
|
300
|
+
return T_noval
|
|
301
|
+
}
|
|
302
|
+
const typestr = typeof value
|
|
303
|
+
if (null === value) {
|
|
304
|
+
return T_scalar | T_null
|
|
305
|
+
} else if (S_number === typestr) {
|
|
306
|
+
if (Number.isInteger(value)) {
|
|
307
|
+
return T_scalar | T_number | T_integer
|
|
308
|
+
} else if (isNaN(value)) {
|
|
309
|
+
return T_noval
|
|
310
|
+
} else {
|
|
311
|
+
return T_scalar | T_number | T_decimal
|
|
312
|
+
}
|
|
313
|
+
} else if (S_string === typestr) {
|
|
314
|
+
return T_scalar | T_string
|
|
315
|
+
} else if (S_boolean === typestr) {
|
|
316
|
+
return T_scalar | T_boolean
|
|
317
|
+
} else if (S_function === typestr) {
|
|
318
|
+
return T_scalar | T_function
|
|
319
|
+
}
|
|
320
|
+
// For languages that have symbolic atoms.
|
|
321
|
+
else if (S_symbol === typestr) {
|
|
322
|
+
return T_scalar | T_symbol
|
|
323
|
+
} else if (Array.isArray(value)) {
|
|
324
|
+
return T_node | T_list
|
|
325
|
+
} else if (S_object === typestr) {
|
|
326
|
+
if (value.constructor instanceof Function) {
|
|
327
|
+
let cname = value.constructor.name
|
|
328
|
+
if ('Object' !== cname && 'Array' !== cname) {
|
|
329
|
+
return T_node | T_instance
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return T_node | T_map
|
|
333
|
+
}
|
|
334
|
+
// Anything else (e.g. bigint) is considered T_any
|
|
335
|
+
return T_any
|
|
336
|
+
}
|
|
337
|
+
// Get a list element. The key should be an integer, or a string
|
|
338
|
+
// that can parse to an integer only. Negative integers count from the end of the list.
|
|
339
|
+
function getelem(val, key, alt) {
|
|
340
|
+
let out = NONE
|
|
341
|
+
if (NONE === val || NONE === key) {
|
|
342
|
+
return alt
|
|
343
|
+
}
|
|
344
|
+
if (islist(val)) {
|
|
345
|
+
let nkey = parseInt(key)
|
|
346
|
+
if (Number.isInteger(nkey) && ('' + key).match(R_INTEGER_KEY)) {
|
|
347
|
+
if (nkey < 0) {
|
|
348
|
+
key = val.length + nkey
|
|
349
|
+
}
|
|
350
|
+
out = val[key]
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// Group A: a stored JSON null is treated as "no value".
|
|
354
|
+
if (null == out) {
|
|
355
|
+
return 0 < (T_function & typify(alt)) ? alt() : alt
|
|
356
|
+
}
|
|
357
|
+
return out
|
|
358
|
+
}
|
|
359
|
+
// Safely get a property of a node. Group A semantics (UNDEF_SPEC.md):
|
|
360
|
+
// a stored null is treated as "no value" and returns alt.
|
|
361
|
+
function getprop(val, key, alt) {
|
|
362
|
+
let out = alt
|
|
363
|
+
if (NONE === val || NONE === key) {
|
|
364
|
+
return alt
|
|
365
|
+
}
|
|
366
|
+
if (isnode(val)) {
|
|
367
|
+
out = val[key]
|
|
368
|
+
}
|
|
369
|
+
if (null == out) {
|
|
370
|
+
return alt
|
|
371
|
+
}
|
|
372
|
+
return out
|
|
373
|
+
}
|
|
374
|
+
// Internal raw read: returns the literally-stored value (including null),
|
|
375
|
+
// or NONE when absent. Group B callers use this to distinguish null from
|
|
376
|
+
// absent. Group A's getprop conflates them.
|
|
377
|
+
function _lookup(val, key) {
|
|
378
|
+
if (NONE === val || NONE === key) return NONE
|
|
379
|
+
if (isnode(val)) return val[key]
|
|
380
|
+
return NONE
|
|
381
|
+
}
|
|
382
|
+
// Convert different types of keys to string representation.
|
|
383
|
+
// String keys are returned as is.
|
|
384
|
+
// Number keys are converted to strings.
|
|
385
|
+
// Floats are truncated to integers.
|
|
386
|
+
// Booleans, objects, arrays, null, undefined all return empty string.
|
|
387
|
+
function strkey(key = NONE) {
|
|
388
|
+
if (NONE === key) {
|
|
389
|
+
return S_MT
|
|
390
|
+
}
|
|
391
|
+
const t = typify(key)
|
|
392
|
+
if (0 < (T_string & t)) {
|
|
393
|
+
return key
|
|
394
|
+
} else if (0 < (T_boolean & t)) {
|
|
395
|
+
return S_MT
|
|
396
|
+
} else if (0 < (T_number & t)) {
|
|
397
|
+
return key % 1 === 0 ? String(key) : String(Math.floor(key))
|
|
398
|
+
}
|
|
399
|
+
return S_MT
|
|
400
|
+
}
|
|
401
|
+
// Sorted keys of a map, or indexes (as strings) of a list.
|
|
402
|
+
// Root utility - only uses language facilities.
|
|
403
|
+
function keysof(val) {
|
|
404
|
+
return !isnode(val) ? [] : ismap(val) ? Object.keys(val).sort() : val.map((_n, i) => S_MT + i)
|
|
405
|
+
}
|
|
406
|
+
// Value of property with name key in node val is defined.
|
|
407
|
+
// Root utility - only uses language facilities.
|
|
408
|
+
function haskey(val, key) {
|
|
409
|
+
return null != getprop(val, key)
|
|
410
|
+
}
|
|
411
|
+
function items(val, apply) {
|
|
412
|
+
let out = keysof(val).map((k) => [k, val[k]])
|
|
413
|
+
if (null != apply) {
|
|
414
|
+
out = out.map(apply)
|
|
415
|
+
}
|
|
416
|
+
return out
|
|
417
|
+
}
|
|
418
|
+
// To replicate the array spread operator:
|
|
419
|
+
// a=1, b=[2,3], c=[4,5]
|
|
420
|
+
// [a,...b,c] -> [1,2,3,[4,5]]
|
|
421
|
+
// flatten([a,b,[c]]) -> [1,2,3,[4,5]]
|
|
422
|
+
// NOTE: [c] ensures c is not expanded
|
|
423
|
+
function flatten(list, depth) {
|
|
424
|
+
if (!islist(list)) {
|
|
425
|
+
return list
|
|
426
|
+
}
|
|
427
|
+
return list.flat(getdef(depth, 1))
|
|
428
|
+
}
|
|
429
|
+
// Filter item values using check function.
|
|
430
|
+
function filter(val, check) {
|
|
431
|
+
let all = items(val)
|
|
432
|
+
let numall = size(all)
|
|
433
|
+
let out = []
|
|
434
|
+
for (let i = 0; i < numall; i++) {
|
|
435
|
+
if (check(all[i])) {
|
|
436
|
+
out.push(all[i][1])
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return out
|
|
440
|
+
}
|
|
441
|
+
// Escape regular expression.
|
|
442
|
+
function escre(s) {
|
|
443
|
+
return re_replace(R_ESCAPE_REGEXP, s, '\\$&')
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Regex utility — uniform re_* API. See /REGEX_API.md.
|
|
447
|
+
// The JS port wraps the built-in RegExp; the dialect is the RE2 subset.
|
|
448
|
+
function re_compile(pattern, flags) {
|
|
449
|
+
if (pattern instanceof RegExp) return pattern
|
|
450
|
+
return new RegExp(pattern, flags)
|
|
451
|
+
}
|
|
452
|
+
function re_find(pattern, input) {
|
|
453
|
+
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern)
|
|
454
|
+
return input.match(re)
|
|
455
|
+
}
|
|
456
|
+
function re_find_all(pattern, input) {
|
|
457
|
+
let re
|
|
458
|
+
if (pattern instanceof RegExp) {
|
|
459
|
+
re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g')
|
|
460
|
+
} else {
|
|
461
|
+
re = new RegExp(pattern, 'g')
|
|
462
|
+
}
|
|
463
|
+
const out = []
|
|
464
|
+
let m
|
|
465
|
+
while ((m = re.exec(input)) !== null) {
|
|
466
|
+
out.push(m)
|
|
467
|
+
if (m[0] === '') re.lastIndex++
|
|
468
|
+
}
|
|
469
|
+
return out
|
|
470
|
+
}
|
|
471
|
+
function re_replace(pattern, input, replacement) {
|
|
472
|
+
let re
|
|
473
|
+
if (pattern instanceof RegExp) {
|
|
474
|
+
re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g')
|
|
475
|
+
} else {
|
|
476
|
+
re = new RegExp(pattern, 'g')
|
|
477
|
+
}
|
|
478
|
+
if (typeof replacement === 'function') {
|
|
479
|
+
return input.replace(re, (...args) => replacement(args.slice(0, -2)))
|
|
480
|
+
}
|
|
481
|
+
return input.replace(re, replacement)
|
|
482
|
+
}
|
|
483
|
+
function re_test(pattern, input) {
|
|
484
|
+
const re = pattern instanceof RegExp ? pattern : new RegExp(pattern)
|
|
485
|
+
return re.test(input)
|
|
486
|
+
}
|
|
487
|
+
function re_escape(s) {
|
|
488
|
+
return escre(s)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Escape URLs.
|
|
492
|
+
function escurl(s) {
|
|
493
|
+
s = null == s ? S_MT : s
|
|
494
|
+
return encodeURIComponent(s)
|
|
495
|
+
}
|
|
496
|
+
// Replace a search string (all), or a regexp, in a source string.
|
|
497
|
+
function replace(s, from, to) {
|
|
498
|
+
let rs = s
|
|
499
|
+
let ts = typify(s)
|
|
500
|
+
if (0 === (T_string & ts)) {
|
|
501
|
+
rs = stringify(s)
|
|
502
|
+
} else if (0 < ((T_noval | T_null) & ts)) {
|
|
503
|
+
rs = S_MT
|
|
504
|
+
} else {
|
|
505
|
+
rs = stringify(s)
|
|
506
|
+
}
|
|
507
|
+
return rs.replace(from, to)
|
|
508
|
+
}
|
|
509
|
+
// Concatenate url part strings, merging sep char as needed.
|
|
510
|
+
function join(arr, sep, url) {
|
|
511
|
+
const sarr = size(arr)
|
|
512
|
+
const sepdef = getdef(sep, S_CM)
|
|
513
|
+
const sepre = 1 === size(sepdef) ? escre(sepdef) : NONE
|
|
514
|
+
const out = filter(
|
|
515
|
+
items(
|
|
516
|
+
// filter(arr, (n) => null != n[1] && S_MT !== n[1]),
|
|
517
|
+
filter(arr, (n) => 0 < (T_string & typify(n[1])) && S_MT !== n[1]),
|
|
518
|
+
(n) => {
|
|
519
|
+
let i = +n[0]
|
|
520
|
+
let s = n[1]
|
|
521
|
+
if (NONE !== sepre && S_MT !== sepre) {
|
|
522
|
+
if (url && 0 === i) {
|
|
523
|
+
s = replace(s, RegExp(sepre + '+$'), S_MT)
|
|
524
|
+
return s
|
|
525
|
+
}
|
|
526
|
+
if (0 < i) {
|
|
527
|
+
s = replace(s, RegExp('^' + sepre + '+'), S_MT)
|
|
528
|
+
}
|
|
529
|
+
if (i < sarr - 1 || !url) {
|
|
530
|
+
s = replace(s, RegExp(sepre + '+$'), S_MT)
|
|
531
|
+
}
|
|
532
|
+
s = replace(
|
|
533
|
+
s,
|
|
534
|
+
RegExp('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
|
|
535
|
+
'$1' + sepdef + '$2',
|
|
536
|
+
)
|
|
537
|
+
}
|
|
538
|
+
return s
|
|
539
|
+
},
|
|
540
|
+
),
|
|
541
|
+
(n) => S_MT !== n[1],
|
|
542
|
+
).join(sepdef)
|
|
543
|
+
return out
|
|
544
|
+
}
|
|
545
|
+
// Output JSON in a "standard" format, with 2 space indents, each property on a new line,
|
|
546
|
+
// and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
|
|
547
|
+
// In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
|
|
548
|
+
function jsonify(val, flags) {
|
|
549
|
+
let str = S_null
|
|
550
|
+
if (null != val) {
|
|
551
|
+
try {
|
|
552
|
+
const indent = getprop(flags, 'indent', 2)
|
|
553
|
+
str = JSON.stringify(val, null, indent)
|
|
554
|
+
if (NONE === str) {
|
|
555
|
+
str = S_null
|
|
556
|
+
}
|
|
557
|
+
const offset = getprop(flags, 'offset', 0)
|
|
558
|
+
if (0 < offset) {
|
|
559
|
+
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
560
|
+
// indented by offset. Assume first brace is on line with asignment, so not offset.
|
|
561
|
+
str =
|
|
562
|
+
'{\n' +
|
|
563
|
+
join(
|
|
564
|
+
items(slice(str.split('\n'), 1), (n) => pad(n[1], 0 - offset - size(n[1]))),
|
|
565
|
+
'\n',
|
|
566
|
+
)
|
|
567
|
+
}
|
|
568
|
+
} catch {
|
|
569
|
+
str = '__JSONIFY_FAILED__'
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return str
|
|
573
|
+
}
|
|
574
|
+
// Safely stringify a value for humans (NOT JSON!).
|
|
575
|
+
function stringify(val, maxlen, pretty) {
|
|
576
|
+
let valstr = S_MT
|
|
577
|
+
pretty = !!pretty
|
|
578
|
+
if (NONE === val) {
|
|
579
|
+
return pretty ? '<>' : valstr
|
|
580
|
+
}
|
|
581
|
+
if (S_string === typeof val) {
|
|
582
|
+
valstr = val
|
|
583
|
+
} else {
|
|
584
|
+
try {
|
|
585
|
+
valstr = JSON.stringify(val, function (_key, val) {
|
|
586
|
+
if (val !== null && typeof val === 'object' && !Array.isArray(val)) {
|
|
587
|
+
// Emit map keys in sorted order without the intermediate key/tuple
|
|
588
|
+
// arrays items() would allocate for every node.
|
|
589
|
+
const keys = Object.keys(val).sort()
|
|
590
|
+
const sortedObj = {}
|
|
591
|
+
for (let i = 0; i < keys.length; i++) {
|
|
592
|
+
sortedObj[keys[i]] = val[keys[i]]
|
|
593
|
+
}
|
|
594
|
+
return sortedObj
|
|
595
|
+
}
|
|
596
|
+
return val
|
|
597
|
+
})
|
|
598
|
+
valstr = valstr.replace(R_QUOTES, S_MT)
|
|
599
|
+
} catch {
|
|
600
|
+
valstr = '__STRINGIFY_FAILED__'
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
if (null != maxlen && -1 < maxlen) {
|
|
604
|
+
let js = valstr.substring(0, maxlen)
|
|
605
|
+
valstr = maxlen < valstr.length ? js.substring(0, maxlen - 3) + '...' : valstr
|
|
606
|
+
}
|
|
607
|
+
if (pretty) {
|
|
608
|
+
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
609
|
+
let c = items(
|
|
610
|
+
[81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69],
|
|
611
|
+
(n) => '\x1b[38;5;' + n[1] + 'm',
|
|
612
|
+
),
|
|
613
|
+
r = '\x1b[0m',
|
|
614
|
+
d = 0,
|
|
615
|
+
o = c[0],
|
|
616
|
+
t = o
|
|
617
|
+
for (const ch of valstr) {
|
|
618
|
+
if (ch === '{' || ch === '[') {
|
|
619
|
+
d++
|
|
620
|
+
o = c[d % c.length]
|
|
621
|
+
t += o + ch
|
|
622
|
+
} else if (ch === '}' || ch === ']') {
|
|
623
|
+
t += o + ch
|
|
624
|
+
d--
|
|
625
|
+
o = c[d % c.length]
|
|
626
|
+
} else {
|
|
627
|
+
t += o + ch
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return t + r
|
|
631
|
+
}
|
|
632
|
+
return valstr
|
|
633
|
+
}
|
|
634
|
+
// Build a human friendly path string.
|
|
635
|
+
function pathify(val, startin, endin) {
|
|
636
|
+
let pathstr = NONE
|
|
637
|
+
let path = islist(val)
|
|
638
|
+
? val
|
|
639
|
+
: S_string == typeof val
|
|
640
|
+
? [val]
|
|
641
|
+
: S_number == typeof val
|
|
642
|
+
? [val]
|
|
643
|
+
: NONE
|
|
644
|
+
const start = null == startin ? 0 : -1 < startin ? startin : 0
|
|
645
|
+
const end = null == endin ? 0 : -1 < endin ? endin : 0
|
|
646
|
+
if (NONE != path && 0 <= start) {
|
|
647
|
+
path = slice(path, start, path.length - end)
|
|
648
|
+
if (0 === path.length) {
|
|
649
|
+
pathstr = '<root>'
|
|
650
|
+
} else {
|
|
651
|
+
pathstr = join(
|
|
652
|
+
items(
|
|
653
|
+
filter(path, (n) => iskey(n[1])),
|
|
654
|
+
(n) => {
|
|
655
|
+
let p = n[1]
|
|
656
|
+
return S_number === typeof p ? S_MT + Math.floor(p) : p.replace(R_DOT, S_MT)
|
|
657
|
+
},
|
|
658
|
+
),
|
|
659
|
+
S_DT,
|
|
660
|
+
)
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
if (NONE === pathstr) {
|
|
664
|
+
pathstr = '<unknown-path' + (NONE === val ? S_MT : S_CN + stringify(val, 47)) + '>'
|
|
665
|
+
}
|
|
666
|
+
return pathstr
|
|
667
|
+
}
|
|
668
|
+
// Clone a JSON-like data structure.
|
|
669
|
+
// NOTE: function and instance values are copied, *not* cloned.
|
|
670
|
+
function clone(val) {
|
|
671
|
+
const refs = []
|
|
672
|
+
// Copy (don't clone) functions and non-plain-object instances by reference.
|
|
673
|
+
// Inlines `0 < ((T_function | T_instance) & typify(v))` (functions, class
|
|
674
|
+
// instances, bigints) without a typify() call per value.
|
|
675
|
+
const replacer = (_k, v) => {
|
|
676
|
+
const t = typeof v
|
|
677
|
+
if (
|
|
678
|
+
S_function === t ||
|
|
679
|
+
'bigint' === t ||
|
|
680
|
+
(S_object === t &&
|
|
681
|
+
null !== v &&
|
|
682
|
+
!Array.isArray(v) &&
|
|
683
|
+
v.constructor instanceof Function &&
|
|
684
|
+
'Object' !== v.constructor.name &&
|
|
685
|
+
'Array' !== v.constructor.name)
|
|
686
|
+
) {
|
|
687
|
+
return (refs.push(v), '`$REF:' + (refs.length - 1) + '`')
|
|
688
|
+
}
|
|
689
|
+
return v
|
|
690
|
+
}
|
|
691
|
+
const reviver = (_k, v, m) =>
|
|
692
|
+
S_string === typeof v ? ((m = v.match(R_CLONE_REF)), m ? refs[m[1]] : v) : v
|
|
693
|
+
if (NONE === val) {
|
|
694
|
+
return NONE
|
|
695
|
+
}
|
|
696
|
+
const json = JSON.stringify(val, replacer)
|
|
697
|
+
// A reviver forces V8 off its native JSON.parse fast path (a large cost),
|
|
698
|
+
// and is only needed to swap `$REF:n` placeholders back for the captured
|
|
699
|
+
// functions/instances — so skip it when none were captured.
|
|
700
|
+
return 0 === refs.length ? JSON.parse(json) : JSON.parse(json, reviver)
|
|
701
|
+
}
|
|
702
|
+
// Define a JSON Object using function arguments.
|
|
703
|
+
function jm(...kv) {
|
|
704
|
+
const kvsize = size(kv)
|
|
705
|
+
const o = {}
|
|
706
|
+
for (let i = 0; i < kvsize; i += 2) {
|
|
707
|
+
// Builders are Group B (value processing): preserve literal stored
|
|
708
|
+
// values, including null. Direct array index distinguishes "slot exists
|
|
709
|
+
// with value null" from "slot beyond end of kv".
|
|
710
|
+
let k = i < kv.length ? kv[i] : '$KEY' + i
|
|
711
|
+
k = 'string' === typeof k ? k : stringify(k)
|
|
712
|
+
o[k] = i + 1 < kv.length ? kv[i + 1] : null
|
|
713
|
+
}
|
|
714
|
+
return o
|
|
715
|
+
}
|
|
716
|
+
// Define a JSON Array using function arguments.
|
|
717
|
+
function jt(...v) {
|
|
718
|
+
const vsize = size(v)
|
|
719
|
+
const a = new Array(vsize)
|
|
720
|
+
for (let i = 0; i < vsize; i++) {
|
|
721
|
+
a[i] = getprop(v, i, null)
|
|
722
|
+
}
|
|
723
|
+
return a
|
|
724
|
+
}
|
|
725
|
+
// Safely delete a property from an object or array element.
|
|
726
|
+
// Undefined arguments and invalid keys are ignored.
|
|
727
|
+
// Returns the (possibly modified) parent.
|
|
728
|
+
// For objects, the property is deleted using the delete operator.
|
|
729
|
+
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
730
|
+
// NOTE: parent list may be new list, thus update references.
|
|
731
|
+
function delprop(parent, key) {
|
|
732
|
+
if (!iskey(key)) {
|
|
733
|
+
return parent
|
|
734
|
+
}
|
|
735
|
+
if (ismap(parent)) {
|
|
736
|
+
key = strkey(key)
|
|
737
|
+
delete parent[key]
|
|
738
|
+
} else if (islist(parent)) {
|
|
739
|
+
// Ensure key is an integer.
|
|
740
|
+
let keyI = +key
|
|
741
|
+
if (isNaN(keyI)) {
|
|
742
|
+
return parent
|
|
743
|
+
}
|
|
744
|
+
keyI = Math.floor(keyI)
|
|
745
|
+
// Delete list element at position keyI, shifting later elements down.
|
|
746
|
+
const psize = size(parent)
|
|
747
|
+
if (0 <= keyI && keyI < psize) {
|
|
748
|
+
for (let pI = keyI; pI < psize - 1; pI++) {
|
|
749
|
+
parent[pI] = parent[pI + 1]
|
|
750
|
+
}
|
|
751
|
+
parent.length = parent.length - 1
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return parent
|
|
755
|
+
}
|
|
756
|
+
// Safely set a property. Undefined arguments and invalid keys are ignored.
|
|
757
|
+
// Returns the (possibly modified) parent.
|
|
758
|
+
// If the parent is a list, and the key is negative, prepend the value.
|
|
759
|
+
// NOTE: If the key is above the list size, append the value; below, prepend.
|
|
760
|
+
// NOTE: parent list may be new list, thus update references.
|
|
761
|
+
function setprop(parent, key, val) {
|
|
762
|
+
if (!iskey(key)) {
|
|
763
|
+
return parent
|
|
764
|
+
}
|
|
765
|
+
if (ismap(parent)) {
|
|
766
|
+
key = S_MT + key
|
|
767
|
+
const pany = parent
|
|
768
|
+
pany[key] = val
|
|
769
|
+
} else if (islist(parent)) {
|
|
770
|
+
// Ensure key is an integer.
|
|
771
|
+
let keyI = +key
|
|
772
|
+
if (isNaN(keyI)) {
|
|
773
|
+
return parent
|
|
774
|
+
}
|
|
775
|
+
keyI = Math.floor(keyI)
|
|
776
|
+
// TODO: DELETE list element
|
|
777
|
+
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
778
|
+
if (0 <= keyI) {
|
|
779
|
+
parent[slice(keyI, 0, size(parent) + 1)] = val
|
|
780
|
+
}
|
|
781
|
+
// Prepend value if keyI is negative
|
|
782
|
+
else {
|
|
783
|
+
parent.unshift(val)
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return parent
|
|
787
|
+
}
|
|
788
|
+
// Walk a data structure depth first, applying a function to each value.
|
|
789
|
+
// The `path` argument passed to the before/after callbacks is a single
|
|
790
|
+
// mutable array per depth, shared across all callback invocations for the
|
|
791
|
+
// lifetime of this top-level walk call. Callbacks that need to store the
|
|
792
|
+
// path MUST clone it (e.g. `path.slice()`); the contents will otherwise
|
|
793
|
+
// be overwritten by subsequent visits.
|
|
794
|
+
function walk(
|
|
795
|
+
// These arguments are the public interface.
|
|
796
|
+
val,
|
|
797
|
+
// Before descending into a node.
|
|
798
|
+
before,
|
|
799
|
+
// After descending into a node.
|
|
800
|
+
after,
|
|
801
|
+
// Maximum recursive depth, default: 32. Use null for infinite depth.
|
|
802
|
+
maxdepth,
|
|
803
|
+
// These areguments are used for recursive state.
|
|
804
|
+
key,
|
|
805
|
+
parent,
|
|
806
|
+
path,
|
|
807
|
+
pool,
|
|
808
|
+
) {
|
|
809
|
+
if (NONE === pool) {
|
|
810
|
+
pool = [[]]
|
|
811
|
+
}
|
|
812
|
+
if (NONE === path) {
|
|
813
|
+
path = pool[0]
|
|
814
|
+
}
|
|
815
|
+
const depth = path.length
|
|
816
|
+
let out = null == before ? val : before(key, val, parent, path)
|
|
817
|
+
maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : MAXDEPTH
|
|
818
|
+
if (0 === maxdepth || (0 < maxdepth && maxdepth <= depth)) {
|
|
819
|
+
return out
|
|
820
|
+
}
|
|
821
|
+
if (isnode(out)) {
|
|
822
|
+
const childDepth = depth + 1
|
|
823
|
+
let childPath = pool[childDepth]
|
|
824
|
+
if (NONE === childPath) {
|
|
825
|
+
childPath = new Array(childDepth)
|
|
826
|
+
pool[childDepth] = childPath
|
|
827
|
+
}
|
|
828
|
+
// Sync prefix [0..depth-1] from the current path. Only needed once per
|
|
829
|
+
// parent: siblings share the same prefix and will each overwrite slot
|
|
830
|
+
// [depth] below.
|
|
831
|
+
for (let i = 0; i < depth; i++) {
|
|
832
|
+
childPath[i] = path[i]
|
|
833
|
+
}
|
|
834
|
+
// Iterate the sorted keys directly (as items() would order them) without
|
|
835
|
+
// building the intermediate [key, value] tuple array per node.
|
|
836
|
+
const ckeys = keysof(out)
|
|
837
|
+
for (let cI = 0; cI < ckeys.length; cI++) {
|
|
838
|
+
const ckey = ckeys[cI]
|
|
839
|
+
childPath[depth] = S_MT + ckey
|
|
840
|
+
setprop(out, ckey, walk(out[ckey], before, after, maxdepth, ckey, out, childPath, pool))
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
out = null == after ? out : after(key, out, parent, path)
|
|
844
|
+
return out
|
|
845
|
+
}
|
|
846
|
+
// Merge a list of values into each other. Later values have
|
|
847
|
+
// precedence. Nodes override scalars. Node kinds (list or map)
|
|
848
|
+
// override each other, and do *not* merge. The first element is
|
|
849
|
+
// modified.
|
|
850
|
+
function merge(val, maxdepth) {
|
|
851
|
+
// const md: number = null == maxdepth ? MAXDEPTH : maxdepth < 0 ? 0 : maxdepth
|
|
852
|
+
const md = slice(maxdepth ?? MAXDEPTH, 0)
|
|
853
|
+
let out = NONE
|
|
854
|
+
// Handle edge cases.
|
|
855
|
+
if (!islist(val)) {
|
|
856
|
+
return val
|
|
857
|
+
}
|
|
858
|
+
const list = val
|
|
859
|
+
const lenlist = list.length
|
|
860
|
+
if (0 === lenlist) {
|
|
861
|
+
return NONE
|
|
862
|
+
} else if (1 === lenlist) {
|
|
863
|
+
return list[0]
|
|
864
|
+
}
|
|
865
|
+
// Merge a list of values.
|
|
866
|
+
out = getprop(list, 0, {})
|
|
867
|
+
for (let oI = 1; oI < lenlist; oI++) {
|
|
868
|
+
let obj = list[oI]
|
|
869
|
+
if (!isnode(obj)) {
|
|
870
|
+
// Nodes win.
|
|
871
|
+
out = obj
|
|
872
|
+
} else {
|
|
873
|
+
// Current value at path end in overriding node.
|
|
874
|
+
let cur = [out]
|
|
875
|
+
// Current value at path end in destination node.
|
|
876
|
+
let dst = [out]
|
|
877
|
+
function before(key, val, _parent, path) {
|
|
878
|
+
const pI = size(path)
|
|
879
|
+
if (md <= pI) {
|
|
880
|
+
setprop(cur[pI - 1], key, val)
|
|
881
|
+
}
|
|
882
|
+
// Scalars just override directly.
|
|
883
|
+
else if (!isnode(val)) {
|
|
884
|
+
cur[pI] = val
|
|
885
|
+
}
|
|
886
|
+
// Descend into override node - Set up correct target in `after` function.
|
|
887
|
+
else {
|
|
888
|
+
// Descend into destination node using same key.
|
|
889
|
+
dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI]
|
|
890
|
+
const tval = dst[pI]
|
|
891
|
+
const vtype = typify(val)
|
|
892
|
+
// Destination empty, so create node (unless override is class instance).
|
|
893
|
+
if (NONE === tval && 0 === (T_instance & vtype)) {
|
|
894
|
+
cur[pI] = islist(val) ? [] : {}
|
|
895
|
+
}
|
|
896
|
+
// Matching override and destination so continue with their values.
|
|
897
|
+
else if (vtype === typify(tval)) {
|
|
898
|
+
cur[pI] = tval
|
|
899
|
+
}
|
|
900
|
+
// Override wins.
|
|
901
|
+
else {
|
|
902
|
+
cur[pI] = val
|
|
903
|
+
// No need to descend when override wins (destination is discarded).
|
|
904
|
+
val = NONE
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
// console.log('BEFORE-END', pathify(path), '@', pI, key,
|
|
908
|
+
// stringify(val, -1, 1), stringify(parent, -1, 1),
|
|
909
|
+
// 'CUR=', stringify(cur, -1, 1), 'DST=', stringify(dst, -1, 1))
|
|
910
|
+
return val
|
|
911
|
+
}
|
|
912
|
+
function after(key, _val, _parent, path) {
|
|
913
|
+
const cI = size(path)
|
|
914
|
+
const target = cur[cI - 1]
|
|
915
|
+
const value = cur[cI]
|
|
916
|
+
// console.log('AFTER-PREP', pathify(path), '@', cI, cur, '|',
|
|
917
|
+
// stringify(key, -1, 1), stringify(value, -1, 1), 'T=', stringify(target, -1, 1))
|
|
918
|
+
setprop(target, key, value)
|
|
919
|
+
return value
|
|
920
|
+
}
|
|
921
|
+
// Walk overriding node, creating paths in output as needed.
|
|
922
|
+
out = walk(obj, before, after, maxdepth)
|
|
923
|
+
// console.log('WALK-DONE', out, obj)
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (0 === md) {
|
|
927
|
+
out = getelem(list, -1)
|
|
928
|
+
out = islist(out) ? [] : ismap(out) ? {} : out
|
|
929
|
+
}
|
|
930
|
+
return out
|
|
931
|
+
}
|
|
932
|
+
// Set a value using a path. Missing path parts are created.
|
|
933
|
+
// String paths create only maps. Use a string list to create list parts.
|
|
934
|
+
function setpath(store, path, val, injdef) {
|
|
935
|
+
const pathType = typify(path)
|
|
936
|
+
const parts =
|
|
937
|
+
0 < (T_list & pathType)
|
|
938
|
+
? path
|
|
939
|
+
: 0 < (T_string & pathType)
|
|
940
|
+
? path.split(S_DT)
|
|
941
|
+
: 0 < (T_number & pathType)
|
|
942
|
+
? [path]
|
|
943
|
+
: NONE
|
|
944
|
+
if (NONE === parts) {
|
|
945
|
+
return NONE
|
|
946
|
+
}
|
|
947
|
+
const base = getprop(injdef, S_base)
|
|
948
|
+
const numparts = size(parts)
|
|
949
|
+
let parent = getprop(store, base, store)
|
|
950
|
+
for (let pI = 0; pI < numparts - 1; pI++) {
|
|
951
|
+
const partKey = getelem(parts, pI)
|
|
952
|
+
let nextParent = getprop(parent, partKey)
|
|
953
|
+
if (!isnode(nextParent)) {
|
|
954
|
+
nextParent = 0 < (T_number & typify(getelem(parts, pI + 1))) ? [] : {}
|
|
955
|
+
setprop(parent, partKey, nextParent)
|
|
956
|
+
}
|
|
957
|
+
parent = nextParent
|
|
958
|
+
}
|
|
959
|
+
if (DELETE === val) {
|
|
960
|
+
delprop(parent, getelem(parts, -1))
|
|
961
|
+
} else {
|
|
962
|
+
setprop(parent, getelem(parts, -1), val)
|
|
963
|
+
}
|
|
964
|
+
return parent
|
|
965
|
+
}
|
|
966
|
+
function getpath(store, path, injdef) {
|
|
967
|
+
// Operate on a string array.
|
|
968
|
+
const parts = islist(path)
|
|
969
|
+
? path
|
|
970
|
+
: 'string' === typeof path
|
|
971
|
+
? path.split(S_DT)
|
|
972
|
+
: 'number' === typeof path
|
|
973
|
+
? [strkey(path)]
|
|
974
|
+
: NONE
|
|
975
|
+
if (NONE === parts) {
|
|
976
|
+
return NONE
|
|
977
|
+
}
|
|
978
|
+
// let root = store
|
|
979
|
+
let val = store
|
|
980
|
+
const base = getprop(injdef, S_base)
|
|
981
|
+
const src = getprop(store, base, store)
|
|
982
|
+
const numparts = size(parts)
|
|
983
|
+
const dparent = getprop(injdef, 'dparent')
|
|
984
|
+
// An empty path (incl empty string) just finds the store.
|
|
985
|
+
if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
|
|
986
|
+
val = src
|
|
987
|
+
} else if (0 < numparts) {
|
|
988
|
+
// Check for $ACTIONs
|
|
989
|
+
if (1 === numparts) {
|
|
990
|
+
val = getprop(store, parts[0])
|
|
991
|
+
}
|
|
992
|
+
if (!isfunc(val)) {
|
|
993
|
+
val = src
|
|
994
|
+
const m = parts[0].match(R_META_PATH)
|
|
995
|
+
if (m && injdef && injdef.meta) {
|
|
996
|
+
val = getprop(injdef.meta, m[1])
|
|
997
|
+
parts[0] = m[3]
|
|
998
|
+
}
|
|
999
|
+
const dpath = getprop(injdef, 'dpath')
|
|
1000
|
+
for (let pI = 0; NONE !== val && pI < numparts; pI++) {
|
|
1001
|
+
let part = parts[pI]
|
|
1002
|
+
if (injdef && S_DKEY === part) {
|
|
1003
|
+
part = getprop(injdef, S_key)
|
|
1004
|
+
} else if (injdef && part.startsWith('$GET:')) {
|
|
1005
|
+
// $GET:path$ -> get store value, use as path part (string)
|
|
1006
|
+
part = stringify(getpath(src, slice(part, 5, -1)))
|
|
1007
|
+
} else if (injdef && part.startsWith('$REF:')) {
|
|
1008
|
+
// $REF:refpath$ -> get spec value, use as path part (string)
|
|
1009
|
+
part = stringify(getpath(getprop(store, S_DSPEC), slice(part, 5, -1)))
|
|
1010
|
+
} else if (injdef && part.startsWith('$META:')) {
|
|
1011
|
+
// $META:metapath$ -> get meta value, use as path part (string)
|
|
1012
|
+
part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)))
|
|
1013
|
+
}
|
|
1014
|
+
// $$ escapes $ (skip the regex for the common no-`$$` segment).
|
|
1015
|
+
if (-1 !== part.indexOf('$$')) {
|
|
1016
|
+
part = part.replace(R_DOUBLE_DOLLAR, '$')
|
|
1017
|
+
}
|
|
1018
|
+
if (S_MT === part) {
|
|
1019
|
+
let ascends = 0
|
|
1020
|
+
while (S_MT === parts[1 + pI]) {
|
|
1021
|
+
ascends++
|
|
1022
|
+
pI++
|
|
1023
|
+
}
|
|
1024
|
+
if (injdef && 0 < ascends) {
|
|
1025
|
+
if (pI === parts.length - 1) {
|
|
1026
|
+
ascends--
|
|
1027
|
+
}
|
|
1028
|
+
if (0 === ascends) {
|
|
1029
|
+
val = dparent
|
|
1030
|
+
} else {
|
|
1031
|
+
// const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
1032
|
+
const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)])
|
|
1033
|
+
if (ascends <= size(dpath)) {
|
|
1034
|
+
val = getpath(store, fullpath)
|
|
1035
|
+
} else {
|
|
1036
|
+
val = NONE
|
|
1037
|
+
}
|
|
1038
|
+
break
|
|
1039
|
+
}
|
|
1040
|
+
} else {
|
|
1041
|
+
val = dparent
|
|
1042
|
+
}
|
|
1043
|
+
} else {
|
|
1044
|
+
val = getprop(val, part)
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
// Inj may provide a custom handler to modify found value.
|
|
1050
|
+
const handler = getprop(injdef, 'handler')
|
|
1051
|
+
if (null != injdef && isfunc(handler)) {
|
|
1052
|
+
const ref = pathify(path)
|
|
1053
|
+
val = handler(injdef, val, ref, store)
|
|
1054
|
+
}
|
|
1055
|
+
// console.log('GETPATH', path, val)
|
|
1056
|
+
return val
|
|
1057
|
+
}
|
|
1058
|
+
// Inject values from a data store into a node recursively, resolving
|
|
1059
|
+
// paths against the store, or current if they are local. The modify
|
|
1060
|
+
// argument allows custom modification of the result. The inj
|
|
1061
|
+
// (Injection) argument is used to maintain recursive state.
|
|
1062
|
+
function inject(val, store, injdef) {
|
|
1063
|
+
const valtype = typeof val
|
|
1064
|
+
let inj = injdef
|
|
1065
|
+
// Create state if at root of injection. The input value is placed
|
|
1066
|
+
// inside a virtual parent holder to simplify edge cases.
|
|
1067
|
+
if (NONE === injdef || null == injdef.mode) {
|
|
1068
|
+
// Set up state assuming we are starting in the virtual parent.
|
|
1069
|
+
inj = new Injection(val, { [S_DTOP]: val })
|
|
1070
|
+
inj.dparent = store
|
|
1071
|
+
inj.errs = getprop(store, S_DERRS, [])
|
|
1072
|
+
inj.meta.__d = 0
|
|
1073
|
+
if (NONE !== injdef) {
|
|
1074
|
+
inj.modify = null == injdef.modify ? inj.modify : injdef.modify
|
|
1075
|
+
inj.extra = null == injdef.extra ? inj.extra : injdef.extra
|
|
1076
|
+
inj.meta = null == injdef.meta ? inj.meta : injdef.meta
|
|
1077
|
+
inj.handler = null == injdef.handler ? inj.handler : injdef.handler
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
inj.descend()
|
|
1081
|
+
// console.log('INJ-START', val, inj.mode, inj.key, inj.val,
|
|
1082
|
+
// 't=', inj.path, 'P=', inj.parent, 'dp=', inj.dparent, 'ST=', store.$TOP)
|
|
1083
|
+
// Descend into node.
|
|
1084
|
+
if (isnode(val)) {
|
|
1085
|
+
// Keys are sorted alphanumerically to ensure determinism.
|
|
1086
|
+
// Injection transforms ($FOO) are processed *after* other keys.
|
|
1087
|
+
// NOTE: the optional digits suffix of the transform can thus be
|
|
1088
|
+
// used to order the transforms.
|
|
1089
|
+
let nodekeys
|
|
1090
|
+
nodekeys = keysof(val)
|
|
1091
|
+
if (ismap(val)) {
|
|
1092
|
+
nodekeys = flatten([
|
|
1093
|
+
filter(nodekeys, (n) => !n[1].includes(S_DS)),
|
|
1094
|
+
filter(nodekeys, (n) => n[1].includes(S_DS)),
|
|
1095
|
+
])
|
|
1096
|
+
} else {
|
|
1097
|
+
nodekeys = keysof(val)
|
|
1098
|
+
}
|
|
1099
|
+
// Each child key-value pair is processed in three injection phases:
|
|
1100
|
+
// 1. inj.mode=M_KEYPRE - Key string is injected, returning a possibly altered key.
|
|
1101
|
+
// 2. inj.mode=M_VAL - The child value is injected.
|
|
1102
|
+
// 3. inj.mode=M_KEYPOST - Key string is injected again, allowing child mutation.
|
|
1103
|
+
for (let nkI = 0; nkI < nodekeys.length; nkI++) {
|
|
1104
|
+
const childinj = inj.child(nkI, nodekeys)
|
|
1105
|
+
const nodekey = childinj.key
|
|
1106
|
+
childinj.mode = M_KEYPRE
|
|
1107
|
+
// Peform the key:pre mode injection on the child key.
|
|
1108
|
+
const prekey = _injectstr(nodekey, store, childinj)
|
|
1109
|
+
// The injection may modify child processing.
|
|
1110
|
+
nkI = childinj.keyI
|
|
1111
|
+
nodekeys = childinj.keys
|
|
1112
|
+
// Prevent further processing by returning an undefined prekey
|
|
1113
|
+
if (NONE !== prekey) {
|
|
1114
|
+
childinj.val = getprop(val, prekey)
|
|
1115
|
+
childinj.mode = M_VAL
|
|
1116
|
+
// Perform the val mode injection on the child value.
|
|
1117
|
+
// NOTE: return value is not used.
|
|
1118
|
+
inject(childinj.val, store, childinj)
|
|
1119
|
+
// The injection may modify child processing.
|
|
1120
|
+
nkI = childinj.keyI
|
|
1121
|
+
nodekeys = childinj.keys
|
|
1122
|
+
// Peform the key:post mode injection on the child key.
|
|
1123
|
+
childinj.mode = M_KEYPOST
|
|
1124
|
+
_injectstr(nodekey, store, childinj)
|
|
1125
|
+
// The injection may modify child processing.
|
|
1126
|
+
nkI = childinj.keyI
|
|
1127
|
+
nodekeys = childinj.keys
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
// Inject paths into string scalars.
|
|
1132
|
+
else if (S_string === valtype) {
|
|
1133
|
+
inj.mode = M_VAL
|
|
1134
|
+
val = _injectstr(val, store, inj)
|
|
1135
|
+
if (SKIP !== val) {
|
|
1136
|
+
inj.setval(val)
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
// Custom modification.
|
|
1140
|
+
if (inj.modify && SKIP !== val) {
|
|
1141
|
+
let mkey = inj.key
|
|
1142
|
+
let mparent = inj.parent
|
|
1143
|
+
let mval = getprop(mparent, mkey)
|
|
1144
|
+
inj.modify(mval, mkey, mparent, inj, store)
|
|
1145
|
+
}
|
|
1146
|
+
// console.log('INJ-VAL', val)
|
|
1147
|
+
inj.val = val
|
|
1148
|
+
// Original val reference may no longer be correct.
|
|
1149
|
+
// This return value is only used as the top level result.
|
|
1150
|
+
return _lookup(inj.parent, S_DTOP)
|
|
1151
|
+
}
|
|
1152
|
+
// The transform_* functions are special command inject handlers (see Injector).
|
|
1153
|
+
// Delete a key from a map or list.
|
|
1154
|
+
const transform_DELETE = (inj) => {
|
|
1155
|
+
inj.setval(NONE)
|
|
1156
|
+
return NONE
|
|
1157
|
+
}
|
|
1158
|
+
// Copy value from source data.
|
|
1159
|
+
const transform_COPY = (inj, _val) => {
|
|
1160
|
+
const ijname = 'COPY'
|
|
1161
|
+
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
1162
|
+
return NONE
|
|
1163
|
+
}
|
|
1164
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1165
|
+
inj.setval(out)
|
|
1166
|
+
return out
|
|
1167
|
+
}
|
|
1168
|
+
// As a value, inject the key of the parent node.
|
|
1169
|
+
// As a key, defined the name of the key property in the source object.
|
|
1170
|
+
const transform_KEY = (inj) => {
|
|
1171
|
+
const { mode, path, parent } = inj
|
|
1172
|
+
// Do nothing in val mode - not an error.
|
|
1173
|
+
if (M_VAL !== mode) {
|
|
1174
|
+
return NONE
|
|
1175
|
+
}
|
|
1176
|
+
// Key is defined by $KEY meta property.
|
|
1177
|
+
const keyspec = _lookup(parent, S_BKEY)
|
|
1178
|
+
if (NONE !== keyspec) {
|
|
1179
|
+
delprop(parent, S_BKEY)
|
|
1180
|
+
return _lookup(inj.dparent, keyspec)
|
|
1181
|
+
}
|
|
1182
|
+
// Key is defined within general purpose $META object.
|
|
1183
|
+
return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2)
|
|
1184
|
+
}
|
|
1185
|
+
// Annotate node. Does nothing itself, just used by
|
|
1186
|
+
// other injectors, and is removed when called.
|
|
1187
|
+
const transform_ANNO = (inj) => {
|
|
1188
|
+
const { parent } = inj
|
|
1189
|
+
delprop(parent, S_BANNO)
|
|
1190
|
+
return NONE
|
|
1191
|
+
}
|
|
1192
|
+
// Merge a list of objects into the current object.
|
|
1193
|
+
// Must be a key in an object. The value is merged over the current object.
|
|
1194
|
+
// If the value is an array, the elements are first merged using `merge`.
|
|
1195
|
+
// If the value is the empty string, merge the top level store.
|
|
1196
|
+
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
1197
|
+
const transform_MERGE = (inj) => {
|
|
1198
|
+
const { mode, key, parent } = inj
|
|
1199
|
+
// Ensures $MERGE is removed from parent list (val mode).
|
|
1200
|
+
let out = NONE
|
|
1201
|
+
if (M_KEYPRE === mode) {
|
|
1202
|
+
out = key
|
|
1203
|
+
}
|
|
1204
|
+
// Operate after child values have been transformed.
|
|
1205
|
+
else if (M_KEYPOST === mode) {
|
|
1206
|
+
out = key
|
|
1207
|
+
let args = getprop(parent, key)
|
|
1208
|
+
args = Array.isArray(args) ? args : [args]
|
|
1209
|
+
// Remove the $MERGE command from a parent map.
|
|
1210
|
+
inj.setval(NONE)
|
|
1211
|
+
// Literals in the parent have precedence, but we still merge onto
|
|
1212
|
+
// the parent object, so that node tree references are not changed.
|
|
1213
|
+
const mergelist = flatten([[parent], args, [clone(parent)]])
|
|
1214
|
+
merge(mergelist)
|
|
1215
|
+
}
|
|
1216
|
+
return out
|
|
1217
|
+
}
|
|
1218
|
+
// Convert a node to a list.
|
|
1219
|
+
// Format: ['`$EACH`', '`source-path-of-node`', child-template]
|
|
1220
|
+
const transform_EACH = (inj, _val, _ref, store) => {
|
|
1221
|
+
const ijname = 'EACH'
|
|
1222
|
+
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1223
|
+
return NONE
|
|
1224
|
+
}
|
|
1225
|
+
// Remove remaining keys to avoid spurious processing.
|
|
1226
|
+
slice(inj.keys, 0, 1, true)
|
|
1227
|
+
// const [err, srcpath, child] = injectorArgs([T_string, T_any], inj)
|
|
1228
|
+
const [err, srcpath, child] = injectorArgs([T_string, T_any], slice(inj.parent, 1))
|
|
1229
|
+
if (NONE !== err) {
|
|
1230
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1231
|
+
return NONE
|
|
1232
|
+
}
|
|
1233
|
+
// Source data.
|
|
1234
|
+
const srcstore = getprop(store, inj.base, store)
|
|
1235
|
+
const src = getpath(srcstore, srcpath, inj)
|
|
1236
|
+
const srctype = typify(src)
|
|
1237
|
+
// Create parallel data structures:
|
|
1238
|
+
// source entries :: child templates
|
|
1239
|
+
let tcur = []
|
|
1240
|
+
let tval = []
|
|
1241
|
+
const tkey = getelem(inj.path, -2)
|
|
1242
|
+
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
|
|
1243
|
+
// Create clones of the child template for each value of the current soruce.
|
|
1244
|
+
if (0 < (T_list & srctype)) {
|
|
1245
|
+
tval = items(src, () => clone(child))
|
|
1246
|
+
} else if (0 < (T_map & srctype)) {
|
|
1247
|
+
tval = items(src, (n) =>
|
|
1248
|
+
merge(
|
|
1249
|
+
[
|
|
1250
|
+
clone(child),
|
|
1251
|
+
// Make a note of the key for $KEY transforms.
|
|
1252
|
+
{ [S_BANNO]: { KEY: n[0] } },
|
|
1253
|
+
],
|
|
1254
|
+
1,
|
|
1255
|
+
),
|
|
1256
|
+
)
|
|
1257
|
+
}
|
|
1258
|
+
let rval = []
|
|
1259
|
+
if (0 < size(tval)) {
|
|
1260
|
+
tcur = null == src ? NONE : Object.values(src)
|
|
1261
|
+
const ckey = getelem(inj.path, -2)
|
|
1262
|
+
const tpath = slice(inj.path, -1)
|
|
1263
|
+
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey])
|
|
1264
|
+
// Parent structure.
|
|
1265
|
+
tcur = { [ckey]: tcur }
|
|
1266
|
+
if (1 < size(tpath)) {
|
|
1267
|
+
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1268
|
+
tcur = { [pkey]: tcur }
|
|
1269
|
+
dpath.push('$:' + pkey)
|
|
1270
|
+
}
|
|
1271
|
+
const tinj = inj.child(0, [ckey])
|
|
1272
|
+
tinj.path = tpath
|
|
1273
|
+
tinj.nodes = slice(inj.nodes, -1)
|
|
1274
|
+
tinj.parent = getelem(tinj.nodes, -1)
|
|
1275
|
+
setprop(tinj.parent, ckey, tval)
|
|
1276
|
+
tinj.val = tval
|
|
1277
|
+
tinj.dpath = dpath
|
|
1278
|
+
tinj.dparent = tcur
|
|
1279
|
+
inject(tval, store, tinj)
|
|
1280
|
+
rval = tinj.val
|
|
1281
|
+
}
|
|
1282
|
+
// _updateAncestors(inj, target, tkey, rval)
|
|
1283
|
+
setprop(target, tkey, rval)
|
|
1284
|
+
// Prevent callee from damaging first list entry (since we are in `val` mode).
|
|
1285
|
+
return rval[0]
|
|
1286
|
+
}
|
|
1287
|
+
// Convert a node to a map.
|
|
1288
|
+
// Format: { '`$PACK`':['source-path', child-template]}
|
|
1289
|
+
const transform_PACK = (inj, _val, _ref, store) => {
|
|
1290
|
+
const { key, path, parent, nodes } = inj
|
|
1291
|
+
const ijname = 'EACH'
|
|
1292
|
+
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1293
|
+
return NONE
|
|
1294
|
+
}
|
|
1295
|
+
// Get arguments.
|
|
1296
|
+
const args = getprop(parent, key)
|
|
1297
|
+
const [err, srcpath, origchildspec] = injectorArgs([T_string, T_any], args)
|
|
1298
|
+
if (NONE !== err) {
|
|
1299
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1300
|
+
return NONE
|
|
1301
|
+
}
|
|
1302
|
+
// Find key and target node.
|
|
1303
|
+
const tkey = getelem(path, -2)
|
|
1304
|
+
const pathsize = size(path)
|
|
1305
|
+
const target = getelem(nodes, pathsize - 2, () => getelem(nodes, pathsize - 1))
|
|
1306
|
+
// Source data
|
|
1307
|
+
const srcstore = getprop(store, inj.base, store)
|
|
1308
|
+
let src = getpath(srcstore, srcpath, inj)
|
|
1309
|
+
// Prepare source as a list.
|
|
1310
|
+
if (!islist(src)) {
|
|
1311
|
+
if (ismap(src)) {
|
|
1312
|
+
src = items(src, (item) => {
|
|
1313
|
+
setprop(item[1], S_BANNO, { KEY: item[0] })
|
|
1314
|
+
return item[1]
|
|
1315
|
+
})
|
|
1316
|
+
} else {
|
|
1317
|
+
src = NONE
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
if (null == src) {
|
|
1321
|
+
return NONE
|
|
1322
|
+
}
|
|
1323
|
+
// Get keypath.
|
|
1324
|
+
const keypath = getprop(origchildspec, S_BKEY)
|
|
1325
|
+
const childspec = delprop(origchildspec, S_BKEY)
|
|
1326
|
+
const child = getprop(childspec, S_BVAL, childspec)
|
|
1327
|
+
// Build parallel target object.
|
|
1328
|
+
let tval = {}
|
|
1329
|
+
items(src, (item) => {
|
|
1330
|
+
const srckey = item[0]
|
|
1331
|
+
const srcnode = item[1]
|
|
1332
|
+
let key = srckey
|
|
1333
|
+
if (NONE !== keypath) {
|
|
1334
|
+
if (keypath.startsWith('`')) {
|
|
1335
|
+
key = inject(keypath, merge([{}, store, { $TOP: srcnode }], 1))
|
|
1336
|
+
} else {
|
|
1337
|
+
key = getpath(srcnode, keypath, inj)
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
const tchild = clone(child)
|
|
1341
|
+
setprop(tval, key, tchild)
|
|
1342
|
+
const anno = getprop(srcnode, S_BANNO)
|
|
1343
|
+
if (NONE === anno) {
|
|
1344
|
+
delprop(tchild, S_BANNO)
|
|
1345
|
+
} else {
|
|
1346
|
+
setprop(tchild, S_BANNO, anno)
|
|
1347
|
+
}
|
|
1348
|
+
})
|
|
1349
|
+
let rval = {}
|
|
1350
|
+
if (!isempty(tval)) {
|
|
1351
|
+
// Build parallel source object.
|
|
1352
|
+
let tsrc = {}
|
|
1353
|
+
src.reduce((a, n, i) => {
|
|
1354
|
+
let kn =
|
|
1355
|
+
null == keypath
|
|
1356
|
+
? i
|
|
1357
|
+
: keypath.startsWith('`')
|
|
1358
|
+
? inject(keypath, merge([{}, store, { $TOP: n }], 1))
|
|
1359
|
+
: getpath(n, keypath, inj)
|
|
1360
|
+
setprop(a, kn, n)
|
|
1361
|
+
return a
|
|
1362
|
+
}, tsrc)
|
|
1363
|
+
const tpath = slice(inj.path, -1)
|
|
1364
|
+
const ckey = getelem(inj.path, -2)
|
|
1365
|
+
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey])
|
|
1366
|
+
let tcur = { [ckey]: tsrc }
|
|
1367
|
+
if (1 < size(tpath)) {
|
|
1368
|
+
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1369
|
+
tcur = { [pkey]: tcur }
|
|
1370
|
+
dpath.push('$:' + pkey)
|
|
1371
|
+
}
|
|
1372
|
+
const tinj = inj.child(0, [ckey])
|
|
1373
|
+
tinj.path = tpath
|
|
1374
|
+
tinj.nodes = slice(inj.nodes, -1)
|
|
1375
|
+
tinj.parent = getelem(tinj.nodes, -1)
|
|
1376
|
+
tinj.val = tval
|
|
1377
|
+
tinj.dpath = dpath
|
|
1378
|
+
tinj.dparent = tcur
|
|
1379
|
+
inject(tval, store, tinj)
|
|
1380
|
+
rval = tinj.val
|
|
1381
|
+
}
|
|
1382
|
+
// _updateAncestors(inj, target, tkey, rval)
|
|
1383
|
+
setprop(target, tkey, rval)
|
|
1384
|
+
// Drop transform key.
|
|
1385
|
+
return NONE
|
|
1386
|
+
}
|
|
1387
|
+
// TODO: not found ref should removed key (setprop NONE)
|
|
1388
|
+
// Reference original spec (enables recursice transformations)
|
|
1389
|
+
// Format: ['`$REF`', '`spec-path`']
|
|
1390
|
+
const transform_REF = (inj, val, _ref, store) => {
|
|
1391
|
+
const { nodes } = inj
|
|
1392
|
+
if (M_VAL !== inj.mode) {
|
|
1393
|
+
return NONE
|
|
1394
|
+
}
|
|
1395
|
+
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1396
|
+
const refpath = _lookup(inj.parent, 1)
|
|
1397
|
+
inj.keyI = size(inj.keys)
|
|
1398
|
+
// Spec reference.
|
|
1399
|
+
const spec = getprop(store, S_DSPEC)()
|
|
1400
|
+
const dpath = slice(inj.path, 1)
|
|
1401
|
+
const ref = getpath(spec, refpath, {
|
|
1402
|
+
// TODO: test relative refs
|
|
1403
|
+
// dpath: inj.path.slice(1),
|
|
1404
|
+
dpath,
|
|
1405
|
+
// dparent: getpath(spec, inj.path.slice(1))
|
|
1406
|
+
dparent: getpath(spec, dpath),
|
|
1407
|
+
})
|
|
1408
|
+
let hasSubRef = false
|
|
1409
|
+
if (isnode(ref)) {
|
|
1410
|
+
walk(ref, (_k, v) => {
|
|
1411
|
+
if ('`$REF`' === v) {
|
|
1412
|
+
hasSubRef = true
|
|
1413
|
+
}
|
|
1414
|
+
return v
|
|
1415
|
+
})
|
|
1416
|
+
}
|
|
1417
|
+
let tref = clone(ref)
|
|
1418
|
+
const cpath = slice(inj.path, -3)
|
|
1419
|
+
const tpath = slice(inj.path, -1)
|
|
1420
|
+
let tcur = getpath(store, cpath)
|
|
1421
|
+
let tval = getpath(store, tpath)
|
|
1422
|
+
let rval = NONE
|
|
1423
|
+
if (!hasSubRef || NONE !== tval) {
|
|
1424
|
+
const tinj = inj.child(0, [getelem(tpath, -1)])
|
|
1425
|
+
tinj.path = tpath
|
|
1426
|
+
tinj.nodes = slice(inj.nodes, -1)
|
|
1427
|
+
tinj.parent = getelem(nodes, -2)
|
|
1428
|
+
tinj.val = tref
|
|
1429
|
+
tinj.dpath = flatten([cpath])
|
|
1430
|
+
tinj.dparent = tcur
|
|
1431
|
+
inject(tref, store, tinj)
|
|
1432
|
+
rval = tinj.val
|
|
1433
|
+
} else {
|
|
1434
|
+
rval = NONE
|
|
1435
|
+
}
|
|
1436
|
+
const grandparent = inj.setval(rval, 2)
|
|
1437
|
+
if (islist(grandparent) && inj.prior) {
|
|
1438
|
+
inj.prior.keyI--
|
|
1439
|
+
}
|
|
1440
|
+
return val
|
|
1441
|
+
}
|
|
1442
|
+
const transform_FORMAT = (inj, _val, _ref, store) => {
|
|
1443
|
+
// console.log('FORMAT-START', inj, _val)
|
|
1444
|
+
// Remove remaining keys to avoid spurious processing.
|
|
1445
|
+
slice(inj.keys, 0, 1, true)
|
|
1446
|
+
if (M_VAL !== inj.mode) {
|
|
1447
|
+
return NONE
|
|
1448
|
+
}
|
|
1449
|
+
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1450
|
+
// TODO: EACH and PACK should accept customm functions too
|
|
1451
|
+
const name = _lookup(inj.parent, 1)
|
|
1452
|
+
const child = _lookup(inj.parent, 2)
|
|
1453
|
+
// Source data.
|
|
1454
|
+
const tkey = getelem(inj.path, -2)
|
|
1455
|
+
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
|
|
1456
|
+
const cinj = injectChild(child, store, inj)
|
|
1457
|
+
const resolved = cinj.val
|
|
1458
|
+
let formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
|
|
1459
|
+
if (NONE === formatter) {
|
|
1460
|
+
inj.errs.push('$FORMAT: unknown format: ' + name + '.')
|
|
1461
|
+
return NONE
|
|
1462
|
+
}
|
|
1463
|
+
let out = walk(resolved, formatter)
|
|
1464
|
+
setprop(target, tkey, out)
|
|
1465
|
+
// _updateAncestors(inj, target, tkey, out)
|
|
1466
|
+
return out
|
|
1467
|
+
}
|
|
1468
|
+
const FORMATTER = {
|
|
1469
|
+
identity: (_k, v) => v,
|
|
1470
|
+
upper: (_k, v) => (isnode(v) ? v : ('' + v).toUpperCase()),
|
|
1471
|
+
lower: (_k, v) => (isnode(v) ? v : ('' + v).toLowerCase()),
|
|
1472
|
+
string: (_k, v) => (isnode(v) ? v : '' + v),
|
|
1473
|
+
number: (_k, v) => {
|
|
1474
|
+
if (isnode(v)) {
|
|
1475
|
+
return v
|
|
1476
|
+
} else {
|
|
1477
|
+
let n = Number(v)
|
|
1478
|
+
if (isNaN(n)) {
|
|
1479
|
+
n = 0
|
|
1480
|
+
}
|
|
1481
|
+
return n
|
|
1482
|
+
}
|
|
1483
|
+
},
|
|
1484
|
+
integer: (_k, v) => {
|
|
1485
|
+
if (isnode(v)) {
|
|
1486
|
+
return v
|
|
1487
|
+
} else {
|
|
1488
|
+
let n = Number(v)
|
|
1489
|
+
if (isNaN(n)) {
|
|
1490
|
+
n = 0
|
|
1491
|
+
}
|
|
1492
|
+
return n | 0
|
|
1493
|
+
}
|
|
1494
|
+
},
|
|
1495
|
+
concat: (k, v) =>
|
|
1496
|
+
null == k && islist(v)
|
|
1497
|
+
? join(
|
|
1498
|
+
items(v, (n) => (isnode(n[1]) ? S_MT : S_MT + n[1])),
|
|
1499
|
+
S_MT,
|
|
1500
|
+
)
|
|
1501
|
+
: v,
|
|
1502
|
+
}
|
|
1503
|
+
const transform_APPLY = (inj, _val, _ref, store) => {
|
|
1504
|
+
const ijname = 'APPLY'
|
|
1505
|
+
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1506
|
+
return NONE
|
|
1507
|
+
}
|
|
1508
|
+
// const [err, apply, child] = injectorArgs([T_function, T_any], inj)
|
|
1509
|
+
const [err, apply, child] = injectorArgs([T_function, T_any], slice(inj.parent, 1))
|
|
1510
|
+
if (NONE !== err) {
|
|
1511
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1512
|
+
return NONE
|
|
1513
|
+
}
|
|
1514
|
+
const tkey = getelem(inj.path, -2)
|
|
1515
|
+
const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
|
|
1516
|
+
const cinj = injectChild(child, store, inj)
|
|
1517
|
+
const resolved = cinj.val
|
|
1518
|
+
const out = apply(resolved, store, cinj)
|
|
1519
|
+
setprop(target, tkey, out)
|
|
1520
|
+
return out
|
|
1521
|
+
}
|
|
1522
|
+
// Transform data using spec.
|
|
1523
|
+
// Only operates on static JSON-like data.
|
|
1524
|
+
// Arrays are treated as if they are objects with indices as keys.
|
|
1525
|
+
function transform(
|
|
1526
|
+
data, // Source data to transform into new data (original not mutated)
|
|
1527
|
+
spec, // Transform specification; output follows this shape
|
|
1528
|
+
injdef,
|
|
1529
|
+
) {
|
|
1530
|
+
// Clone the spec so that the clone can be modified in place as the transform result.
|
|
1531
|
+
const origspec = spec
|
|
1532
|
+
spec = clone(origspec)
|
|
1533
|
+
const extra = injdef?.extra
|
|
1534
|
+
const collect = null != injdef?.errs
|
|
1535
|
+
const errs = injdef?.errs || []
|
|
1536
|
+
const extraTransforms = {}
|
|
1537
|
+
const extraData =
|
|
1538
|
+
null == extra
|
|
1539
|
+
? NONE
|
|
1540
|
+
: items(extra).reduce(
|
|
1541
|
+
(a, n) => (n[0].startsWith(S_DS) ? (extraTransforms[n[0]] = n[1]) : (a[n[0]] = n[1]), a),
|
|
1542
|
+
{},
|
|
1543
|
+
)
|
|
1544
|
+
const dataClone = merge([isempty(extraData) ? NONE : clone(extraData), clone(data)])
|
|
1545
|
+
// Define a top level store that provides transform operations.
|
|
1546
|
+
const store = merge(
|
|
1547
|
+
[
|
|
1548
|
+
{
|
|
1549
|
+
// The inject function recognises this special location for the root of the source data.
|
|
1550
|
+
// NOTE: to escape data that contains "`$FOO`" keys at the top level,
|
|
1551
|
+
// place that data inside a holding map: { myholder: mydata }.
|
|
1552
|
+
$TOP: dataClone,
|
|
1553
|
+
$SPEC: () => origspec,
|
|
1554
|
+
// Escape backtick (this also works inside backticks).
|
|
1555
|
+
$BT: () => S_BT,
|
|
1556
|
+
// Escape dollar sign (this also works inside backticks).
|
|
1557
|
+
$DS: () => S_DS,
|
|
1558
|
+
// Insert current date and time as an ISO string.
|
|
1559
|
+
$WHEN: () => new Date().toISOString(),
|
|
1560
|
+
$DELETE: transform_DELETE,
|
|
1561
|
+
$COPY: transform_COPY,
|
|
1562
|
+
$KEY: transform_KEY,
|
|
1563
|
+
$ANNO: transform_ANNO,
|
|
1564
|
+
$MERGE: transform_MERGE,
|
|
1565
|
+
$EACH: transform_EACH,
|
|
1566
|
+
$PACK: transform_PACK,
|
|
1567
|
+
$REF: transform_REF,
|
|
1568
|
+
$FORMAT: transform_FORMAT,
|
|
1569
|
+
$APPLY: transform_APPLY,
|
|
1570
|
+
},
|
|
1571
|
+
// Custom extra transforms, if any.
|
|
1572
|
+
extraTransforms,
|
|
1573
|
+
{
|
|
1574
|
+
$ERRS: errs,
|
|
1575
|
+
},
|
|
1576
|
+
],
|
|
1577
|
+
1,
|
|
1578
|
+
)
|
|
1579
|
+
const out = inject(spec, store, injdef)
|
|
1580
|
+
const generr = 0 < size(errs) && !collect
|
|
1581
|
+
if (generr) {
|
|
1582
|
+
throw new Error(join(errs, ' | '))
|
|
1583
|
+
}
|
|
1584
|
+
return out
|
|
1585
|
+
}
|
|
1586
|
+
// A required string value. NOTE: Rejects empty strings.
|
|
1587
|
+
const validate_STRING = (inj) => {
|
|
1588
|
+
let out = _lookup(inj.dparent, inj.key)
|
|
1589
|
+
const t = typify(out)
|
|
1590
|
+
if (0 === (T_string & t)) {
|
|
1591
|
+
let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
|
|
1592
|
+
inj.errs.push(msg)
|
|
1593
|
+
return NONE
|
|
1594
|
+
}
|
|
1595
|
+
if (S_MT === out) {
|
|
1596
|
+
let msg = 'Empty string at ' + pathify(inj.path, 1)
|
|
1597
|
+
inj.errs.push(msg)
|
|
1598
|
+
return NONE
|
|
1599
|
+
}
|
|
1600
|
+
return out
|
|
1601
|
+
}
|
|
1602
|
+
const validate_TYPE = (inj, _val, ref) => {
|
|
1603
|
+
const tname = slice(ref, 1).toLowerCase()
|
|
1604
|
+
const typev = 1 << (31 - TYPENAME.indexOf(tname))
|
|
1605
|
+
let out = _lookup(inj.dparent, inj.key)
|
|
1606
|
+
const t = typify(out)
|
|
1607
|
+
// console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
|
|
1608
|
+
if (0 === (t & typev)) {
|
|
1609
|
+
inj.errs.push(_invalidTypeMsg(inj.path, tname, t, out, 'V1001'))
|
|
1610
|
+
return NONE
|
|
1611
|
+
}
|
|
1612
|
+
return out
|
|
1613
|
+
}
|
|
1614
|
+
// Allow any value.
|
|
1615
|
+
const validate_ANY = (inj) => {
|
|
1616
|
+
let out = _lookup(inj.dparent, inj.key)
|
|
1617
|
+
return out
|
|
1618
|
+
}
|
|
1619
|
+
// Specify child values for map or list.
|
|
1620
|
+
// Map syntax: {'`$CHILD`': child-template }
|
|
1621
|
+
// List syntax: ['`$CHILD`', child-template ]
|
|
1622
|
+
const validate_CHILD = (inj) => {
|
|
1623
|
+
const { mode, key, parent, keys, path } = inj
|
|
1624
|
+
// Setup data structures for validation by cloning child template.
|
|
1625
|
+
// Map syntax.
|
|
1626
|
+
if (M_KEYPRE === mode) {
|
|
1627
|
+
const childtm = _lookup(parent, key)
|
|
1628
|
+
// Get corresponding current object.
|
|
1629
|
+
const pkey = getelem(path, -2)
|
|
1630
|
+
let tval = getprop(inj.dparent, pkey)
|
|
1631
|
+
if (NONE == tval) {
|
|
1632
|
+
tval = {}
|
|
1633
|
+
} else if (!ismap(tval)) {
|
|
1634
|
+
inj.errs.push(_invalidTypeMsg(slice(inj.path, -1), S_object, typify(tval), tval), 'V0220')
|
|
1635
|
+
return NONE
|
|
1636
|
+
}
|
|
1637
|
+
const ckeys = keysof(tval)
|
|
1638
|
+
for (let ckey of ckeys) {
|
|
1639
|
+
setprop(parent, ckey, clone(childtm))
|
|
1640
|
+
// NOTE: modifying inj! This extends the child value loop in inject.
|
|
1641
|
+
keys.push(ckey)
|
|
1642
|
+
}
|
|
1643
|
+
// Remove $CHILD to cleanup ouput.
|
|
1644
|
+
inj.setval(NONE)
|
|
1645
|
+
return NONE
|
|
1646
|
+
}
|
|
1647
|
+
// List syntax.
|
|
1648
|
+
if (M_VAL === mode) {
|
|
1649
|
+
if (!islist(parent)) {
|
|
1650
|
+
// $CHILD was not inside a list.
|
|
1651
|
+
inj.errs.push('Invalid $CHILD as value')
|
|
1652
|
+
return NONE
|
|
1653
|
+
}
|
|
1654
|
+
const childtm = getprop(parent, 1)
|
|
1655
|
+
if (NONE === inj.dparent) {
|
|
1656
|
+
// Empty list as default.
|
|
1657
|
+
// parent.length = 0
|
|
1658
|
+
slice(parent, 0, 0, true)
|
|
1659
|
+
return NONE
|
|
1660
|
+
}
|
|
1661
|
+
if (!islist(inj.dparent)) {
|
|
1662
|
+
const msg = _invalidTypeMsg(
|
|
1663
|
+
slice(inj.path, -1),
|
|
1664
|
+
S_list,
|
|
1665
|
+
typify(inj.dparent),
|
|
1666
|
+
inj.dparent,
|
|
1667
|
+
'V0230',
|
|
1668
|
+
)
|
|
1669
|
+
inj.errs.push(msg)
|
|
1670
|
+
inj.keyI = size(parent)
|
|
1671
|
+
return inj.dparent
|
|
1672
|
+
}
|
|
1673
|
+
// Clone children and reset inj key index.
|
|
1674
|
+
// The inject child loop will now iterate over the cloned children,
|
|
1675
|
+
// validating them against the current list values.
|
|
1676
|
+
items(inj.dparent, (n) => setprop(parent, n[0], clone(childtm)))
|
|
1677
|
+
slice(parent, 0, inj.dparent.length, true)
|
|
1678
|
+
// NOTE: modifying inj! This extends the child value loop in inject
|
|
1679
|
+
// to cover every cloned child.
|
|
1680
|
+
for (let ckeyI = size(keys); ckeyI < size(parent); ckeyI++) {
|
|
1681
|
+
keys.push(strkey(ckeyI))
|
|
1682
|
+
}
|
|
1683
|
+
// Restart the child value loop at the first element (the loop
|
|
1684
|
+
// increments keyI on resume) so that the first element is also
|
|
1685
|
+
// validated against the child template.
|
|
1686
|
+
inj.keyI = -1
|
|
1687
|
+
// SKIP leaves the cloned child template in place at the first
|
|
1688
|
+
// element so the resumed loop can validate it.
|
|
1689
|
+
return SKIP
|
|
1690
|
+
}
|
|
1691
|
+
return NONE
|
|
1692
|
+
}
|
|
1693
|
+
// TODO: implement SOME, ALL
|
|
1694
|
+
// FIX: ONE should mean exactly one, not at least one (=SOME)
|
|
1695
|
+
// TODO: implement a generate validate_ALT to do all of these
|
|
1696
|
+
// Match at least one of the specified shapes.
|
|
1697
|
+
// Syntax: ['`$ONE`', alt0, alt1, ...]
|
|
1698
|
+
const validate_ONE = (inj, _val, _ref, store) => {
|
|
1699
|
+
const { mode, parent, keyI } = inj
|
|
1700
|
+
// Only operate in val mode, since parent is a list.
|
|
1701
|
+
if (M_VAL === mode) {
|
|
1702
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
1703
|
+
inj.errs.push(
|
|
1704
|
+
'The $ONE validator at field ' +
|
|
1705
|
+
pathify(inj.path, 1, 1) +
|
|
1706
|
+
' must be the first element of an array.',
|
|
1707
|
+
)
|
|
1708
|
+
return
|
|
1709
|
+
}
|
|
1710
|
+
inj.keyI = size(inj.keys)
|
|
1711
|
+
// Clean up structure, replacing [$ONE, ...] with current
|
|
1712
|
+
inj.setval(inj.dparent, 2)
|
|
1713
|
+
inj.path = slice(inj.path, -1)
|
|
1714
|
+
inj.key = getelem(inj.path, -1)
|
|
1715
|
+
let tvals = slice(parent, 1)
|
|
1716
|
+
if (0 === size(tvals)) {
|
|
1717
|
+
inj.errs.push(
|
|
1718
|
+
'The $ONE validator at field ' +
|
|
1719
|
+
pathify(inj.path, 1, 1) +
|
|
1720
|
+
' must have at least one argument.',
|
|
1721
|
+
)
|
|
1722
|
+
return
|
|
1723
|
+
}
|
|
1724
|
+
// See if we can find a match.
|
|
1725
|
+
for (let tval of tvals) {
|
|
1726
|
+
// If match, then errs.length = 0
|
|
1727
|
+
let terrs = []
|
|
1728
|
+
const vstore = merge([{}, store], 1)
|
|
1729
|
+
vstore.$TOP = inj.dparent
|
|
1730
|
+
const vcurrent = validate(inj.dparent, tval, {
|
|
1731
|
+
extra: vstore,
|
|
1732
|
+
errs: terrs,
|
|
1733
|
+
meta: inj.meta,
|
|
1734
|
+
})
|
|
1735
|
+
inj.setval(vcurrent, -2)
|
|
1736
|
+
// Accept current value if there was a match
|
|
1737
|
+
if (0 === size(terrs)) {
|
|
1738
|
+
return
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
// There was no match.
|
|
1742
|
+
const valdesc = replace(
|
|
1743
|
+
join(
|
|
1744
|
+
items(tvals, (n) => stringify(n[1])),
|
|
1745
|
+
', ',
|
|
1746
|
+
),
|
|
1747
|
+
R_TRANSFORM_NAME,
|
|
1748
|
+
(_m, p1) => p1.toLowerCase(),
|
|
1749
|
+
)
|
|
1750
|
+
inj.errs.push(
|
|
1751
|
+
_invalidTypeMsg(
|
|
1752
|
+
inj.path,
|
|
1753
|
+
(1 < size(tvals) ? 'one of ' : '') + valdesc,
|
|
1754
|
+
typify(inj.dparent),
|
|
1755
|
+
inj.dparent,
|
|
1756
|
+
'V0210',
|
|
1757
|
+
),
|
|
1758
|
+
)
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
const validate_EXACT = (inj) => {
|
|
1762
|
+
const { mode, parent, key, keyI } = inj
|
|
1763
|
+
// Only operate in val mode, since parent is a list.
|
|
1764
|
+
if (M_VAL === mode) {
|
|
1765
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
1766
|
+
inj.errs.push(
|
|
1767
|
+
'The $EXACT validator at field ' +
|
|
1768
|
+
pathify(inj.path, 1, 1) +
|
|
1769
|
+
' must be the first element of an array.',
|
|
1770
|
+
)
|
|
1771
|
+
return
|
|
1772
|
+
}
|
|
1773
|
+
inj.keyI = size(inj.keys)
|
|
1774
|
+
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1775
|
+
inj.setval(inj.dparent, 2)
|
|
1776
|
+
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
1777
|
+
inj.path = slice(inj.path, 0, -1)
|
|
1778
|
+
inj.key = getelem(inj.path, -1)
|
|
1779
|
+
let tvals = slice(parent, 1)
|
|
1780
|
+
if (0 === size(tvals)) {
|
|
1781
|
+
inj.errs.push(
|
|
1782
|
+
'The $EXACT validator at field ' +
|
|
1783
|
+
pathify(inj.path, 1, 1) +
|
|
1784
|
+
' must have at least one argument.',
|
|
1785
|
+
)
|
|
1786
|
+
return
|
|
1787
|
+
}
|
|
1788
|
+
// See if we can find an exact value match.
|
|
1789
|
+
let currentstr = undefined
|
|
1790
|
+
for (let tval of tvals) {
|
|
1791
|
+
let exactmatch = tval === inj.dparent
|
|
1792
|
+
if (!exactmatch && isnode(tval)) {
|
|
1793
|
+
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr
|
|
1794
|
+
const tvalstr = stringify(tval)
|
|
1795
|
+
exactmatch = tvalstr === currentstr
|
|
1796
|
+
}
|
|
1797
|
+
if (exactmatch) {
|
|
1798
|
+
return
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
// There was no match.
|
|
1802
|
+
const valdesc = replace(
|
|
1803
|
+
join(
|
|
1804
|
+
items(tvals, (n) => stringify(n[1])),
|
|
1805
|
+
', ',
|
|
1806
|
+
),
|
|
1807
|
+
R_TRANSFORM_NAME,
|
|
1808
|
+
(_m, p1) => p1.toLowerCase(),
|
|
1809
|
+
)
|
|
1810
|
+
inj.errs.push(
|
|
1811
|
+
_invalidTypeMsg(
|
|
1812
|
+
inj.path,
|
|
1813
|
+
(1 < size(inj.path) ? '' : 'value ') +
|
|
1814
|
+
'exactly equal to ' +
|
|
1815
|
+
(1 === size(tvals) ? '' : 'one of ') +
|
|
1816
|
+
valdesc,
|
|
1817
|
+
typify(inj.dparent),
|
|
1818
|
+
inj.dparent,
|
|
1819
|
+
'V0110',
|
|
1820
|
+
),
|
|
1821
|
+
)
|
|
1822
|
+
} else {
|
|
1823
|
+
delprop(parent, key)
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
// This is the "modify" argument to inject. Use this to perform
|
|
1827
|
+
// generic validation. Runs *after* any special commands.
|
|
1828
|
+
const _validation = (pval, key, parent, inj) => {
|
|
1829
|
+
if (NONE === inj) {
|
|
1830
|
+
return
|
|
1831
|
+
}
|
|
1832
|
+
if (SKIP === pval) {
|
|
1833
|
+
return
|
|
1834
|
+
}
|
|
1835
|
+
// select needs exact matches
|
|
1836
|
+
const exact = getprop(inj.meta, S_BEXACT, false)
|
|
1837
|
+
// Current val to verify.
|
|
1838
|
+
const cval = getprop(inj.dparent, key)
|
|
1839
|
+
if (NONE === inj || (!exact && NONE === cval)) {
|
|
1840
|
+
return
|
|
1841
|
+
}
|
|
1842
|
+
const ptype = typify(pval)
|
|
1843
|
+
// Delete any special commands remaining.
|
|
1844
|
+
if (0 < (T_string & ptype) && pval.includes(S_DS)) {
|
|
1845
|
+
return
|
|
1846
|
+
}
|
|
1847
|
+
const ctype = typify(cval)
|
|
1848
|
+
// Type mismatch.
|
|
1849
|
+
if (ptype !== ctype && NONE !== pval) {
|
|
1850
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0010'))
|
|
1851
|
+
return
|
|
1852
|
+
}
|
|
1853
|
+
if (ismap(cval)) {
|
|
1854
|
+
if (!ismap(pval)) {
|
|
1855
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0020'))
|
|
1856
|
+
return
|
|
1857
|
+
}
|
|
1858
|
+
const ckeys = keysof(cval)
|
|
1859
|
+
const pkeys = keysof(pval)
|
|
1860
|
+
// Empty spec object {} means object can be open (any keys).
|
|
1861
|
+
if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
|
|
1862
|
+
const badkeys = []
|
|
1863
|
+
for (let ckey of ckeys) {
|
|
1864
|
+
if (NONE === _lookup(pval, ckey)) {
|
|
1865
|
+
badkeys.push(ckey)
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
// Closed object, so reject extra keys not in shape.
|
|
1869
|
+
if (0 < size(badkeys)) {
|
|
1870
|
+
const msg = 'Unexpected keys at field ' + pathify(inj.path, 1) + S_VIZ + join(badkeys, ', ')
|
|
1871
|
+
inj.errs.push(msg)
|
|
1872
|
+
}
|
|
1873
|
+
} else {
|
|
1874
|
+
// Object is open, so merge in extra keys.
|
|
1875
|
+
merge([pval, cval])
|
|
1876
|
+
if (isnode(pval)) {
|
|
1877
|
+
delprop(pval, '`$OPEN`')
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
} else if (islist(cval)) {
|
|
1881
|
+
if (!islist(pval)) {
|
|
1882
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0030'))
|
|
1883
|
+
}
|
|
1884
|
+
} else if (exact) {
|
|
1885
|
+
if (cval !== pval) {
|
|
1886
|
+
const pathmsg = 1 < size(inj.path) ? 'at field ' + pathify(inj.path, 1) + S_VIZ : S_MT
|
|
1887
|
+
inj.errs.push('Value ' + pathmsg + cval + ' should equal ' + pval + S_DT)
|
|
1888
|
+
}
|
|
1889
|
+
} else {
|
|
1890
|
+
// Spec value was a default, copy over data
|
|
1891
|
+
setprop(parent, key, cval)
|
|
1892
|
+
}
|
|
1893
|
+
return
|
|
1894
|
+
}
|
|
1895
|
+
// Validate a data structure against a shape specification. The shape
|
|
1896
|
+
// specification follows the "by example" principle. Plain data in
|
|
1897
|
+
// teh shape is treated as default values that also specify the
|
|
1898
|
+
// required type. Thus shape {a:1} validates {a:2}, since the types
|
|
1899
|
+
// (number) match, but not {a:'A'}. Shape {a;1} against data {}
|
|
1900
|
+
// returns {a:1} as a=1 is the default value of the a key. Special
|
|
1901
|
+
// validation commands (in the same syntax as transform ) are also
|
|
1902
|
+
// provided to specify required values. Thus shape {a:'`$STRING`'}
|
|
1903
|
+
// validates {a:'A'} but not {a:1}. Empty map or list means the node
|
|
1904
|
+
// is open, and if missing an empty default is inserted.
|
|
1905
|
+
function validate(
|
|
1906
|
+
data, // Source data to transform into new data (original not mutated)
|
|
1907
|
+
spec, // Transform specification; output follows this shape
|
|
1908
|
+
injdef,
|
|
1909
|
+
) {
|
|
1910
|
+
const extra = injdef?.extra
|
|
1911
|
+
const collect = null != injdef?.errs
|
|
1912
|
+
const errs = injdef?.errs || []
|
|
1913
|
+
const store = merge(
|
|
1914
|
+
[
|
|
1915
|
+
{
|
|
1916
|
+
// Remove the transform commands.
|
|
1917
|
+
$DELETE: null,
|
|
1918
|
+
$COPY: null,
|
|
1919
|
+
$KEY: null,
|
|
1920
|
+
$META: null,
|
|
1921
|
+
$MERGE: null,
|
|
1922
|
+
$EACH: null,
|
|
1923
|
+
$PACK: null,
|
|
1924
|
+
$STRING: validate_STRING,
|
|
1925
|
+
$NUMBER: validate_TYPE,
|
|
1926
|
+
$INTEGER: validate_TYPE,
|
|
1927
|
+
$DECIMAL: validate_TYPE,
|
|
1928
|
+
$BOOLEAN: validate_TYPE,
|
|
1929
|
+
$NULL: validate_TYPE,
|
|
1930
|
+
$NIL: validate_TYPE,
|
|
1931
|
+
$MAP: validate_TYPE,
|
|
1932
|
+
$LIST: validate_TYPE,
|
|
1933
|
+
$FUNCTION: validate_TYPE,
|
|
1934
|
+
$INSTANCE: validate_TYPE,
|
|
1935
|
+
$ANY: validate_ANY,
|
|
1936
|
+
$CHILD: validate_CHILD,
|
|
1937
|
+
$ONE: validate_ONE,
|
|
1938
|
+
$EXACT: validate_EXACT,
|
|
1939
|
+
},
|
|
1940
|
+
getdef(extra, {}),
|
|
1941
|
+
// A special top level value to collect errors.
|
|
1942
|
+
// NOTE: collecterrs parameter always wins.
|
|
1943
|
+
{
|
|
1944
|
+
$ERRS: errs,
|
|
1945
|
+
},
|
|
1946
|
+
],
|
|
1947
|
+
1,
|
|
1948
|
+
)
|
|
1949
|
+
let meta = getprop(injdef, 'meta', {})
|
|
1950
|
+
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false))
|
|
1951
|
+
const out = transform(data, spec, {
|
|
1952
|
+
meta,
|
|
1953
|
+
extra: store,
|
|
1954
|
+
modify: _validation,
|
|
1955
|
+
handler: _validatehandler,
|
|
1956
|
+
errs,
|
|
1957
|
+
})
|
|
1958
|
+
const generr = 0 < size(errs) && !collect
|
|
1959
|
+
if (generr) {
|
|
1960
|
+
throw new Error(join(errs, ' | '))
|
|
1961
|
+
}
|
|
1962
|
+
return out
|
|
1963
|
+
}
|
|
1964
|
+
const select_AND = (inj, _val, _ref, store) => {
|
|
1965
|
+
if (M_KEYPRE === inj.mode) {
|
|
1966
|
+
const terms = _lookup(inj.parent, inj.key)
|
|
1967
|
+
const ppath = slice(inj.path, -1)
|
|
1968
|
+
const point = getpath(store, ppath)
|
|
1969
|
+
const vstore = merge([{}, store], 1)
|
|
1970
|
+
vstore.$TOP = point
|
|
1971
|
+
for (let term of terms) {
|
|
1972
|
+
let terrs = []
|
|
1973
|
+
validate(point, term, {
|
|
1974
|
+
extra: vstore,
|
|
1975
|
+
errs: terrs,
|
|
1976
|
+
meta: inj.meta,
|
|
1977
|
+
})
|
|
1978
|
+
if (0 != size(terrs)) {
|
|
1979
|
+
inj.errs.push(
|
|
1980
|
+
'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms),
|
|
1981
|
+
)
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
const gkey = getelem(inj.path, -2)
|
|
1985
|
+
const gp = getelem(inj.nodes, -2)
|
|
1986
|
+
setprop(gp, gkey, point)
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
const select_OR = (inj, _val, _ref, store) => {
|
|
1990
|
+
if (M_KEYPRE === inj.mode) {
|
|
1991
|
+
const terms = _lookup(inj.parent, inj.key)
|
|
1992
|
+
const ppath = slice(inj.path, -1)
|
|
1993
|
+
const point = getpath(store, ppath)
|
|
1994
|
+
const vstore = merge([{}, store], 1)
|
|
1995
|
+
vstore.$TOP = point
|
|
1996
|
+
for (let term of terms) {
|
|
1997
|
+
let terrs = []
|
|
1998
|
+
validate(point, term, {
|
|
1999
|
+
extra: vstore,
|
|
2000
|
+
errs: terrs,
|
|
2001
|
+
meta: inj.meta,
|
|
2002
|
+
})
|
|
2003
|
+
if (0 === size(terrs)) {
|
|
2004
|
+
const gkey = getelem(inj.path, -2)
|
|
2005
|
+
const gp = getelem(inj.nodes, -2)
|
|
2006
|
+
setprop(gp, gkey, point)
|
|
2007
|
+
return
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
inj.errs.push('OR:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
const select_NOT = (inj, _val, _ref, store) => {
|
|
2014
|
+
if (M_KEYPRE === inj.mode) {
|
|
2015
|
+
const term = _lookup(inj.parent, inj.key)
|
|
2016
|
+
const ppath = slice(inj.path, -1)
|
|
2017
|
+
const point = getpath(store, ppath)
|
|
2018
|
+
const vstore = merge([{}, store], 1)
|
|
2019
|
+
vstore.$TOP = point
|
|
2020
|
+
let terrs = []
|
|
2021
|
+
validate(point, term, {
|
|
2022
|
+
extra: vstore,
|
|
2023
|
+
errs: terrs,
|
|
2024
|
+
meta: inj.meta,
|
|
2025
|
+
})
|
|
2026
|
+
if (0 == size(terrs)) {
|
|
2027
|
+
inj.errs.push('NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
|
|
2028
|
+
}
|
|
2029
|
+
const gkey = getelem(inj.path, -2)
|
|
2030
|
+
const gp = getelem(inj.nodes, -2)
|
|
2031
|
+
setprop(gp, gkey, point)
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
const select_CMP = (inj, _val, ref, store) => {
|
|
2035
|
+
if (M_KEYPRE === inj.mode) {
|
|
2036
|
+
const term = _lookup(inj.parent, inj.key)
|
|
2037
|
+
// const src = getprop(store, inj.base, store)
|
|
2038
|
+
const gkey = getelem(inj.path, -2)
|
|
2039
|
+
// const tval = getprop(src, gkey)
|
|
2040
|
+
const ppath = slice(inj.path, -1)
|
|
2041
|
+
const point = getpath(store, ppath)
|
|
2042
|
+
let pass = false
|
|
2043
|
+
if ('$GT' === ref && point > term) {
|
|
2044
|
+
pass = true
|
|
2045
|
+
} else if ('$LT' === ref && point < term) {
|
|
2046
|
+
pass = true
|
|
2047
|
+
} else if ('$GTE' === ref && point >= term) {
|
|
2048
|
+
pass = true
|
|
2049
|
+
} else if ('$LTE' === ref && point <= term) {
|
|
2050
|
+
pass = true
|
|
2051
|
+
} else if ('$LIKE' === ref && stringify(point).match(RegExp(term))) {
|
|
2052
|
+
pass = true
|
|
2053
|
+
}
|
|
2054
|
+
if (pass) {
|
|
2055
|
+
// Update spec to match found value so that _validate does not complain.
|
|
2056
|
+
const gp = getelem(inj.nodes, -2)
|
|
2057
|
+
setprop(gp, gkey, point)
|
|
2058
|
+
} else {
|
|
2059
|
+
inj.errs.push(
|
|
2060
|
+
'CMP: ' +
|
|
2061
|
+
pathify(ppath) +
|
|
2062
|
+
S_VIZ +
|
|
2063
|
+
stringify(point) +
|
|
2064
|
+
' fail:' +
|
|
2065
|
+
ref +
|
|
2066
|
+
' ' +
|
|
2067
|
+
stringify(term),
|
|
2068
|
+
)
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
return NONE
|
|
2072
|
+
}
|
|
2073
|
+
// Select children from a top-level object that match a MongoDB-style query.
|
|
2074
|
+
// Supports $and, $or, and equality comparisons.
|
|
2075
|
+
// For arrays, children are elements; for objects, children are values.
|
|
2076
|
+
// TODO: swap arg order for consistency
|
|
2077
|
+
function select(children, query) {
|
|
2078
|
+
if (!isnode(children)) {
|
|
2079
|
+
return []
|
|
2080
|
+
}
|
|
2081
|
+
if (ismap(children)) {
|
|
2082
|
+
children = items(children, (n) => {
|
|
2083
|
+
setprop(n[1], S_DKEY, n[0])
|
|
2084
|
+
return n[1]
|
|
2085
|
+
})
|
|
2086
|
+
} else {
|
|
2087
|
+
children = items(children, (n) => (setprop(n[1], S_DKEY, +n[0]), n[1]))
|
|
2088
|
+
}
|
|
2089
|
+
const results = []
|
|
2090
|
+
const injdef = {
|
|
2091
|
+
errs: [],
|
|
2092
|
+
meta: { [S_BEXACT]: true },
|
|
2093
|
+
extra: {
|
|
2094
|
+
$AND: select_AND,
|
|
2095
|
+
$OR: select_OR,
|
|
2096
|
+
$NOT: select_NOT,
|
|
2097
|
+
$GT: select_CMP,
|
|
2098
|
+
$LT: select_CMP,
|
|
2099
|
+
$GTE: select_CMP,
|
|
2100
|
+
$LTE: select_CMP,
|
|
2101
|
+
$LIKE: select_CMP,
|
|
2102
|
+
},
|
|
2103
|
+
}
|
|
2104
|
+
const q = clone(query)
|
|
2105
|
+
walk(q, (_k, v) => {
|
|
2106
|
+
if (ismap(v)) {
|
|
2107
|
+
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true))
|
|
2108
|
+
}
|
|
2109
|
+
return v
|
|
2110
|
+
})
|
|
2111
|
+
for (const child of children) {
|
|
2112
|
+
injdef.errs = []
|
|
2113
|
+
validate(child, clone(q), injdef)
|
|
2114
|
+
if (0 === size(injdef.errs)) {
|
|
2115
|
+
results.push(child)
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
return results
|
|
2119
|
+
}
|
|
2120
|
+
// Injection state used for recursive injection into JSON - like data structures.
|
|
2121
|
+
class Injection {
|
|
2122
|
+
constructor(val, parent) {
|
|
2123
|
+
this.val = val
|
|
2124
|
+
this.parent = parent
|
|
2125
|
+
this.errs = []
|
|
2126
|
+
this.dparent = NONE
|
|
2127
|
+
this.dpath = [S_DTOP]
|
|
2128
|
+
this.mode = M_VAL
|
|
2129
|
+
this.full = false
|
|
2130
|
+
this.keyI = 0
|
|
2131
|
+
this.keys = [S_DTOP]
|
|
2132
|
+
this.key = S_DTOP
|
|
2133
|
+
this.path = [S_DTOP]
|
|
2134
|
+
this.nodes = [parent]
|
|
2135
|
+
this.handler = _injecthandler
|
|
2136
|
+
this.base = S_DTOP
|
|
2137
|
+
this.meta = {}
|
|
2138
|
+
}
|
|
2139
|
+
toString(prefix) {
|
|
2140
|
+
return (
|
|
2141
|
+
'INJ' +
|
|
2142
|
+
(null == prefix ? '' : S_FS + prefix) +
|
|
2143
|
+
S_CN +
|
|
2144
|
+
pad(pathify(this.path, 1)) +
|
|
2145
|
+
MODENAME[this.mode] +
|
|
2146
|
+
(this.full ? '/full' : '') +
|
|
2147
|
+
S_CN +
|
|
2148
|
+
'key=' +
|
|
2149
|
+
this.keyI +
|
|
2150
|
+
S_FS +
|
|
2151
|
+
this.key +
|
|
2152
|
+
S_FS +
|
|
2153
|
+
S_OS +
|
|
2154
|
+
this.keys +
|
|
2155
|
+
S_CS +
|
|
2156
|
+
' p=' +
|
|
2157
|
+
stringify(this.parent, -1, 1) +
|
|
2158
|
+
' m=' +
|
|
2159
|
+
stringify(this.meta, -1, 1) +
|
|
2160
|
+
' d/' +
|
|
2161
|
+
pathify(this.dpath, 1) +
|
|
2162
|
+
'=' +
|
|
2163
|
+
stringify(this.dparent, -1, 1) +
|
|
2164
|
+
' r=' +
|
|
2165
|
+
stringify(this.nodes[0]?.[S_DTOP], -1, 1)
|
|
2166
|
+
)
|
|
2167
|
+
}
|
|
2168
|
+
descend() {
|
|
2169
|
+
this.meta.__d++
|
|
2170
|
+
const parentkey = getelem(this.path, -2)
|
|
2171
|
+
// Resolve current node in store for local paths.
|
|
2172
|
+
if (NONE === this.dparent) {
|
|
2173
|
+
// Even if there's no data, dpath should continue to match path, so that
|
|
2174
|
+
// relative paths work properly.
|
|
2175
|
+
if (1 < size(this.dpath)) {
|
|
2176
|
+
this.dpath = flatten([this.dpath, parentkey])
|
|
2177
|
+
}
|
|
2178
|
+
} else {
|
|
2179
|
+
// this.dparent is the containing node of the current store value.
|
|
2180
|
+
if (null != parentkey) {
|
|
2181
|
+
this.dparent = getprop(this.dparent, parentkey)
|
|
2182
|
+
let lastpart = getelem(this.dpath, -1)
|
|
2183
|
+
if (lastpart === '$:' + parentkey) {
|
|
2184
|
+
this.dpath = slice(this.dpath, -1)
|
|
2185
|
+
} else {
|
|
2186
|
+
this.dpath = flatten([this.dpath, parentkey])
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
// TODO: is this needed?
|
|
2191
|
+
return this.dparent
|
|
2192
|
+
}
|
|
2193
|
+
child(keyI, keys) {
|
|
2194
|
+
const key = strkey(keys[keyI])
|
|
2195
|
+
const val = this.val
|
|
2196
|
+
const cinj = new Injection(getprop(val, key), val)
|
|
2197
|
+
cinj.keyI = keyI
|
|
2198
|
+
cinj.keys = keys
|
|
2199
|
+
cinj.key = key
|
|
2200
|
+
cinj.path = flatten([getdef(this.path, []), key])
|
|
2201
|
+
cinj.nodes = flatten([getdef(this.nodes, []), [val]])
|
|
2202
|
+
cinj.mode = this.mode
|
|
2203
|
+
cinj.handler = this.handler
|
|
2204
|
+
cinj.modify = this.modify
|
|
2205
|
+
cinj.base = this.base
|
|
2206
|
+
cinj.meta = this.meta
|
|
2207
|
+
cinj.errs = this.errs
|
|
2208
|
+
cinj.prior = this
|
|
2209
|
+
cinj.dpath = flatten([this.dpath])
|
|
2210
|
+
cinj.dparent = this.dparent
|
|
2211
|
+
return cinj
|
|
2212
|
+
}
|
|
2213
|
+
setval(val, ancestor) {
|
|
2214
|
+
let parent = NONE
|
|
2215
|
+
if (null == ancestor || ancestor < 2) {
|
|
2216
|
+
parent =
|
|
2217
|
+
NONE === val
|
|
2218
|
+
? (this.parent = delprop(this.parent, this.key))
|
|
2219
|
+
: setprop(this.parent, this.key, val)
|
|
2220
|
+
} else {
|
|
2221
|
+
const aval = getelem(this.nodes, 0 - ancestor)
|
|
2222
|
+
const akey = getelem(this.path, 0 - ancestor)
|
|
2223
|
+
parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val)
|
|
2224
|
+
}
|
|
2225
|
+
// console.log('SETVAL', val, this.key, this.parent)
|
|
2226
|
+
return parent
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
// Internal utilities
|
|
2230
|
+
// ==================
|
|
2231
|
+
// // Update all references to target in inj.nodes.
|
|
2232
|
+
// function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
|
|
2233
|
+
// // SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
|
|
2234
|
+
// setprop(target, tkey, tval)
|
|
2235
|
+
// }
|
|
2236
|
+
// Build a type validation error message.
|
|
2237
|
+
function _invalidTypeMsg(path, needtype, vt, v, _whence) {
|
|
2238
|
+
let vs = null == v ? 'no value' : stringify(v)
|
|
2239
|
+
return (
|
|
2240
|
+
'Expected ' +
|
|
2241
|
+
(1 < size(path) ? 'field ' + pathify(path, 1) + ' to be ' : '') +
|
|
2242
|
+
needtype +
|
|
2243
|
+
', but found ' +
|
|
2244
|
+
(null != v ? typename(vt) + S_VIZ : '') +
|
|
2245
|
+
vs +
|
|
2246
|
+
// Uncomment to help debug validation errors.
|
|
2247
|
+
// ' [' + _whence + ']' +
|
|
2248
|
+
'.'
|
|
2249
|
+
)
|
|
2250
|
+
}
|
|
2251
|
+
// Default inject handler for transforms. If the path resolves to a function,
|
|
2252
|
+
// call the function passing the injection inj. This is how transforms operate.
|
|
2253
|
+
const _injecthandler = (inj, val, ref, store) => {
|
|
2254
|
+
let out = val
|
|
2255
|
+
const iscmd = isfunc(val) && (NONE === ref || ref.startsWith(S_DS))
|
|
2256
|
+
// Only call val function if it is a special command ($NAME format).
|
|
2257
|
+
// TODO: OR if meta.'$CALL'
|
|
2258
|
+
if (iscmd) {
|
|
2259
|
+
out = val(inj, val, ref, store)
|
|
2260
|
+
}
|
|
2261
|
+
// Update parent with value. Ensures references remain in node tree.
|
|
2262
|
+
else if (M_VAL === inj.mode && inj.full) {
|
|
2263
|
+
inj.setval(val)
|
|
2264
|
+
}
|
|
2265
|
+
return out
|
|
2266
|
+
}
|
|
2267
|
+
const _validatehandler = (inj, val, ref, store) => {
|
|
2268
|
+
let out = val
|
|
2269
|
+
const m = ref.match(R_META_PATH)
|
|
2270
|
+
const ismetapath = null != m
|
|
2271
|
+
if (ismetapath) {
|
|
2272
|
+
if ('=' === m[2]) {
|
|
2273
|
+
inj.setval([S_BEXACT, val])
|
|
2274
|
+
} else {
|
|
2275
|
+
inj.setval(val)
|
|
2276
|
+
}
|
|
2277
|
+
inj.keyI = -1
|
|
2278
|
+
out = SKIP
|
|
2279
|
+
} else {
|
|
2280
|
+
out = _injecthandler(inj, val, ref, store)
|
|
2281
|
+
}
|
|
2282
|
+
return out
|
|
2283
|
+
}
|
|
2284
|
+
// Inject values from a data store into a string. Not a public utility - used by
|
|
2285
|
+
// `inject`. Inject are marked with `path` where path is resolved
|
|
2286
|
+
// with getpath against the store or current (if defined)
|
|
2287
|
+
// arguments. See `getpath`. Custom injection handling can be
|
|
2288
|
+
// provided by inj.handler (this is used for transform functions).
|
|
2289
|
+
// The path can also have the special syntax $NAME999 where NAME is
|
|
2290
|
+
// upper case letters only, and 999 is any digits, which are
|
|
2291
|
+
// discarded. This syntax specifies the name of a transform, and
|
|
2292
|
+
// optionally allows transforms to be ordered by alphanumeric sorting.
|
|
2293
|
+
function _injectstr(val, store, inj) {
|
|
2294
|
+
// Can't inject into non-strings
|
|
2295
|
+
if (S_string !== typeof val || S_MT === val) {
|
|
2296
|
+
return S_MT
|
|
2297
|
+
}
|
|
2298
|
+
let out = val
|
|
2299
|
+
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
2300
|
+
const m = val.match(R_INJECTION_FULL)
|
|
2301
|
+
// Full string of the val is an injection.
|
|
2302
|
+
if (m) {
|
|
2303
|
+
if (null != inj) {
|
|
2304
|
+
inj.full = true
|
|
2305
|
+
}
|
|
2306
|
+
let pathref = m[1]
|
|
2307
|
+
// Special escapes inside injection.
|
|
2308
|
+
if (3 < size(pathref)) {
|
|
2309
|
+
pathref = pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
|
|
2310
|
+
}
|
|
2311
|
+
// Get the extracted path reference.
|
|
2312
|
+
out = getpath(store, pathref, inj)
|
|
2313
|
+
} else {
|
|
2314
|
+
// Check for injections within the string.
|
|
2315
|
+
const partial = (_m, ref) => {
|
|
2316
|
+
// Special escapes inside injection.
|
|
2317
|
+
if (3 < size(ref)) {
|
|
2318
|
+
ref = ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
|
|
2319
|
+
}
|
|
2320
|
+
if (inj) {
|
|
2321
|
+
inj.full = false
|
|
2322
|
+
}
|
|
2323
|
+
const found = getpath(store, ref, inj)
|
|
2324
|
+
// Ensure inject value is a string.
|
|
2325
|
+
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
|
|
2326
|
+
}
|
|
2327
|
+
out = val.replace(R_INJECTION_PARTIAL, partial)
|
|
2328
|
+
// Also call the inj handler on the entire string, providing the
|
|
2329
|
+
// option for custom injection.
|
|
2330
|
+
if (null != inj && isfunc(inj.handler)) {
|
|
2331
|
+
inj.full = true
|
|
2332
|
+
out = inj.handler(inj, out, val, store)
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
return out
|
|
2336
|
+
}
|
|
2337
|
+
// Handler Utilities
|
|
2338
|
+
// =================
|
|
2339
|
+
const MODENAME = {
|
|
2340
|
+
[M_VAL]: 'val',
|
|
2341
|
+
[M_KEYPRE]: 'key:pre',
|
|
2342
|
+
[M_KEYPOST]: 'key:post',
|
|
2343
|
+
}
|
|
2344
|
+
const PLACEMENT = {
|
|
2345
|
+
[M_VAL]: 'value',
|
|
2346
|
+
[M_KEYPRE]: S_key,
|
|
2347
|
+
[M_KEYPOST]: S_key,
|
|
2348
|
+
}
|
|
2349
|
+
function checkPlacement(modes, ijname, parentTypes, inj) {
|
|
2350
|
+
if (0 === (modes & inj.mode)) {
|
|
2351
|
+
inj.errs.push(
|
|
2352
|
+
'$' +
|
|
2353
|
+
ijname +
|
|
2354
|
+
': invalid placement as ' +
|
|
2355
|
+
PLACEMENT[inj.mode] +
|
|
2356
|
+
', expected: ' +
|
|
2357
|
+
join(
|
|
2358
|
+
items(
|
|
2359
|
+
[M_KEYPRE, M_KEYPOST, M_VAL].filter((m) => modes & m),
|
|
2360
|
+
(n) => PLACEMENT[n[1]],
|
|
2361
|
+
),
|
|
2362
|
+
',',
|
|
2363
|
+
) +
|
|
2364
|
+
'.',
|
|
2365
|
+
)
|
|
2366
|
+
return false
|
|
2367
|
+
}
|
|
2368
|
+
if (!isempty(parentTypes)) {
|
|
2369
|
+
const ptype = typify(inj.parent)
|
|
2370
|
+
if (0 === (parentTypes & ptype)) {
|
|
2371
|
+
inj.errs.push(
|
|
2372
|
+
'$' +
|
|
2373
|
+
ijname +
|
|
2374
|
+
': invalid placement in parent ' +
|
|
2375
|
+
typename(ptype) +
|
|
2376
|
+
', expected: ' +
|
|
2377
|
+
typename(parentTypes) +
|
|
2378
|
+
'.',
|
|
2379
|
+
)
|
|
2380
|
+
return false
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
return true
|
|
2384
|
+
}
|
|
2385
|
+
// function injectorArgs(argTypes: number[], inj: Injection): any {
|
|
2386
|
+
function injectorArgs(argTypes, args) {
|
|
2387
|
+
const numargs = size(argTypes)
|
|
2388
|
+
const found = new Array(1 + numargs)
|
|
2389
|
+
found[0] = NONE
|
|
2390
|
+
for (let argI = 0; argI < numargs; argI++) {
|
|
2391
|
+
// const arg = inj.parent[1 + argI]
|
|
2392
|
+
const arg = args[argI]
|
|
2393
|
+
const argType = typify(arg)
|
|
2394
|
+
if (0 === (argTypes[argI] & argType)) {
|
|
2395
|
+
found[0] =
|
|
2396
|
+
'invalid argument: ' +
|
|
2397
|
+
stringify(arg, 22) +
|
|
2398
|
+
' (' +
|
|
2399
|
+
typename(argType) +
|
|
2400
|
+
' at position ' +
|
|
2401
|
+
(1 + argI) +
|
|
2402
|
+
') is not of type: ' +
|
|
2403
|
+
typename(argTypes[argI]) +
|
|
2404
|
+
'.'
|
|
2405
|
+
break
|
|
2406
|
+
}
|
|
2407
|
+
found[1 + argI] = arg
|
|
2408
|
+
}
|
|
2409
|
+
return found
|
|
2410
|
+
}
|
|
2411
|
+
function injectChild(child, store, inj) {
|
|
2412
|
+
let cinj = inj
|
|
2413
|
+
// Replace ['`$FORMAT`',...] with child
|
|
2414
|
+
if (null != inj.prior) {
|
|
2415
|
+
if (null != inj.prior.prior) {
|
|
2416
|
+
cinj = inj.prior.prior.child(inj.prior.keyI, inj.prior.keys)
|
|
2417
|
+
cinj.val = child
|
|
2418
|
+
setprop(cinj.parent, inj.prior.key, child)
|
|
2419
|
+
} else {
|
|
2420
|
+
cinj = inj.prior.child(inj.keyI, inj.keys)
|
|
2421
|
+
cinj.val = child
|
|
2422
|
+
setprop(cinj.parent, inj.key, child)
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2425
|
+
// console.log('FORMAT-INJECT-CHILD', child)
|
|
2426
|
+
inject(child, store, cinj)
|
|
2427
|
+
return cinj
|
|
2428
|
+
}
|
|
2429
|
+
class StructUtility {
|
|
2430
|
+
constructor() {
|
|
2431
|
+
this.clone = clone
|
|
2432
|
+
this.delprop = delprop
|
|
2433
|
+
this.escre = escre
|
|
2434
|
+
this.escurl = escurl
|
|
2435
|
+
this.filter = filter
|
|
2436
|
+
this.flatten = flatten
|
|
2437
|
+
this.getdef = getdef
|
|
2438
|
+
this.getelem = getelem
|
|
2439
|
+
this.getpath = getpath
|
|
2440
|
+
this.getprop = getprop
|
|
2441
|
+
this.haskey = haskey
|
|
2442
|
+
this.inject = inject
|
|
2443
|
+
this.isempty = isempty
|
|
2444
|
+
this.isfunc = isfunc
|
|
2445
|
+
this.iskey = iskey
|
|
2446
|
+
this.islist = islist
|
|
2447
|
+
this.ismap = ismap
|
|
2448
|
+
this.isnode = isnode
|
|
2449
|
+
this.items = items
|
|
2450
|
+
this.join = join
|
|
2451
|
+
this.jsonify = jsonify
|
|
2452
|
+
this.keysof = keysof
|
|
2453
|
+
this.merge = merge
|
|
2454
|
+
this.pad = pad
|
|
2455
|
+
this.pathify = pathify
|
|
2456
|
+
this.select = select
|
|
2457
|
+
this.setpath = setpath
|
|
2458
|
+
this.setprop = setprop
|
|
2459
|
+
this.size = size
|
|
2460
|
+
this.slice = slice
|
|
2461
|
+
this.strkey = strkey
|
|
2462
|
+
this.stringify = stringify
|
|
2463
|
+
this.transform = transform
|
|
2464
|
+
this.typify = typify
|
|
2465
|
+
this.typename = typename
|
|
2466
|
+
this.validate = validate
|
|
2467
|
+
this.walk = walk
|
|
2468
|
+
this.re_compile = re_compile
|
|
2469
|
+
this.re_find = re_find
|
|
2470
|
+
this.re_find_all = re_find_all
|
|
2471
|
+
this.re_replace = re_replace
|
|
2472
|
+
this.re_test = re_test
|
|
2473
|
+
this.re_escape = re_escape
|
|
2474
|
+
this.SKIP = SKIP
|
|
2475
|
+
this.DELETE = DELETE
|
|
2476
|
+
this.jm = jm
|
|
2477
|
+
this.jt = jt
|
|
2478
|
+
this.tn = typename
|
|
2479
|
+
this.T_any = T_any
|
|
2480
|
+
this.T_noval = T_noval
|
|
2481
|
+
this.T_boolean = T_boolean
|
|
2482
|
+
this.T_decimal = T_decimal
|
|
2483
|
+
this.T_integer = T_integer
|
|
2484
|
+
this.T_number = T_number
|
|
2485
|
+
this.T_string = T_string
|
|
2486
|
+
this.T_function = T_function
|
|
2487
|
+
this.T_symbol = T_symbol
|
|
2488
|
+
this.T_null = T_null
|
|
2489
|
+
this.T_list = T_list
|
|
2490
|
+
this.T_map = T_map
|
|
2491
|
+
this.T_instance = T_instance
|
|
2492
|
+
this.T_scalar = T_scalar
|
|
2493
|
+
this.T_node = T_node
|
|
2494
|
+
this.checkPlacement = checkPlacement
|
|
2495
|
+
this.injectorArgs = injectorArgs
|
|
2496
|
+
this.injectChild = injectChild
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
module.exports = {
|
|
2501
|
+
StructUtility,
|
|
2502
|
+
Injection,
|
|
2503
|
+
|
|
2504
|
+
re_compile,
|
|
2505
|
+
re_find,
|
|
2506
|
+
re_find_all,
|
|
2507
|
+
re_replace,
|
|
2508
|
+
re_test,
|
|
2509
|
+
re_escape,
|
|
2510
|
+
|
|
2511
|
+
clone,
|
|
2512
|
+
delprop,
|
|
2513
|
+
escre,
|
|
2514
|
+
escurl,
|
|
2515
|
+
filter,
|
|
2516
|
+
flatten,
|
|
2517
|
+
getdef,
|
|
2518
|
+
getelem,
|
|
2519
|
+
getpath,
|
|
2520
|
+
getprop,
|
|
2521
|
+
haskey,
|
|
2522
|
+
inject,
|
|
2523
|
+
isempty,
|
|
2524
|
+
isfunc,
|
|
2525
|
+
iskey,
|
|
2526
|
+
islist,
|
|
2527
|
+
ismap,
|
|
2528
|
+
isnode,
|
|
2529
|
+
items,
|
|
2530
|
+
join,
|
|
2531
|
+
jsonify,
|
|
2532
|
+
keysof,
|
|
2533
|
+
merge,
|
|
2534
|
+
pad,
|
|
2535
|
+
pathify,
|
|
2536
|
+
select,
|
|
2537
|
+
setpath,
|
|
2538
|
+
setprop,
|
|
2539
|
+
size,
|
|
2540
|
+
slice,
|
|
2541
|
+
strkey,
|
|
2542
|
+
stringify,
|
|
2543
|
+
transform,
|
|
2544
|
+
typify,
|
|
2545
|
+
typename,
|
|
2546
|
+
validate,
|
|
2547
|
+
walk,
|
|
2548
|
+
|
|
2549
|
+
SKIP,
|
|
2550
|
+
DELETE,
|
|
2551
|
+
|
|
2552
|
+
jm,
|
|
2553
|
+
jt,
|
|
2554
|
+
|
|
2555
|
+
T_any,
|
|
2556
|
+
T_noval,
|
|
2557
|
+
T_boolean,
|
|
2558
|
+
T_decimal,
|
|
2559
|
+
T_integer,
|
|
2560
|
+
T_number,
|
|
2561
|
+
T_string,
|
|
2562
|
+
T_function,
|
|
2563
|
+
T_symbol,
|
|
2564
|
+
T_null,
|
|
2565
|
+
T_list,
|
|
2566
|
+
T_map,
|
|
2567
|
+
T_instance,
|
|
2568
|
+
T_scalar,
|
|
2569
|
+
T_node,
|
|
2570
|
+
|
|
2571
|
+
M_KEYPRE,
|
|
2572
|
+
M_KEYPOST,
|
|
2573
|
+
M_VAL,
|
|
2574
|
+
|
|
2575
|
+
MODENAME,
|
|
2576
|
+
|
|
2577
|
+
checkPlacement,
|
|
2578
|
+
injectorArgs,
|
|
2579
|
+
injectChild,
|
|
2580
|
+
}
|