@voxgig/struct 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/StructUtility.d.ts +75 -11
- package/dist/StructUtility.js +860 -394
- package/dist/StructUtility.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/StructUtility.ts +1020 -422
package/src/StructUtility.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* Copyright (c) 2025 Voxgig Ltd. MIT LICENSE. */
|
|
1
|
+
/* Copyright (c) 2025-2026 Voxgig Ltd. MIT LICENSE. */
|
|
2
2
|
|
|
3
|
-
// VERSION: @voxgig/struct 0.0.
|
|
3
|
+
// VERSION: @voxgig/struct 0.0.10
|
|
4
4
|
|
|
5
5
|
/* Voxgig Struct
|
|
6
6
|
* =============
|
|
@@ -33,13 +33,15 @@
|
|
|
33
33
|
* - stringify: human-friendly string version of a value.
|
|
34
34
|
* - escre: escape a regular expresion string.
|
|
35
35
|
* - escurl: escape a url.
|
|
36
|
-
* -
|
|
36
|
+
* - join: join parts of a url, merging forward slashes.
|
|
37
37
|
*
|
|
38
38
|
* This set of functions and supporting utilities is designed to work
|
|
39
39
|
* uniformly across many languages, meaning that some code that may be
|
|
40
40
|
* functionally redundant in specific languages is still retained to
|
|
41
41
|
* keep the code human comparable.
|
|
42
42
|
*
|
|
43
|
+
* NOTE: Lists are assumed to be mutable and reference stable.
|
|
44
|
+
*
|
|
43
45
|
* NOTE: In this code JSON nulls are in general *not* considered the
|
|
44
46
|
* same as the undefined value in the given language. However most
|
|
45
47
|
* JSON parsers do use the undefined value to represent JSON
|
|
@@ -54,16 +56,16 @@
|
|
|
54
56
|
|
|
55
57
|
// String constants are explicitly defined.
|
|
56
58
|
|
|
57
|
-
// Mode value for inject step.
|
|
58
|
-
const
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const S_MKEY = 'key'
|
|
59
|
+
// Mode value for inject step (bitfield).
|
|
60
|
+
const M_KEYPRE = 1
|
|
61
|
+
const M_KEYPOST = 2
|
|
62
|
+
const M_VAL = 4
|
|
62
63
|
|
|
63
|
-
// Special
|
|
64
|
+
// Special strings.
|
|
64
65
|
const S_BKEY = '`$KEY`'
|
|
65
66
|
const S_BANNO = '`$ANNO`'
|
|
66
67
|
const S_BEXACT = '`$EXACT`'
|
|
68
|
+
const S_BVAL = '`$VAL`'
|
|
67
69
|
|
|
68
70
|
const S_DKEY = '$KEY'
|
|
69
71
|
const S_DTOP = '$TOP'
|
|
@@ -71,16 +73,26 @@ const S_DERRS = '$ERRS'
|
|
|
71
73
|
const S_DSPEC = '$SPEC'
|
|
72
74
|
|
|
73
75
|
// General strings.
|
|
74
|
-
const
|
|
76
|
+
const S_list = 'list'
|
|
75
77
|
const S_base = 'base'
|
|
76
78
|
const S_boolean = 'boolean'
|
|
77
79
|
const S_function = 'function'
|
|
80
|
+
const S_symbol = 'symbol'
|
|
78
81
|
const S_instance = 'instance'
|
|
79
82
|
const S_key = 'key'
|
|
83
|
+
const S_any = 'any'
|
|
84
|
+
const S_nil = 'nil'
|
|
80
85
|
const S_null = 'null'
|
|
81
86
|
const S_number = 'number'
|
|
82
87
|
const S_object = 'object'
|
|
83
88
|
const S_string = 'string'
|
|
89
|
+
const S_decimal = 'decimal'
|
|
90
|
+
const S_integer = 'integer'
|
|
91
|
+
const S_map = 'map'
|
|
92
|
+
const S_scalar = 'scalar'
|
|
93
|
+
const S_node = 'node'
|
|
94
|
+
|
|
95
|
+
// Character strings.
|
|
84
96
|
const S_BT = '`'
|
|
85
97
|
const S_CN = ':'
|
|
86
98
|
const S_CS = ']'
|
|
@@ -91,14 +103,57 @@ const S_KEY = 'KEY'
|
|
|
91
103
|
const S_MT = ''
|
|
92
104
|
const S_OS = '['
|
|
93
105
|
const S_SP = ' '
|
|
106
|
+
const S_CM = ','
|
|
94
107
|
const S_VIZ = ': '
|
|
95
108
|
|
|
109
|
+
// Types
|
|
110
|
+
let t = 31
|
|
111
|
+
const T_any = (1 << t--) - 1
|
|
112
|
+
const T_noval = 1 << t-- // Means property absent, undefined. Also NOT a scalar!
|
|
113
|
+
const T_boolean = 1 << t--
|
|
114
|
+
const T_decimal = 1 << t--
|
|
115
|
+
const T_integer = 1 << t--
|
|
116
|
+
const T_number = 1 << t--
|
|
117
|
+
const T_string = 1 << t--
|
|
118
|
+
const T_function = 1 << t--
|
|
119
|
+
const T_symbol = 1 << t--
|
|
120
|
+
const T_null = 1 << t-- // The actual JSON null value.
|
|
121
|
+
t -= 7
|
|
122
|
+
const T_list = 1 << t--
|
|
123
|
+
const T_map = 1 << t--
|
|
124
|
+
const T_instance = 1 << t--
|
|
125
|
+
t -= 4
|
|
126
|
+
const T_scalar = 1 << t--
|
|
127
|
+
const T_node = 1 << t--
|
|
128
|
+
|
|
129
|
+
const TYPENAME = [
|
|
130
|
+
S_any,
|
|
131
|
+
S_nil,
|
|
132
|
+
S_boolean,
|
|
133
|
+
S_decimal,
|
|
134
|
+
S_integer,
|
|
135
|
+
S_number,
|
|
136
|
+
S_string,
|
|
137
|
+
S_function,
|
|
138
|
+
S_symbol,
|
|
139
|
+
S_null,
|
|
140
|
+
'', '', '',
|
|
141
|
+
'', '', '', '',
|
|
142
|
+
S_list,
|
|
143
|
+
S_map,
|
|
144
|
+
S_instance,
|
|
145
|
+
'', '', '', '',
|
|
146
|
+
S_scalar,
|
|
147
|
+
S_node,
|
|
148
|
+
]
|
|
96
149
|
|
|
97
150
|
// The standard undefined value for this language.
|
|
98
|
-
const
|
|
151
|
+
const NONE = undefined
|
|
99
152
|
|
|
100
|
-
// Private
|
|
153
|
+
// Private markers
|
|
101
154
|
const SKIP = { '`$SKIP`': true }
|
|
155
|
+
const DELETE = { '`$DELETE`': true }
|
|
156
|
+
|
|
102
157
|
|
|
103
158
|
// Regular expression constants
|
|
104
159
|
const R_INTEGER_KEY = /^[-0-9]+$/ // Match integer keys (including <0).
|
|
@@ -108,7 +163,7 @@ const R_LEADING_TRAILING_SLASH = /([^\/])\/+/ // Multiple slashes in UR
|
|
|
108
163
|
const R_LEADING_SLASH = /^\/+/ // Leading slashes in URLs.
|
|
109
164
|
const R_QUOTES = /"/g // Double quotes for removal.
|
|
110
165
|
const R_DOT = /\./g // Dots in path strings.
|
|
111
|
-
const
|
|
166
|
+
const R_CLONE_REF = /^`\$REF:([0-9]+)`$/ // Copy reference in cloning.
|
|
112
167
|
const R_META_PATH = /^([^$]+)\$([=~])(.+)$/ // Meta path syntax.
|
|
113
168
|
const R_DOUBLE_DOLLAR = /\$\$/g // Double dollar escape sequence.
|
|
114
169
|
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g // Transform command names.
|
|
@@ -117,6 +172,8 @@ const R_BT_ESCAPE = /\$BT/g // Backtick escape sequen
|
|
|
117
172
|
const R_DS_ESCAPE = /\$DS/g // Dollar sign escape sequence.
|
|
118
173
|
const R_INJECTION_PARTIAL = /`([^`]+)`/g // Partial string injection pattern.
|
|
119
174
|
|
|
175
|
+
// Default max depth (for walk etc).
|
|
176
|
+
const MAXDEPTH = 32
|
|
120
177
|
|
|
121
178
|
// Keys are strings for maps, or integers for lists.
|
|
122
179
|
type PropKey = string | number
|
|
@@ -128,8 +185,7 @@ type Indexable = { [key: string]: any } & { [key: number]: any }
|
|
|
128
185
|
// For each key in a node (map or list), perform value injections in
|
|
129
186
|
// three phases: on key value, before child, and then on key value again.
|
|
130
187
|
// This mode is passed via the Injection structure.
|
|
131
|
-
type InjectMode =
|
|
132
|
-
|
|
188
|
+
type InjectMode = number
|
|
133
189
|
|
|
134
190
|
// Handle value injections using backtick escape sequences:
|
|
135
191
|
// - `a.b.c`: insert value at {a:{b:{c:1}}}
|
|
@@ -141,7 +197,6 @@ type Injector = (
|
|
|
141
197
|
store: any, // Current source root value.
|
|
142
198
|
) => any
|
|
143
199
|
|
|
144
|
-
|
|
145
200
|
// Apply a custom modification to injections.
|
|
146
201
|
type Modify = (
|
|
147
202
|
val: any, // Value.
|
|
@@ -151,11 +206,10 @@ type Modify = (
|
|
|
151
206
|
store?: any, // Store, if any
|
|
152
207
|
) => void
|
|
153
208
|
|
|
154
|
-
|
|
155
209
|
// Function applied to each node and leaf when walking a node structure depth first.
|
|
156
210
|
// For {a:{b:1}} the call sequence args will be: b, 1, {b:1}, [a,b].
|
|
157
211
|
type WalkApply = (
|
|
158
|
-
// Map keys are strings, list keys are numbers, top key is
|
|
212
|
+
// Map keys are strings, list keys are numbers, top key is NONE
|
|
159
213
|
key: string | number | undefined,
|
|
160
214
|
val: any,
|
|
161
215
|
parent: any,
|
|
@@ -163,6 +217,20 @@ type WalkApply = (
|
|
|
163
217
|
) => any
|
|
164
218
|
|
|
165
219
|
|
|
220
|
+
// Return type string for narrowest type.
|
|
221
|
+
function typename(t: number) {
|
|
222
|
+
return getelem(TYPENAME, Math.clz32(t), TYPENAME[0])
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
// Get a defined value. Returns alt if val is undefined.
|
|
227
|
+
function getdef(val: any, alt: any) {
|
|
228
|
+
if (NONE === val) {
|
|
229
|
+
return alt
|
|
230
|
+
}
|
|
231
|
+
return val
|
|
232
|
+
}
|
|
233
|
+
|
|
166
234
|
|
|
167
235
|
// Value is a node - defined, and a map (hash) or list (array).
|
|
168
236
|
// NOTE: typescript
|
|
@@ -232,11 +300,14 @@ function size(val: any): number {
|
|
|
232
300
|
}
|
|
233
301
|
|
|
234
302
|
|
|
235
|
-
// Extract part of an array or string into a new value, from the start
|
|
236
|
-
// If no end is specified, extract to the
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
|
|
303
|
+
// Extract part of an array or string into a new value, from the start
|
|
304
|
+
// point to the end point. If no end is specified, extract to the
|
|
305
|
+
// full length of the value. Negative arguments count from the end of
|
|
306
|
+
// the value. For numbers, perform min and max bounding, where start
|
|
307
|
+
// is inclusive, and end is *exclusive*.
|
|
308
|
+
// NOTE: input lists are not mutated by default. Use the mutate
|
|
309
|
+
// argument to mutate lists in place.
|
|
310
|
+
function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boolean): V {
|
|
240
311
|
if (S_number === typeof val) {
|
|
241
312
|
start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start
|
|
242
313
|
end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1
|
|
@@ -280,7 +351,15 @@ function slice<V extends any>(val: V, start?: number, end?: number): V {
|
|
|
280
351
|
|
|
281
352
|
if (-1 < start && start <= end && end <= vlen) {
|
|
282
353
|
if (islist(val)) {
|
|
283
|
-
|
|
354
|
+
if (mutate) {
|
|
355
|
+
for (let i = 0, j = start; j < end; i++, j++) {
|
|
356
|
+
val[i] = val[j]
|
|
357
|
+
}
|
|
358
|
+
val.length = (end - start)
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
val = val.slice(start, end) as V
|
|
362
|
+
}
|
|
284
363
|
}
|
|
285
364
|
else if (S_string === typeof val) {
|
|
286
365
|
val = (val as string).substring(start, end) as V
|
|
@@ -300,6 +379,7 @@ function slice<V extends any>(val: V, start?: number, end?: number): V {
|
|
|
300
379
|
}
|
|
301
380
|
|
|
302
381
|
|
|
382
|
+
// String padding.
|
|
303
383
|
function pad(str: any, padding?: number, padchar?: string): string {
|
|
304
384
|
str = S_string === typeof str ? str : stringify(str)
|
|
305
385
|
padding = null == padding ? 44 : padding
|
|
@@ -308,48 +388,71 @@ function pad(str: any, padding?: number, padchar?: string): string {
|
|
|
308
388
|
}
|
|
309
389
|
|
|
310
390
|
|
|
311
|
-
// Determine the type of a value as a
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
function typify(value: any): string {
|
|
317
|
-
if (value === null || value === undefined) {
|
|
318
|
-
return S_null
|
|
391
|
+
// Determine the type of a value as a bit code.
|
|
392
|
+
function typify(value: any): number {
|
|
393
|
+
|
|
394
|
+
if (undefined === value) {
|
|
395
|
+
return T_noval
|
|
319
396
|
}
|
|
320
397
|
|
|
321
|
-
const
|
|
398
|
+
const typestr = typeof value
|
|
322
399
|
|
|
323
|
-
if (
|
|
324
|
-
return
|
|
400
|
+
if (null === value) {
|
|
401
|
+
return T_scalar | T_null
|
|
402
|
+
}
|
|
403
|
+
else if (S_number === typestr) {
|
|
404
|
+
if (Number.isInteger(value)) {
|
|
405
|
+
return T_scalar | T_number | T_integer
|
|
406
|
+
}
|
|
407
|
+
else if (isNaN(value)) {
|
|
408
|
+
return T_noval
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
return T_scalar | T_number | T_decimal
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
else if (S_string === typestr) {
|
|
415
|
+
return T_scalar | T_string
|
|
416
|
+
}
|
|
417
|
+
else if (S_boolean === typestr) {
|
|
418
|
+
return T_scalar | T_boolean
|
|
419
|
+
}
|
|
420
|
+
else if (S_function === typestr) {
|
|
421
|
+
return T_scalar | T_function
|
|
325
422
|
}
|
|
326
423
|
|
|
327
|
-
|
|
328
|
-
|
|
424
|
+
// For languages that have symbolic atoms.
|
|
425
|
+
else if (S_symbol === typestr) {
|
|
426
|
+
return T_scalar | T_symbol
|
|
329
427
|
}
|
|
330
428
|
|
|
331
|
-
if (
|
|
429
|
+
else if (Array.isArray(value)) {
|
|
430
|
+
return T_node | T_list
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
else if (S_object === typestr) {
|
|
332
434
|
|
|
333
435
|
if (value.constructor instanceof Function) {
|
|
334
436
|
let cname = value.constructor.name
|
|
335
437
|
if ('Object' !== cname && 'Array' !== cname) {
|
|
336
|
-
return
|
|
438
|
+
return T_node | T_instance
|
|
337
439
|
}
|
|
338
440
|
}
|
|
339
441
|
|
|
340
|
-
return
|
|
442
|
+
return T_node | T_map
|
|
341
443
|
}
|
|
342
444
|
|
|
343
|
-
|
|
445
|
+
// Anything else (e.g. bigint) is considered T_any
|
|
446
|
+
return T_any
|
|
344
447
|
}
|
|
345
448
|
|
|
346
449
|
|
|
347
450
|
// Get a list element. The key should be an integer, or a string
|
|
348
451
|
// that can parse to an integer only. Negative integers count from the end of the list.
|
|
349
452
|
function getelem(val: any, key: any, alt?: any) {
|
|
350
|
-
let out =
|
|
453
|
+
let out = NONE
|
|
351
454
|
|
|
352
|
-
if (
|
|
455
|
+
if (NONE === val || NONE === key) {
|
|
353
456
|
return alt
|
|
354
457
|
}
|
|
355
458
|
|
|
@@ -363,8 +466,8 @@ function getelem(val: any, key: any, alt?: any) {
|
|
|
363
466
|
}
|
|
364
467
|
}
|
|
365
468
|
|
|
366
|
-
if (
|
|
367
|
-
return alt
|
|
469
|
+
if (NONE === out) {
|
|
470
|
+
return 0 < (T_function & typify(alt)) ? alt() : alt
|
|
368
471
|
}
|
|
369
472
|
|
|
370
473
|
return out
|
|
@@ -376,7 +479,7 @@ function getelem(val: any, key: any, alt?: any) {
|
|
|
376
479
|
function getprop(val: any, key: any, alt?: any) {
|
|
377
480
|
let out = alt
|
|
378
481
|
|
|
379
|
-
if (
|
|
482
|
+
if (NONE === val || NONE === key) {
|
|
380
483
|
return alt
|
|
381
484
|
}
|
|
382
485
|
|
|
@@ -384,7 +487,7 @@ function getprop(val: any, key: any, alt?: any) {
|
|
|
384
487
|
out = val[key]
|
|
385
488
|
}
|
|
386
489
|
|
|
387
|
-
if (
|
|
490
|
+
if (NONE === out) {
|
|
388
491
|
return alt
|
|
389
492
|
}
|
|
390
493
|
|
|
@@ -397,20 +500,20 @@ function getprop(val: any, key: any, alt?: any) {
|
|
|
397
500
|
// Number keys are converted to strings.
|
|
398
501
|
// Floats are truncated to integers.
|
|
399
502
|
// Booleans, objects, arrays, null, undefined all return empty string.
|
|
400
|
-
function strkey(key: any =
|
|
401
|
-
if (
|
|
503
|
+
function strkey(key: any = NONE): string {
|
|
504
|
+
if (NONE === key) {
|
|
402
505
|
return S_MT
|
|
403
506
|
}
|
|
404
507
|
|
|
405
|
-
|
|
508
|
+
const t = typify(key)
|
|
509
|
+
|
|
510
|
+
if (0 < (T_string & t)) {
|
|
406
511
|
return key
|
|
407
512
|
}
|
|
408
|
-
|
|
409
|
-
if (typeof key === S_boolean) {
|
|
513
|
+
else if (0 < (T_boolean & t)) {
|
|
410
514
|
return S_MT
|
|
411
515
|
}
|
|
412
|
-
|
|
413
|
-
if (typeof key === S_number) {
|
|
516
|
+
else if (0 < (T_number & t)) {
|
|
414
517
|
return key % 1 === 0 ? String(key) : String(Math.floor(key))
|
|
415
518
|
}
|
|
416
519
|
|
|
@@ -419,6 +522,7 @@ function strkey(key: any = UNDEF): string {
|
|
|
419
522
|
|
|
420
523
|
|
|
421
524
|
// Sorted keys of a map, or indexes (as strings) of a list.
|
|
525
|
+
// Root utility - only uses language facilities.
|
|
422
526
|
function keysof(val: any): string[] {
|
|
423
527
|
return !isnode(val) ? [] :
|
|
424
528
|
ismap(val) ? Object.keys(val).sort() : (val as any).map((_n: any, i: number) => S_MT + i)
|
|
@@ -426,22 +530,60 @@ function keysof(val: any): string[] {
|
|
|
426
530
|
|
|
427
531
|
|
|
428
532
|
// Value of property with name key in node val is defined.
|
|
533
|
+
// Root utility - only uses language facilities.
|
|
429
534
|
function haskey(val: any, key: any) {
|
|
430
|
-
return
|
|
535
|
+
return NONE !== getprop(val, key)
|
|
431
536
|
}
|
|
432
537
|
|
|
433
538
|
|
|
434
539
|
// List the sorted keys of a map or list as an array of tuples of the form [key, value].
|
|
435
540
|
// As with keysof, list indexes are converted to strings.
|
|
436
|
-
|
|
437
|
-
|
|
541
|
+
// Root utility - only uses language facilities.
|
|
542
|
+
function items(val: any): [string, any][];
|
|
543
|
+
function items<T>(val: any, apply: (item: [string, any]) => T): T[];
|
|
544
|
+
function items(
|
|
545
|
+
val: any,
|
|
546
|
+
apply?: (item: [string, any]) => any
|
|
547
|
+
): any[] {
|
|
548
|
+
let out: [string, any][] = keysof(val).map((k: any) => [k, val[k]])
|
|
549
|
+
if (null != apply) {
|
|
550
|
+
out = out.map(apply)
|
|
551
|
+
}
|
|
552
|
+
return out
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
// To replicate the array spread operator:
|
|
557
|
+
// a=1, b=[2,3], c=[4,5]
|
|
558
|
+
// [a,...b,c] -> [1,2,3,[4,5]]
|
|
559
|
+
// flatten([a,b,[c]]) -> [1,2,3,[4,5]]
|
|
560
|
+
// NOTE: [c] ensures c is not expanded
|
|
561
|
+
function flatten(list: any[], depth?: number) {
|
|
562
|
+
if (!islist(list)) {
|
|
563
|
+
return list
|
|
564
|
+
}
|
|
565
|
+
return list.flat(getdef(depth, 1))
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
// Filter item values using check function.
|
|
570
|
+
function filter(val: any, check: (item: [string, any]) => boolean): any[] {
|
|
571
|
+
let all = items(val)
|
|
572
|
+
let numall = size(all)
|
|
573
|
+
let out = []
|
|
574
|
+
for (let i = 0; i < numall; i++) {
|
|
575
|
+
if (check(all[i])) {
|
|
576
|
+
out.push(all[i][1])
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return out
|
|
438
580
|
}
|
|
439
581
|
|
|
440
582
|
|
|
441
583
|
// Escape regular expression.
|
|
442
584
|
function escre(s: string) {
|
|
443
|
-
s = null == s ? S_MT : s
|
|
444
|
-
return
|
|
585
|
+
// s = null == s ? S_MT : s
|
|
586
|
+
return replace(s, R_ESCAPE_REGEXP, '\\$&')
|
|
445
587
|
}
|
|
446
588
|
|
|
447
589
|
|
|
@@ -452,16 +594,59 @@ function escurl(s: string) {
|
|
|
452
594
|
}
|
|
453
595
|
|
|
454
596
|
|
|
455
|
-
//
|
|
456
|
-
function
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
597
|
+
// Replace a search string (all), or a regexp, in a source string.
|
|
598
|
+
function replace(s: string, from: string | RegExp, to: any) {
|
|
599
|
+
let rs = s
|
|
600
|
+
let ts = typify(s)
|
|
601
|
+
if (0 === (T_string & ts)) {
|
|
602
|
+
rs = stringify(s)
|
|
603
|
+
}
|
|
604
|
+
else if (0 < ((T_noval | T_null) & ts)) {
|
|
605
|
+
rs = S_MT
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
rs = stringify(s)
|
|
609
|
+
}
|
|
610
|
+
return rs.replace(from, to)
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
// Concatenate url part strings, merging sep char as needed.
|
|
615
|
+
function join(arr: any[], sep?: string, url?: boolean) {
|
|
616
|
+
const sarr = size(arr)
|
|
617
|
+
const sepdef = getdef(sep, S_CM)
|
|
618
|
+
const sepre = 1 === size(sepdef) ? escre(sepdef) : NONE
|
|
619
|
+
const out = filter(
|
|
620
|
+
items(
|
|
621
|
+
// filter(arr, (n) => null != n[1] && S_MT !== n[1]),
|
|
622
|
+
filter(arr, (n) => (0 < (T_string & typify(n[1]))) && S_MT !== n[1]),
|
|
623
|
+
(n) => {
|
|
624
|
+
let i = +n[0]
|
|
625
|
+
let s = n[1]
|
|
626
|
+
|
|
627
|
+
if (NONE !== sepre && S_MT !== sepre) {
|
|
628
|
+
if (url && 0 === i) {
|
|
629
|
+
s = replace(s, RegExp(sepre + '+$'), S_MT)
|
|
630
|
+
return s
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
if (0 < i) {
|
|
634
|
+
s = replace(s, RegExp('^' + sepre + '+'), S_MT)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (i < sarr - 1 || !url) {
|
|
638
|
+
s = replace(s, RegExp(sepre + '+$'), S_MT)
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
s = replace(s, RegExp('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
|
|
642
|
+
'$1' + sepdef + '$2')
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
return s
|
|
646
|
+
}), (n) => S_MT !== n[1])
|
|
647
|
+
.join(sepdef)
|
|
648
|
+
|
|
649
|
+
return out
|
|
465
650
|
}
|
|
466
651
|
|
|
467
652
|
|
|
@@ -470,19 +655,27 @@ function joinurl(sarr: any[]) {
|
|
|
470
655
|
// In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
|
|
471
656
|
function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
|
|
472
657
|
let str = S_null
|
|
658
|
+
|
|
473
659
|
if (null != val) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
str
|
|
660
|
+
try {
|
|
661
|
+
const indent = getprop(flags, 'indent', 2)
|
|
662
|
+
str = JSON.stringify(val, null, indent)
|
|
663
|
+
if (NONE === str) {
|
|
664
|
+
str = S_null
|
|
665
|
+
}
|
|
666
|
+
const offset = getprop(flags, 'offset', 0)
|
|
667
|
+
if (0 < offset) {
|
|
668
|
+
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
669
|
+
// indented by offset. Assume first brace is on line with asignment, so not offset.
|
|
670
|
+
str = '{\n' +
|
|
671
|
+
join(
|
|
672
|
+
items(
|
|
673
|
+
slice(str.split('\n'), 1),
|
|
674
|
+
(n: any) => pad(n[1], 0 - offset - size(n[1]))), '\n')
|
|
675
|
+
}
|
|
478
676
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
482
|
-
// indented by offset.
|
|
483
|
-
str = '{\n' + str.split('\n').slice(1)
|
|
484
|
-
.map(n => pad(n, 0 - offset - size(n)))
|
|
485
|
-
.join('\n')
|
|
677
|
+
catch (e: any) {
|
|
678
|
+
str = '__JSONIFY_FAILED__'
|
|
486
679
|
}
|
|
487
680
|
}
|
|
488
681
|
|
|
@@ -495,7 +688,7 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
495
688
|
let valstr = S_MT
|
|
496
689
|
pretty = !!pretty
|
|
497
690
|
|
|
498
|
-
if (
|
|
691
|
+
if (NONE === val) {
|
|
499
692
|
return pretty ? '<>' : valstr
|
|
500
693
|
}
|
|
501
694
|
|
|
@@ -511,9 +704,9 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
511
704
|
!Array.isArray(val)
|
|
512
705
|
) {
|
|
513
706
|
const sortedObj: any = {}
|
|
514
|
-
|
|
515
|
-
sortedObj[
|
|
516
|
-
}
|
|
707
|
+
items(val, (n) => {
|
|
708
|
+
sortedObj[n[0]] = val[n[0]]
|
|
709
|
+
})
|
|
517
710
|
return sortedObj
|
|
518
711
|
}
|
|
519
712
|
return val
|
|
@@ -521,7 +714,7 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
521
714
|
valstr = valstr.replace(R_QUOTES, S_MT)
|
|
522
715
|
}
|
|
523
716
|
catch (err: any) {
|
|
524
|
-
valstr =
|
|
717
|
+
valstr = '__STRINGIFY_FAILED__'
|
|
525
718
|
}
|
|
526
719
|
}
|
|
527
720
|
|
|
@@ -532,8 +725,9 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
532
725
|
|
|
533
726
|
if (pretty) {
|
|
534
727
|
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
535
|
-
let c =
|
|
536
|
-
|
|
728
|
+
let c = items(
|
|
729
|
+
[81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69],
|
|
730
|
+
(n) => '\x1b[38;5;' + n[1] + 'm'),
|
|
537
731
|
r = '\x1b[0m', d = 0, o = c[0], t = o
|
|
538
732
|
for (const ch of valstr) {
|
|
539
733
|
if (ch === '{' || ch === '[') {
|
|
@@ -554,34 +748,34 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
554
748
|
|
|
555
749
|
// Build a human friendly path string.
|
|
556
750
|
function pathify(val: any, startin?: number, endin?: number) {
|
|
557
|
-
let pathstr: string | undefined =
|
|
751
|
+
let pathstr: string | undefined = NONE
|
|
558
752
|
|
|
559
753
|
let path: any[] | undefined = islist(val) ? val :
|
|
560
754
|
S_string == typeof val ? [val] :
|
|
561
755
|
S_number == typeof val ? [val] :
|
|
562
|
-
|
|
756
|
+
NONE
|
|
563
757
|
|
|
564
758
|
const start = null == startin ? 0 : -1 < startin ? startin : 0
|
|
565
759
|
const end = null == endin ? 0 : -1 < endin ? endin : 0
|
|
566
760
|
|
|
567
|
-
if (
|
|
761
|
+
if (NONE != path && 0 <= start) {
|
|
568
762
|
path = slice(path, start, path.length - end)
|
|
569
763
|
if (0 === path.length) {
|
|
570
764
|
pathstr = '<root>'
|
|
571
765
|
}
|
|
572
766
|
else {
|
|
573
|
-
pathstr =
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
767
|
+
pathstr = join(
|
|
768
|
+
items(
|
|
769
|
+
filter(path, (n) => iskey(n[1])), (n) => {
|
|
770
|
+
let p = n[1]
|
|
771
|
+
return S_number === typeof p ? S_MT + Math.floor(p) :
|
|
772
|
+
p.replace(R_DOT, S_MT)
|
|
773
|
+
}), S_DT)
|
|
580
774
|
}
|
|
581
775
|
}
|
|
582
776
|
|
|
583
|
-
if (
|
|
584
|
-
pathstr = '<unknown-path' + (
|
|
777
|
+
if (NONE === pathstr) {
|
|
778
|
+
pathstr = '<unknown-path' + (NONE === val ? S_MT : S_CN + stringify(val, 47)) + '>'
|
|
585
779
|
}
|
|
586
780
|
|
|
587
781
|
return pathstr
|
|
@@ -589,19 +783,21 @@ function pathify(val: any, startin?: number, endin?: number) {
|
|
|
589
783
|
|
|
590
784
|
|
|
591
785
|
// Clone a JSON-like data structure.
|
|
592
|
-
// NOTE: function
|
|
786
|
+
// NOTE: function and instance values are copied, *not* cloned.
|
|
593
787
|
function clone(val: any): any {
|
|
594
788
|
const refs: any[] = []
|
|
595
|
-
const
|
|
596
|
-
|
|
789
|
+
const reftype = T_function | T_instance
|
|
790
|
+
const replacer: any = (_k: any, v: any) => 0 < (reftype & typify(v)) ?
|
|
791
|
+
(refs.push(v), '`$REF:' + (refs.length - 1) + '`') : v
|
|
597
792
|
const reviver: any = (_k: any, v: any, m: any) => S_string === typeof v ?
|
|
598
|
-
(m = v.match(
|
|
599
|
-
|
|
793
|
+
(m = v.match(R_CLONE_REF), m ? refs[m[1]] : v) : v
|
|
794
|
+
const out = NONE === val ? NONE : JSON.parse(JSON.stringify(val, replacer), reviver)
|
|
795
|
+
return out
|
|
600
796
|
}
|
|
601
797
|
|
|
602
798
|
|
|
603
799
|
// Define a JSON Object using function arguments.
|
|
604
|
-
function
|
|
800
|
+
function jm(...kv: any[]): Record<string, any> {
|
|
605
801
|
const kvsize = size(kv)
|
|
606
802
|
const o: any = {}
|
|
607
803
|
for (let i = 0; i < kvsize; i += 2) {
|
|
@@ -614,7 +810,7 @@ function jo(...kv: any[]): Record<string, any> {
|
|
|
614
810
|
|
|
615
811
|
|
|
616
812
|
// Define a JSON Array using function arguments.
|
|
617
|
-
function
|
|
813
|
+
function jt(...v: any[]): any[] {
|
|
618
814
|
const vsize = size(v)
|
|
619
815
|
const a: any = new Array(vsize)
|
|
620
816
|
for (let i = 0; i < vsize; i++) {
|
|
@@ -624,19 +820,18 @@ function ja(...v: any[]): any[] {
|
|
|
624
820
|
}
|
|
625
821
|
|
|
626
822
|
|
|
627
|
-
|
|
628
823
|
// Safely delete a property from an object or array element.
|
|
629
824
|
// Undefined arguments and invalid keys are ignored.
|
|
630
825
|
// Returns the (possibly modified) parent.
|
|
631
826
|
// For objects, the property is deleted using the delete operator.
|
|
632
827
|
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
828
|
+
// NOTE: parent list may be new list, thus update references.
|
|
633
829
|
function delprop<PARENT>(parent: PARENT, key: any): PARENT {
|
|
634
830
|
if (!iskey(key)) {
|
|
635
831
|
return parent
|
|
636
832
|
}
|
|
637
833
|
|
|
638
834
|
if (ismap(parent)) {
|
|
639
|
-
// key = S_MT + key
|
|
640
835
|
key = strkey(key)
|
|
641
836
|
delete (parent as any)[key]
|
|
642
837
|
}
|
|
@@ -651,10 +846,12 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
|
|
|
651
846
|
keyI = Math.floor(keyI)
|
|
652
847
|
|
|
653
848
|
// Delete list element at position keyI, shifting later elements down.
|
|
654
|
-
|
|
655
|
-
|
|
849
|
+
const psize = size(parent)
|
|
850
|
+
if (0 <= keyI && keyI < psize) {
|
|
851
|
+
for (let pI = keyI; pI < psize - 1; pI++) {
|
|
656
852
|
parent[pI] = parent[pI + 1]
|
|
657
853
|
}
|
|
854
|
+
|
|
658
855
|
parent.length = parent.length - 1
|
|
659
856
|
}
|
|
660
857
|
}
|
|
@@ -667,6 +864,7 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
|
|
|
667
864
|
// Returns the (possibly modified) parent.
|
|
668
865
|
// If the parent is a list, and the key is negative, prepend the value.
|
|
669
866
|
// NOTE: If the key is above the list size, append the value; below, prepend.
|
|
867
|
+
// NOTE: parent list may be new list, thus update references.
|
|
670
868
|
function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
671
869
|
if (!iskey(key)) {
|
|
672
870
|
return parent
|
|
@@ -687,9 +885,11 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
|
687
885
|
|
|
688
886
|
keyI = Math.floor(keyI)
|
|
689
887
|
|
|
888
|
+
// TODO: DELETE list element
|
|
889
|
+
|
|
690
890
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
691
891
|
if (0 <= keyI) {
|
|
692
|
-
parent[
|
|
892
|
+
parent[slice(keyI, 0, size(parent) + 1)] = val
|
|
693
893
|
}
|
|
694
894
|
|
|
695
895
|
// Prepend value if keyI is negative
|
|
@@ -721,26 +921,27 @@ function walk(
|
|
|
721
921
|
parent?: any,
|
|
722
922
|
path?: string[]
|
|
723
923
|
): any {
|
|
724
|
-
|
|
924
|
+
if (NONE === path) {
|
|
925
|
+
path = []
|
|
926
|
+
}
|
|
725
927
|
|
|
726
|
-
|
|
928
|
+
let out = null == before ? val : before(key, val, parent, path)
|
|
929
|
+
|
|
930
|
+
maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : MAXDEPTH
|
|
727
931
|
if (0 === maxdepth || (null != path && 0 < maxdepth && maxdepth <= path.length)) {
|
|
728
932
|
return out
|
|
729
933
|
}
|
|
730
934
|
|
|
731
|
-
// if (isnode(val)) {
|
|
732
935
|
if (isnode(out)) {
|
|
733
|
-
// for (let [ckey, child] of items(val)) {
|
|
734
936
|
for (let [ckey, child] of items(out)) {
|
|
735
|
-
// setprop(val, ckey, walk(
|
|
736
937
|
setprop(out, ckey, walk(
|
|
737
|
-
|
|
738
|
-
|
|
938
|
+
child, before, after, maxdepth, ckey, out,
|
|
939
|
+
flatten([getdef(path, []), S_MT + ckey])
|
|
940
|
+
))
|
|
739
941
|
}
|
|
740
942
|
}
|
|
741
943
|
|
|
742
|
-
|
|
743
|
-
out = null == after ? out : after(key, out, parent, path || [])
|
|
944
|
+
out = null == after ? out : after(key, out, parent, path)
|
|
744
945
|
|
|
745
946
|
return out
|
|
746
947
|
}
|
|
@@ -751,7 +952,9 @@ function walk(
|
|
|
751
952
|
// override each other, and do *not* merge. The first element is
|
|
752
953
|
// modified.
|
|
753
954
|
function merge(val: any, maxdepth?: number): any {
|
|
754
|
-
|
|
955
|
+
// const md: number = null == maxdepth ? MAXDEPTH : maxdepth < 0 ? 0 : maxdepth
|
|
956
|
+
const md: number = slice(maxdepth ?? MAXDEPTH, 0)
|
|
957
|
+
let out: any = NONE
|
|
755
958
|
|
|
756
959
|
// Handle edge cases.
|
|
757
960
|
if (!islist(val)) {
|
|
@@ -762,7 +965,7 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
762
965
|
const lenlist = list.length
|
|
763
966
|
|
|
764
967
|
if (0 === lenlist) {
|
|
765
|
-
return
|
|
968
|
+
return NONE
|
|
766
969
|
}
|
|
767
970
|
else if (1 === lenlist) {
|
|
768
971
|
return list[0]
|
|
@@ -793,8 +996,12 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
793
996
|
) {
|
|
794
997
|
const pI = size(path)
|
|
795
998
|
|
|
999
|
+
if (md <= pI) {
|
|
1000
|
+
setprop(cur[pI - 1], key, val)
|
|
1001
|
+
}
|
|
1002
|
+
|
|
796
1003
|
// Scalars just override directly.
|
|
797
|
-
if (!isnode(val)) {
|
|
1004
|
+
else if (!isnode(val)) {
|
|
798
1005
|
cur[pI] = val
|
|
799
1006
|
}
|
|
800
1007
|
|
|
@@ -805,8 +1012,8 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
805
1012
|
dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI]
|
|
806
1013
|
const tval = dst[pI]
|
|
807
1014
|
|
|
808
|
-
// Destination empty, so create node.
|
|
809
|
-
if (
|
|
1015
|
+
// Destination empty, so create node (unless override is class instance).
|
|
1016
|
+
if (NONE === tval && 0 === (T_instance & typify(val))) {
|
|
810
1017
|
cur[pI] = islist(val) ? [] : {}
|
|
811
1018
|
}
|
|
812
1019
|
|
|
@@ -820,7 +1027,7 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
820
1027
|
cur[pI] = val
|
|
821
1028
|
|
|
822
1029
|
// No need to descend when override wins (destination is discarded).
|
|
823
|
-
val =
|
|
1030
|
+
val = NONE
|
|
824
1031
|
}
|
|
825
1032
|
}
|
|
826
1033
|
|
|
@@ -831,7 +1038,6 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
831
1038
|
return val
|
|
832
1039
|
}
|
|
833
1040
|
|
|
834
|
-
|
|
835
1041
|
function after(
|
|
836
1042
|
key: string | number | undefined,
|
|
837
1043
|
_val: any,
|
|
@@ -842,7 +1048,7 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
842
1048
|
const target = cur[cI - 1]
|
|
843
1049
|
const value = cur[cI]
|
|
844
1050
|
|
|
845
|
-
// console.log('AFTER-PREP', pathify(path), '@', cI,
|
|
1051
|
+
// console.log('AFTER-PREP', pathify(path), '@', cI, cur, '|',
|
|
846
1052
|
// stringify(key, -1, 1), stringify(value, -1, 1), 'T=', stringify(target, -1, 1))
|
|
847
1053
|
|
|
848
1054
|
setprop(target, key, value)
|
|
@@ -851,22 +1057,71 @@ function merge(val: any, maxdepth?: number): any {
|
|
|
851
1057
|
|
|
852
1058
|
// Walk overriding node, creating paths in output as needed.
|
|
853
1059
|
out = walk(obj, before, after, maxdepth)
|
|
1060
|
+
// console.log('WALK-DONE', out, obj)
|
|
854
1061
|
}
|
|
855
1062
|
}
|
|
856
1063
|
|
|
1064
|
+
if (0 === md) {
|
|
1065
|
+
out = getelem(list, -1)
|
|
1066
|
+
out = islist(out) ? [] : ismap(out) ? {} : out
|
|
1067
|
+
}
|
|
1068
|
+
|
|
857
1069
|
return out
|
|
858
1070
|
}
|
|
859
1071
|
|
|
860
1072
|
|
|
1073
|
+
// Set a value using a path. Missing path parts are created.
|
|
1074
|
+
// String paths create only maps. Use a string list to create list parts.
|
|
1075
|
+
function setpath(
|
|
1076
|
+
store: any,
|
|
1077
|
+
path: number | string | string[],
|
|
1078
|
+
val: any,
|
|
1079
|
+
injdef?: Partial<Injection>
|
|
1080
|
+
) {
|
|
1081
|
+
const pathType = typify(path)
|
|
1082
|
+
|
|
1083
|
+
const parts = 0 < (T_list & pathType) ? path :
|
|
1084
|
+
0 < (T_string & pathType) ? (path as string).split(S_DT) :
|
|
1085
|
+
0 < (T_number & pathType) ? [path] : NONE
|
|
1086
|
+
|
|
1087
|
+
if (NONE === parts) {
|
|
1088
|
+
return NONE
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
const base = getprop(injdef, S_base)
|
|
1092
|
+
const numparts = size(parts)
|
|
1093
|
+
let parent = getprop(store, base, store)
|
|
1094
|
+
|
|
1095
|
+
for (let pI = 0; pI < numparts - 1; pI++) {
|
|
1096
|
+
const partKey = getelem(parts, pI)
|
|
1097
|
+
let nextParent = getprop(parent, partKey)
|
|
1098
|
+
if (!isnode(nextParent)) {
|
|
1099
|
+
nextParent = 0 < (T_number & typify(getelem(parts, pI + 1))) ? [] : {}
|
|
1100
|
+
setprop(parent, partKey, nextParent)
|
|
1101
|
+
}
|
|
1102
|
+
parent = nextParent
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
if (DELETE === val) {
|
|
1106
|
+
delprop(parent, getelem(parts, -1))
|
|
1107
|
+
}
|
|
1108
|
+
else {
|
|
1109
|
+
setprop(parent, getelem(parts, -1), val)
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
return parent
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
|
|
861
1116
|
function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
|
|
862
1117
|
|
|
863
1118
|
// Operate on a string array.
|
|
864
1119
|
const parts = islist(path) ? path :
|
|
865
1120
|
'string' === typeof path ? path.split(S_DT) :
|
|
866
|
-
'number' === typeof path ? [strkey(path)] :
|
|
1121
|
+
'number' === typeof path ? [strkey(path)] : NONE
|
|
867
1122
|
|
|
868
|
-
if (
|
|
869
|
-
return
|
|
1123
|
+
if (NONE === parts) {
|
|
1124
|
+
return NONE
|
|
870
1125
|
}
|
|
871
1126
|
|
|
872
1127
|
// let root = store
|
|
@@ -898,7 +1153,7 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
|
|
|
898
1153
|
|
|
899
1154
|
const dpath = getprop(injdef, 'dpath')
|
|
900
1155
|
|
|
901
|
-
for (let pI = 0;
|
|
1156
|
+
for (let pI = 0; NONE !== val && pI < numparts; pI++) {
|
|
902
1157
|
let part = parts[pI]
|
|
903
1158
|
|
|
904
1159
|
if (injdef && S_DKEY === part) {
|
|
@@ -906,15 +1161,15 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
|
|
|
906
1161
|
}
|
|
907
1162
|
else if (injdef && part.startsWith('$GET:')) {
|
|
908
1163
|
// $GET:path$ -> get store value, use as path part (string)
|
|
909
|
-
part = stringify(getpath(src, part
|
|
1164
|
+
part = stringify(getpath(src, slice(part, 5, -1)))
|
|
910
1165
|
}
|
|
911
1166
|
else if (injdef && part.startsWith('$REF:')) {
|
|
912
1167
|
// $REF:refpath$ -> get spec value, use as path part (string)
|
|
913
|
-
part = stringify(getpath(getprop(store, S_DSPEC), part
|
|
1168
|
+
part = stringify(getpath(getprop(store, S_DSPEC), slice(part, 5, -1)))
|
|
914
1169
|
}
|
|
915
1170
|
else if (injdef && part.startsWith('$META:')) {
|
|
916
1171
|
// $META:metapath$ -> get meta value, use as path part (string)
|
|
917
|
-
part = stringify(getpath(getprop(injdef, 'meta'), part
|
|
1172
|
+
part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)))
|
|
918
1173
|
}
|
|
919
1174
|
|
|
920
1175
|
// $$ escapes $
|
|
@@ -937,14 +1192,16 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
|
|
|
937
1192
|
val = dparent
|
|
938
1193
|
}
|
|
939
1194
|
else {
|
|
940
|
-
const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
1195
|
+
// const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
1196
|
+
const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)])
|
|
941
1197
|
|
|
942
1198
|
if (ascends <= size(dpath)) {
|
|
943
1199
|
val = getpath(store, fullpath)
|
|
944
1200
|
}
|
|
945
1201
|
else {
|
|
946
|
-
val =
|
|
1202
|
+
val = NONE
|
|
947
1203
|
}
|
|
1204
|
+
|
|
948
1205
|
break
|
|
949
1206
|
}
|
|
950
1207
|
}
|
|
@@ -966,13 +1223,14 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
|
|
|
966
1223
|
val = handler(injdef, val, ref, store)
|
|
967
1224
|
}
|
|
968
1225
|
|
|
1226
|
+
// console.log('GETPATH', path, val)
|
|
1227
|
+
|
|
969
1228
|
return val
|
|
970
1229
|
}
|
|
971
1230
|
|
|
972
1231
|
|
|
973
|
-
|
|
974
1232
|
// Inject values from a data store into a node recursively, resolving
|
|
975
|
-
// paths against the store, or current if they are local.
|
|
1233
|
+
// paths against the store, or current if they are local. The modify
|
|
976
1234
|
// argument allows custom modification of the result. The inj
|
|
977
1235
|
// (Injection) argument is used to maintain recursive state.
|
|
978
1236
|
function inject(
|
|
@@ -985,14 +1243,14 @@ function inject(
|
|
|
985
1243
|
|
|
986
1244
|
// Create state if at root of injection. The input value is placed
|
|
987
1245
|
// inside a virtual parent holder to simplify edge cases.
|
|
988
|
-
if (
|
|
1246
|
+
if (NONE === injdef || null == injdef.mode) {
|
|
989
1247
|
// Set up state assuming we are starting in the virtual parent.
|
|
990
1248
|
inj = new Injection(val, { [S_DTOP]: val })
|
|
991
1249
|
inj.dparent = store
|
|
992
1250
|
inj.errs = getprop(store, S_DERRS, [])
|
|
993
1251
|
inj.meta.__d = 0
|
|
994
1252
|
|
|
995
|
-
if (
|
|
1253
|
+
if (NONE !== injdef) {
|
|
996
1254
|
inj.modify = null == injdef.modify ? inj.modify : injdef.modify
|
|
997
1255
|
inj.extra = null == injdef.extra ? inj.extra : injdef.extra
|
|
998
1256
|
inj.meta = null == injdef.meta ? inj.meta : injdef.meta
|
|
@@ -1002,6 +1260,9 @@ function inject(
|
|
|
1002
1260
|
|
|
1003
1261
|
inj.descend()
|
|
1004
1262
|
|
|
1263
|
+
// console.log('INJ-START', val, inj.mode, inj.key, inj.val,
|
|
1264
|
+
// 't=', inj.path, 'P=', inj.parent, 'dp=', inj.dparent, 'ST=', store.$TOP)
|
|
1265
|
+
|
|
1005
1266
|
// Descend into node.
|
|
1006
1267
|
if (isnode(val)) {
|
|
1007
1268
|
|
|
@@ -1009,21 +1270,29 @@ function inject(
|
|
|
1009
1270
|
// Injection transforms ($FOO) are processed *after* other keys.
|
|
1010
1271
|
// NOTE: the optional digits suffix of the transform can thus be
|
|
1011
1272
|
// used to order the transforms.
|
|
1012
|
-
let nodekeys = ismap(val) ? [
|
|
1013
|
-
...Object.keys(val).filter(k => !k.includes(S_DS)).sort(),
|
|
1014
|
-
...Object.keys(val).filter(k => k.includes(S_DS)).sort(),
|
|
1015
|
-
] : (val as any).map((_n: any, i: number) => i)
|
|
1016
1273
|
|
|
1274
|
+
let nodekeys: any[]
|
|
1275
|
+
nodekeys = keysof(val)
|
|
1276
|
+
|
|
1277
|
+
if (ismap(val)) {
|
|
1278
|
+
nodekeys = flatten([
|
|
1279
|
+
filter(nodekeys, (n => !n[1].includes(S_DS))),
|
|
1280
|
+
filter(nodekeys, (n => n[1].includes(S_DS))),
|
|
1281
|
+
])
|
|
1282
|
+
}
|
|
1283
|
+
else {
|
|
1284
|
+
nodekeys = keysof(val)
|
|
1285
|
+
}
|
|
1017
1286
|
|
|
1018
1287
|
// Each child key-value pair is processed in three injection phases:
|
|
1019
|
-
// 1. inj.mode=
|
|
1020
|
-
// 2. inj.mode=
|
|
1021
|
-
// 3. inj.mode=
|
|
1288
|
+
// 1. inj.mode=M_KEYPRE - Key string is injected, returning a possibly altered key.
|
|
1289
|
+
// 2. inj.mode=M_VAL - The child value is injected.
|
|
1290
|
+
// 3. inj.mode=M_KEYPOST - Key string is injected again, allowing child mutation.
|
|
1022
1291
|
for (let nkI = 0; nkI < nodekeys.length; nkI++) {
|
|
1023
1292
|
|
|
1024
1293
|
const childinj = inj.child(nkI, nodekeys)
|
|
1025
1294
|
const nodekey = childinj.key
|
|
1026
|
-
childinj.mode =
|
|
1295
|
+
childinj.mode = M_KEYPRE
|
|
1027
1296
|
|
|
1028
1297
|
// Peform the key:pre mode injection on the child key.
|
|
1029
1298
|
const prekey = _injectstr(nodekey, store, childinj)
|
|
@@ -1033,9 +1302,9 @@ function inject(
|
|
|
1033
1302
|
nodekeys = childinj.keys
|
|
1034
1303
|
|
|
1035
1304
|
// Prevent further processing by returning an undefined prekey
|
|
1036
|
-
if (
|
|
1305
|
+
if (NONE !== prekey) {
|
|
1037
1306
|
childinj.val = getprop(val, prekey)
|
|
1038
|
-
childinj.mode =
|
|
1307
|
+
childinj.mode = M_VAL
|
|
1039
1308
|
|
|
1040
1309
|
// Perform the val mode injection on the child value.
|
|
1041
1310
|
// NOTE: return value is not used.
|
|
@@ -1046,7 +1315,7 @@ function inject(
|
|
|
1046
1315
|
nodekeys = childinj.keys
|
|
1047
1316
|
|
|
1048
1317
|
// Peform the key:post mode injection on the child key.
|
|
1049
|
-
childinj.mode =
|
|
1318
|
+
childinj.mode = M_KEYPOST
|
|
1050
1319
|
_injectstr(nodekey, store, childinj)
|
|
1051
1320
|
|
|
1052
1321
|
// The injection may modify child processing.
|
|
@@ -1058,7 +1327,7 @@ function inject(
|
|
|
1058
1327
|
|
|
1059
1328
|
// Inject paths into string scalars.
|
|
1060
1329
|
else if (S_string === valtype) {
|
|
1061
|
-
inj.mode =
|
|
1330
|
+
inj.mode = M_VAL
|
|
1062
1331
|
val = _injectstr(val, store, inj)
|
|
1063
1332
|
if (SKIP !== val) {
|
|
1064
1333
|
inj.setval(val)
|
|
@@ -1080,6 +1349,8 @@ function inject(
|
|
|
1080
1349
|
)
|
|
1081
1350
|
}
|
|
1082
1351
|
|
|
1352
|
+
// console.log('INJ-VAL', val)
|
|
1353
|
+
|
|
1083
1354
|
inj.val = val
|
|
1084
1355
|
|
|
1085
1356
|
// Original val reference may no longer be correct.
|
|
@@ -1092,21 +1363,22 @@ function inject(
|
|
|
1092
1363
|
|
|
1093
1364
|
// Delete a key from a map or list.
|
|
1094
1365
|
const transform_DELETE: Injector = (inj: Injection) => {
|
|
1095
|
-
inj.setval(
|
|
1096
|
-
return
|
|
1366
|
+
inj.setval(NONE)
|
|
1367
|
+
return NONE
|
|
1097
1368
|
}
|
|
1098
1369
|
|
|
1099
1370
|
|
|
1100
1371
|
// Copy value from source data.
|
|
1101
1372
|
const transform_COPY: Injector = (inj: Injection, _val: any) => {
|
|
1102
|
-
const
|
|
1373
|
+
const ijname = 'COPY'
|
|
1103
1374
|
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
out = getprop(inj.dparent, key)
|
|
1107
|
-
inj.setval(out)
|
|
1375
|
+
if (!checkPlacement(M_VAL, ijname, T_any, inj)) {
|
|
1376
|
+
return NONE
|
|
1108
1377
|
}
|
|
1109
1378
|
|
|
1379
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1380
|
+
inj.setval(out)
|
|
1381
|
+
|
|
1110
1382
|
return out
|
|
1111
1383
|
}
|
|
1112
1384
|
|
|
@@ -1116,29 +1388,30 @@ const transform_COPY: Injector = (inj: Injection, _val: any) => {
|
|
|
1116
1388
|
const transform_KEY: Injector = (inj: Injection) => {
|
|
1117
1389
|
const { mode, path, parent } = inj
|
|
1118
1390
|
|
|
1119
|
-
// Do nothing in val mode.
|
|
1120
|
-
if (
|
|
1121
|
-
return
|
|
1391
|
+
// Do nothing in val mode - not an error.
|
|
1392
|
+
if (M_VAL !== mode) {
|
|
1393
|
+
return NONE
|
|
1122
1394
|
}
|
|
1123
1395
|
|
|
1124
1396
|
// Key is defined by $KEY meta property.
|
|
1125
1397
|
const keyspec = getprop(parent, S_BKEY)
|
|
1126
|
-
if (
|
|
1398
|
+
if (NONE !== keyspec) {
|
|
1127
1399
|
delprop(parent, S_BKEY)
|
|
1128
1400
|
return getprop(inj.dparent, keyspec)
|
|
1129
1401
|
}
|
|
1130
1402
|
|
|
1131
1403
|
// Key is defined within general purpose $META object.
|
|
1132
|
-
return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1404
|
+
// return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
1405
|
+
return getprop(getprop(parent, S_BANNO), S_KEY, getelem(path, -2))
|
|
1133
1406
|
}
|
|
1134
1407
|
|
|
1135
1408
|
|
|
1136
|
-
//
|
|
1409
|
+
// Annotate node. Does nothing itself, just used by
|
|
1137
1410
|
// other injectors, and is removed when called.
|
|
1138
1411
|
const transform_ANNO: Injector = (inj: Injection) => {
|
|
1139
1412
|
const { parent } = inj
|
|
1140
1413
|
delprop(parent, S_BANNO)
|
|
1141
|
-
return
|
|
1414
|
+
return NONE
|
|
1142
1415
|
}
|
|
1143
1416
|
|
|
1144
1417
|
|
|
@@ -1151,29 +1424,27 @@ const transform_MERGE: Injector = (inj: Injection) => {
|
|
|
1151
1424
|
const { mode, key, parent } = inj
|
|
1152
1425
|
|
|
1153
1426
|
// Ensures $MERGE is removed from parent list (val mode).
|
|
1154
|
-
let out: any =
|
|
1427
|
+
let out: any = NONE
|
|
1155
1428
|
|
|
1156
|
-
if (
|
|
1429
|
+
if (M_KEYPRE === mode) {
|
|
1157
1430
|
out = key
|
|
1158
1431
|
}
|
|
1159
1432
|
|
|
1160
1433
|
// Operate after child values have been transformed.
|
|
1161
|
-
else if (
|
|
1434
|
+
else if (M_KEYPOST === mode) {
|
|
1162
1435
|
out = key
|
|
1163
1436
|
|
|
1164
1437
|
let args = getprop(parent, key)
|
|
1165
1438
|
args = Array.isArray(args) ? args : [args]
|
|
1166
1439
|
|
|
1167
1440
|
// Remove the $MERGE command from a parent map.
|
|
1168
|
-
inj.setval(
|
|
1441
|
+
inj.setval(NONE)
|
|
1169
1442
|
|
|
1170
1443
|
// Literals in the parent have precedence, but we still merge onto
|
|
1171
1444
|
// the parent object, so that node tree references are not changed.
|
|
1172
|
-
const mergelist = [parent,
|
|
1445
|
+
const mergelist = flatten([[parent], args, [clone(parent)]])
|
|
1173
1446
|
|
|
1174
1447
|
merge(mergelist)
|
|
1175
|
-
|
|
1176
|
-
// return key
|
|
1177
1448
|
}
|
|
1178
1449
|
|
|
1179
1450
|
return out
|
|
@@ -1188,65 +1459,63 @@ const transform_EACH: Injector = (
|
|
|
1188
1459
|
_ref: string,
|
|
1189
1460
|
store: any
|
|
1190
1461
|
) => {
|
|
1462
|
+
const ijname = 'EACH'
|
|
1191
1463
|
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
inj.keys.length = 1
|
|
1464
|
+
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1465
|
+
return NONE
|
|
1195
1466
|
}
|
|
1196
1467
|
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
}
|
|
1468
|
+
// Remove remaining keys to avoid spurious processing.
|
|
1469
|
+
slice(inj.keys, 0, 1, true)
|
|
1200
1470
|
|
|
1201
|
-
//
|
|
1202
|
-
const srcpath =
|
|
1203
|
-
|
|
1471
|
+
// const [err, srcpath, child] = injectorArgs([T_string, T_any], inj)
|
|
1472
|
+
const [err, srcpath, child] = injectorArgs([T_string, T_any], slice(inj.parent, 1))
|
|
1473
|
+
if (NONE !== err) {
|
|
1474
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1475
|
+
return NONE
|
|
1476
|
+
}
|
|
1204
1477
|
|
|
1205
1478
|
// Source data.
|
|
1206
1479
|
const srcstore = getprop(store, inj.base, store)
|
|
1207
1480
|
|
|
1208
1481
|
const src = getpath(srcstore, srcpath, inj)
|
|
1482
|
+
const srctype = typify(src)
|
|
1209
1483
|
|
|
1210
1484
|
// Create parallel data structures:
|
|
1211
1485
|
// source entries :: child templates
|
|
1212
1486
|
let tcur: any = []
|
|
1213
1487
|
let tval: any = []
|
|
1214
1488
|
|
|
1215
|
-
const tkey = inj.path
|
|
1216
|
-
const target = inj.nodes
|
|
1489
|
+
const tkey = getelem(inj.path, -2)
|
|
1490
|
+
const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
|
|
1217
1491
|
|
|
1218
1492
|
// Create clones of the child template for each value of the current soruce.
|
|
1219
|
-
if (
|
|
1220
|
-
tval = src
|
|
1493
|
+
if (0 < (T_list & srctype)) {
|
|
1494
|
+
tval = items(src, () => clone(child))
|
|
1221
1495
|
}
|
|
1222
|
-
else if (
|
|
1223
|
-
tval =
|
|
1224
|
-
|
|
1225
|
-
|
|
1496
|
+
else if (0 < (T_map & srctype)) {
|
|
1497
|
+
tval = items(src, (n => merge([
|
|
1498
|
+
clone(child),
|
|
1226
1499
|
// Make a note of the key for $KEY transforms.
|
|
1227
|
-
[S_BANNO]: { KEY: n[0] }
|
|
1228
|
-
|
|
1500
|
+
{ [S_BANNO]: { KEY: n[0] } }
|
|
1501
|
+
], 1)))
|
|
1229
1502
|
}
|
|
1230
1503
|
|
|
1231
1504
|
let rval = []
|
|
1232
1505
|
|
|
1233
1506
|
if (0 < size(tval)) {
|
|
1234
|
-
tcur = null == src ?
|
|
1507
|
+
tcur = null == src ? NONE : Object.values(src)
|
|
1235
1508
|
|
|
1236
1509
|
const ckey = getelem(inj.path, -2)
|
|
1237
1510
|
|
|
1238
1511
|
const tpath = slice(inj.path, -1)
|
|
1239
|
-
const dpath = [S_DTOP,
|
|
1240
|
-
|
|
1512
|
+
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey])
|
|
1241
1513
|
|
|
1242
1514
|
// Parent structure.
|
|
1243
|
-
|
|
1244
|
-
// const ckey = getelem(cpath, -1)
|
|
1245
1515
|
tcur = { [ckey]: tcur }
|
|
1246
1516
|
|
|
1247
|
-
if (1 < tpath
|
|
1517
|
+
if (1 < size(tpath)) {
|
|
1248
1518
|
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1249
|
-
// const pkey = getelem(cpath, -2, S_DTOP)
|
|
1250
1519
|
tcur = { [pkey]: tcur }
|
|
1251
1520
|
dpath.push('$:' + pkey)
|
|
1252
1521
|
}
|
|
@@ -1266,7 +1535,8 @@ const transform_EACH: Injector = (
|
|
|
1266
1535
|
rval = tinj.val
|
|
1267
1536
|
}
|
|
1268
1537
|
|
|
1269
|
-
_updateAncestors(inj, target, tkey, rval)
|
|
1538
|
+
// _updateAncestors(inj, target, tkey, rval)
|
|
1539
|
+
setprop(target, tkey, rval)
|
|
1270
1540
|
|
|
1271
1541
|
// Prevent callee from damaging first list entry (since we are in `val` mode).
|
|
1272
1542
|
return rval[0]
|
|
@@ -1274,7 +1544,7 @@ const transform_EACH: Injector = (
|
|
|
1274
1544
|
|
|
1275
1545
|
|
|
1276
1546
|
// Convert a node to a map.
|
|
1277
|
-
// Format: { '`$PACK`':['
|
|
1547
|
+
// Format: { '`$PACK`':['source-path', child-template]}
|
|
1278
1548
|
const transform_PACK: Injector = (
|
|
1279
1549
|
inj: Injection,
|
|
1280
1550
|
_val: any,
|
|
@@ -1283,78 +1553,105 @@ const transform_PACK: Injector = (
|
|
|
1283
1553
|
) => {
|
|
1284
1554
|
const { mode, key, path, parent, nodes } = inj
|
|
1285
1555
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1556
|
+
const ijname = 'EACH'
|
|
1557
|
+
|
|
1558
|
+
if (!checkPlacement(M_KEYPRE, ijname, T_map, inj)) {
|
|
1559
|
+
return NONE
|
|
1289
1560
|
}
|
|
1290
1561
|
|
|
1291
1562
|
// Get arguments.
|
|
1292
|
-
const args = parent
|
|
1293
|
-
const srcpath =
|
|
1294
|
-
|
|
1563
|
+
const args = getprop(parent, key)
|
|
1564
|
+
const [err, srcpath, origchildspec] = injectorArgs([T_string, T_any], args)
|
|
1565
|
+
if (NONE !== err) {
|
|
1566
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1567
|
+
return NONE
|
|
1568
|
+
}
|
|
1295
1569
|
|
|
1296
1570
|
// Find key and target node.
|
|
1297
|
-
const keyprop = child[S_BKEY]
|
|
1298
1571
|
const tkey = getelem(path, -2)
|
|
1299
|
-
const
|
|
1572
|
+
const pathsize = size(path)
|
|
1573
|
+
const target = getelem(nodes, pathsize - 2, () => getelem(nodes, pathsize - 1))
|
|
1300
1574
|
|
|
1301
1575
|
// Source data
|
|
1302
1576
|
const srcstore = getprop(store, inj.base, store)
|
|
1303
|
-
|
|
1304
1577
|
let src = getpath(srcstore, srcpath, inj)
|
|
1305
1578
|
|
|
1306
1579
|
// Prepare source as a list.
|
|
1307
|
-
|
|
1308
|
-
ismap(src)
|
|
1309
|
-
|
|
1310
|
-
(
|
|
1311
|
-
|
|
1580
|
+
if (!islist(src)) {
|
|
1581
|
+
if (ismap(src)) {
|
|
1582
|
+
src = items(src, (item: [string, any]) => {
|
|
1583
|
+
setprop(item[1], S_BANNO, { KEY: item[0] })
|
|
1584
|
+
return item[1]
|
|
1585
|
+
})
|
|
1586
|
+
}
|
|
1587
|
+
else {
|
|
1588
|
+
src = NONE
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1312
1591
|
|
|
1313
1592
|
if (null == src) {
|
|
1314
|
-
return
|
|
1593
|
+
return NONE
|
|
1315
1594
|
}
|
|
1316
1595
|
|
|
1317
|
-
// Get
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1596
|
+
// Get keypath.
|
|
1597
|
+
const keypath = getprop(origchildspec, S_BKEY)
|
|
1598
|
+
const childspec = delprop(origchildspec, S_BKEY)
|
|
1599
|
+
|
|
1600
|
+
const child = getprop(childspec, S_BVAL, childspec)
|
|
1321
1601
|
|
|
1322
1602
|
// Build parallel target object.
|
|
1323
1603
|
let tval: any = {}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
const
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1604
|
+
|
|
1605
|
+
items(src, (item: [string, any]) => {
|
|
1606
|
+
const srckey = item[0]
|
|
1607
|
+
const srcnode = item[1]
|
|
1608
|
+
|
|
1609
|
+
let key: string = srckey
|
|
1610
|
+
if (NONE !== keypath) {
|
|
1611
|
+
if (keypath.startsWith('`')) {
|
|
1612
|
+
key = inject(keypath, merge([{}, store, { $TOP: srcnode }], 1))
|
|
1613
|
+
}
|
|
1614
|
+
else {
|
|
1615
|
+
key = getpath(srcnode, keypath, inj)
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
const tchild = clone(child)
|
|
1620
|
+
setprop(tval, key, tchild)
|
|
1621
|
+
|
|
1622
|
+
const anno = getprop(srcnode, S_BANNO)
|
|
1623
|
+
if (NONE === anno) {
|
|
1624
|
+
delprop(tchild, S_BANNO)
|
|
1331
1625
|
}
|
|
1332
1626
|
else {
|
|
1333
|
-
setprop(
|
|
1627
|
+
setprop(tchild, S_BANNO, anno)
|
|
1334
1628
|
}
|
|
1335
|
-
|
|
1336
|
-
}, tval)
|
|
1629
|
+
})
|
|
1337
1630
|
|
|
1338
1631
|
let rval = {}
|
|
1339
1632
|
|
|
1340
|
-
if (
|
|
1633
|
+
if (!isempty(tval)) {
|
|
1341
1634
|
|
|
1342
1635
|
// Build parallel source object.
|
|
1343
|
-
let
|
|
1344
|
-
src.reduce((a: any, n: any) => {
|
|
1345
|
-
let kn =
|
|
1636
|
+
let tsrc: any = {}
|
|
1637
|
+
src.reduce((a: any, n: any, i: any) => {
|
|
1638
|
+
let kn = null == keypath ? i :
|
|
1639
|
+
keypath.startsWith('`') ?
|
|
1640
|
+
inject(keypath, merge([{}, store, { $TOP: n }], 1)) :
|
|
1641
|
+
getpath(n, keypath, inj)
|
|
1642
|
+
|
|
1346
1643
|
setprop(a, kn, n)
|
|
1347
1644
|
return a
|
|
1348
|
-
},
|
|
1645
|
+
}, tsrc)
|
|
1349
1646
|
|
|
1350
1647
|
const tpath = slice(inj.path, -1)
|
|
1351
1648
|
|
|
1352
1649
|
const ckey = getelem(inj.path, -2)
|
|
1353
|
-
const dpath = [S_DTOP,
|
|
1650
|
+
const dpath = flatten([S_DTOP, srcpath.split(S_DT), '$:' + ckey])
|
|
1354
1651
|
|
|
1355
|
-
tcur = { [ckey]:
|
|
1652
|
+
let tcur = { [ckey]: tsrc }
|
|
1356
1653
|
|
|
1357
|
-
if (1 < tpath
|
|
1654
|
+
if (1 < size(tpath)) {
|
|
1358
1655
|
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1359
1656
|
tcur = { [pkey]: tcur }
|
|
1360
1657
|
dpath.push('$:' + pkey)
|
|
@@ -1364,7 +1661,6 @@ const transform_PACK: Injector = (
|
|
|
1364
1661
|
tinj.path = tpath
|
|
1365
1662
|
tinj.nodes = slice(inj.nodes, -1)
|
|
1366
1663
|
|
|
1367
|
-
// tinj.parent = tcur
|
|
1368
1664
|
tinj.parent = getelem(tinj.nodes, -1)
|
|
1369
1665
|
tinj.val = tval
|
|
1370
1666
|
|
|
@@ -1375,14 +1671,15 @@ const transform_PACK: Injector = (
|
|
|
1375
1671
|
rval = tinj.val
|
|
1376
1672
|
}
|
|
1377
1673
|
|
|
1378
|
-
_updateAncestors(inj, target, tkey, rval)
|
|
1674
|
+
// _updateAncestors(inj, target, tkey, rval)
|
|
1675
|
+
setprop(target, tkey, rval)
|
|
1379
1676
|
|
|
1380
1677
|
// Drop transform key.
|
|
1381
|
-
return
|
|
1678
|
+
return NONE
|
|
1382
1679
|
}
|
|
1383
1680
|
|
|
1384
1681
|
|
|
1385
|
-
// TODO: not found ref should removed key (setprop
|
|
1682
|
+
// TODO: not found ref should removed key (setprop NONE)
|
|
1386
1683
|
// Reference original spec (enables recursice transformations)
|
|
1387
1684
|
// Format: ['`$REF`', '`spec-path`']
|
|
1388
1685
|
const transform_REF: Injector = (
|
|
@@ -1393,21 +1690,24 @@ const transform_REF: Injector = (
|
|
|
1393
1690
|
) => {
|
|
1394
1691
|
const { nodes } = inj
|
|
1395
1692
|
|
|
1396
|
-
if (
|
|
1397
|
-
return
|
|
1693
|
+
if (M_VAL !== inj.mode) {
|
|
1694
|
+
return NONE
|
|
1398
1695
|
}
|
|
1399
1696
|
|
|
1400
1697
|
// Get arguments: ['`$REF`', 'ref-path'].
|
|
1401
1698
|
const refpath = getprop(inj.parent, 1)
|
|
1402
|
-
inj.keyI = inj.keys
|
|
1699
|
+
inj.keyI = size(inj.keys)
|
|
1403
1700
|
|
|
1404
1701
|
// Spec reference.
|
|
1405
1702
|
const spec = getprop(store, S_DSPEC)()
|
|
1406
1703
|
|
|
1704
|
+
const dpath = slice(inj.path, 1)
|
|
1407
1705
|
const ref = getpath(spec, refpath, {
|
|
1408
1706
|
// TODO: test relative refs
|
|
1409
|
-
dpath: inj.path.slice(1),
|
|
1410
|
-
|
|
1707
|
+
// dpath: inj.path.slice(1),
|
|
1708
|
+
dpath,
|
|
1709
|
+
// dparent: getpath(spec, inj.path.slice(1))
|
|
1710
|
+
dparent: getpath(spec, dpath),
|
|
1411
1711
|
})
|
|
1412
1712
|
|
|
1413
1713
|
let hasSubRef = false
|
|
@@ -1426,9 +1726,9 @@ const transform_REF: Injector = (
|
|
|
1426
1726
|
const tpath = slice(inj.path, -1)
|
|
1427
1727
|
let tcur = getpath(store, cpath)
|
|
1428
1728
|
let tval = getpath(store, tpath)
|
|
1429
|
-
let rval =
|
|
1729
|
+
let rval = NONE
|
|
1430
1730
|
|
|
1431
|
-
if (!hasSubRef ||
|
|
1731
|
+
if (!hasSubRef || NONE !== tval) {
|
|
1432
1732
|
const tinj = inj.child(0, [getelem(tpath, -1)])
|
|
1433
1733
|
|
|
1434
1734
|
tinj.path = tpath
|
|
@@ -1436,7 +1736,7 @@ const transform_REF: Injector = (
|
|
|
1436
1736
|
tinj.parent = getelem(nodes, -2)
|
|
1437
1737
|
tinj.val = tref
|
|
1438
1738
|
|
|
1439
|
-
tinj.dpath = [
|
|
1739
|
+
tinj.dpath = flatten([cpath])
|
|
1440
1740
|
tinj.dparent = tcur
|
|
1441
1741
|
|
|
1442
1742
|
inject(tref, store, tinj)
|
|
@@ -1444,7 +1744,7 @@ const transform_REF: Injector = (
|
|
|
1444
1744
|
rval = tinj.val
|
|
1445
1745
|
}
|
|
1446
1746
|
else {
|
|
1447
|
-
rval =
|
|
1747
|
+
rval = NONE
|
|
1448
1748
|
}
|
|
1449
1749
|
|
|
1450
1750
|
const grandparent = inj.setval(rval, 2)
|
|
@@ -1457,6 +1757,117 @@ const transform_REF: Injector = (
|
|
|
1457
1757
|
}
|
|
1458
1758
|
|
|
1459
1759
|
|
|
1760
|
+
const transform_FORMAT: Injector = (
|
|
1761
|
+
inj: Injection,
|
|
1762
|
+
_val: any,
|
|
1763
|
+
_ref: string,
|
|
1764
|
+
store: any
|
|
1765
|
+
) => {
|
|
1766
|
+
// console.log('FORMAT-START', inj, _val)
|
|
1767
|
+
|
|
1768
|
+
// Remove remaining keys to avoid spurious processing.
|
|
1769
|
+
slice(inj.keys, 0, 1, true)
|
|
1770
|
+
|
|
1771
|
+
if (M_VAL !== inj.mode) {
|
|
1772
|
+
return NONE
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
// Get arguments: ['`$FORMAT`', 'name', child].
|
|
1776
|
+
// TODO: EACH and PACK should accept customm functions too
|
|
1777
|
+
const name = getprop(inj.parent, 1)
|
|
1778
|
+
const child = getprop(inj.parent, 2)
|
|
1779
|
+
|
|
1780
|
+
// Source data.
|
|
1781
|
+
const tkey = getelem(inj.path, -2)
|
|
1782
|
+
const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
|
|
1783
|
+
|
|
1784
|
+
const cinj = injectChild(child, store, inj)
|
|
1785
|
+
const resolved = cinj.val
|
|
1786
|
+
|
|
1787
|
+
let formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
|
|
1788
|
+
|
|
1789
|
+
if (NONE === formatter) {
|
|
1790
|
+
inj.errs.push('$FORMAT: unknown format: ' + name + '.')
|
|
1791
|
+
return NONE
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
let out = walk(resolved, formatter)
|
|
1795
|
+
|
|
1796
|
+
setprop(target, tkey, out)
|
|
1797
|
+
// _updateAncestors(inj, target, tkey, out)
|
|
1798
|
+
|
|
1799
|
+
return out
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
const FORMATTER: Record<string, WalkApply> = {
|
|
1804
|
+
identity: (_k: any, v: any) => v,
|
|
1805
|
+
upper: (_k: any, v: any) => isnode(v) ? v : ('' + v).toUpperCase(),
|
|
1806
|
+
lower: (_k: any, v: any) => isnode(v) ? v : ('' + v).toLowerCase(),
|
|
1807
|
+
string: (_k: any, v: any) => isnode(v) ? v : ('' + v),
|
|
1808
|
+
number: (_k: any, v: any) => {
|
|
1809
|
+
if (isnode(v)) {
|
|
1810
|
+
return v
|
|
1811
|
+
}
|
|
1812
|
+
else {
|
|
1813
|
+
let n = Number(v)
|
|
1814
|
+
if (isNaN(n)) {
|
|
1815
|
+
n = 0
|
|
1816
|
+
}
|
|
1817
|
+
return n
|
|
1818
|
+
}
|
|
1819
|
+
},
|
|
1820
|
+
integer: (_k: any, v: any) => {
|
|
1821
|
+
if (isnode(v)) {
|
|
1822
|
+
return v
|
|
1823
|
+
}
|
|
1824
|
+
else {
|
|
1825
|
+
let n = Number(v)
|
|
1826
|
+
if (isNaN(n)) {
|
|
1827
|
+
n = 0
|
|
1828
|
+
}
|
|
1829
|
+
return n | 0
|
|
1830
|
+
}
|
|
1831
|
+
},
|
|
1832
|
+
concat: (k: any, v: any) =>
|
|
1833
|
+
null == k && islist(v) ? join(items(v, (n => isnode(n[1]) ? S_MT : (S_MT + n[1]))), S_MT) : v
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
const transform_APPLY: Injector = (
|
|
1839
|
+
inj: Injection,
|
|
1840
|
+
_val: any,
|
|
1841
|
+
_ref: string,
|
|
1842
|
+
store: any
|
|
1843
|
+
) => {
|
|
1844
|
+
const ijname = 'APPLY'
|
|
1845
|
+
|
|
1846
|
+
if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
|
|
1847
|
+
return NONE
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
// const [err, apply, child] = injectorArgs([T_function, T_any], inj)
|
|
1851
|
+
const [err, apply, child] = injectorArgs([T_function, T_any], slice(inj.parent, 1))
|
|
1852
|
+
if (NONE !== err) {
|
|
1853
|
+
inj.errs.push('$' + ijname + ': ' + err)
|
|
1854
|
+
return NONE
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
const tkey = getelem(inj.path, -2)
|
|
1858
|
+
const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
|
|
1859
|
+
|
|
1860
|
+
const cinj = injectChild(child, store, inj)
|
|
1861
|
+
const resolved = cinj.val
|
|
1862
|
+
|
|
1863
|
+
const out = apply(resolved, store, cinj)
|
|
1864
|
+
|
|
1865
|
+
setprop(target, tkey, out)
|
|
1866
|
+
|
|
1867
|
+
return out
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
|
|
1460
1871
|
// Transform data using spec.
|
|
1461
1872
|
// Only operates on static JSON-like data.
|
|
1462
1873
|
// Arrays are treated as if they are objects with indices as keys.
|
|
@@ -1470,51 +1881,66 @@ function transform(
|
|
|
1470
1881
|
spec = clone(origspec)
|
|
1471
1882
|
|
|
1472
1883
|
const extra = injdef?.extra
|
|
1473
|
-
|
|
1884
|
+
|
|
1885
|
+
const collect = null != injdef?.errs
|
|
1886
|
+
const errs = injdef?.errs || []
|
|
1474
1887
|
|
|
1475
1888
|
const extraTransforms: any = {}
|
|
1476
|
-
const extraData = null == extra ?
|
|
1889
|
+
const extraData = null == extra ? NONE : items(extra)
|
|
1477
1890
|
.reduce((a: any, n: any[]) =>
|
|
1478
1891
|
(n[0].startsWith(S_DS) ? extraTransforms[n[0]] = n[1] : (a[n[0]] = n[1]), a), {})
|
|
1479
1892
|
|
|
1480
1893
|
const dataClone = merge([
|
|
1481
|
-
isempty(extraData) ?
|
|
1894
|
+
isempty(extraData) ? NONE : clone(extraData),
|
|
1482
1895
|
clone(data),
|
|
1483
1896
|
])
|
|
1484
1897
|
|
|
1485
1898
|
// Define a top level store that provides transform operations.
|
|
1486
|
-
const store =
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1899
|
+
const store = merge([
|
|
1900
|
+
{
|
|
1901
|
+
// The inject function recognises this special location for the root of the source data.
|
|
1902
|
+
// NOTE: to escape data that contains "`$FOO`" keys at the top level,
|
|
1903
|
+
// place that data inside a holding map: { myholder: mydata }.
|
|
1904
|
+
$TOP: dataClone,
|
|
1905
|
+
|
|
1906
|
+
$SPEC: () => origspec,
|
|
1907
|
+
|
|
1908
|
+
// Escape backtick (this also works inside backticks).
|
|
1909
|
+
$BT: () => S_BT,
|
|
1910
|
+
|
|
1911
|
+
// Escape dollar sign (this also works inside backticks).
|
|
1912
|
+
$DS: () => S_DS,
|
|
1913
|
+
|
|
1914
|
+
// Insert current date and time as an ISO string.
|
|
1915
|
+
$WHEN: () => new Date().toISOString(),
|
|
1916
|
+
|
|
1917
|
+
$DELETE: transform_DELETE,
|
|
1918
|
+
$COPY: transform_COPY,
|
|
1919
|
+
$KEY: transform_KEY,
|
|
1920
|
+
$ANNO: transform_ANNO,
|
|
1921
|
+
$MERGE: transform_MERGE,
|
|
1922
|
+
$EACH: transform_EACH,
|
|
1923
|
+
$PACK: transform_PACK,
|
|
1924
|
+
$REF: transform_REF,
|
|
1925
|
+
$FORMAT: transform_FORMAT,
|
|
1926
|
+
$APPLY: transform_APPLY,
|
|
1927
|
+
},
|
|
1494
1928
|
|
|
1495
|
-
//
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
// Escape dollar sign (this also works inside backticks).
|
|
1499
|
-
$DS: () => S_DS,
|
|
1929
|
+
// Custom extra transforms, if any.
|
|
1930
|
+
extraTransforms,
|
|
1500
1931
|
|
|
1501
|
-
|
|
1502
|
-
|
|
1932
|
+
{
|
|
1933
|
+
$ERRS: errs,
|
|
1934
|
+
}
|
|
1935
|
+
], 1)
|
|
1503
1936
|
|
|
1504
|
-
|
|
1505
|
-
$COPY: transform_COPY,
|
|
1506
|
-
$KEY: transform_KEY,
|
|
1507
|
-
$ANNO: transform_ANNO,
|
|
1508
|
-
$MERGE: transform_MERGE,
|
|
1509
|
-
$EACH: transform_EACH,
|
|
1510
|
-
$PACK: transform_PACK,
|
|
1511
|
-
$REF: transform_REF,
|
|
1937
|
+
const out = inject(spec, store, injdef)
|
|
1512
1938
|
|
|
1513
|
-
|
|
1514
|
-
|
|
1939
|
+
const generr = (0 < size(errs) && !collect)
|
|
1940
|
+
if (generr) {
|
|
1941
|
+
throw new Error(join(errs, ' | '))
|
|
1515
1942
|
}
|
|
1516
1943
|
|
|
1517
|
-
const out = inject(spec, store, injdef)
|
|
1518
1944
|
return out
|
|
1519
1945
|
}
|
|
1520
1946
|
|
|
@@ -1524,16 +1950,16 @@ const validate_STRING: Injector = (inj: Injection) => {
|
|
|
1524
1950
|
let out = getprop(inj.dparent, inj.key)
|
|
1525
1951
|
|
|
1526
1952
|
const t = typify(out)
|
|
1527
|
-
if (
|
|
1953
|
+
if (0 === (T_string & t)) {
|
|
1528
1954
|
let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
|
|
1529
1955
|
inj.errs.push(msg)
|
|
1530
|
-
return
|
|
1956
|
+
return NONE
|
|
1531
1957
|
}
|
|
1532
1958
|
|
|
1533
1959
|
if (S_MT === out) {
|
|
1534
1960
|
let msg = 'Empty string at ' + pathify(inj.path, 1)
|
|
1535
1961
|
inj.errs.push(msg)
|
|
1536
|
-
return
|
|
1962
|
+
return NONE
|
|
1537
1963
|
}
|
|
1538
1964
|
|
|
1539
1965
|
return out
|
|
@@ -1543,13 +1969,17 @@ const validate_STRING: Injector = (inj: Injection) => {
|
|
|
1543
1969
|
|
|
1544
1970
|
|
|
1545
1971
|
const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
|
|
1546
|
-
|
|
1972
|
+
const tname = slice(ref, 1).toLowerCase()
|
|
1973
|
+
const typev = 1 << (31 - TYPENAME.indexOf(tname))
|
|
1547
1974
|
let out = getprop(inj.dparent, inj.key)
|
|
1548
1975
|
|
|
1549
1976
|
const t = typify(out)
|
|
1550
|
-
|
|
1977
|
+
|
|
1978
|
+
// console.log('TYPE', tname, typev, tn(typev), 'O=', t, tn(t), out, 'C=', t & typev)
|
|
1979
|
+
|
|
1980
|
+
if (0 === (t & typev)) {
|
|
1551
1981
|
inj.errs.push(_invalidTypeMsg(inj.path, tname, t, out, 'V1001'))
|
|
1552
|
-
return
|
|
1982
|
+
return NONE
|
|
1553
1983
|
}
|
|
1554
1984
|
|
|
1555
1985
|
return out
|
|
@@ -1573,20 +2003,20 @@ const validate_CHILD: Injector = (inj: Injection) => {
|
|
|
1573
2003
|
// Setup data structures for validation by cloning child template.
|
|
1574
2004
|
|
|
1575
2005
|
// Map syntax.
|
|
1576
|
-
if (
|
|
2006
|
+
if (M_KEYPRE === mode) {
|
|
1577
2007
|
const childtm = getprop(parent, key)
|
|
1578
2008
|
|
|
1579
2009
|
// Get corresponding current object.
|
|
1580
|
-
const pkey =
|
|
2010
|
+
const pkey = getelem(path, -2)
|
|
1581
2011
|
let tval = getprop(inj.dparent, pkey)
|
|
1582
2012
|
|
|
1583
|
-
if (
|
|
2013
|
+
if (NONE == tval) {
|
|
1584
2014
|
tval = {}
|
|
1585
2015
|
}
|
|
1586
2016
|
else if (!ismap(tval)) {
|
|
1587
2017
|
inj.errs.push(_invalidTypeMsg(
|
|
1588
2018
|
slice(inj.path, -1), S_object, typify(tval), tval), 'V0220')
|
|
1589
|
-
return
|
|
2019
|
+
return NONE
|
|
1590
2020
|
}
|
|
1591
2021
|
|
|
1592
2022
|
const ckeys = keysof(tval)
|
|
@@ -1598,52 +2028,56 @@ const validate_CHILD: Injector = (inj: Injection) => {
|
|
|
1598
2028
|
}
|
|
1599
2029
|
|
|
1600
2030
|
// Remove $CHILD to cleanup ouput.
|
|
1601
|
-
inj.setval(
|
|
1602
|
-
return
|
|
2031
|
+
inj.setval(NONE)
|
|
2032
|
+
return NONE
|
|
1603
2033
|
}
|
|
1604
2034
|
|
|
1605
2035
|
// List syntax.
|
|
1606
|
-
if (
|
|
2036
|
+
if (M_VAL === mode) {
|
|
1607
2037
|
|
|
1608
2038
|
if (!islist(parent)) {
|
|
1609
2039
|
// $CHILD was not inside a list.
|
|
1610
2040
|
inj.errs.push('Invalid $CHILD as value')
|
|
1611
|
-
return
|
|
2041
|
+
return NONE
|
|
1612
2042
|
}
|
|
1613
2043
|
|
|
1614
2044
|
const childtm = getprop(parent, 1)
|
|
1615
2045
|
|
|
1616
|
-
if (
|
|
2046
|
+
if (NONE === inj.dparent) {
|
|
1617
2047
|
// Empty list as default.
|
|
1618
|
-
parent.length = 0
|
|
1619
|
-
|
|
2048
|
+
// parent.length = 0
|
|
2049
|
+
slice(parent, 0, 0, true)
|
|
2050
|
+
return NONE
|
|
1620
2051
|
}
|
|
1621
2052
|
|
|
1622
2053
|
if (!islist(inj.dparent)) {
|
|
1623
2054
|
const msg = _invalidTypeMsg(
|
|
1624
|
-
slice(inj.path, -1),
|
|
2055
|
+
slice(inj.path, -1), S_list, typify(inj.dparent), inj.dparent, 'V0230')
|
|
1625
2056
|
inj.errs.push(msg)
|
|
1626
|
-
inj.keyI = parent
|
|
2057
|
+
inj.keyI = size(parent)
|
|
1627
2058
|
return inj.dparent
|
|
1628
2059
|
}
|
|
1629
2060
|
|
|
1630
2061
|
// Clone children abd reset inj key index.
|
|
1631
2062
|
// The inject child loop will now iterate over the cloned children,
|
|
1632
2063
|
// validating them againt the current list values.
|
|
1633
|
-
|
|
1634
|
-
inj.dparent.
|
|
1635
|
-
parent.length = inj.dparent.length
|
|
2064
|
+
items(inj.dparent, (n) => setprop(parent, n[0], clone(childtm)))
|
|
2065
|
+
slice(parent, 0, inj.dparent.length, true)
|
|
1636
2066
|
inj.keyI = 0
|
|
2067
|
+
|
|
1637
2068
|
const out = getprop(inj.dparent, 0)
|
|
1638
2069
|
return out
|
|
1639
2070
|
}
|
|
1640
2071
|
|
|
1641
|
-
return
|
|
2072
|
+
return NONE
|
|
1642
2073
|
}
|
|
1643
2074
|
|
|
1644
2075
|
|
|
2076
|
+
// TODO: implement SOME, ALL
|
|
2077
|
+
// FIX: ONE should mean exactly one, not at least one (=SOME)
|
|
2078
|
+
// TODO: implement a generate validate_ALT to do all of these
|
|
1645
2079
|
// Match at least one of the specified shapes.
|
|
1646
|
-
// Syntax: ['`$ONE`', alt0, alt1, ...]
|
|
2080
|
+
// Syntax: ['`$ONE`', alt0, alt1, ...]
|
|
1647
2081
|
const validate_ONE: Injector = (
|
|
1648
2082
|
inj: Injection,
|
|
1649
2083
|
_val: any,
|
|
@@ -1653,7 +2087,7 @@ const validate_ONE: Injector = (
|
|
|
1653
2087
|
const { mode, parent, keyI } = inj
|
|
1654
2088
|
|
|
1655
2089
|
// Only operate in val mode, since parent is a list.
|
|
1656
|
-
if (
|
|
2090
|
+
if (M_VAL === mode) {
|
|
1657
2091
|
if (!islist(parent) || 0 !== keyI) {
|
|
1658
2092
|
inj.errs.push('The $ONE validator at field ' +
|
|
1659
2093
|
pathify(inj.path, 1, 1) +
|
|
@@ -1661,7 +2095,7 @@ const validate_ONE: Injector = (
|
|
|
1661
2095
|
return
|
|
1662
2096
|
}
|
|
1663
2097
|
|
|
1664
|
-
inj.keyI = inj.keys
|
|
2098
|
+
inj.keyI = size(inj.keys)
|
|
1665
2099
|
|
|
1666
2100
|
// Clean up structure, replacing [$ONE, ...] with current
|
|
1667
2101
|
inj.setval(inj.dparent, 2)
|
|
@@ -1670,7 +2104,7 @@ const validate_ONE: Injector = (
|
|
|
1670
2104
|
inj.key = getelem(inj.path, -1)
|
|
1671
2105
|
|
|
1672
2106
|
let tvals = slice(parent, 1)
|
|
1673
|
-
if (0 === tvals
|
|
2107
|
+
if (0 === size(tvals)) {
|
|
1674
2108
|
inj.errs.push('The $ONE validator at field ' +
|
|
1675
2109
|
pathify(inj.path, 1, 1) +
|
|
1676
2110
|
' must have at least one argument.')
|
|
@@ -1683,7 +2117,7 @@ const validate_ONE: Injector = (
|
|
|
1683
2117
|
// If match, then errs.length = 0
|
|
1684
2118
|
let terrs: any[] = []
|
|
1685
2119
|
|
|
1686
|
-
const vstore = {
|
|
2120
|
+
const vstore = merge([{}, store], 1)
|
|
1687
2121
|
vstore.$TOP = inj.dparent
|
|
1688
2122
|
|
|
1689
2123
|
const vcurrent = validate(inj.dparent, tval, {
|
|
@@ -1695,21 +2129,19 @@ const validate_ONE: Injector = (
|
|
|
1695
2129
|
inj.setval(vcurrent, -2)
|
|
1696
2130
|
|
|
1697
2131
|
// Accept current value if there was a match
|
|
1698
|
-
if (0 === terrs
|
|
2132
|
+
if (0 === size(terrs)) {
|
|
1699
2133
|
return
|
|
1700
2134
|
}
|
|
1701
2135
|
}
|
|
1702
2136
|
|
|
1703
2137
|
// There was no match.
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
.join(', ')
|
|
1708
|
-
.replace(R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
|
|
2138
|
+
const valdesc =
|
|
2139
|
+
replace(join(items(tvals, (n) => stringify(n[1])), ', '),
|
|
2140
|
+
R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
|
|
1709
2141
|
|
|
1710
2142
|
inj.errs.push(_invalidTypeMsg(
|
|
1711
2143
|
inj.path,
|
|
1712
|
-
(1 < tvals
|
|
2144
|
+
(1 < size(tvals) ? 'one of ' : '') + valdesc,
|
|
1713
2145
|
typify(inj.dparent), inj.dparent, 'V0210'))
|
|
1714
2146
|
}
|
|
1715
2147
|
}
|
|
@@ -1719,7 +2151,7 @@ const validate_EXACT: Injector = (inj: Injection) => {
|
|
|
1719
2151
|
const { mode, parent, key, keyI } = inj
|
|
1720
2152
|
|
|
1721
2153
|
// Only operate in val mode, since parent is a list.
|
|
1722
|
-
if (
|
|
2154
|
+
if (M_VAL === mode) {
|
|
1723
2155
|
if (!islist(parent) || 0 !== keyI) {
|
|
1724
2156
|
inj.errs.push('The $EXACT validator at field ' +
|
|
1725
2157
|
pathify(inj.path, 1, 1) +
|
|
@@ -1727,16 +2159,17 @@ const validate_EXACT: Injector = (inj: Injection) => {
|
|
|
1727
2159
|
return
|
|
1728
2160
|
}
|
|
1729
2161
|
|
|
1730
|
-
inj.keyI = inj.keys
|
|
2162
|
+
inj.keyI = size(inj.keys)
|
|
1731
2163
|
|
|
1732
2164
|
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1733
2165
|
inj.setval(inj.dparent, 2)
|
|
1734
2166
|
|
|
1735
|
-
inj.path = slice(inj.path, 0, inj.path
|
|
2167
|
+
// inj.path = slice(inj.path, 0, size(inj.path) - 1)
|
|
2168
|
+
inj.path = slice(inj.path, 0, -1)
|
|
1736
2169
|
inj.key = getelem(inj.path, -1)
|
|
1737
2170
|
|
|
1738
2171
|
let tvals = slice(parent, 1)
|
|
1739
|
-
if (0 === tvals
|
|
2172
|
+
if (0 === size(tvals)) {
|
|
1740
2173
|
inj.errs.push('The $EXACT validator at field ' +
|
|
1741
2174
|
pathify(inj.path, 1, 1) +
|
|
1742
2175
|
' must have at least one argument.')
|
|
@@ -1759,15 +2192,15 @@ const validate_EXACT: Injector = (inj: Injection) => {
|
|
|
1759
2192
|
}
|
|
1760
2193
|
}
|
|
1761
2194
|
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
2195
|
+
// There was no match.
|
|
2196
|
+
const valdesc =
|
|
2197
|
+
replace(join(items(tvals, (n) => stringify(n[1])), ', '),
|
|
2198
|
+
R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
|
|
1766
2199
|
|
|
1767
2200
|
inj.errs.push(_invalidTypeMsg(
|
|
1768
2201
|
inj.path,
|
|
1769
|
-
(1 < inj.path
|
|
1770
|
-
'exactly equal to ' + (1 === tvals
|
|
2202
|
+
(1 < size(inj.path) ? '' : 'value ') +
|
|
2203
|
+
'exactly equal to ' + (1 === size(tvals) ? '' : 'one of ') + valdesc,
|
|
1771
2204
|
typify(inj.dparent), inj.dparent, 'V0110'))
|
|
1772
2205
|
}
|
|
1773
2206
|
else {
|
|
@@ -1785,7 +2218,7 @@ const _validation: Modify = (
|
|
|
1785
2218
|
inj?: Injection,
|
|
1786
2219
|
) => {
|
|
1787
2220
|
|
|
1788
|
-
if (
|
|
2221
|
+
if (NONE === inj) {
|
|
1789
2222
|
return
|
|
1790
2223
|
}
|
|
1791
2224
|
|
|
@@ -1799,28 +2232,28 @@ const _validation: Modify = (
|
|
|
1799
2232
|
// Current val to verify.
|
|
1800
2233
|
const cval = getprop(inj.dparent, key)
|
|
1801
2234
|
|
|
1802
|
-
if (
|
|
2235
|
+
if (NONE === inj || (!exact && NONE === cval)) {
|
|
1803
2236
|
return
|
|
1804
2237
|
}
|
|
1805
2238
|
|
|
1806
2239
|
const ptype = typify(pval)
|
|
1807
2240
|
|
|
1808
2241
|
// Delete any special commands remaining.
|
|
1809
|
-
if (
|
|
2242
|
+
if (0 < (T_string & ptype) && pval.includes(S_DS)) {
|
|
1810
2243
|
return
|
|
1811
2244
|
}
|
|
1812
2245
|
|
|
1813
2246
|
const ctype = typify(cval)
|
|
1814
2247
|
|
|
1815
2248
|
// Type mismatch.
|
|
1816
|
-
if (ptype !== ctype &&
|
|
1817
|
-
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0010'))
|
|
2249
|
+
if (ptype !== ctype && NONE !== pval) {
|
|
2250
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0010'))
|
|
1818
2251
|
return
|
|
1819
2252
|
}
|
|
1820
2253
|
|
|
1821
2254
|
if (ismap(cval)) {
|
|
1822
2255
|
if (!ismap(pval)) {
|
|
1823
|
-
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0020'))
|
|
2256
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0020'))
|
|
1824
2257
|
return
|
|
1825
2258
|
}
|
|
1826
2259
|
|
|
@@ -1828,7 +2261,7 @@ const _validation: Modify = (
|
|
|
1828
2261
|
const pkeys = keysof(pval)
|
|
1829
2262
|
|
|
1830
2263
|
// Empty spec object {} means object can be open (any keys).
|
|
1831
|
-
if (0 < pkeys
|
|
2264
|
+
if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
|
|
1832
2265
|
const badkeys = []
|
|
1833
2266
|
for (let ckey of ckeys) {
|
|
1834
2267
|
if (!haskey(pval, ckey)) {
|
|
@@ -1837,9 +2270,9 @@ const _validation: Modify = (
|
|
|
1837
2270
|
}
|
|
1838
2271
|
|
|
1839
2272
|
// Closed object, so reject extra keys not in shape.
|
|
1840
|
-
if (0 < badkeys
|
|
2273
|
+
if (0 < size(badkeys)) {
|
|
1841
2274
|
const msg =
|
|
1842
|
-
'Unexpected keys at field ' + pathify(inj.path, 1) + S_VIZ +
|
|
2275
|
+
'Unexpected keys at field ' + pathify(inj.path, 1) + S_VIZ + join(badkeys, ', ')
|
|
1843
2276
|
inj.errs.push(msg)
|
|
1844
2277
|
}
|
|
1845
2278
|
}
|
|
@@ -1853,7 +2286,7 @@ const _validation: Modify = (
|
|
|
1853
2286
|
}
|
|
1854
2287
|
else if (islist(cval)) {
|
|
1855
2288
|
if (!islist(pval)) {
|
|
1856
|
-
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0030'))
|
|
2289
|
+
inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0030'))
|
|
1857
2290
|
}
|
|
1858
2291
|
}
|
|
1859
2292
|
else if (exact) {
|
|
@@ -1893,51 +2326,57 @@ function validate(
|
|
|
1893
2326
|
const collect = null != injdef?.errs
|
|
1894
2327
|
const errs = injdef?.errs || []
|
|
1895
2328
|
|
|
1896
|
-
const store =
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
2329
|
+
const store = merge([
|
|
2330
|
+
{
|
|
2331
|
+
// Remove the transform commands.
|
|
2332
|
+
$DELETE: null,
|
|
2333
|
+
$COPY: null,
|
|
2334
|
+
$KEY: null,
|
|
2335
|
+
$META: null,
|
|
2336
|
+
$MERGE: null,
|
|
2337
|
+
$EACH: null,
|
|
2338
|
+
$PACK: null,
|
|
2339
|
+
|
|
2340
|
+
$STRING: validate_STRING,
|
|
2341
|
+
$NUMBER: validate_TYPE,
|
|
2342
|
+
$INTEGER: validate_TYPE,
|
|
2343
|
+
$DECIMAL: validate_TYPE,
|
|
2344
|
+
$BOOLEAN: validate_TYPE,
|
|
2345
|
+
$NULL: validate_TYPE,
|
|
2346
|
+
$NIL: validate_TYPE,
|
|
2347
|
+
$MAP: validate_TYPE,
|
|
2348
|
+
$LIST: validate_TYPE,
|
|
2349
|
+
$FUNCTION: validate_TYPE,
|
|
2350
|
+
$INSTANCE: validate_TYPE,
|
|
2351
|
+
$ANY: validate_ANY,
|
|
2352
|
+
$CHILD: validate_CHILD,
|
|
2353
|
+
$ONE: validate_ONE,
|
|
2354
|
+
$EXACT: validate_EXACT,
|
|
2355
|
+
},
|
|
2356
|
+
|
|
2357
|
+
getdef(extra, {}),
|
|
1919
2358
|
|
|
1920
2359
|
// A special top level value to collect errors.
|
|
1921
|
-
// NOTE: collecterrs
|
|
1922
|
-
|
|
1923
|
-
|
|
2360
|
+
// NOTE: collecterrs parameter always wins.
|
|
2361
|
+
{
|
|
2362
|
+
$ERRS: errs,
|
|
2363
|
+
}
|
|
2364
|
+
], 1)
|
|
1924
2365
|
|
|
1925
|
-
let meta =
|
|
1926
|
-
|
|
1927
|
-
if (injdef?.meta) {
|
|
1928
|
-
meta = merge([meta, injdef.meta])
|
|
1929
|
-
}
|
|
2366
|
+
let meta = getprop(injdef, 'meta', {})
|
|
2367
|
+
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false))
|
|
1930
2368
|
|
|
1931
2369
|
const out = transform(data, spec, {
|
|
1932
2370
|
meta,
|
|
1933
2371
|
extra: store,
|
|
1934
2372
|
modify: _validation,
|
|
1935
|
-
handler: _validatehandler
|
|
2373
|
+
handler: _validatehandler,
|
|
2374
|
+
errs,
|
|
1936
2375
|
})
|
|
1937
2376
|
|
|
1938
|
-
const generr = (0 < errs
|
|
2377
|
+
const generr = (0 < size(errs) && !collect)
|
|
1939
2378
|
if (generr) {
|
|
1940
|
-
throw new Error(
|
|
2379
|
+
throw new Error(join(errs, ' | '))
|
|
1941
2380
|
}
|
|
1942
2381
|
|
|
1943
2382
|
return out
|
|
@@ -1945,18 +2384,16 @@ function validate(
|
|
|
1945
2384
|
|
|
1946
2385
|
|
|
1947
2386
|
const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
1948
|
-
if (
|
|
2387
|
+
if (M_KEYPRE === inj.mode) {
|
|
1949
2388
|
const terms = getprop(inj.parent, inj.key)
|
|
1950
2389
|
|
|
1951
2390
|
const ppath = slice(inj.path, -1)
|
|
1952
2391
|
const point = getpath(store, ppath)
|
|
1953
2392
|
|
|
1954
|
-
const vstore = {
|
|
2393
|
+
const vstore = merge([{}, store], 1)
|
|
1955
2394
|
vstore.$TOP = point
|
|
1956
2395
|
|
|
1957
2396
|
for (let term of terms) {
|
|
1958
|
-
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1959
|
-
|
|
1960
2397
|
let terrs: any[] = []
|
|
1961
2398
|
|
|
1962
2399
|
validate(point, term, {
|
|
@@ -1965,7 +2402,7 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
|
|
|
1965
2402
|
meta: inj.meta,
|
|
1966
2403
|
})
|
|
1967
2404
|
|
|
1968
|
-
if (0 != terrs
|
|
2405
|
+
if (0 != size(terrs)) {
|
|
1969
2406
|
inj.errs.push(
|
|
1970
2407
|
'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
|
|
1971
2408
|
}
|
|
@@ -1979,13 +2416,13 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
|
|
|
1979
2416
|
|
|
1980
2417
|
|
|
1981
2418
|
const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
1982
|
-
if (
|
|
2419
|
+
if (M_KEYPRE === inj.mode) {
|
|
1983
2420
|
const terms = getprop(inj.parent, inj.key)
|
|
1984
2421
|
|
|
1985
2422
|
const ppath = slice(inj.path, -1)
|
|
1986
2423
|
const point = getpath(store, ppath)
|
|
1987
2424
|
|
|
1988
|
-
const vstore = {
|
|
2425
|
+
const vstore = merge([{}, store], 1)
|
|
1989
2426
|
vstore.$TOP = point
|
|
1990
2427
|
|
|
1991
2428
|
for (let term of terms) {
|
|
@@ -1997,7 +2434,7 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
|
|
|
1997
2434
|
meta: inj.meta,
|
|
1998
2435
|
})
|
|
1999
2436
|
|
|
2000
|
-
if (0 === terrs
|
|
2437
|
+
if (0 === size(terrs)) {
|
|
2001
2438
|
const gkey = getelem(inj.path, -2)
|
|
2002
2439
|
const gp = getelem(inj.nodes, -2)
|
|
2003
2440
|
setprop(gp, gkey, point)
|
|
@@ -2013,13 +2450,13 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
|
|
|
2013
2450
|
|
|
2014
2451
|
|
|
2015
2452
|
const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
2016
|
-
if (
|
|
2453
|
+
if (M_KEYPRE === inj.mode) {
|
|
2017
2454
|
const term = getprop(inj.parent, inj.key)
|
|
2018
2455
|
|
|
2019
2456
|
const ppath = slice(inj.path, -1)
|
|
2020
2457
|
const point = getpath(store, ppath)
|
|
2021
2458
|
|
|
2022
|
-
const vstore = {
|
|
2459
|
+
const vstore = merge([{}, store], 1)
|
|
2023
2460
|
vstore.$TOP = point
|
|
2024
2461
|
|
|
2025
2462
|
let terrs: any[] = []
|
|
@@ -2030,7 +2467,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
|
|
|
2030
2467
|
meta: inj.meta,
|
|
2031
2468
|
})
|
|
2032
2469
|
|
|
2033
|
-
if (0 == terrs
|
|
2470
|
+
if (0 == size(terrs)) {
|
|
2034
2471
|
inj.errs.push(
|
|
2035
2472
|
'NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
|
|
2036
2473
|
}
|
|
@@ -2043,7 +2480,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
|
|
|
2043
2480
|
|
|
2044
2481
|
|
|
2045
2482
|
const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
|
|
2046
|
-
if (
|
|
2483
|
+
if (M_KEYPRE === inj.mode) {
|
|
2047
2484
|
const term = getprop(inj.parent, inj.key)
|
|
2048
2485
|
// const src = getprop(store, inj.base, store)
|
|
2049
2486
|
const gkey = getelem(inj.path, -2)
|
|
@@ -2082,7 +2519,7 @@ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any
|
|
|
2082
2519
|
}
|
|
2083
2520
|
}
|
|
2084
2521
|
|
|
2085
|
-
return
|
|
2522
|
+
return NONE
|
|
2086
2523
|
}
|
|
2087
2524
|
|
|
2088
2525
|
|
|
@@ -2096,10 +2533,13 @@ function select(children: any, query: any): any[] {
|
|
|
2096
2533
|
}
|
|
2097
2534
|
|
|
2098
2535
|
if (ismap(children)) {
|
|
2099
|
-
children = items(children
|
|
2536
|
+
children = items(children, n => {
|
|
2537
|
+
setprop(n[1], S_DKEY, n[0])
|
|
2538
|
+
return n[1]
|
|
2539
|
+
})
|
|
2100
2540
|
}
|
|
2101
2541
|
else {
|
|
2102
|
-
children = (children
|
|
2542
|
+
children = items(children, (n) => (setprop(n[1], S_DKEY, +n[0]), n[1]))
|
|
2103
2543
|
}
|
|
2104
2544
|
|
|
2105
2545
|
const results: any[] = []
|
|
@@ -2141,10 +2581,9 @@ function select(children: any, query: any): any[] {
|
|
|
2141
2581
|
}
|
|
2142
2582
|
|
|
2143
2583
|
|
|
2144
|
-
|
|
2145
2584
|
// Injection state used for recursive injection into JSON - like data structures.
|
|
2146
2585
|
class Injection {
|
|
2147
|
-
mode: InjectMode // Injection mode:
|
|
2586
|
+
mode: InjectMode // Injection mode: M_KEYPRE, M_VAL, M_KEYPOST.
|
|
2148
2587
|
full: boolean // Transform escape was full key name.
|
|
2149
2588
|
keyI: number // Index of parent key in list of parent keys.
|
|
2150
2589
|
keys: string[] // List of parent keys.
|
|
@@ -2155,7 +2594,7 @@ class Injection {
|
|
|
2155
2594
|
nodes: any[] // Stack of ancestor nodes.
|
|
2156
2595
|
handler: Injector // Custom handler for injections.
|
|
2157
2596
|
errs: any[] // Error collector.
|
|
2158
|
-
meta: Record<string, any> // Custom meta data.
|
|
2597
|
+
meta: Record<string, any> // Custom meta data. NOTE: do not merge, values must remain as-is.
|
|
2159
2598
|
dparent: any // Current data parent node (contains current data value).
|
|
2160
2599
|
dpath: string[] // Current data value path
|
|
2161
2600
|
base?: string // Base key for data in store, if any.
|
|
@@ -2168,10 +2607,10 @@ class Injection {
|
|
|
2168
2607
|
this.parent = parent
|
|
2169
2608
|
this.errs = []
|
|
2170
2609
|
|
|
2171
|
-
this.dparent =
|
|
2610
|
+
this.dparent = NONE
|
|
2172
2611
|
this.dpath = [S_DTOP]
|
|
2173
2612
|
|
|
2174
|
-
this.mode =
|
|
2613
|
+
this.mode = M_VAL
|
|
2175
2614
|
this.full = false
|
|
2176
2615
|
this.keyI = 0
|
|
2177
2616
|
this.keys = [S_DTOP]
|
|
@@ -2187,7 +2626,7 @@ class Injection {
|
|
|
2187
2626
|
toString(prefix?: string) {
|
|
2188
2627
|
return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
|
|
2189
2628
|
pad(pathify(this.path, 1)) +
|
|
2190
|
-
this.mode + (this.full ? '/full' : '') + S_CN +
|
|
2629
|
+
MODENAME[this.mode] + (this.full ? '/full' : '') + S_CN +
|
|
2191
2630
|
'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
|
|
2192
2631
|
' p=' + stringify(this.parent, -1, 1) +
|
|
2193
2632
|
' m=' + stringify(this.meta, -1, 1) +
|
|
@@ -2201,12 +2640,12 @@ class Injection {
|
|
|
2201
2640
|
const parentkey = getelem(this.path, -2)
|
|
2202
2641
|
|
|
2203
2642
|
// Resolve current node in store for local paths.
|
|
2204
|
-
if (
|
|
2643
|
+
if (NONE === this.dparent) {
|
|
2205
2644
|
|
|
2206
2645
|
// Even if there's no data, dpath should continue to match path, so that
|
|
2207
2646
|
// relative paths work properly.
|
|
2208
|
-
if (1 < this.dpath
|
|
2209
|
-
this.dpath = [
|
|
2647
|
+
if (1 < size(this.dpath)) {
|
|
2648
|
+
this.dpath = flatten([this.dpath, parentkey])
|
|
2210
2649
|
}
|
|
2211
2650
|
}
|
|
2212
2651
|
else {
|
|
@@ -2219,7 +2658,7 @@ class Injection {
|
|
|
2219
2658
|
this.dpath = slice(this.dpath, -1)
|
|
2220
2659
|
}
|
|
2221
2660
|
else {
|
|
2222
|
-
this.dpath = [
|
|
2661
|
+
this.dpath = flatten([this.dpath, parentkey])
|
|
2223
2662
|
}
|
|
2224
2663
|
}
|
|
2225
2664
|
}
|
|
@@ -2238,8 +2677,8 @@ class Injection {
|
|
|
2238
2677
|
cinj.keys = keys
|
|
2239
2678
|
cinj.key = key
|
|
2240
2679
|
|
|
2241
|
-
cinj.path = [
|
|
2242
|
-
cinj.nodes = [
|
|
2680
|
+
cinj.path = flatten([getdef(this.path, []), key])
|
|
2681
|
+
cinj.nodes = flatten([getdef(this.nodes, []), [val]])
|
|
2243
2682
|
|
|
2244
2683
|
cinj.mode = this.mode
|
|
2245
2684
|
cinj.handler = this.handler
|
|
@@ -2249,7 +2688,7 @@ class Injection {
|
|
|
2249
2688
|
cinj.errs = this.errs
|
|
2250
2689
|
cinj.prior = this
|
|
2251
2690
|
|
|
2252
|
-
cinj.dpath = [
|
|
2691
|
+
cinj.dpath = flatten([this.dpath])
|
|
2253
2692
|
cinj.dparent = this.dparent
|
|
2254
2693
|
|
|
2255
2694
|
return cinj
|
|
@@ -2257,18 +2696,22 @@ class Injection {
|
|
|
2257
2696
|
|
|
2258
2697
|
|
|
2259
2698
|
setval(val: any, ancestor?: number) {
|
|
2699
|
+
let parent = NONE
|
|
2260
2700
|
if (null == ancestor || ancestor < 2) {
|
|
2261
|
-
|
|
2262
|
-
delprop(this.parent, this.key) :
|
|
2701
|
+
parent = NONE === val ?
|
|
2702
|
+
this.parent = delprop(this.parent, this.key) :
|
|
2263
2703
|
setprop(this.parent, this.key, val)
|
|
2264
2704
|
}
|
|
2265
2705
|
else {
|
|
2266
2706
|
const aval = getelem(this.nodes, 0 - ancestor)
|
|
2267
2707
|
const akey = getelem(this.path, 0 - ancestor)
|
|
2268
|
-
|
|
2708
|
+
parent = NONE === val ?
|
|
2269
2709
|
delprop(aval, akey) :
|
|
2270
2710
|
setprop(aval, akey, val)
|
|
2271
2711
|
}
|
|
2712
|
+
|
|
2713
|
+
// console.log('SETVAL', val, this.key, this.parent)
|
|
2714
|
+
return parent
|
|
2272
2715
|
}
|
|
2273
2716
|
}
|
|
2274
2717
|
|
|
@@ -2277,21 +2720,21 @@ class Injection {
|
|
|
2277
2720
|
// ==================
|
|
2278
2721
|
|
|
2279
2722
|
|
|
2280
|
-
// Update all references to target in inj.nodes.
|
|
2281
|
-
function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
}
|
|
2723
|
+
// // Update all references to target in inj.nodes.
|
|
2724
|
+
// function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
|
|
2725
|
+
// // SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
|
|
2726
|
+
// setprop(target, tkey, tval)
|
|
2727
|
+
// }
|
|
2285
2728
|
|
|
2286
2729
|
|
|
2287
2730
|
// Build a type validation error message.
|
|
2288
|
-
function _invalidTypeMsg(path: any, needtype: string, vt:
|
|
2731
|
+
function _invalidTypeMsg(path: any, needtype: string, vt: number, v: any, _whence?: string) {
|
|
2289
2732
|
let vs = null == v ? 'no value' : stringify(v)
|
|
2290
2733
|
|
|
2291
2734
|
return 'Expected ' +
|
|
2292
|
-
(1 < path
|
|
2735
|
+
(1 < size(path) ? ('field ' + pathify(path, 1) + ' to be ') : '') +
|
|
2293
2736
|
needtype + ', but found ' +
|
|
2294
|
-
(null != v ? vt + S_VIZ : '') + vs +
|
|
2737
|
+
(null != v ? typename(vt) + S_VIZ : '') + vs +
|
|
2295
2738
|
|
|
2296
2739
|
// Uncomment to help debug validation errors.
|
|
2297
2740
|
// ' [' + _whence + ']' +
|
|
@@ -2309,7 +2752,7 @@ const _injecthandler: Injector = (
|
|
|
2309
2752
|
store: any
|
|
2310
2753
|
): any => {
|
|
2311
2754
|
let out = val
|
|
2312
|
-
const iscmd = isfunc(val) && (
|
|
2755
|
+
const iscmd = isfunc(val) && (NONE === ref || ref.startsWith(S_DS))
|
|
2313
2756
|
|
|
2314
2757
|
// Only call val function if it is a special command ($NAME format).
|
|
2315
2758
|
// TODO: OR if meta.'$CALL'
|
|
@@ -2319,7 +2762,7 @@ const _injecthandler: Injector = (
|
|
|
2319
2762
|
}
|
|
2320
2763
|
|
|
2321
2764
|
// Update parent with value. Ensures references remain in node tree.
|
|
2322
|
-
else if (
|
|
2765
|
+
else if (M_VAL === inj.mode && inj.full) {
|
|
2323
2766
|
inj.setval(val)
|
|
2324
2767
|
}
|
|
2325
2768
|
|
|
@@ -2389,9 +2832,9 @@ function _injectstr(
|
|
|
2389
2832
|
let pathref = m[1]
|
|
2390
2833
|
|
|
2391
2834
|
// Special escapes inside injection.
|
|
2392
|
-
|
|
2393
|
-
pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
|
|
2394
|
-
|
|
2835
|
+
if (3 < size(pathref)) {
|
|
2836
|
+
pathref = pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
|
|
2837
|
+
}
|
|
2395
2838
|
|
|
2396
2839
|
// Get the extracted path reference.
|
|
2397
2840
|
out = getpath(store, pathref, inj)
|
|
@@ -2401,14 +2844,19 @@ function _injectstr(
|
|
|
2401
2844
|
// Check for injections within the string.
|
|
2402
2845
|
const partial = (_m: string, ref: string) => {
|
|
2403
2846
|
// Special escapes inside injection.
|
|
2404
|
-
|
|
2847
|
+
|
|
2848
|
+
if (3 < size(ref)) {
|
|
2849
|
+
ref = ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2405
2852
|
if (inj) {
|
|
2406
2853
|
inj.full = false
|
|
2407
2854
|
}
|
|
2855
|
+
|
|
2408
2856
|
const found = getpath(store, ref, inj)
|
|
2409
2857
|
|
|
2410
2858
|
// Ensure inject value is a string.
|
|
2411
|
-
return
|
|
2859
|
+
return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
|
|
2412
2860
|
}
|
|
2413
2861
|
|
|
2414
2862
|
out = val.replace(R_INJECTION_PARTIAL, partial)
|
|
@@ -2425,11 +2873,101 @@ function _injectstr(
|
|
|
2425
2873
|
}
|
|
2426
2874
|
|
|
2427
2875
|
|
|
2876
|
+
// Handler Utilities
|
|
2877
|
+
// =================
|
|
2878
|
+
|
|
2879
|
+
|
|
2880
|
+
const MODENAME: any = {
|
|
2881
|
+
[M_VAL]: 'val',
|
|
2882
|
+
[M_KEYPRE]: 'key:pre',
|
|
2883
|
+
[M_KEYPOST]: 'key:post',
|
|
2884
|
+
}
|
|
2885
|
+
|
|
2886
|
+
const PLACEMENT: any = {
|
|
2887
|
+
[M_VAL]: 'value',
|
|
2888
|
+
[M_KEYPRE]: S_key,
|
|
2889
|
+
[M_KEYPOST]: S_key,
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
function checkPlacement(
|
|
2893
|
+
modes: InjectMode,
|
|
2894
|
+
ijname: string,
|
|
2895
|
+
parentTypes: number,
|
|
2896
|
+
inj: Injection
|
|
2897
|
+
): boolean {
|
|
2898
|
+
if (0 === (modes & inj.mode)) {
|
|
2899
|
+
inj.errs.push('$' + ijname + ': invalid placement as ' + PLACEMENT[inj.mode] +
|
|
2900
|
+
', expected: ' + join(items(
|
|
2901
|
+
[M_KEYPRE, M_KEYPOST, M_VAL].filter(m => modes & m),
|
|
2902
|
+
(n: any) => PLACEMENT[n[1]]), ',') + '.')
|
|
2903
|
+
return false
|
|
2904
|
+
}
|
|
2905
|
+
if (!isempty(parentTypes)) {
|
|
2906
|
+
const ptype = typify(inj.parent)
|
|
2907
|
+
if (0 === (parentTypes & ptype)) {
|
|
2908
|
+
inj.errs.push('$' + ijname + ': invalid placement in parent ' + typename(ptype) +
|
|
2909
|
+
', expected: ' + typename(parentTypes) + '.')
|
|
2910
|
+
return false
|
|
2911
|
+
|
|
2912
|
+
}
|
|
2913
|
+
}
|
|
2914
|
+
return true
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
|
|
2918
|
+
// function injectorArgs(argTypes: number[], inj: Injection): any {
|
|
2919
|
+
function injectorArgs(argTypes: number[], args: any[]): any {
|
|
2920
|
+
const numargs = size(argTypes)
|
|
2921
|
+
const found = new Array(1 + numargs)
|
|
2922
|
+
found[0] = NONE
|
|
2923
|
+
for (let argI = 0; argI < numargs; argI++) {
|
|
2924
|
+
// const arg = inj.parent[1 + argI]
|
|
2925
|
+
const arg = args[argI]
|
|
2926
|
+
const argType = typify(arg)
|
|
2927
|
+
if (0 === (argTypes[argI] & argType)) {
|
|
2928
|
+
found[0] = 'invalid argument: ' + stringify(arg, 22) +
|
|
2929
|
+
' (' + typename(argType) + ' at position ' + (1 + argI) +
|
|
2930
|
+
') is not of type: ' + typename(argTypes[argI]) + '.'
|
|
2931
|
+
break
|
|
2932
|
+
}
|
|
2933
|
+
found[1 + argI] = arg
|
|
2934
|
+
}
|
|
2935
|
+
return found
|
|
2936
|
+
}
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
function injectChild(child: any, store: any, inj: Injection): Injection {
|
|
2940
|
+
let cinj = inj
|
|
2941
|
+
|
|
2942
|
+
// Replace ['`$FORMAT`',...] with child
|
|
2943
|
+
if (null != inj.prior) {
|
|
2944
|
+
if (null != inj.prior.prior) {
|
|
2945
|
+
cinj = inj.prior.prior.child(inj.prior.keyI, inj.prior.keys)
|
|
2946
|
+
cinj.val = child
|
|
2947
|
+
setprop(cinj.parent, inj.prior.key, child)
|
|
2948
|
+
}
|
|
2949
|
+
else {
|
|
2950
|
+
cinj = inj.prior.child(inj.keyI, inj.keys)
|
|
2951
|
+
cinj.val = child
|
|
2952
|
+
setprop(cinj.parent, inj.key, child)
|
|
2953
|
+
}
|
|
2954
|
+
}
|
|
2955
|
+
|
|
2956
|
+
// console.log('FORMAT-INJECT-CHILD', child)
|
|
2957
|
+
inject(child, store, cinj)
|
|
2958
|
+
|
|
2959
|
+
return cinj
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
|
|
2428
2963
|
class StructUtility {
|
|
2429
2964
|
clone = clone
|
|
2430
2965
|
delprop = delprop
|
|
2431
2966
|
escre = escre
|
|
2432
2967
|
escurl = escurl
|
|
2968
|
+
filter = filter
|
|
2969
|
+
flatten = flatten
|
|
2970
|
+
getdef = getdef
|
|
2433
2971
|
getelem = getelem
|
|
2434
2972
|
getpath = getpath
|
|
2435
2973
|
getprop = getprop
|
|
@@ -2442,13 +2980,14 @@ class StructUtility {
|
|
|
2442
2980
|
ismap = ismap
|
|
2443
2981
|
isnode = isnode
|
|
2444
2982
|
items = items
|
|
2445
|
-
|
|
2983
|
+
join = join
|
|
2446
2984
|
jsonify = jsonify
|
|
2447
2985
|
keysof = keysof
|
|
2448
2986
|
merge = merge
|
|
2449
2987
|
pad = pad
|
|
2450
2988
|
pathify = pathify
|
|
2451
2989
|
select = select
|
|
2990
|
+
setpath = setpath
|
|
2452
2991
|
setprop = setprop
|
|
2453
2992
|
size = size
|
|
2454
2993
|
slice = slice
|
|
@@ -2456,11 +2995,36 @@ class StructUtility {
|
|
|
2456
2995
|
stringify = stringify
|
|
2457
2996
|
transform = transform
|
|
2458
2997
|
typify = typify
|
|
2998
|
+
typename = typename
|
|
2459
2999
|
validate = validate
|
|
2460
3000
|
walk = walk
|
|
2461
3001
|
|
|
2462
|
-
|
|
2463
|
-
|
|
3002
|
+
SKIP = SKIP
|
|
3003
|
+
DELETE = DELETE
|
|
3004
|
+
|
|
3005
|
+
jm = jm
|
|
3006
|
+
jt = jt
|
|
3007
|
+
tn = typename
|
|
3008
|
+
|
|
3009
|
+
T_any = T_any
|
|
3010
|
+
T_noval = T_noval
|
|
3011
|
+
T_boolean = T_boolean
|
|
3012
|
+
T_decimal = T_decimal
|
|
3013
|
+
T_integer = T_integer
|
|
3014
|
+
T_number = T_number
|
|
3015
|
+
T_string = T_string
|
|
3016
|
+
T_function = T_function
|
|
3017
|
+
T_symbol = T_symbol
|
|
3018
|
+
T_null = T_null
|
|
3019
|
+
T_list = T_list
|
|
3020
|
+
T_map = T_map
|
|
3021
|
+
T_instance = T_instance
|
|
3022
|
+
T_scalar = T_scalar
|
|
3023
|
+
T_node = T_node
|
|
3024
|
+
|
|
3025
|
+
checkPlacement = checkPlacement
|
|
3026
|
+
injectorArgs = injectorArgs
|
|
3027
|
+
injectChild = injectChild
|
|
2464
3028
|
}
|
|
2465
3029
|
|
|
2466
3030
|
export {
|
|
@@ -2469,6 +3033,9 @@ export {
|
|
|
2469
3033
|
delprop,
|
|
2470
3034
|
escre,
|
|
2471
3035
|
escurl,
|
|
3036
|
+
filter,
|
|
3037
|
+
flatten,
|
|
3038
|
+
getdef,
|
|
2472
3039
|
getelem,
|
|
2473
3040
|
getpath,
|
|
2474
3041
|
getprop,
|
|
@@ -2481,13 +3048,14 @@ export {
|
|
|
2481
3048
|
ismap,
|
|
2482
3049
|
isnode,
|
|
2483
3050
|
items,
|
|
2484
|
-
|
|
3051
|
+
join,
|
|
2485
3052
|
jsonify,
|
|
2486
3053
|
keysof,
|
|
2487
3054
|
merge,
|
|
2488
3055
|
pad,
|
|
2489
3056
|
pathify,
|
|
2490
3057
|
select,
|
|
3058
|
+
setpath,
|
|
2491
3059
|
setprop,
|
|
2492
3060
|
size,
|
|
2493
3061
|
slice,
|
|
@@ -2495,11 +3063,41 @@ export {
|
|
|
2495
3063
|
stringify,
|
|
2496
3064
|
transform,
|
|
2497
3065
|
typify,
|
|
3066
|
+
typename,
|
|
2498
3067
|
validate,
|
|
2499
3068
|
walk,
|
|
2500
3069
|
|
|
2501
|
-
|
|
2502
|
-
|
|
3070
|
+
SKIP,
|
|
3071
|
+
DELETE,
|
|
3072
|
+
|
|
3073
|
+
jm,
|
|
3074
|
+
jt,
|
|
3075
|
+
|
|
3076
|
+
T_any,
|
|
3077
|
+
T_noval,
|
|
3078
|
+
T_boolean,
|
|
3079
|
+
T_decimal,
|
|
3080
|
+
T_integer,
|
|
3081
|
+
T_number,
|
|
3082
|
+
T_string,
|
|
3083
|
+
T_function,
|
|
3084
|
+
T_symbol,
|
|
3085
|
+
T_null,
|
|
3086
|
+
T_list,
|
|
3087
|
+
T_map,
|
|
3088
|
+
T_instance,
|
|
3089
|
+
T_scalar,
|
|
3090
|
+
T_node,
|
|
3091
|
+
|
|
3092
|
+
M_KEYPRE,
|
|
3093
|
+
M_KEYPOST,
|
|
3094
|
+
M_VAL,
|
|
3095
|
+
|
|
3096
|
+
MODENAME,
|
|
3097
|
+
|
|
3098
|
+
checkPlacement,
|
|
3099
|
+
injectorArgs,
|
|
3100
|
+
injectChild,
|
|
2503
3101
|
}
|
|
2504
3102
|
|
|
2505
3103
|
export type {
|