@voxgig/struct 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /* Copyright (c) 2025-2026 Voxgig Ltd. MIT LICENSE. */
2
2
 
3
- // VERSION: @voxgig/struct 0.1.0
3
+ // VERSION: @voxgig/struct 0.2.0
4
4
 
5
5
  /* Voxgig Struct
6
6
  * =============
@@ -53,7 +53,6 @@
53
53
  *
54
54
  */
55
55
 
56
-
57
56
  // String constants are explicitly defined.
58
57
 
59
58
  // Mode value for inject step (bitfield).
@@ -137,12 +136,20 @@ const TYPENAME = [
137
136
  S_function,
138
137
  S_symbol,
139
138
  S_null,
140
- '', '', '',
141
- '', '', '', '',
139
+ '',
140
+ '',
141
+ '',
142
+ '',
143
+ '',
144
+ '',
145
+ '',
142
146
  S_list,
143
147
  S_map,
144
148
  S_instance,
145
- '', '', '', '',
149
+ '',
150
+ '',
151
+ '',
152
+ '',
146
153
  S_scalar,
147
154
  S_node,
148
155
  ]
@@ -154,23 +161,19 @@ const NONE = undefined
154
161
  const SKIP = { '`$SKIP`': true }
155
162
  const DELETE = { '`$DELETE`': true }
156
163
 
157
-
158
164
  // Regular expression constants
159
- const R_INTEGER_KEY = /^[-0-9]+$/ // Match integer keys (including <0).
160
- const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g // Chars that need escaping in regexp.
161
- const R_TRAILING_SLASH = /\/+$/ // Trailing slashes in URLs.
162
- const R_LEADING_TRAILING_SLASH = /([^\/])\/+/ // Multiple slashes in URL middle.
163
- const R_LEADING_SLASH = /^\/+/ // Leading slashes in URLs.
164
- const R_QUOTES = /"/g // Double quotes for removal.
165
- const R_DOT = /\./g // Dots in path strings.
166
- const R_CLONE_REF = /^`\$REF:([0-9]+)`$/ // Copy reference in cloning.
167
- const R_META_PATH = /^([^$]+)\$([=~])(.+)$/ // Meta path syntax.
168
- const R_DOUBLE_DOLLAR = /\$\$/g // Double dollar escape sequence.
169
- const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g // Transform command names.
170
- const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/ // Full string injection pattern.
171
- const R_BT_ESCAPE = /\$BT/g // Backtick escape sequence.
172
- const R_DS_ESCAPE = /\$DS/g // Dollar sign escape sequence.
173
- const R_INJECTION_PARTIAL = /`([^`]+)`/g // Partial string injection pattern.
165
+ const R_INTEGER_KEY = /^[-0-9]+$/ // Match integer keys (including <0).
166
+ const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g // Chars that need escaping in regexp.
167
+ const R_QUOTES = /"/g // Double quotes for removal.
168
+ const R_DOT = /\./g // Dots in path strings.
169
+ const R_CLONE_REF = /^`\$REF:([0-9]+)`$/ // Copy reference in cloning.
170
+ const R_META_PATH = /^([^$]+)\$([=~])(.+)$/ // Meta path syntax.
171
+ const R_DOUBLE_DOLLAR = /\$\$/g // Double dollar escape sequence.
172
+ const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g // Transform command names.
173
+ const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/ // Full string injection pattern.
174
+ const R_BT_ESCAPE = /\$BT/g // Backtick escape sequence.
175
+ const R_DS_ESCAPE = /\$DS/g // Dollar sign escape sequence.
176
+ const R_INJECTION_PARTIAL = /`([^`]+)`/g // Partial string injection pattern.
174
177
 
175
178
  // Default max depth (for walk etc).
176
179
  const MAXDEPTH = 32
@@ -181,7 +184,6 @@ type PropKey = string | number
181
184
  // Type that can be indexed by both string and number keys.
182
185
  type Indexable = { [key: string]: any } & { [key: number]: any }
183
186
 
184
-
185
187
  // For each key in a node (map or list), perform value injections in
186
188
  // three phases: on key value, before child, and then on key value again.
187
189
  // This mode is passed via the Injection structure.
@@ -191,19 +193,19 @@ type InjectMode = number
191
193
  // - `a.b.c`: insert value at {a:{b:{c:1}}}
192
194
  // - `$FOO`: apply transform FOO
193
195
  type Injector = (
194
- inj: Injection, // Injection state.
195
- val: any, // Injection value specification.
196
- ref: string, // Original injection reference string.
197
- store: any, // Current source root value.
196
+ inj: Injection, // Injection state.
197
+ val: any, // Injection value specification.
198
+ ref: string, // Original injection reference string.
199
+ store: any, // Current source root value.
198
200
  ) => any
199
201
 
200
202
  // Apply a custom modification to injections.
201
203
  type Modify = (
202
- val: any, // Value.
203
- key?: PropKey, // Value key, if any,
204
- parent?: any, // Parent node, if any.
205
- inj?: Injection, // Injection state, if any.
206
- store?: any, // Store, if any
204
+ val: any, // Value.
205
+ key?: PropKey, // Value key, if any,
206
+ parent?: any, // Parent node, if any.
207
+ inj?: Injection, // Injection state, if any.
208
+ store?: any, // Store, if any
207
209
  ) => void
208
210
 
209
211
  // Function applied to each node and leaf when walking a node structure depth first.
@@ -217,16 +219,14 @@ type WalkApply = (
217
219
  key: string | number | undefined,
218
220
  val: any,
219
221
  parent: any,
220
- path: string[]
222
+ path: string[],
221
223
  ) => any
222
224
 
223
-
224
225
  // Return type string for narrowest type.
225
226
  function typename(t: number) {
226
227
  return getelem(TYPENAME, Math.clz32(t), TYPENAME[0])
227
228
  }
228
229
 
229
-
230
230
  // Get a defined value. Returns alt if val is undefined.
231
231
  function getdef(val: any, alt: any) {
232
232
  if (NONE === val) {
@@ -235,7 +235,6 @@ function getdef(val: any, alt: any) {
235
235
  return val
236
236
  }
237
237
 
238
-
239
238
  // Value is a node - defined, and a map (hash) or list (array).
240
239
  // NOTE: typescript
241
240
  // things
@@ -243,47 +242,43 @@ function isnode(val: any): val is Indexable {
243
242
  return null != val && S_object == typeof val
244
243
  }
245
244
 
246
-
247
245
  // Value is a defined map (hash) with string keys.
248
246
  function ismap(val: any): val is { [key: string]: any } {
249
247
  return null != val && S_object == typeof val && !Array.isArray(val)
250
248
  }
251
249
 
252
-
253
250
  // Value is a defined list (array) with integer keys (indexes).
254
251
  function islist(val: any): val is any[] {
255
252
  return Array.isArray(val)
256
253
  }
257
254
 
258
-
259
255
  // Value is a defined string (non-empty) or integer key.
260
256
  function iskey(key: any): key is PropKey {
261
257
  const keytype = typeof key
262
258
  return (S_string === keytype && S_MT !== key) || S_number === keytype
263
259
  }
264
260
 
265
-
266
261
  // Check for an "empty" value - undefined, empty string, array, object.
267
262
  function isempty(val: any) {
268
- return null == val || S_MT === val ||
263
+ return (
264
+ null == val ||
265
+ S_MT === val ||
269
266
  (Array.isArray(val) && 0 === val.length) ||
270
267
  (S_object === typeof val && 0 === Object.keys(val).length)
268
+ )
271
269
  }
272
270
 
273
-
274
271
  // Value is a function.
275
- function isfunc(val: any): val is Function {
272
+ function isfunc(val: any): val is (...args: any[]) => any {
276
273
  return S_function === typeof val
277
274
  }
278
275
 
279
-
280
276
  // The integer size of the value. For arrays and strings, the length,
281
277
  // for numbers, the integer part, for boolean, true is 1 and falso 0, for all other values, 0.
282
278
  function size(val: any): number {
283
279
  if (islist(val)) {
284
280
  return val.length
285
- }
286
- else if (ismap(val)) {
281
+ } else if (ismap(val)) {
287
282
  return Object.keys(val).length
288
283
  }
289
284
 
@@ -291,19 +286,15 @@ function size(val: any): number {
291
286
 
292
287
  if (S_string == valtype) {
293
288
  return val.length
294
- }
295
- else if (S_number == typeof val) {
289
+ } else if (S_number == typeof val) {
296
290
  return Math.floor(val)
297
- }
298
- else if (S_boolean == typeof val) {
291
+ } else if (S_boolean == typeof val) {
299
292
  return true === val ? 1 : 0
300
- }
301
- else {
293
+ } else {
302
294
  return 0
303
295
  }
304
296
  }
305
297
 
306
-
307
298
  // Extract part of an array or string into a new value, from the start
308
299
  // point to the end point. If no end is specified, extract to the
309
300
  // full length of the value. Negative arguments count from the end of
@@ -311,7 +302,7 @@ function size(val: any): number {
311
302
  // is inclusive, and end is *exclusive*.
312
303
  // NOTE: input lists are not mutated by default. Use the mutate
313
304
  // argument to mutate lists in place.
314
- function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boolean): V {
305
+ function slice<V>(val: V, start?: number, end?: number, mutate?: boolean): V {
315
306
  if (S_number === typeof val) {
316
307
  start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start
317
308
  end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1
@@ -331,21 +322,16 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
331
322
  end = 0
332
323
  }
333
324
  start = 0
334
- }
335
-
336
- else if (null != end) {
325
+ } else if (null != end) {
337
326
  if (end < 0) {
338
327
  end = vlen + end
339
328
  if (end < 0) {
340
329
  end = 0
341
330
  }
342
- }
343
- else if (vlen < end) {
331
+ } else if (vlen < end) {
344
332
  end = vlen
345
333
  }
346
- }
347
-
348
- else {
334
+ } else {
349
335
  end = vlen
350
336
  }
351
337
 
@@ -359,21 +345,17 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
359
345
  for (let i = 0, j = start; j < end; i++, j++) {
360
346
  val[i] = val[j]
361
347
  }
362
- val.length = (end - start)
363
- }
364
- else {
348
+ val.length = end - start
349
+ } else {
365
350
  val = val.slice(start, end) as V
366
351
  }
367
- }
368
- else if (S_string === typeof val) {
352
+ } else if (S_string === typeof val) {
369
353
  val = (val as string).substring(start, end) as V
370
354
  }
371
- }
372
- else {
355
+ } else {
373
356
  if (islist(val)) {
374
357
  val = [] as V
375
- }
376
- else if (S_string === typeof val) {
358
+ } else if (S_string === typeof val) {
377
359
  val = S_MT as V
378
360
  }
379
361
  }
@@ -382,19 +364,16 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
382
364
  return val
383
365
  }
384
366
 
385
-
386
367
  // String padding.
387
368
  function pad(str: any, padding?: number, padchar?: string): string {
388
369
  str = S_string === typeof str ? str : stringify(str)
389
370
  padding = null == padding ? 44 : padding
390
- padchar = null == padchar ? S_SP : ((padchar + S_SP)[0])
371
+ padchar = null == padchar ? S_SP : (padchar + S_SP)[0]
391
372
  return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar)
392
373
  }
393
374
 
394
-
395
375
  // Determine the type of a value as a bit code.
396
376
  function typify(value: any): number {
397
-
398
377
  if (undefined === value) {
399
378
  return T_noval
400
379
  }
@@ -403,41 +382,30 @@ function typify(value: any): number {
403
382
 
404
383
  if (null === value) {
405
384
  return T_scalar | T_null
406
- }
407
- else if (S_number === typestr) {
385
+ } else if (S_number === typestr) {
408
386
  if (Number.isInteger(value)) {
409
387
  return T_scalar | T_number | T_integer
410
- }
411
- else if (isNaN(value)) {
388
+ } else if (isNaN(value)) {
412
389
  return T_noval
413
- }
414
- else {
390
+ } else {
415
391
  return T_scalar | T_number | T_decimal
416
392
  }
417
- }
418
- else if (S_string === typestr) {
393
+ } else if (S_string === typestr) {
419
394
  return T_scalar | T_string
420
- }
421
- else if (S_boolean === typestr) {
395
+ } else if (S_boolean === typestr) {
422
396
  return T_scalar | T_boolean
423
- }
424
- else if (S_function === typestr) {
397
+ } else if (S_function === typestr) {
425
398
  return T_scalar | T_function
426
399
  }
427
400
 
428
401
  // For languages that have symbolic atoms.
429
402
  else if (S_symbol === typestr) {
430
403
  return T_scalar | T_symbol
431
- }
432
-
433
- else if (Array.isArray(value)) {
404
+ } else if (Array.isArray(value)) {
434
405
  return T_node | T_list
435
- }
436
-
437
- else if (S_object === typestr) {
438
-
406
+ } else if (S_object === typestr) {
439
407
  if (value.constructor instanceof Function) {
440
- let cname = value.constructor.name
408
+ const cname = value.constructor.name
441
409
  if ('Object' !== cname && 'Array' !== cname) {
442
410
  return T_node | T_instance
443
411
  }
@@ -450,7 +418,6 @@ function typify(value: any): number {
450
418
  return T_any
451
419
  }
452
420
 
453
-
454
421
  // Get a list element. The key should be an integer, or a string
455
422
  // that can parse to an integer only. Negative integers count from the end of the list.
456
423
  function getelem(val: any, key: any, alt?: any) {
@@ -461,8 +428,8 @@ function getelem(val: any, key: any, alt?: any) {
461
428
  }
462
429
 
463
430
  if (islist(val)) {
464
- let nkey = parseInt(key)
465
- if (Number.isInteger(nkey) && ('' + key).match(R_INTEGER_KEY)) {
431
+ const nkey = parseInt(key)
432
+ if (Number.isInteger(nkey) && re_test(R_INTEGER_KEY, '' + key)) {
466
433
  if (nkey < 0) {
467
434
  key = val.length + nkey
468
435
  }
@@ -470,14 +437,14 @@ function getelem(val: any, key: any, alt?: any) {
470
437
  }
471
438
  }
472
439
 
473
- if (NONE === out) {
440
+ // null at a slot counts as "no value" — same Group A rule as getprop.
441
+ if (null == out) {
474
442
  return 0 < (T_function & typify(alt)) ? alt() : alt
475
443
  }
476
444
 
477
445
  return out
478
446
  }
479
447
 
480
-
481
448
  // Safely get a property of a node. Undefined arguments return undefined.
482
449
  // If the key is not found, return the alternative value, if any.
483
450
  function getprop(val: any, key: any, alt?: any) {
@@ -491,13 +458,27 @@ function getprop(val: any, key: any, alt?: any) {
491
458
  out = val[key]
492
459
  }
493
460
 
494
- if (NONE === out) {
461
+ // JSON null at a key is treated as "no value" for the default-substitution
462
+ // rule. This unifies cross-port behaviour: every host language conflates
463
+ // null and absent at the value level either always or via positional
464
+ // checks; the library follows that constraint on observation.
465
+ if (null == out) {
495
466
  return alt
496
467
  }
497
468
 
498
469
  return out
499
470
  }
500
471
 
472
+ // Internal: literal value lookup that preserves stored JSON null. Group B
473
+ // callers (validate / transform commands / builders / inject internals) use
474
+ // this when they need to inspect the raw stored value at a slot regardless
475
+ // of whether it is null. The public getprop / getelem / haskey APIs treat
476
+ // null as absent (Group A) per UNDEF_SPEC.md.
477
+ function _lookup(val: any, key: any): any {
478
+ if (NONE === val || NONE === key) return NONE
479
+ if (isnode(val)) return val[key]
480
+ return NONE
481
+ }
501
482
 
502
483
  // Convert different types of keys to string representation.
503
484
  // String keys are returned as is.
@@ -513,42 +494,38 @@ function strkey(key: any = NONE): string {
513
494
 
514
495
  if (0 < (T_string & t)) {
515
496
  return key
516
- }
517
- else if (0 < (T_boolean & t)) {
497
+ } else if (0 < (T_boolean & t)) {
518
498
  return S_MT
519
- }
520
- else if (0 < (T_number & t)) {
499
+ } else if (0 < (T_number & t)) {
521
500
  return key % 1 === 0 ? String(key) : String(Math.floor(key))
522
501
  }
523
502
 
524
503
  return S_MT
525
504
  }
526
505
 
527
-
528
506
  // Sorted keys of a map, or indexes (as strings) of a list.
529
507
  // Root utility - only uses language facilities.
530
508
  function keysof(val: any): string[] {
531
- return !isnode(val) ? [] :
532
- ismap(val) ? Object.keys(val).sort() : (val as any).map((_n: any, i: number) => S_MT + i)
509
+ return !isnode(val)
510
+ ? []
511
+ : ismap(val)
512
+ ? Object.keys(val).sort()
513
+ : (val as any).map((_n: any, i: number) => S_MT + i)
533
514
  }
534
515
 
535
-
536
516
  // Value of property with name key in node val is defined.
537
517
  // Root utility - only uses language facilities.
538
518
  function haskey(val: any, key: any) {
539
- return NONE !== getprop(val, key)
519
+ // null at a key counts as "no value" — same rule as getprop.
520
+ return null != getprop(val, key)
540
521
  }
541
522
 
542
-
543
523
  // List the sorted keys of a map or list as an array of tuples of the form [key, value].
544
524
  // As with keysof, list indexes are converted to strings.
545
525
  // Root utility - only uses language facilities.
546
- function items(val: any): [string, any][];
547
- function items<T>(val: any, apply: (item: [string, any]) => T): T[];
548
- function items(
549
- val: any,
550
- apply?: (item: [string, any]) => any
551
- ): any[] {
526
+ function items(val: any): [string, any][]
527
+ function items<T>(val: any, apply: (item: [string, any]) => T): T[]
528
+ function items(val: any, apply?: (item: [string, any]) => any): any[] {
552
529
  let out: [string, any][] = keysof(val).map((k: any) => [k, val[k]])
553
530
  if (null != apply) {
554
531
  out = out.map(apply)
@@ -556,7 +533,6 @@ function items(
556
533
  return out
557
534
  }
558
535
 
559
-
560
536
  // To replicate the array spread operator:
561
537
  // a=1, b=[2,3], c=[4,5]
562
538
  // [a,...b,c] -> [1,2,3,[4,5]]
@@ -569,12 +545,11 @@ function flatten(list: any[], depth?: number) {
569
545
  return list.flat(getdef(depth, 1))
570
546
  }
571
547
 
572
-
573
548
  // Filter item values using check function.
574
549
  function filter(val: any, check: (item: [string, any]) => boolean): any[] {
575
- let all = items(val)
576
- let numall = size(all)
577
- let out = []
550
+ const all = items(val)
551
+ const numall = size(all)
552
+ const out = []
578
553
  for (let i = 0; i < numall; i++) {
579
554
  if (check(all[i])) {
580
555
  out.push(all[i][1])
@@ -583,13 +558,15 @@ function filter(val: any, check: (item: [string, any]) => boolean): any[] {
583
558
  return out
584
559
  }
585
560
 
586
-
587
561
  // Escape regular expression.
588
562
  function escre(s: string) {
589
- // s = null == s ? S_MT : s
590
- return replace(s, R_ESCAPE_REGEXP, '\\$&')
563
+ return re_replace(R_ESCAPE_REGEXP, s, '\\$&')
591
564
  }
592
565
 
566
+ // Alias so call sites read naturally next to the other re_* helpers.
567
+ function re_escape(s: string) {
568
+ return escre(s)
569
+ }
593
570
 
594
571
  // Escape URLs.
595
572
  function escurl(s: string) {
@@ -597,24 +574,89 @@ function escurl(s: string) {
597
574
  return encodeURIComponent(s)
598
575
  }
599
576
 
577
+ // ===========================================================================
578
+ // Regex utility — uniform API. Every port exposes these same names. The
579
+ // dialect is the RE2 subset documented in /REGEX.md. Internal library code
580
+ // uses these helpers instead of host-language regex methods directly.
581
+ // ===========================================================================
582
+
583
+ // Compile a regex (or return as-is if already compiled). Cached behaviour is
584
+ // up to the port; in TS we just construct a RegExp.
585
+ function re_compile(pattern: string | RegExp, flags?: string): RegExp {
586
+ if (pattern instanceof RegExp) {
587
+ return pattern
588
+ }
589
+ return new RegExp(pattern, flags)
590
+ }
591
+
592
+ // First match. Returns [whole, capture1, ...] or null.
593
+ function re_find(pattern: string | RegExp, input: string): RegExpMatchArray | null {
594
+ const re = pattern instanceof RegExp ? pattern : new RegExp(pattern)
595
+ return input.match(re)
596
+ }
597
+
598
+ // All non-overlapping matches, left to right. Each element is the same shape
599
+ // as re_find's array.
600
+ function re_find_all(pattern: string | RegExp, input: string): RegExpMatchArray[] {
601
+ let re: RegExp
602
+ if (pattern instanceof RegExp) {
603
+ re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g')
604
+ } else {
605
+ re = new RegExp(pattern, 'g')
606
+ }
607
+ const out: RegExpMatchArray[] = []
608
+ let m: RegExpExecArray | null
609
+ while ((m = re.exec(input)) !== null) {
610
+ out.push(m as unknown as RegExpMatchArray)
611
+ if (m[0] === '') re.lastIndex++ // avoid infinite loop on empty match
612
+ }
613
+ return out
614
+ }
615
+
616
+ // Replace every match. `replacement` is either a string (with $& and $1..$9)
617
+ // or a callback receiving the same array as re_find returns.
618
+ function re_replace(
619
+ pattern: string | RegExp,
620
+ input: string,
621
+ replacement: string | ((m: RegExpMatchArray) => string),
622
+ ): string {
623
+ let re: RegExp
624
+ if (pattern instanceof RegExp) {
625
+ re = pattern.global ? pattern : new RegExp(pattern.source, pattern.flags + 'g')
626
+ } else {
627
+ re = new RegExp(pattern, 'g')
628
+ }
629
+ if (typeof replacement === 'function') {
630
+ return input.replace(re, (...args: any[]) => {
631
+ // Strip the trailing offset + full string args String.prototype.replace passes.
632
+ // The callback below receives just the capture groups (whole at [0]).
633
+ const groups = args.slice(0, -2) as unknown as RegExpMatchArray
634
+ return replacement(groups)
635
+ })
636
+ }
637
+ return input.replace(re, replacement)
638
+ }
639
+
640
+ // Boolean test for any match.
641
+ function re_test(pattern: string | RegExp, input: string): boolean {
642
+ const re = pattern instanceof RegExp ? pattern : new RegExp(pattern)
643
+ return re.test(input)
644
+ }
600
645
 
601
646
  // Replace a search string (all), or a regexp, in a source string.
602
647
  function replace(s: string, from: string | RegExp, to: any) {
603
648
  let rs = s
604
- let ts = typify(s)
649
+ const ts = typify(s)
605
650
  if (0 === (T_string & ts)) {
606
651
  rs = stringify(s)
607
- }
608
- else if (0 < ((T_noval | T_null) & ts)) {
652
+ } else if (0 < ((T_noval | T_null) & ts)) {
609
653
  rs = S_MT
610
- }
611
- else {
654
+ } else {
612
655
  rs = stringify(s)
613
656
  }
614
657
  return rs.replace(from, to)
615
658
  }
616
659
 
617
-
618
660
  // Concatenate url part strings, merging sep char as needed.
619
661
  function join(arr: any[], sep?: string, url?: boolean) {
620
662
  const sarr = size(arr)
@@ -623,41 +665,45 @@ function join(arr: any[], sep?: string, url?: boolean) {
623
665
  const out = filter(
624
666
  items(
625
667
  // filter(arr, (n) => null != n[1] && S_MT !== n[1]),
626
- filter(arr, (n) => (0 < (T_string & typify(n[1]))) && S_MT !== n[1]),
668
+ filter(arr, (n) => 0 < (T_string & typify(n[1])) && S_MT !== n[1]),
627
669
  (n) => {
628
- let i = +n[0]
670
+ const i = +n[0]
629
671
  let s = n[1]
630
672
 
631
673
  if (NONE !== sepre && S_MT !== sepre) {
632
674
  if (url && 0 === i) {
633
- s = replace(s, RegExp(sepre + '+$'), S_MT)
675
+ s = re_replace(re_compile(sepre + '+$'), s, S_MT)
634
676
  return s
635
677
  }
636
678
 
637
679
  if (0 < i) {
638
- s = replace(s, RegExp('^' + sepre + '+'), S_MT)
680
+ s = re_replace(re_compile('^' + sepre + '+'), s, S_MT)
639
681
  }
640
682
 
641
683
  if (i < sarr - 1 || !url) {
642
- s = replace(s, RegExp(sepre + '+$'), S_MT)
684
+ s = re_replace(re_compile(sepre + '+$'), s, S_MT)
643
685
  }
644
686
 
645
- s = replace(s, RegExp('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
646
- '$1' + sepdef + '$2')
687
+ s = re_replace(
688
+ re_compile('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
689
+ s,
690
+ '$1' + sepdef + '$2',
691
+ )
647
692
  }
648
693
 
649
694
  return s
650
- }), (n) => S_MT !== n[1])
651
- .join(sepdef)
695
+ },
696
+ ),
697
+ (n) => S_MT !== n[1],
698
+ ).join(sepdef)
652
699
 
653
700
  return out
654
701
  }
655
702
 
656
-
657
703
  // Output JSON in a "standard" format, with 2 space indents, each property on a new line,
658
704
  // and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
659
705
  // In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
660
- function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
706
+ function jsonify(val: any, flags?: { indent?: number; offset?: number }) {
661
707
  let str = S_null
662
708
 
663
709
  if (null != val) {
@@ -671,14 +717,14 @@ function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
671
717
  if (0 < offset) {
672
718
  // Left offset entire indented JSON so that it aligns with surrounding code
673
719
  // indented by offset. Assume first brace is on line with asignment, so not offset.
674
- str = '{\n' +
720
+ str =
721
+ '{\n' +
675
722
  join(
676
- items(
677
- slice(str.split('\n'), 1),
678
- (n: any) => pad(n[1], 0 - offset - size(n[1]))), '\n')
723
+ items(slice(str.split('\n'), 1), (n: any) => pad(n[1], 0 - offset - size(n[1]))),
724
+ '\n',
725
+ )
679
726
  }
680
- }
681
- catch (e: any) {
727
+ } catch {
682
728
  str = '__JSONIFY_FAILED__'
683
729
  }
684
730
  }
@@ -686,7 +732,6 @@ function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
686
732
  return str
687
733
  }
688
734
 
689
-
690
735
  // Safely stringify a value for humans (NOT JSON!).
691
736
  function stringify(val: any, maxlen?: number, pretty?: any): string {
692
737
  let valstr = S_MT
@@ -698,15 +743,10 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
698
743
 
699
744
  if (S_string === typeof val) {
700
745
  valstr = val
701
- }
702
- else {
746
+ } else {
703
747
  try {
704
- valstr = JSON.stringify(val, function(_key: string, val: any) {
705
- if (
706
- val !== null &&
707
- typeof val === "object" &&
708
- !Array.isArray(val)
709
- ) {
748
+ valstr = JSON.stringify(val, function (_key: string, val: any) {
749
+ if (val !== null && typeof val === 'object' && !Array.isArray(val)) {
710
750
  const sortedObj: any = {}
711
751
  items(val, (n) => {
712
752
  sortedObj[n[0]] = val[n[0]]
@@ -715,49 +755,57 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
715
755
  }
716
756
  return val
717
757
  })
718
- valstr = valstr.replace(R_QUOTES, S_MT)
719
- }
720
- catch (err: any) {
758
+ valstr = re_replace(R_QUOTES, valstr, S_MT)
759
+ } catch {
721
760
  valstr = '__STRINGIFY_FAILED__'
722
761
  }
723
762
  }
724
763
 
725
764
  if (null != maxlen && -1 < maxlen) {
726
- let js = valstr.substring(0, maxlen)
727
- valstr = maxlen < valstr.length ? (js.substring(0, maxlen - 3) + '...') : valstr
765
+ const js = valstr.substring(0, maxlen)
766
+ valstr = maxlen < valstr.length ? js.substring(0, maxlen - 3) + '...' : valstr
728
767
  }
729
768
 
730
769
  if (pretty) {
731
770
  // Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
732
- let c = items(
771
+ const c = items(
733
772
  [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69],
734
- (n) => '\x1b[38;5;' + n[1] + 'm'),
735
- r = '\x1b[0m', d = 0, o = c[0], t = o
773
+ (n) => '\x1b[38;5;' + n[1] + 'm',
774
+ )
775
+ const r = '\x1b[0m'
776
+ let d = 0,
777
+ o = c[0],
778
+ t = o
736
779
  for (const ch of valstr) {
737
780
  if (ch === '{' || ch === '[') {
738
- d++; o = c[d % c.length]; t += o + ch
781
+ d++
782
+ o = c[d % c.length]
783
+ t += o + ch
739
784
  } else if (ch === '}' || ch === ']') {
740
- t += o + ch; d--; o = c[d % c.length]
785
+ t += o + ch
786
+ d--
787
+ o = c[d % c.length]
741
788
  } else {
742
789
  t += o + ch
743
790
  }
744
791
  }
745
792
  return t + r
746
-
747
793
  }
748
794
 
749
795
  return valstr
750
796
  }
751
797
 
752
-
753
798
  // Build a human friendly path string.
754
799
  function pathify(val: any, startin?: number, endin?: number) {
755
800
  let pathstr: string | undefined = NONE
756
801
 
757
- let path: any[] | undefined = islist(val) ? val :
758
- S_string == typeof val ? [val] :
759
- S_number == typeof val ? [val] :
760
- NONE
802
+ let path: any[] | undefined = islist(val)
803
+ ? val
804
+ : S_string == typeof val
805
+ ? [val]
806
+ : S_number == typeof val
807
+ ? [val]
808
+ : NONE
761
809
 
762
810
  const start = null == startin ? 0 : -1 < startin ? startin : 0
763
811
  const end = null == endin ? 0 : -1 < endin ? endin : 0
@@ -766,15 +814,17 @@ function pathify(val: any, startin?: number, endin?: number) {
766
814
  path = slice(path, start, path.length - end)
767
815
  if (0 === path.length) {
768
816
  pathstr = '<root>'
769
- }
770
- else {
817
+ } else {
771
818
  pathstr = join(
772
819
  items(
773
- filter(path, (n) => iskey(n[1])), (n) => {
774
- let p = n[1]
775
- return S_number === typeof p ? S_MT + Math.floor(p) :
776
- p.replace(R_DOT, S_MT)
777
- }), S_DT)
820
+ filter(path, (n) => iskey(n[1])),
821
+ (n) => {
822
+ const p = n[1]
823
+ return S_number === typeof p ? S_MT + Math.floor(p) : re_replace(R_DOT, p, S_MT)
824
+ },
825
+ ),
826
+ S_DT,
827
+ )
778
828
  }
779
829
  }
780
830
 
@@ -785,34 +835,34 @@ function pathify(val: any, startin?: number, endin?: number) {
785
835
  return pathstr
786
836
  }
787
837
 
788
-
789
838
  // Clone a JSON-like data structure.
790
839
  // NOTE: function and instance values are copied, *not* cloned.
791
840
  function clone(val: any): any {
792
841
  const refs: any[] = []
793
842
  const reftype = T_function | T_instance
794
- const replacer: any = (_k: any, v: any) => 0 < (reftype & typify(v)) ?
795
- (refs.push(v), '`$REF:' + (refs.length - 1) + '`') : v
796
- const reviver: any = (_k: any, v: any, m: any) => S_string === typeof v ?
797
- (m = v.match(R_CLONE_REF), m ? refs[m[1]] : v) : v
843
+ const replacer: any = (_k: any, v: any) =>
844
+ 0 < (reftype & typify(v)) ? (refs.push(v), '`$REF:' + (refs.length - 1) + '`') : v
845
+ const reviver: any = (_k: any, v: any, m: any) =>
846
+ S_string === typeof v ? ((m = re_find(R_CLONE_REF, v)), m ? refs[m[1]] : v) : v
798
847
  const out = NONE === val ? NONE : JSON.parse(JSON.stringify(val, replacer), reviver)
799
848
  return out
800
849
  }
801
850
 
802
-
803
851
  // Define a JSON Object using function arguments.
804
852
  function jm(...kv: any[]): Record<string, any> {
805
853
  const kvsize = size(kv)
806
854
  const o: any = {}
807
855
  for (let i = 0; i < kvsize; i += 2) {
808
- let k = getprop(kv, i, '$KEY' + i)
856
+ // Builders are Group B (value processing): preserve literal stored
857
+ // values, including null. Direct array index distinguishes "slot exists
858
+ // with value null" from "slot beyond end of kv".
859
+ let k = i < kv.length ? kv[i] : '$KEY' + i
809
860
  k = 'string' === typeof k ? k : stringify(k)
810
- o[k] = getprop(kv, i + 1, null)
861
+ o[k] = i + 1 < kv.length ? kv[i + 1] : null
811
862
  }
812
863
  return o
813
864
  }
814
865
 
815
-
816
866
  // Define a JSON Array using function arguments.
817
867
  function jt(...v: any[]): any[] {
818
868
  const vsize = size(v)
@@ -823,8 +873,7 @@ function jt(...v: any[]): any[] {
823
873
  return a
824
874
  }
825
875
 
826
-
827
- // Safely delete a property from an object or array element.
876
+ // Safely delete a property from an object or array element.
828
877
  // Undefined arguments and invalid keys are ignored.
829
878
  // Returns the (possibly modified) parent.
830
879
  // For objects, the property is deleted using the delete operator.
@@ -838,8 +887,7 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
838
887
  if (ismap(parent)) {
839
888
  key = strkey(key)
840
889
  delete (parent as any)[key]
841
- }
842
- else if (islist(parent)) {
890
+ } else if (islist(parent)) {
843
891
  // Ensure key is an integer.
844
892
  let keyI = +key
845
893
 
@@ -863,7 +911,6 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
863
911
  return parent
864
912
  }
865
913
 
866
-
867
914
  // Safely set a property. Undefined arguments and invalid keys are ignored.
868
915
  // Returns the (possibly modified) parent.
869
916
  // If the parent is a list, and the key is negative, prepend the value.
@@ -878,8 +925,7 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
878
925
  key = S_MT + key
879
926
  const pany = parent as any
880
927
  pany[key] = val
881
- }
882
- else if (islist(parent)) {
928
+ } else if (islist(parent)) {
883
929
  // Ensure key is an integer.
884
930
  let keyI = +key
885
931
 
@@ -905,7 +951,6 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
905
951
  return parent
906
952
  }
907
953
 
908
-
909
954
  // Walk a data structure depth first, applying a function to each value.
910
955
  // The `path` argument passed to the before/after callbacks is a single
911
956
  // mutable array per depth, shared across all callback invocations for the
@@ -929,7 +974,7 @@ function walk(
929
974
  key?: string | number,
930
975
  parent?: any,
931
976
  path?: string[],
932
- pool?: string[][]
977
+ pool?: string[][],
933
978
  ): any {
934
979
  if (NONE === pool) {
935
980
  pool = [[]]
@@ -961,11 +1006,9 @@ function walk(
961
1006
  childPath[i] = path[i]
962
1007
  }
963
1008
 
964
- for (let [ckey, child] of items(out)) {
1009
+ for (const [ckey, child] of items(out)) {
965
1010
  childPath[depth] = S_MT + ckey
966
- setprop(out, ckey, walk(
967
- child, before, after, maxdepth, ckey, out, childPath, pool
968
- ))
1011
+ setprop(out, ckey, walk(child, before, after, maxdepth, ckey, out, childPath, pool))
969
1012
  }
970
1013
  }
971
1014
 
@@ -974,7 +1017,6 @@ function walk(
974
1017
  return out
975
1018
  }
976
1019
 
977
-
978
1020
  // Merge a list of values into each other. Later values have
979
1021
  // precedence. Nodes override scalars. Node kinds (list or map)
980
1022
  // override each other, and do *not* merge. The first element is
@@ -994,8 +1036,7 @@ function merge(val: any, maxdepth?: number): any {
994
1036
 
995
1037
  if (0 === lenlist) {
996
1038
  return NONE
997
- }
998
- else if (1 === lenlist) {
1039
+ } else if (1 === lenlist) {
999
1040
  return list[0]
1000
1041
  }
1001
1042
 
@@ -1003,25 +1044,19 @@ function merge(val: any, maxdepth?: number): any {
1003
1044
  out = getprop(list, 0, {})
1004
1045
 
1005
1046
  for (let oI = 1; oI < lenlist; oI++) {
1006
- let obj = list[oI]
1047
+ const obj = list[oI]
1007
1048
 
1008
1049
  if (!isnode(obj)) {
1009
1050
  // Nodes win.
1010
1051
  out = obj
1011
- }
1012
- else {
1052
+ } else {
1013
1053
  // Current value at path end in overriding node.
1014
- let cur: any[] = [out]
1054
+ const cur: any[] = [out]
1015
1055
 
1016
1056
  // Current value at path end in destination node.
1017
- let dst: any[] = [out]
1018
-
1019
- function before(
1020
- key: string | number | undefined,
1021
- val: any,
1022
- _parent: any,
1023
- path: string[]
1024
- ) {
1057
+ const dst: any[] = [out]
1058
+
1059
+ function before(key: string | number | undefined, val: any, _parent: any, path: string[]) {
1025
1060
  const pI = size(path)
1026
1061
 
1027
1062
  if (md <= pI) {
@@ -1035,7 +1070,6 @@ function merge(val: any, maxdepth?: number): any {
1035
1070
 
1036
1071
  // Descend into override node - Set up correct target in `after` function.
1037
1072
  else {
1038
-
1039
1073
  // Descend into destination node using same key.
1040
1074
  dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI]
1041
1075
  const tval = dst[pI]
@@ -1066,12 +1100,7 @@ function merge(val: any, maxdepth?: number): any {
1066
1100
  return val
1067
1101
  }
1068
1102
 
1069
- function after(
1070
- key: string | number | undefined,
1071
- _val: any,
1072
- _parent: any,
1073
- path: string[]
1074
- ) {
1103
+ function after(key: string | number | undefined, _val: any, _parent: any, path: string[]) {
1075
1104
  const cI = size(path)
1076
1105
  const target = cur[cI - 1]
1077
1106
  const value = cur[cI]
@@ -1097,20 +1126,24 @@ function merge(val: any, maxdepth?: number): any {
1097
1126
  return out
1098
1127
  }
1099
1128
 
1100
-
1101
1129
  // Set a value using a path. Missing path parts are created.
1102
1130
  // String paths create only maps. Use a string list to create list parts.
1103
1131
  function setpath(
1104
1132
  store: any,
1105
1133
  path: number | string | string[],
1106
1134
  val: any,
1107
- injdef?: Partial<Injection>
1135
+ injdef?: Partial<Injection>,
1108
1136
  ) {
1109
1137
  const pathType = typify(path)
1110
1138
 
1111
- const parts = 0 < (T_list & pathType) ? path :
1112
- 0 < (T_string & pathType) ? (path as string).split(S_DT) :
1113
- 0 < (T_number & pathType) ? [path] : NONE
1139
+ const parts =
1140
+ 0 < (T_list & pathType)
1141
+ ? path
1142
+ : 0 < (T_string & pathType)
1143
+ ? (path as string).split(S_DT)
1144
+ : 0 < (T_number & pathType)
1145
+ ? [path]
1146
+ : NONE
1114
1147
 
1115
1148
  if (NONE === parts) {
1116
1149
  return NONE
@@ -1132,21 +1165,22 @@ function setpath(
1132
1165
 
1133
1166
  if (DELETE === val) {
1134
1167
  delprop(parent, getelem(parts, -1))
1135
- }
1136
- else {
1168
+ } else {
1137
1169
  setprop(parent, getelem(parts, -1), val)
1138
1170
  }
1139
1171
 
1140
1172
  return parent
1141
1173
  }
1142
1174
 
1143
-
1144
1175
  function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
1145
-
1146
1176
  // Operate on a string array.
1147
- const parts = islist(path) ? path :
1148
- 'string' === typeof path ? path.split(S_DT) :
1149
- 'number' === typeof path ? [strkey(path)] : NONE
1177
+ const parts = islist(path)
1178
+ ? path
1179
+ : 'string' === typeof path
1180
+ ? path.split(S_DT)
1181
+ : 'number' === typeof path
1182
+ ? [strkey(path)]
1183
+ : NONE
1150
1184
 
1151
1185
  if (NONE === parts) {
1152
1186
  return NONE
@@ -1162,9 +1196,7 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1162
1196
  // An empty path (incl empty string) just finds the store.
1163
1197
  if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
1164
1198
  val = src
1165
- }
1166
- else if (0 < numparts) {
1167
-
1199
+ } else if (0 < numparts) {
1168
1200
  // Check for $ACTIONs
1169
1201
  if (1 === numparts) {
1170
1202
  val = getprop(store, parts[0])
@@ -1173,7 +1205,7 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1173
1205
  if (!isfunc(val)) {
1174
1206
  val = src
1175
1207
 
1176
- const m = parts[0].match(R_META_PATH)
1208
+ const m = re_find(R_META_PATH, parts[0])
1177
1209
  if (m && injdef && injdef.meta) {
1178
1210
  val = getprop(injdef.meta, m[1])
1179
1211
  parts[0] = m[3]
@@ -1186,25 +1218,21 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1186
1218
 
1187
1219
  if (injdef && S_DKEY === part) {
1188
1220
  part = getprop(injdef, S_key)
1189
- }
1190
- else if (injdef && part.startsWith('$GET:')) {
1221
+ } else if (injdef && part.startsWith('$GET:')) {
1191
1222
  // $GET:path$ -> get store value, use as path part (string)
1192
1223
  part = stringify(getpath(src, slice(part, 5, -1)))
1193
- }
1194
- else if (injdef && part.startsWith('$REF:')) {
1224
+ } else if (injdef && part.startsWith('$REF:')) {
1195
1225
  // $REF:refpath$ -> get spec value, use as path part (string)
1196
1226
  part = stringify(getpath(getprop(store, S_DSPEC), slice(part, 5, -1)))
1197
- }
1198
- else if (injdef && part.startsWith('$META:')) {
1227
+ } else if (injdef && part.startsWith('$META:')) {
1199
1228
  // $META:metapath$ -> get meta value, use as path part (string)
1200
1229
  part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)))
1201
1230
  }
1202
1231
 
1203
1232
  // $$ escapes $
1204
- part = part.replace(R_DOUBLE_DOLLAR, '$')
1233
+ part = re_replace(R_DOUBLE_DOLLAR, part, '$')
1205
1234
 
1206
1235
  if (S_MT === part) {
1207
-
1208
1236
  let ascends = 0
1209
1237
  while (S_MT === parts[1 + pI]) {
1210
1238
  ascends++
@@ -1218,26 +1246,22 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1218
1246
 
1219
1247
  if (0 === ascends) {
1220
1248
  val = dparent
1221
- }
1222
- else {
1249
+ } else {
1223
1250
  // const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
1224
1251
  const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)])
1225
1252
 
1226
1253
  if (ascends <= size(dpath)) {
1227
1254
  val = getpath(store, fullpath)
1228
- }
1229
- else {
1255
+ } else {
1230
1256
  val = NONE
1231
1257
  }
1232
1258
 
1233
1259
  break
1234
1260
  }
1235
- }
1236
- else {
1261
+ } else {
1237
1262
  val = dparent
1238
1263
  }
1239
- }
1240
- else {
1264
+ } else {
1241
1265
  val = getprop(val, part)
1242
1266
  }
1243
1267
  }
@@ -1256,16 +1280,11 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1256
1280
  return val
1257
1281
  }
1258
1282
 
1259
-
1260
1283
  // Inject values from a data store into a node recursively, resolving
1261
1284
  // paths against the store, or current if they are local. The modify
1262
1285
  // argument allows custom modification of the result. The inj
1263
1286
  // (Injection) argument is used to maintain recursive state.
1264
- function inject(
1265
- val: any,
1266
- store: any,
1267
- injdef?: Partial<Injection>,
1268
- ) {
1287
+ function inject(val: any, store: any, injdef?: Partial<Injection>) {
1269
1288
  const valtype = typeof val
1270
1289
  let inj: Injection = injdef as Injection
1271
1290
 
@@ -1293,7 +1312,6 @@ function inject(
1293
1312
 
1294
1313
  // Descend into node.
1295
1314
  if (isnode(val)) {
1296
-
1297
1315
  // Keys are sorted alphanumerically to ensure determinism.
1298
1316
  // Injection transforms ($FOO) are processed *after* other keys.
1299
1317
  // NOTE: the optional digits suffix of the transform can thus be
@@ -1304,11 +1322,10 @@ function inject(
1304
1322
 
1305
1323
  if (ismap(val)) {
1306
1324
  nodekeys = flatten([
1307
- filter(nodekeys, (n => !n[1].includes(S_DS))),
1308
- filter(nodekeys, (n => n[1].includes(S_DS))),
1325
+ filter(nodekeys, (n) => !n[1].includes(S_DS)),
1326
+ filter(nodekeys, (n) => n[1].includes(S_DS)),
1309
1327
  ])
1310
- }
1311
- else {
1328
+ } else {
1312
1329
  nodekeys = keysof(val)
1313
1330
  }
1314
1331
 
@@ -1317,7 +1334,6 @@ function inject(
1317
1334
  // 2. inj.mode=M_VAL - The child value is injected.
1318
1335
  // 3. inj.mode=M_KEYPOST - Key string is injected again, allowing child mutation.
1319
1336
  for (let nkI = 0; nkI < nodekeys.length; nkI++) {
1320
-
1321
1337
  const childinj = inj.child(nkI, nodekeys)
1322
1338
  const nodekey = childinj.key
1323
1339
  childinj.mode = M_KEYPRE
@@ -1364,17 +1380,11 @@ function inject(
1364
1380
 
1365
1381
  // Custom modification.
1366
1382
  if (inj.modify && SKIP !== val) {
1367
- let mkey = inj.key
1368
- let mparent = inj.parent
1369
- let mval = getprop(mparent, mkey)
1370
-
1371
- inj.modify(
1372
- mval,
1373
- mkey,
1374
- mparent,
1375
- inj,
1376
- store
1377
- )
1383
+ const mkey = inj.key
1384
+ const mparent = inj.parent
1385
+ const mval = getprop(mparent, mkey)
1386
+
1387
+ inj.modify(mval, mkey, mparent, inj, store)
1378
1388
  }
1379
1389
 
1380
1390
  // console.log('INJ-VAL', val)
@@ -1383,10 +1393,9 @@ function inject(
1383
1393
 
1384
1394
  // Original val reference may no longer be correct.
1385
1395
  // This return value is only used as the top level result.
1386
- return getprop(inj.parent, S_DTOP)
1396
+ return _lookup(inj.parent, S_DTOP)
1387
1397
  }
1388
1398
 
1389
-
1390
1399
  // The transform_* functions are special command inject handlers (see Injector).
1391
1400
 
1392
1401
  // Delete a key from a map or list.
@@ -1395,7 +1404,6 @@ const transform_DELETE: Injector = (inj: Injection) => {
1395
1404
  return NONE
1396
1405
  }
1397
1406
 
1398
-
1399
1407
  // Copy value from source data.
1400
1408
  const transform_COPY: Injector = (inj: Injection, _val: any) => {
1401
1409
  const ijname = 'COPY'
@@ -1404,13 +1412,12 @@ const transform_COPY: Injector = (inj: Injection, _val: any) => {
1404
1412
  return NONE
1405
1413
  }
1406
1414
 
1407
- let out = getprop(inj.dparent, inj.key)
1415
+ const out = _lookup(inj.dparent, inj.key)
1408
1416
  inj.setval(out)
1409
1417
 
1410
1418
  return out
1411
1419
  }
1412
1420
 
1413
-
1414
1421
  // As a value, inject the key of the parent node.
1415
1422
  // As a key, defined the name of the key property in the source object.
1416
1423
  const transform_KEY: Injector = (inj: Injection) => {
@@ -1422,7 +1429,7 @@ const transform_KEY: Injector = (inj: Injection) => {
1422
1429
  }
1423
1430
 
1424
1431
  // Key is defined by $KEY meta property.
1425
- const keyspec = getprop(parent, S_BKEY)
1432
+ const keyspec = _lookup(parent, S_BKEY)
1426
1433
  if (NONE !== keyspec) {
1427
1434
  delprop(parent, S_BKEY)
1428
1435
  return getprop(inj.dparent, keyspec)
@@ -1430,10 +1437,9 @@ const transform_KEY: Injector = (inj: Injection) => {
1430
1437
 
1431
1438
  // Key is defined within general purpose $META object.
1432
1439
  // return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
1433
- return getprop(getprop(parent, S_BANNO), S_KEY, getelem(path, -2))
1440
+ return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2)
1434
1441
  }
1435
1442
 
1436
-
1437
1443
  // Annotate node. Does nothing itself, just used by
1438
1444
  // other injectors, and is removed when called.
1439
1445
  const transform_ANNO: Injector = (inj: Injection) => {
@@ -1442,10 +1448,9 @@ const transform_ANNO: Injector = (inj: Injection) => {
1442
1448
  return NONE
1443
1449
  }
1444
1450
 
1445
-
1446
- // Merge a list of objects into the current object.
1451
+ // Merge a list of objects into the current object.
1447
1452
  // Must be a key in an object. The value is merged over the current object.
1448
- // If the value is an array, the elements are first merged using `merge`.
1453
+ // If the value is an array, the elements are first merged using `merge`.
1449
1454
  // If the value is the empty string, merge the top level store.
1450
1455
  // Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
1451
1456
  const transform_MERGE: Injector = (inj: Injection) => {
@@ -1478,15 +1483,9 @@ const transform_MERGE: Injector = (inj: Injection) => {
1478
1483
  return out
1479
1484
  }
1480
1485
 
1481
-
1482
1486
  // Convert a node to a list.
1483
1487
  // Format: ['`$EACH`', '`source-path-of-node`', child-template]
1484
- const transform_EACH: Injector = (
1485
- inj: Injection,
1486
- _val: any,
1487
- _ref: string,
1488
- store: any
1489
- ) => {
1488
+ const transform_EACH: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1490
1489
  const ijname = 'EACH'
1491
1490
 
1492
1491
  if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
@@ -1515,18 +1514,22 @@ const transform_EACH: Injector = (
1515
1514
  let tval: any = []
1516
1515
 
1517
1516
  const tkey = getelem(inj.path, -2)
1518
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1517
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1519
1518
 
1520
1519
  // Create clones of the child template for each value of the current soruce.
1521
1520
  if (0 < (T_list & srctype)) {
1522
1521
  tval = items(src, () => clone(child))
1523
- }
1524
- else if (0 < (T_map & srctype)) {
1525
- tval = items(src, (n => merge([
1526
- clone(child),
1527
- // Make a note of the key for $KEY transforms.
1528
- { [S_BANNO]: { KEY: n[0] } }
1529
- ], 1)))
1522
+ } else if (0 < (T_map & srctype)) {
1523
+ tval = items(src, (n) =>
1524
+ merge(
1525
+ [
1526
+ clone(child),
1527
+ // Make a note of the key for $KEY transforms.
1528
+ { [S_BANNO]: { KEY: n[0] } },
1529
+ ],
1530
+ 1,
1531
+ ),
1532
+ )
1530
1533
  }
1531
1534
 
1532
1535
  let rval = []
@@ -1570,16 +1573,10 @@ const transform_EACH: Injector = (
1570
1573
  return rval[0]
1571
1574
  }
1572
1575
 
1573
-
1574
1576
  // Convert a node to a map.
1575
1577
  // Format: { '`$PACK`':['source-path', child-template]}
1576
- const transform_PACK: Injector = (
1577
- inj: Injection,
1578
- _val: any,
1579
- _ref: string,
1580
- store: any
1581
- ) => {
1582
- const { mode, key, path, parent, nodes } = inj
1578
+ const transform_PACK: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1579
+ const { key, path, parent, nodes } = inj
1583
1580
 
1584
1581
  const ijname = 'EACH'
1585
1582
 
@@ -1611,8 +1608,7 @@ const transform_PACK: Injector = (
1611
1608
  setprop(item[1], S_BANNO, { KEY: item[0] })
1612
1609
  return item[1]
1613
1610
  })
1614
- }
1615
- else {
1611
+ } else {
1616
1612
  src = NONE
1617
1613
  }
1618
1614
  }
@@ -1628,7 +1624,7 @@ const transform_PACK: Injector = (
1628
1624
  const child = getprop(childspec, S_BVAL, childspec)
1629
1625
 
1630
1626
  // Build parallel target object.
1631
- let tval: any = {}
1627
+ const tval: any = {}
1632
1628
 
1633
1629
  items(src, (item: [string, any]) => {
1634
1630
  const srckey = item[0]
@@ -1638,8 +1634,7 @@ const transform_PACK: Injector = (
1638
1634
  if (NONE !== keypath) {
1639
1635
  if (keypath.startsWith('`')) {
1640
1636
  key = inject(keypath, merge([{}, store, { $TOP: srcnode }], 1))
1641
- }
1642
- else {
1637
+ } else {
1643
1638
  key = getpath(srcnode, keypath, inj)
1644
1639
  }
1645
1640
  }
@@ -1650,8 +1645,7 @@ const transform_PACK: Injector = (
1650
1645
  const anno = getprop(srcnode, S_BANNO)
1651
1646
  if (NONE === anno) {
1652
1647
  delprop(tchild, S_BANNO)
1653
- }
1654
- else {
1648
+ } else {
1655
1649
  setprop(tchild, S_BANNO, anno)
1656
1650
  }
1657
1651
  })
@@ -1659,14 +1653,15 @@ const transform_PACK: Injector = (
1659
1653
  let rval = {}
1660
1654
 
1661
1655
  if (!isempty(tval)) {
1662
-
1663
1656
  // Build parallel source object.
1664
- let tsrc: any = {}
1657
+ const tsrc: any = {}
1665
1658
  src.reduce((a: any, n: any, i: any) => {
1666
- let kn = null == keypath ? i :
1667
- keypath.startsWith('`') ?
1668
- inject(keypath, merge([{}, store, { $TOP: n }], 1)) :
1669
- getpath(n, keypath, inj)
1659
+ const kn =
1660
+ null == keypath
1661
+ ? i
1662
+ : keypath.startsWith('`')
1663
+ ? inject(keypath, merge([{}, store, { $TOP: n }], 1))
1664
+ : getpath(n, keypath, inj)
1670
1665
 
1671
1666
  setprop(a, kn, n)
1672
1667
  return a
@@ -1706,16 +1701,10 @@ const transform_PACK: Injector = (
1706
1701
  return NONE
1707
1702
  }
1708
1703
 
1709
-
1710
1704
  // TODO: not found ref should removed key (setprop NONE)
1711
1705
  // Reference original spec (enables recursice transformations)
1712
1706
  // Format: ['`$REF`', '`spec-path`']
1713
- const transform_REF: Injector = (
1714
- inj: Injection,
1715
- val: any,
1716
- _ref: string,
1717
- store: any
1718
- ) => {
1707
+ const transform_REF: Injector = (inj: Injection, val: any, _ref: string, store: any) => {
1719
1708
  const { nodes } = inj
1720
1709
 
1721
1710
  if (M_VAL !== inj.mode) {
@@ -1723,7 +1712,7 @@ const transform_REF: Injector = (
1723
1712
  }
1724
1713
 
1725
1714
  // Get arguments: ['`$REF`', 'ref-path'].
1726
- const refpath = getprop(inj.parent, 1)
1715
+ const refpath = _lookup(inj.parent, 1)
1727
1716
  inj.keyI = size(inj.keys)
1728
1717
 
1729
1718
  // Spec reference.
@@ -1748,12 +1737,12 @@ const transform_REF: Injector = (
1748
1737
  })
1749
1738
  }
1750
1739
 
1751
- let tref = clone(ref)
1740
+ const tref = clone(ref)
1752
1741
 
1753
1742
  const cpath = slice(inj.path, -3)
1754
1743
  const tpath = slice(inj.path, -1)
1755
- let tcur = getpath(store, cpath)
1756
- let tval = getpath(store, tpath)
1744
+ const tcur = getpath(store, cpath)
1745
+ const tval = getpath(store, tpath)
1757
1746
  let rval = NONE
1758
1747
 
1759
1748
  if (!hasSubRef || NONE !== tval) {
@@ -1770,8 +1759,7 @@ const transform_REF: Injector = (
1770
1759
  inject(tref, store, tinj)
1771
1760
 
1772
1761
  rval = tinj.val
1773
- }
1774
- else {
1762
+ } else {
1775
1763
  rval = NONE
1776
1764
  }
1777
1765
 
@@ -1784,13 +1772,7 @@ const transform_REF: Injector = (
1784
1772
  return val
1785
1773
  }
1786
1774
 
1787
-
1788
- const transform_FORMAT: Injector = (
1789
- inj: Injection,
1790
- _val: any,
1791
- _ref: string,
1792
- store: any
1793
- ) => {
1775
+ const transform_FORMAT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1794
1776
  // console.log('FORMAT-START', inj, _val)
1795
1777
 
1796
1778
  // Remove remaining keys to avoid spurious processing.
@@ -1802,24 +1784,24 @@ const transform_FORMAT: Injector = (
1802
1784
 
1803
1785
  // Get arguments: ['`$FORMAT`', 'name', child].
1804
1786
  // TODO: EACH and PACK should accept customm functions too
1805
- const name = getprop(inj.parent, 1)
1806
- const child = getprop(inj.parent, 2)
1787
+ const name = _lookup(inj.parent, 1)
1788
+ const child = _lookup(inj.parent, 2)
1807
1789
 
1808
1790
  // Source data.
1809
1791
  const tkey = getelem(inj.path, -2)
1810
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1792
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1811
1793
 
1812
1794
  const cinj = injectChild(child, store, inj)
1813
1795
  const resolved = cinj.val
1814
1796
 
1815
- let formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
1797
+ const formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
1816
1798
 
1817
1799
  if (NONE === formatter) {
1818
1800
  inj.errs.push('$FORMAT: unknown format: ' + name + '.')
1819
1801
  return NONE
1820
1802
  }
1821
1803
 
1822
- let out = walk(resolved, formatter)
1804
+ const out = walk(resolved, formatter)
1823
1805
 
1824
1806
  setprop(target, tkey, out)
1825
1807
  // _updateAncestors(inj, target, tkey, out)
@@ -1827,17 +1809,15 @@ const transform_FORMAT: Injector = (
1827
1809
  return out
1828
1810
  }
1829
1811
 
1830
-
1831
1812
  const FORMATTER: Record<string, WalkApply> = {
1832
1813
  identity: (_k: any, v: any) => v,
1833
- upper: (_k: any, v: any) => isnode(v) ? v : ('' + v).toUpperCase(),
1834
- lower: (_k: any, v: any) => isnode(v) ? v : ('' + v).toLowerCase(),
1835
- string: (_k: any, v: any) => isnode(v) ? v : ('' + v),
1814
+ upper: (_k: any, v: any) => (isnode(v) ? v : ('' + v).toUpperCase()),
1815
+ lower: (_k: any, v: any) => (isnode(v) ? v : ('' + v).toLowerCase()),
1816
+ string: (_k: any, v: any) => (isnode(v) ? v : '' + v),
1836
1817
  number: (_k: any, v: any) => {
1837
1818
  if (isnode(v)) {
1838
1819
  return v
1839
- }
1840
- else {
1820
+ } else {
1841
1821
  let n = Number(v)
1842
1822
  if (isNaN(n)) {
1843
1823
  n = 0
@@ -1848,8 +1828,7 @@ const FORMATTER: Record<string, WalkApply> = {
1848
1828
  integer: (_k: any, v: any) => {
1849
1829
  if (isnode(v)) {
1850
1830
  return v
1851
- }
1852
- else {
1831
+ } else {
1853
1832
  let n = Number(v)
1854
1833
  if (isNaN(n)) {
1855
1834
  n = 0
@@ -1858,17 +1837,15 @@ const FORMATTER: Record<string, WalkApply> = {
1858
1837
  }
1859
1838
  },
1860
1839
  concat: (k: any, v: any) =>
1861
- null == k && islist(v) ? join(items(v, (n => isnode(n[1]) ? S_MT : (S_MT + n[1]))), S_MT) : v
1840
+ null == k && islist(v)
1841
+ ? join(
1842
+ items(v, (n) => (isnode(n[1]) ? S_MT : S_MT + n[1])),
1843
+ S_MT,
1844
+ )
1845
+ : v,
1862
1846
  }
1863
1847
 
1864
-
1865
-
1866
- const transform_APPLY: Injector = (
1867
- inj: Injection,
1868
- _val: any,
1869
- _ref: string,
1870
- store: any
1871
- ) => {
1848
+ const transform_APPLY: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1872
1849
  const ijname = 'APPLY'
1873
1850
 
1874
1851
  if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
@@ -1883,7 +1860,7 @@ const transform_APPLY: Injector = (
1883
1860
  }
1884
1861
 
1885
1862
  const tkey = getelem(inj.path, -2)
1886
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1863
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1887
1864
 
1888
1865
  const cinj = injectChild(child, store, inj)
1889
1866
  const resolved = cinj.val
@@ -1895,14 +1872,13 @@ const transform_APPLY: Injector = (
1895
1872
  return out
1896
1873
  }
1897
1874
 
1898
-
1899
1875
  // Transform data using spec.
1900
1876
  // Only operates on static JSON-like data.
1901
1877
  // Arrays are treated as if they are objects with indices as keys.
1902
1878
  function transform(
1903
1879
  data: any, // Source data to transform into new data (original not mutated)
1904
1880
  spec: any, // Transform specification; output follows this shape
1905
- injdef?: Partial<Injection>
1881
+ injdef?: Partial<Injection>,
1906
1882
  ) {
1907
1883
  // Clone the spec so that the clone can be modified in place as the transform result.
1908
1884
  const origspec = spec
@@ -1914,57 +1890,64 @@ function transform(
1914
1890
  const errs = injdef?.errs || []
1915
1891
 
1916
1892
  const extraTransforms: any = {}
1917
- const extraData = null == extra ? NONE : items(extra)
1918
- .reduce((a: any, n: any[]) =>
1919
- (n[0].startsWith(S_DS) ? extraTransforms[n[0]] = n[1] : (a[n[0]] = n[1]), a), {})
1920
-
1921
- const dataClone = merge([
1922
- isempty(extraData) ? NONE : clone(extraData),
1923
- clone(data),
1924
- ])
1893
+ const extraData =
1894
+ null == extra
1895
+ ? NONE
1896
+ : items(extra).reduce(
1897
+ (a: any, n: any[]) => (
1898
+ n[0].startsWith(S_DS) ? (extraTransforms[n[0]] = n[1]) : (a[n[0]] = n[1]),
1899
+ a
1900
+ ),
1901
+ {},
1902
+ )
1903
+
1904
+ const dataClone = merge([isempty(extraData) ? NONE : clone(extraData), clone(data)])
1925
1905
 
1926
1906
  // Define a top level store that provides transform operations.
1927
- const store = merge([
1928
- {
1929
- // The inject function recognises this special location for the root of the source data.
1930
- // NOTE: to escape data that contains "`$FOO`" keys at the top level,
1931
- // place that data inside a holding map: { myholder: mydata }.
1932
- $TOP: dataClone,
1933
-
1934
- $SPEC: () => origspec,
1935
-
1936
- // Escape backtick (this also works inside backticks).
1937
- $BT: () => S_BT,
1938
-
1939
- // Escape dollar sign (this also works inside backticks).
1940
- $DS: () => S_DS,
1941
-
1942
- // Insert current date and time as an ISO string.
1943
- $WHEN: () => new Date().toISOString(),
1944
-
1945
- $DELETE: transform_DELETE,
1946
- $COPY: transform_COPY,
1947
- $KEY: transform_KEY,
1948
- $ANNO: transform_ANNO,
1949
- $MERGE: transform_MERGE,
1950
- $EACH: transform_EACH,
1951
- $PACK: transform_PACK,
1952
- $REF: transform_REF,
1953
- $FORMAT: transform_FORMAT,
1954
- $APPLY: transform_APPLY,
1955
- },
1956
-
1957
- // Custom extra transforms, if any.
1958
- extraTransforms,
1959
-
1960
- {
1961
- $ERRS: errs,
1962
- }
1963
- ], 1)
1907
+ const store = merge(
1908
+ [
1909
+ {
1910
+ // The inject function recognises this special location for the root of the source data.
1911
+ // NOTE: to escape data that contains "`$FOO`" keys at the top level,
1912
+ // place that data inside a holding map: { myholder: mydata }.
1913
+ $TOP: dataClone,
1914
+
1915
+ $SPEC: () => origspec,
1916
+
1917
+ // Escape backtick (this also works inside backticks).
1918
+ $BT: () => S_BT,
1919
+
1920
+ // Escape dollar sign (this also works inside backticks).
1921
+ $DS: () => S_DS,
1922
+
1923
+ // Insert current date and time as an ISO string.
1924
+ $WHEN: () => new Date().toISOString(),
1925
+
1926
+ $DELETE: transform_DELETE,
1927
+ $COPY: transform_COPY,
1928
+ $KEY: transform_KEY,
1929
+ $ANNO: transform_ANNO,
1930
+ $MERGE: transform_MERGE,
1931
+ $EACH: transform_EACH,
1932
+ $PACK: transform_PACK,
1933
+ $REF: transform_REF,
1934
+ $FORMAT: transform_FORMAT,
1935
+ $APPLY: transform_APPLY,
1936
+ },
1937
+
1938
+ // Custom extra transforms, if any.
1939
+ extraTransforms,
1940
+
1941
+ {
1942
+ $ERRS: errs,
1943
+ },
1944
+ ],
1945
+ 1,
1946
+ )
1964
1947
 
1965
1948
  const out = inject(spec, store, injdef)
1966
1949
 
1967
- const generr = (0 < size(errs) && !collect)
1950
+ const generr = 0 < size(errs) && !collect
1968
1951
  if (generr) {
1969
1952
  throw new Error(join(errs, ' | '))
1970
1953
  }
@@ -1972,20 +1955,19 @@ function transform(
1972
1955
  return out
1973
1956
  }
1974
1957
 
1975
-
1976
1958
  // A required string value. NOTE: Rejects empty strings.
1977
1959
  const validate_STRING: Injector = (inj: Injection) => {
1978
- let out = getprop(inj.dparent, inj.key)
1960
+ const out = _lookup(inj.dparent, inj.key)
1979
1961
 
1980
1962
  const t = typify(out)
1981
1963
  if (0 === (T_string & t)) {
1982
- let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
1964
+ const msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
1983
1965
  inj.errs.push(msg)
1984
1966
  return NONE
1985
1967
  }
1986
1968
 
1987
1969
  if (S_MT === out) {
1988
- let msg = 'Empty string at ' + pathify(inj.path, 1)
1970
+ const msg = 'Empty string at ' + pathify(inj.path, 1)
1989
1971
  inj.errs.push(msg)
1990
1972
  return NONE
1991
1973
  }
@@ -1993,13 +1975,10 @@ const validate_STRING: Injector = (inj: Injection) => {
1993
1975
  return out
1994
1976
  }
1995
1977
 
1996
-
1997
-
1998
-
1999
1978
  const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
2000
1979
  const tname = slice(ref, 1).toLowerCase()
2001
1980
  const typev = 1 << (31 - TYPENAME.indexOf(tname))
2002
- let out = getprop(inj.dparent, inj.key)
1981
+ const out = _lookup(inj.dparent, inj.key)
2003
1982
 
2004
1983
  const t = typify(out)
2005
1984
 
@@ -2013,15 +1992,12 @@ const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
2013
1992
  return out
2014
1993
  }
2015
1994
 
2016
-
2017
1995
  // Allow any value.
2018
1996
  const validate_ANY: Injector = (inj: Injection) => {
2019
- let out = getprop(inj.dparent, inj.key)
1997
+ const out = _lookup(inj.dparent, inj.key)
2020
1998
  return out
2021
1999
  }
2022
2000
 
2023
-
2024
-
2025
2001
  // Specify child values for map or list.
2026
2002
  // Map syntax: {'`$CHILD`': child-template }
2027
2003
  // List syntax: ['`$CHILD`', child-template ]
@@ -2040,15 +2016,13 @@ const validate_CHILD: Injector = (inj: Injection) => {
2040
2016
 
2041
2017
  if (NONE == tval) {
2042
2018
  tval = {}
2043
- }
2044
- else if (!ismap(tval)) {
2045
- inj.errs.push(_invalidTypeMsg(
2046
- slice(inj.path, -1), S_object, typify(tval), tval), 'V0220')
2019
+ } else if (!ismap(tval)) {
2020
+ inj.errs.push(_invalidTypeMsg(slice(inj.path, -1), S_object, typify(tval), tval), 'V0220')
2047
2021
  return NONE
2048
2022
  }
2049
2023
 
2050
2024
  const ckeys = keysof(tval)
2051
- for (let ckey of ckeys) {
2025
+ for (const ckey of ckeys) {
2052
2026
  setprop(parent, ckey, clone(childtm))
2053
2027
 
2054
2028
  // NOTE: modifying inj! This extends the child value loop in inject.
@@ -2062,14 +2036,13 @@ const validate_CHILD: Injector = (inj: Injection) => {
2062
2036
 
2063
2037
  // List syntax.
2064
2038
  if (M_VAL === mode) {
2065
-
2066
2039
  if (!islist(parent)) {
2067
2040
  // $CHILD was not inside a list.
2068
2041
  inj.errs.push('Invalid $CHILD as value')
2069
2042
  return NONE
2070
2043
  }
2071
2044
 
2072
- const childtm = getprop(parent, 1)
2045
+ const childtm = _lookup(parent, 1)
2073
2046
 
2074
2047
  if (NONE === inj.dparent) {
2075
2048
  // Empty list as default.
@@ -2080,7 +2053,12 @@ const validate_CHILD: Injector = (inj: Injection) => {
2080
2053
 
2081
2054
  if (!islist(inj.dparent)) {
2082
2055
  const msg = _invalidTypeMsg(
2083
- slice(inj.path, -1), S_list, typify(inj.dparent), inj.dparent, 'V0230')
2056
+ slice(inj.path, -1),
2057
+ S_list,
2058
+ typify(inj.dparent),
2059
+ inj.dparent,
2060
+ 'V0230',
2061
+ )
2084
2062
  inj.errs.push(msg)
2085
2063
  inj.keyI = size(parent)
2086
2064
  return inj.dparent
@@ -2100,26 +2078,22 @@ const validate_CHILD: Injector = (inj: Injection) => {
2100
2078
  return NONE
2101
2079
  }
2102
2080
 
2103
-
2104
2081
  // TODO: implement SOME, ALL
2105
2082
  // FIX: ONE should mean exactly one, not at least one (=SOME)
2106
2083
  // TODO: implement a generate validate_ALT to do all of these
2107
2084
  // Match at least one of the specified shapes.
2108
2085
  // Syntax: ['`$ONE`', alt0, alt1, ...]
2109
- const validate_ONE: Injector = (
2110
- inj: Injection,
2111
- _val: any,
2112
- _ref: string,
2113
- store: any
2114
- ) => {
2086
+ const validate_ONE: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2115
2087
  const { mode, parent, keyI } = inj
2116
2088
 
2117
2089
  // Only operate in val mode, since parent is a list.
2118
2090
  if (M_VAL === mode) {
2119
2091
  if (!islist(parent) || 0 !== keyI) {
2120
- inj.errs.push('The $ONE validator at field ' +
2121
- pathify(inj.path, 1, 1) +
2122
- ' must be the first element of an array.')
2092
+ inj.errs.push(
2093
+ 'The $ONE validator at field ' +
2094
+ pathify(inj.path, 1, 1) +
2095
+ ' must be the first element of an array.',
2096
+ )
2123
2097
  return
2124
2098
  }
2125
2099
 
@@ -2131,19 +2105,20 @@ const validate_ONE: Injector = (
2131
2105
  inj.path = slice(inj.path, -1)
2132
2106
  inj.key = getelem(inj.path, -1)
2133
2107
 
2134
- let tvals = slice(parent, 1)
2108
+ const tvals = slice(parent, 1)
2135
2109
  if (0 === size(tvals)) {
2136
- inj.errs.push('The $ONE validator at field ' +
2137
- pathify(inj.path, 1, 1) +
2138
- ' must have at least one argument.')
2110
+ inj.errs.push(
2111
+ 'The $ONE validator at field ' +
2112
+ pathify(inj.path, 1, 1) +
2113
+ ' must have at least one argument.',
2114
+ )
2139
2115
  return
2140
2116
  }
2141
2117
 
2142
2118
  // See if we can find a match.
2143
- for (let tval of tvals) {
2144
-
2119
+ for (const tval of tvals) {
2145
2120
  // If match, then errs.length = 0
2146
- let terrs: any[] = []
2121
+ const terrs: any[] = []
2147
2122
 
2148
2123
  const vstore = merge([{}, store], 1)
2149
2124
  vstore.$TOP = inj.dparent
@@ -2163,27 +2138,38 @@ const validate_ONE: Injector = (
2163
2138
  }
2164
2139
 
2165
2140
  // There was no match.
2166
- const valdesc =
2167
- replace(join(items(tvals, (n) => stringify(n[1])), ', '),
2168
- R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
2141
+ const valdesc = replace(
2142
+ join(
2143
+ items(tvals, (n) => stringify(n[1])),
2144
+ ', ',
2145
+ ),
2146
+ R_TRANSFORM_NAME,
2147
+ (_m: any, p1: string) => p1.toLowerCase(),
2148
+ )
2169
2149
 
2170
- inj.errs.push(_invalidTypeMsg(
2171
- inj.path,
2172
- (1 < size(tvals) ? 'one of ' : '') + valdesc,
2173
- typify(inj.dparent), inj.dparent, 'V0210'))
2150
+ inj.errs.push(
2151
+ _invalidTypeMsg(
2152
+ inj.path,
2153
+ (1 < size(tvals) ? 'one of ' : '') + valdesc,
2154
+ typify(inj.dparent),
2155
+ inj.dparent,
2156
+ 'V0210',
2157
+ ),
2158
+ )
2174
2159
  }
2175
2160
  }
2176
2161
 
2177
-
2178
2162
  const validate_EXACT: Injector = (inj: Injection) => {
2179
2163
  const { mode, parent, key, keyI } = inj
2180
2164
 
2181
2165
  // Only operate in val mode, since parent is a list.
2182
2166
  if (M_VAL === mode) {
2183
2167
  if (!islist(parent) || 0 !== keyI) {
2184
- inj.errs.push('The $EXACT validator at field ' +
2185
- pathify(inj.path, 1, 1) +
2186
- ' must be the first element of an array.')
2168
+ inj.errs.push(
2169
+ 'The $EXACT validator at field ' +
2170
+ pathify(inj.path, 1, 1) +
2171
+ ' must be the first element of an array.',
2172
+ )
2187
2173
  return
2188
2174
  }
2189
2175
 
@@ -2196,17 +2182,19 @@ const validate_EXACT: Injector = (inj: Injection) => {
2196
2182
  inj.path = slice(inj.path, 0, -1)
2197
2183
  inj.key = getelem(inj.path, -1)
2198
2184
 
2199
- let tvals = slice(parent, 1)
2185
+ const tvals = slice(parent, 1)
2200
2186
  if (0 === size(tvals)) {
2201
- inj.errs.push('The $EXACT validator at field ' +
2202
- pathify(inj.path, 1, 1) +
2203
- ' must have at least one argument.')
2187
+ inj.errs.push(
2188
+ 'The $EXACT validator at field ' +
2189
+ pathify(inj.path, 1, 1) +
2190
+ ' must have at least one argument.',
2191
+ )
2204
2192
  return
2205
2193
  }
2206
2194
 
2207
2195
  // See if we can find an exact value match.
2208
2196
  let currentstr: string | undefined = undefined
2209
- for (let tval of tvals) {
2197
+ for (const tval of tvals) {
2210
2198
  let exactmatch = tval === inj.dparent
2211
2199
 
2212
2200
  if (!exactmatch && isnode(tval)) {
@@ -2221,31 +2209,35 @@ const validate_EXACT: Injector = (inj: Injection) => {
2221
2209
  }
2222
2210
 
2223
2211
  // There was no match.
2224
- const valdesc =
2225
- replace(join(items(tvals, (n) => stringify(n[1])), ', '),
2226
- R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
2212
+ const valdesc = replace(
2213
+ join(
2214
+ items(tvals, (n) => stringify(n[1])),
2215
+ ', ',
2216
+ ),
2217
+ R_TRANSFORM_NAME,
2218
+ (_m: any, p1: string) => p1.toLowerCase(),
2219
+ )
2227
2220
 
2228
- inj.errs.push(_invalidTypeMsg(
2229
- inj.path,
2230
- (1 < size(inj.path) ? '' : 'value ') +
2231
- 'exactly equal to ' + (1 === size(tvals) ? '' : 'one of ') + valdesc,
2232
- typify(inj.dparent), inj.dparent, 'V0110'))
2233
- }
2234
- else {
2221
+ inj.errs.push(
2222
+ _invalidTypeMsg(
2223
+ inj.path,
2224
+ (1 < size(inj.path) ? '' : 'value ') +
2225
+ 'exactly equal to ' +
2226
+ (1 === size(tvals) ? '' : 'one of ') +
2227
+ valdesc,
2228
+ typify(inj.dparent),
2229
+ inj.dparent,
2230
+ 'V0110',
2231
+ ),
2232
+ )
2233
+ } else {
2235
2234
  delprop(parent, key)
2236
2235
  }
2237
2236
  }
2238
2237
 
2239
-
2240
2238
  // This is the "modify" argument to inject. Use this to perform
2241
2239
  // generic validation. Runs *after* any special commands.
2242
- const _validation: Modify = (
2243
- pval: any,
2244
- key?: any,
2245
- parent?: any,
2246
- inj?: Injection,
2247
- ) => {
2248
-
2240
+ const _validation: Modify = (pval: any, key?: any, parent?: any, inj?: Injection) => {
2249
2241
  if (NONE === inj) {
2250
2242
  return
2251
2243
  }
@@ -2291,40 +2283,37 @@ const _validation: Modify = (
2291
2283
  // Empty spec object {} means object can be open (any keys).
2292
2284
  if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
2293
2285
  const badkeys = []
2294
- for (let ckey of ckeys) {
2295
- if (!haskey(pval, ckey)) {
2286
+ for (const ckey of ckeys) {
2287
+ // Literal presence: _validation needs to know if the SHAPE declares
2288
+ // this key, regardless of whether the validator stored null in that
2289
+ // slot. The Group A haskey would miss null-valued slots.
2290
+ if (NONE === _lookup(pval, ckey)) {
2296
2291
  badkeys.push(ckey)
2297
2292
  }
2298
2293
  }
2299
2294
 
2300
2295
  // Closed object, so reject extra keys not in shape.
2301
2296
  if (0 < size(badkeys)) {
2302
- const msg =
2303
- 'Unexpected keys at field ' + pathify(inj.path, 1) + S_VIZ + join(badkeys, ', ')
2297
+ const msg = 'Unexpected keys at field ' + pathify(inj.path, 1) + S_VIZ + join(badkeys, ', ')
2304
2298
  inj.errs.push(msg)
2305
2299
  }
2306
- }
2307
- else {
2300
+ } else {
2308
2301
  // Object is open, so merge in extra keys.
2309
2302
  merge([pval, cval])
2310
2303
  if (isnode(pval)) {
2311
2304
  delprop(pval, '`$OPEN`')
2312
2305
  }
2313
2306
  }
2314
- }
2315
- else if (islist(cval)) {
2307
+ } else if (islist(cval)) {
2316
2308
  if (!islist(pval)) {
2317
2309
  inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0030'))
2318
2310
  }
2319
- }
2320
- else if (exact) {
2311
+ } else if (exact) {
2321
2312
  if (cval !== pval) {
2322
2313
  const pathmsg = 1 < size(inj.path) ? 'at field ' + pathify(inj.path, 1) + S_VIZ : S_MT
2323
- inj.errs.push('Value ' + pathmsg + cval +
2324
- ' should equal ' + pval + S_DT)
2314
+ inj.errs.push('Value ' + pathmsg + cval + ' should equal ' + pval + S_DT)
2325
2315
  }
2326
- }
2327
- else {
2316
+ } else {
2328
2317
  // Spec value was a default, copy over data
2329
2318
  setprop(parent, key, cval)
2330
2319
  }
@@ -2332,8 +2321,6 @@ const _validation: Modify = (
2332
2321
  return
2333
2322
  }
2334
2323
 
2335
-
2336
-
2337
2324
  // Validate a data structure against a shape specification. The shape
2338
2325
  // specification follows the "by example" principle. Plain data in
2339
2326
  // teh shape is treated as default values that also specify the
@@ -2347,51 +2334,54 @@ const _validation: Modify = (
2347
2334
  function validate(
2348
2335
  data: any, // Source data to transform into new data (original not mutated)
2349
2336
  spec: any, // Transform specification; output follows this shape
2350
- injdef?: Partial<Injection>
2337
+ injdef?: Partial<Injection>,
2351
2338
  ) {
2352
2339
  const extra = injdef?.extra
2353
2340
 
2354
2341
  const collect = null != injdef?.errs
2355
2342
  const errs = injdef?.errs || []
2356
2343
 
2357
- const store = merge([
2358
- {
2359
- // Remove the transform commands.
2360
- $DELETE: null,
2361
- $COPY: null,
2362
- $KEY: null,
2363
- $META: null,
2364
- $MERGE: null,
2365
- $EACH: null,
2366
- $PACK: null,
2367
-
2368
- $STRING: validate_STRING,
2369
- $NUMBER: validate_TYPE,
2370
- $INTEGER: validate_TYPE,
2371
- $DECIMAL: validate_TYPE,
2372
- $BOOLEAN: validate_TYPE,
2373
- $NULL: validate_TYPE,
2374
- $NIL: validate_TYPE,
2375
- $MAP: validate_TYPE,
2376
- $LIST: validate_TYPE,
2377
- $FUNCTION: validate_TYPE,
2378
- $INSTANCE: validate_TYPE,
2379
- $ANY: validate_ANY,
2380
- $CHILD: validate_CHILD,
2381
- $ONE: validate_ONE,
2382
- $EXACT: validate_EXACT,
2383
- },
2384
-
2385
- getdef(extra, {}),
2386
-
2387
- // A special top level value to collect errors.
2388
- // NOTE: collecterrs parameter always wins.
2389
- {
2390
- $ERRS: errs,
2391
- }
2392
- ], 1)
2393
-
2394
- let meta = getprop(injdef, 'meta', {})
2344
+ const store = merge(
2345
+ [
2346
+ {
2347
+ // Remove the transform commands.
2348
+ $DELETE: null,
2349
+ $COPY: null,
2350
+ $KEY: null,
2351
+ $META: null,
2352
+ $MERGE: null,
2353
+ $EACH: null,
2354
+ $PACK: null,
2355
+
2356
+ $STRING: validate_STRING,
2357
+ $NUMBER: validate_TYPE,
2358
+ $INTEGER: validate_TYPE,
2359
+ $DECIMAL: validate_TYPE,
2360
+ $BOOLEAN: validate_TYPE,
2361
+ $NULL: validate_TYPE,
2362
+ $NIL: validate_TYPE,
2363
+ $MAP: validate_TYPE,
2364
+ $LIST: validate_TYPE,
2365
+ $FUNCTION: validate_TYPE,
2366
+ $INSTANCE: validate_TYPE,
2367
+ $ANY: validate_ANY,
2368
+ $CHILD: validate_CHILD,
2369
+ $ONE: validate_ONE,
2370
+ $EXACT: validate_EXACT,
2371
+ },
2372
+
2373
+ getdef(extra, {}),
2374
+
2375
+ // A special top level value to collect errors.
2376
+ // NOTE: collecterrs parameter always wins.
2377
+ {
2378
+ $ERRS: errs,
2379
+ },
2380
+ ],
2381
+ 1,
2382
+ )
2383
+
2384
+ const meta = getprop(injdef, 'meta', {})
2395
2385
  setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false))
2396
2386
 
2397
2387
  const out = transform(data, spec, {
@@ -2402,7 +2392,7 @@ function validate(
2402
2392
  errs,
2403
2393
  })
2404
2394
 
2405
- const generr = (0 < size(errs) && !collect)
2395
+ const generr = 0 < size(errs) && !collect
2406
2396
  if (generr) {
2407
2397
  throw new Error(join(errs, ' | '))
2408
2398
  }
@@ -2410,10 +2400,9 @@ function validate(
2410
2400
  return out
2411
2401
  }
2412
2402
 
2413
-
2414
2403
  const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2415
2404
  if (M_KEYPRE === inj.mode) {
2416
- const terms = getprop(inj.parent, inj.key)
2405
+ const terms = _lookup(inj.parent, inj.key)
2417
2406
 
2418
2407
  const ppath = slice(inj.path, -1)
2419
2408
  const point = getpath(store, ppath)
@@ -2421,8 +2410,8 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2421
2410
  const vstore = merge([{}, store], 1)
2422
2411
  vstore.$TOP = point
2423
2412
 
2424
- for (let term of terms) {
2425
- let terrs: any[] = []
2413
+ for (const term of terms) {
2414
+ const terrs: any[] = []
2426
2415
 
2427
2416
  validate(point, term, {
2428
2417
  extra: vstore,
@@ -2432,7 +2421,8 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2432
2421
 
2433
2422
  if (0 != size(terrs)) {
2434
2423
  inj.errs.push(
2435
- 'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2424
+ 'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms),
2425
+ )
2436
2426
  }
2437
2427
  }
2438
2428
 
@@ -2442,10 +2432,9 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2442
2432
  }
2443
2433
  }
2444
2434
 
2445
-
2446
2435
  const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2447
2436
  if (M_KEYPRE === inj.mode) {
2448
- const terms = getprop(inj.parent, inj.key)
2437
+ const terms = _lookup(inj.parent, inj.key)
2449
2438
 
2450
2439
  const ppath = slice(inj.path, -1)
2451
2440
  const point = getpath(store, ppath)
@@ -2453,8 +2442,8 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
2453
2442
  const vstore = merge([{}, store], 1)
2454
2443
  vstore.$TOP = point
2455
2444
 
2456
- for (let term of terms) {
2457
- let terrs: any[] = []
2445
+ for (const term of terms) {
2446
+ const terrs: any[] = []
2458
2447
 
2459
2448
  validate(point, term, {
2460
2449
  extra: vstore,
@@ -2471,15 +2460,13 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
2471
2460
  }
2472
2461
  }
2473
2462
 
2474
- inj.errs.push(
2475
- 'OR:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2463
+ inj.errs.push('OR:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2476
2464
  }
2477
2465
  }
2478
2466
 
2479
-
2480
2467
  const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2481
2468
  if (M_KEYPRE === inj.mode) {
2482
- const term = getprop(inj.parent, inj.key)
2469
+ const term = _lookup(inj.parent, inj.key)
2483
2470
 
2484
2471
  const ppath = slice(inj.path, -1)
2485
2472
  const point = getpath(store, ppath)
@@ -2487,7 +2474,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2487
2474
  const vstore = merge([{}, store], 1)
2488
2475
  vstore.$TOP = point
2489
2476
 
2490
- let terrs: any[] = []
2477
+ const terrs: any[] = []
2491
2478
 
2492
2479
  validate(point, term, {
2493
2480
  extra: vstore,
@@ -2496,8 +2483,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2496
2483
  })
2497
2484
 
2498
2485
  if (0 == size(terrs)) {
2499
- inj.errs.push(
2500
- 'NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
2486
+ inj.errs.push('NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
2501
2487
  }
2502
2488
 
2503
2489
  const gkey = getelem(inj.path, -2)
@@ -2506,10 +2492,9 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2506
2492
  }
2507
2493
  }
2508
2494
 
2509
-
2510
2495
  const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
2511
2496
  if (M_KEYPRE === inj.mode) {
2512
- const term = getprop(inj.parent, inj.key)
2497
+ const term = _lookup(inj.parent, inj.key)
2513
2498
  // const src = getprop(store, inj.base, store)
2514
2499
  const gkey = getelem(inj.path, -2)
2515
2500
 
@@ -2522,17 +2507,13 @@ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any
2522
2507
 
2523
2508
  if ('$GT' === ref && point > term) {
2524
2509
  pass = true
2525
- }
2526
- else if ('$LT' === ref && point < term) {
2510
+ } else if ('$LT' === ref && point < term) {
2527
2511
  pass = true
2528
- }
2529
- else if ('$GTE' === ref && point >= term) {
2512
+ } else if ('$GTE' === ref && point >= term) {
2530
2513
  pass = true
2531
- }
2532
- else if ('$LTE' === ref && point <= term) {
2514
+ } else if ('$LTE' === ref && point <= term) {
2533
2515
  pass = true
2534
- }
2535
- else if ('$LIKE' === ref && stringify(point).match(RegExp(term))) {
2516
+ } else if ('$LIKE' === ref && re_test(re_compile(term), stringify(point))) {
2536
2517
  pass = true
2537
2518
  }
2538
2519
 
@@ -2540,17 +2521,23 @@ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any
2540
2521
  // Update spec to match found value so that _validate does not complain.
2541
2522
  const gp = getelem(inj.nodes, -2)
2542
2523
  setprop(gp, gkey, point)
2543
- }
2544
- else {
2545
- inj.errs.push('CMP: ' + pathify(ppath) + S_VIZ + stringify(point) +
2546
- ' fail:' + ref + ' ' + stringify(term))
2524
+ } else {
2525
+ inj.errs.push(
2526
+ 'CMP: ' +
2527
+ pathify(ppath) +
2528
+ S_VIZ +
2529
+ stringify(point) +
2530
+ ' fail:' +
2531
+ ref +
2532
+ ' ' +
2533
+ stringify(term),
2534
+ )
2547
2535
  }
2548
2536
  }
2549
2537
 
2550
2538
  return NONE
2551
2539
  }
2552
2540
 
2553
-
2554
2541
  // Select children from a top-level object that match a MongoDB-style query.
2555
2542
  // Supports $and, $or, and equality comparisons.
2556
2543
  // For arrays, children are elements; for objects, children are values.
@@ -2561,12 +2548,11 @@ function select(children: any, query: any): any[] {
2561
2548
  }
2562
2549
 
2563
2550
  if (ismap(children)) {
2564
- children = items(children, n => {
2551
+ children = items(children, (n) => {
2565
2552
  setprop(n[1], S_DKEY, n[0])
2566
2553
  return n[1]
2567
2554
  })
2568
- }
2569
- else {
2555
+ } else {
2570
2556
  children = items(children, (n) => (setprop(n[1], S_DKEY, +n[0]), n[1]))
2571
2557
  }
2572
2558
 
@@ -2583,7 +2569,7 @@ function select(children: any, query: any): any[] {
2583
2569
  $GTE: select_CMP,
2584
2570
  $LTE: select_CMP,
2585
2571
  $LIKE: select_CMP,
2586
- }
2572
+ },
2587
2573
  }
2588
2574
 
2589
2575
  const q = clone(query)
@@ -2608,26 +2594,25 @@ function select(children: any, query: any): any[] {
2608
2594
  return results
2609
2595
  }
2610
2596
 
2611
-
2612
2597
  // Injection state used for recursive injection into JSON - like data structures.
2613
2598
  class Injection {
2614
- mode: InjectMode // Injection mode: M_KEYPRE, M_VAL, M_KEYPOST.
2615
- full: boolean // Transform escape was full key name.
2616
- keyI: number // Index of parent key in list of parent keys.
2617
- keys: string[] // List of parent keys.
2618
- key: string // Current parent key.
2619
- val: any // Current child value.
2620
- parent: any // Current parent (in transform specification).
2621
- path: string[] // Path to current node.
2622
- nodes: any[] // Stack of ancestor nodes.
2623
- handler: Injector // Custom handler for injections.
2624
- errs: any[] // Error collector.
2599
+ mode: InjectMode // Injection mode: M_KEYPRE, M_VAL, M_KEYPOST.
2600
+ full: boolean // Transform escape was full key name.
2601
+ keyI: number // Index of parent key in list of parent keys.
2602
+ keys: string[] // List of parent keys.
2603
+ key: string // Current parent key.
2604
+ val: any // Current child value.
2605
+ parent: any // Current parent (in transform specification).
2606
+ path: string[] // Path to current node.
2607
+ nodes: any[] // Stack of ancestor nodes.
2608
+ handler: Injector // Custom handler for injections.
2609
+ errs: any[] // Error collector.
2625
2610
  meta: Record<string, any> // Custom meta data. NOTE: do not merge, values must remain as-is.
2626
- dparent: any // Current data parent node (contains current data value).
2627
- dpath: string[] // Current data value path
2628
- base?: string // Base key for data in store, if any.
2629
- modify?: Modify // Modify injection output.
2630
- prior?: Injection // Parent (aka prior) injection.
2611
+ dparent: any // Current data parent node (contains current data value).
2612
+ dpath: string[] // Current data value path
2613
+ base?: string // Base key for data in store, if any.
2614
+ modify?: Modify // Modify injection output.
2615
+ prior?: Injection // Parent (aka prior) injection.
2631
2616
  extra?: any
2632
2617
 
2633
2618
  constructor(val: any, parent: any) {
@@ -2650,42 +2635,56 @@ class Injection {
2650
2635
  this.meta = {}
2651
2636
  }
2652
2637
 
2653
-
2654
2638
  toString(prefix?: string) {
2655
- return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
2639
+ return (
2640
+ 'INJ' +
2641
+ (null == prefix ? '' : S_FS + prefix) +
2642
+ S_CN +
2656
2643
  pad(pathify(this.path, 1)) +
2657
- MODENAME[this.mode] + (this.full ? '/full' : '') + S_CN +
2658
- 'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
2659
- ' p=' + stringify(this.parent, -1, 1) +
2660
- ' m=' + stringify(this.meta, -1, 1) +
2661
- ' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
2662
- ' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1)
2644
+ MODENAME[this.mode] +
2645
+ (this.full ? '/full' : '') +
2646
+ S_CN +
2647
+ 'key=' +
2648
+ this.keyI +
2649
+ S_FS +
2650
+ this.key +
2651
+ S_FS +
2652
+ S_OS +
2653
+ this.keys +
2654
+ S_CS +
2655
+ ' p=' +
2656
+ stringify(this.parent, -1, 1) +
2657
+ ' m=' +
2658
+ stringify(this.meta, -1, 1) +
2659
+ ' d/' +
2660
+ pathify(this.dpath, 1) +
2661
+ '=' +
2662
+ stringify(this.dparent, -1, 1) +
2663
+ ' r=' +
2664
+ stringify(this.nodes[0]?.[S_DTOP], -1, 1)
2665
+ )
2663
2666
  }
2664
2667
 
2665
-
2666
2668
  descend() {
2667
2669
  this.meta.__d++
2668
2670
  const parentkey = getelem(this.path, -2)
2669
2671
 
2670
2672
  // Resolve current node in store for local paths.
2671
2673
  if (NONE === this.dparent) {
2672
-
2673
2674
  // Even if there's no data, dpath should continue to match path, so that
2674
2675
  // relative paths work properly.
2675
2676
  if (1 < size(this.dpath)) {
2676
2677
  this.dpath = flatten([this.dpath, parentkey])
2677
2678
  }
2678
- }
2679
- else {
2679
+ } else {
2680
2680
  // this.dparent is the containing node of the current store value.
2681
2681
  if (null != parentkey) {
2682
2682
  this.dparent = getprop(this.dparent, parentkey)
2683
2683
 
2684
- let lastpart = getelem(this.dpath, -1)
2684
+ const lastpart = getelem(this.dpath, -1)
2685
2685
  if (lastpart === '$:' + parentkey) {
2686
2686
  this.dpath = slice(this.dpath, -1)
2687
- }
2688
- else {
2687
+ } else {
2689
2688
  this.dpath = flatten([this.dpath, parentkey])
2690
2689
  }
2691
2690
  }
@@ -2695,7 +2694,6 @@ class Injection {
2695
2694
  return this.dparent
2696
2695
  }
2697
2696
 
2698
-
2699
2697
  child(keyI: number, keys: string[]) {
2700
2698
  const key = strkey(keys[keyI])
2701
2699
  const val = this.val
@@ -2722,20 +2720,17 @@ class Injection {
2722
2720
  return cinj
2723
2721
  }
2724
2722
 
2725
-
2726
2723
  setval(val: any, ancestor?: number) {
2727
2724
  let parent = NONE
2728
2725
  if (null == ancestor || ancestor < 2) {
2729
- parent = NONE === val ?
2730
- this.parent = delprop(this.parent, this.key) :
2731
- setprop(this.parent, this.key, val)
2732
- }
2733
- else {
2726
+ parent =
2727
+ NONE === val
2728
+ ? (this.parent = delprop(this.parent, this.key))
2729
+ : setprop(this.parent, this.key, val)
2730
+ } else {
2734
2731
  const aval = getelem(this.nodes, 0 - ancestor)
2735
2732
  const akey = getelem(this.path, 0 - ancestor)
2736
- parent = NONE === val ?
2737
- delprop(aval, akey) :
2738
- setprop(aval, akey, val)
2733
+ parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val)
2739
2734
  }
2740
2735
 
2741
2736
  // console.log('SETVAL', val, this.key, this.parent)
@@ -2743,42 +2738,36 @@ class Injection {
2743
2738
  }
2744
2739
  }
2745
2740
 
2746
-
2747
2741
  // Internal utilities
2748
2742
  // ==================
2749
2743
 
2750
-
2751
2744
  // // Update all references to target in inj.nodes.
2752
2745
  // function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
2753
2746
  // // SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
2754
2747
  // setprop(target, tkey, tval)
2755
2748
  // }
2756
2749
 
2757
-
2758
2750
  // Build a type validation error message.
2759
2751
  function _invalidTypeMsg(path: any, needtype: string, vt: number, v: any, _whence?: string) {
2760
- let vs = null == v ? 'no value' : stringify(v)
2761
-
2762
- return 'Expected ' +
2763
- (1 < size(path) ? ('field ' + pathify(path, 1) + ' to be ') : '') +
2764
- needtype + ', but found ' +
2765
- (null != v ? typename(vt) + S_VIZ : '') + vs +
2766
-
2752
+ const vs = null == v ? 'no value' : stringify(v)
2753
+
2754
+ return (
2755
+ 'Expected ' +
2756
+ (1 < size(path) ? 'field ' + pathify(path, 1) + ' to be ' : '') +
2757
+ needtype +
2758
+ ', but found ' +
2759
+ (null != v ? typename(vt) + S_VIZ : '') +
2760
+ vs +
2767
2761
  // Uncomment to help debug validation errors.
2768
2762
  // ' [' + _whence + ']' +
2769
2763
 
2770
2764
  '.'
2765
+ )
2771
2766
  }
2772
2767
 
2773
-
2774
2768
  // Default inject handler for transforms. If the path resolves to a function,
2775
2769
  // call the function passing the injection inj. This is how transforms operate.
2776
- const _injecthandler: Injector = (
2777
- inj: Injection,
2778
- val: any,
2779
- ref: string,
2780
- store: any
2781
- ): any => {
2770
+ const _injecthandler: Injector = (inj: Injection, val: any, ref: string, store: any): any => {
2782
2771
  let out = val
2783
2772
  const iscmd = isfunc(val) && (NONE === ref || ref.startsWith(S_DS))
2784
2773
 
@@ -2797,37 +2786,28 @@ const _injecthandler: Injector = (
2797
2786
  return out
2798
2787
  }
2799
2788
 
2800
-
2801
- const _validatehandler: Injector = (
2802
- inj: Injection,
2803
- val: any,
2804
- ref: string,
2805
- store: any
2806
- ): any => {
2789
+ const _validatehandler: Injector = (inj: Injection, val: any, ref: string, store: any): any => {
2807
2790
  let out = val
2808
2791
 
2809
- const m = ref.match(R_META_PATH)
2792
+ const m = re_find(R_META_PATH, ref)
2810
2793
  const ismetapath = null != m
2811
2794
 
2812
2795
  if (ismetapath) {
2813
2796
  if ('=' === m[2]) {
2814
2797
  inj.setval([S_BEXACT, val])
2815
- }
2816
- else {
2798
+ } else {
2817
2799
  inj.setval(val)
2818
2800
  }
2819
2801
  inj.keyI = -1
2820
2802
 
2821
2803
  out = SKIP
2822
- }
2823
- else {
2804
+ } else {
2824
2805
  out = _injecthandler(inj, val, ref, store)
2825
2806
  }
2826
2807
 
2827
2808
  return out
2828
2809
  }
2829
2810
 
2830
-
2831
2811
  // Inject values from a data store into a string. Not a public utility - used by
2832
2812
  // `inject`. Inject are marked with `path` where path is resolved
2833
2813
  // with getpath against the store or current (if defined)
@@ -2837,11 +2817,7 @@ const _validatehandler: Injector = (
2837
2817
  // upper case letters only, and 999 is any digits, which are
2838
2818
  // discarded. This syntax specifies the name of a transform, and
2839
2819
  // optionally allows transforms to be ordered by alphanumeric sorting.
2840
- function _injectstr(
2841
- val: string,
2842
- store: any,
2843
- inj?: Injection
2844
- ): any {
2820
+ function _injectstr(val: string, store: any, inj?: Injection): any {
2845
2821
  // Can't inject into non-strings
2846
2822
  if (S_string !== typeof val || S_MT === val) {
2847
2823
  return S_MT
@@ -2850,7 +2826,7 @@ function _injectstr(
2850
2826
  let out: any = val
2851
2827
 
2852
2828
  // Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
2853
- const m = val.match(R_INJECTION_FULL)
2829
+ const m = re_find(R_INJECTION_FULL, val)
2854
2830
 
2855
2831
  // Full string of the val is an injection.
2856
2832
  if (m) {
@@ -2861,20 +2837,18 @@ function _injectstr(
2861
2837
 
2862
2838
  // Special escapes inside injection.
2863
2839
  if (3 < size(pathref)) {
2864
- pathref = pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
2840
+ pathref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, pathref, S_BT), S_DS)
2865
2841
  }
2866
2842
 
2867
2843
  // Get the extracted path reference.
2868
2844
  out = getpath(store, pathref, inj)
2869
- }
2870
-
2871
- else {
2845
+ } else {
2872
2846
  // Check for injections within the string.
2873
2847
  const partial = (_m: string, ref: string) => {
2874
2848
  // Special escapes inside injection.
2875
2849
 
2876
2850
  if (3 < size(ref)) {
2877
- ref = ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS)
2851
+ ref = re_replace(R_DS_ESCAPE, re_replace(R_BT_ESCAPE, ref, S_BT), S_DS)
2878
2852
  }
2879
2853
 
2880
2854
  if (inj) {
@@ -2887,7 +2861,7 @@ function _injectstr(
2887
2861
  return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
2888
2862
  }
2889
2863
 
2890
- out = val.replace(R_INJECTION_PARTIAL, partial)
2864
+ out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]))
2891
2865
 
2892
2866
  // Also call the inj handler on the entire string, providing the
2893
2867
  // option for custom injection.
@@ -2900,11 +2874,9 @@ function _injectstr(
2900
2874
  return out
2901
2875
  }
2902
2876
 
2903
-
2904
2877
  // Handler Utilities
2905
2878
  // =================
2906
2879
 
2907
-
2908
2880
  const MODENAME: any = {
2909
2881
  [M_VAL]: 'val',
2910
2882
  [M_KEYPRE]: 'key:pre',
@@ -2921,28 +2893,44 @@ function checkPlacement(
2921
2893
  modes: InjectMode,
2922
2894
  ijname: string,
2923
2895
  parentTypes: number,
2924
- inj: Injection
2896
+ inj: Injection,
2925
2897
  ): boolean {
2926
2898
  if (0 === (modes & inj.mode)) {
2927
- inj.errs.push('$' + ijname + ': invalid placement as ' + PLACEMENT[inj.mode] +
2928
- ', expected: ' + join(items(
2929
- [M_KEYPRE, M_KEYPOST, M_VAL].filter(m => modes & m),
2930
- (n: any) => PLACEMENT[n[1]]), ',') + '.')
2899
+ inj.errs.push(
2900
+ '$' +
2901
+ ijname +
2902
+ ': invalid placement as ' +
2903
+ PLACEMENT[inj.mode] +
2904
+ ', expected: ' +
2905
+ join(
2906
+ items(
2907
+ [M_KEYPRE, M_KEYPOST, M_VAL].filter((m) => modes & m),
2908
+ (n: any) => PLACEMENT[n[1]],
2909
+ ),
2910
+ ',',
2911
+ ) +
2912
+ '.',
2913
+ )
2931
2914
  return false
2932
2915
  }
2933
2916
  if (!isempty(parentTypes)) {
2934
2917
  const ptype = typify(inj.parent)
2935
2918
  if (0 === (parentTypes & ptype)) {
2936
- inj.errs.push('$' + ijname + ': invalid placement in parent ' + typename(ptype) +
2937
- ', expected: ' + typename(parentTypes) + '.')
2919
+ inj.errs.push(
2920
+ '$' +
2921
+ ijname +
2922
+ ': invalid placement in parent ' +
2923
+ typename(ptype) +
2924
+ ', expected: ' +
2925
+ typename(parentTypes) +
2926
+ '.',
2927
+ )
2938
2928
  return false
2939
-
2940
2929
  }
2941
2930
  }
2942
2931
  return true
2943
2932
  }
2944
2933
 
2945
-
2946
2934
  // function injectorArgs(argTypes: number[], inj: Injection): any {
2947
2935
  function injectorArgs(argTypes: number[], args: any[]): any {
2948
2936
  const numargs = size(argTypes)
@@ -2953,9 +2941,16 @@ function injectorArgs(argTypes: number[], args: any[]): any {
2953
2941
  const arg = args[argI]
2954
2942
  const argType = typify(arg)
2955
2943
  if (0 === (argTypes[argI] & argType)) {
2956
- found[0] = 'invalid argument: ' + stringify(arg, 22) +
2957
- ' (' + typename(argType) + ' at position ' + (1 + argI) +
2958
- ') is not of type: ' + typename(argTypes[argI]) + '.'
2944
+ found[0] =
2945
+ 'invalid argument: ' +
2946
+ stringify(arg, 22) +
2947
+ ' (' +
2948
+ typename(argType) +
2949
+ ' at position ' +
2950
+ (1 + argI) +
2951
+ ') is not of type: ' +
2952
+ typename(argTypes[argI]) +
2953
+ '.'
2959
2954
  break
2960
2955
  }
2961
2956
  found[1 + argI] = arg
@@ -2963,7 +2958,6 @@ function injectorArgs(argTypes: number[], args: any[]): any {
2963
2958
  return found
2964
2959
  }
2965
2960
 
2966
-
2967
2961
  function injectChild(child: any, store: any, inj: Injection): Injection {
2968
2962
  let cinj = inj
2969
2963
 
@@ -2973,8 +2967,7 @@ function injectChild(child: any, store: any, inj: Injection): Injection {
2973
2967
  cinj = inj.prior.prior.child(inj.prior.keyI, inj.prior.keys)
2974
2968
  cinj.val = child
2975
2969
  setprop(cinj.parent, inj.prior.key, child)
2976
- }
2977
- else {
2970
+ } else {
2978
2971
  cinj = inj.prior.child(inj.keyI, inj.keys)
2979
2972
  cinj.val = child
2980
2973
  setprop(cinj.parent, inj.key, child)
@@ -2987,7 +2980,6 @@ function injectChild(child: any, store: any, inj: Injection): Injection {
2987
2980
  return cinj
2988
2981
  }
2989
2982
 
2990
-
2991
2983
  class StructUtility {
2992
2984
  clone = clone
2993
2985
  delprop = delprop
@@ -3094,13 +3086,16 @@ export {
3094
3086
  typename,
3095
3087
  validate,
3096
3088
  walk,
3097
-
3089
+ re_compile,
3090
+ re_find,
3091
+ re_find_all,
3092
+ re_replace,
3093
+ re_test,
3094
+ re_escape,
3098
3095
  SKIP,
3099
3096
  DELETE,
3100
-
3101
3097
  jm,
3102
3098
  jt,
3103
-
3104
3099
  T_any,
3105
3100
  T_noval,
3106
3101
  T_boolean,
@@ -3116,20 +3111,13 @@ export {
3116
3111
  T_instance,
3117
3112
  T_scalar,
3118
3113
  T_node,
3119
-
3120
3114
  M_KEYPRE,
3121
3115
  M_KEYPOST,
3122
3116
  M_VAL,
3123
-
3124
3117
  MODENAME,
3125
-
3126
3118
  checkPlacement,
3127
3119
  injectorArgs,
3128
3120
  injectChild,
3129
3121
  }
3130
3122
 
3131
- export type {
3132
- Injection,
3133
- Injector,
3134
- WalkApply
3135
- }
3123
+ export type { Injection, Injector, WalkApply }