@voxgig/struct 0.0.10 → 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.0.10
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,38 +193,40 @@ 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.
210
212
  // For {a:{b:1}} the call sequence args will be: b, 1, {b:1}, [a,b].
213
+ // NOTE: the `path` array passed to the callback is reused across calls during
214
+ // a single walk (one shared array per depth). It is valid only for the
215
+ // duration of the callback invocation. If a callback needs to retain the
216
+ // path, it MUST clone it (e.g. `path.slice()`).
211
217
  type WalkApply = (
212
- // Map keys are strings, list keys are numbers, top key is NONE
218
+ // Map keys are strings, list keys are numbers, top key is NONE
213
219
  key: string | number | undefined,
214
220
  val: any,
215
221
  parent: any,
216
- path: string[]
222
+ path: string[],
217
223
  ) => any
218
224
 
219
-
220
225
  // Return type string for narrowest type.
221
226
  function typename(t: number) {
222
227
  return getelem(TYPENAME, Math.clz32(t), TYPENAME[0])
223
228
  }
224
229
 
225
-
226
230
  // Get a defined value. Returns alt if val is undefined.
227
231
  function getdef(val: any, alt: any) {
228
232
  if (NONE === val) {
@@ -231,7 +235,6 @@ function getdef(val: any, alt: any) {
231
235
  return val
232
236
  }
233
237
 
234
-
235
238
  // Value is a node - defined, and a map (hash) or list (array).
236
239
  // NOTE: typescript
237
240
  // things
@@ -239,47 +242,43 @@ function isnode(val: any): val is Indexable {
239
242
  return null != val && S_object == typeof val
240
243
  }
241
244
 
242
-
243
245
  // Value is a defined map (hash) with string keys.
244
246
  function ismap(val: any): val is { [key: string]: any } {
245
247
  return null != val && S_object == typeof val && !Array.isArray(val)
246
248
  }
247
249
 
248
-
249
250
  // Value is a defined list (array) with integer keys (indexes).
250
251
  function islist(val: any): val is any[] {
251
252
  return Array.isArray(val)
252
253
  }
253
254
 
254
-
255
255
  // Value is a defined string (non-empty) or integer key.
256
256
  function iskey(key: any): key is PropKey {
257
257
  const keytype = typeof key
258
258
  return (S_string === keytype && S_MT !== key) || S_number === keytype
259
259
  }
260
260
 
261
-
262
261
  // Check for an "empty" value - undefined, empty string, array, object.
263
262
  function isempty(val: any) {
264
- return null == val || S_MT === val ||
263
+ return (
264
+ null == val ||
265
+ S_MT === val ||
265
266
  (Array.isArray(val) && 0 === val.length) ||
266
267
  (S_object === typeof val && 0 === Object.keys(val).length)
268
+ )
267
269
  }
268
270
 
269
-
270
271
  // Value is a function.
271
- function isfunc(val: any): val is Function {
272
+ function isfunc(val: any): val is (...args: any[]) => any {
272
273
  return S_function === typeof val
273
274
  }
274
275
 
275
-
276
276
  // The integer size of the value. For arrays and strings, the length,
277
277
  // for numbers, the integer part, for boolean, true is 1 and falso 0, for all other values, 0.
278
278
  function size(val: any): number {
279
279
  if (islist(val)) {
280
280
  return val.length
281
- }
282
- else if (ismap(val)) {
281
+ } else if (ismap(val)) {
283
282
  return Object.keys(val).length
284
283
  }
285
284
 
@@ -287,19 +286,15 @@ function size(val: any): number {
287
286
 
288
287
  if (S_string == valtype) {
289
288
  return val.length
290
- }
291
- else if (S_number == typeof val) {
289
+ } else if (S_number == typeof val) {
292
290
  return Math.floor(val)
293
- }
294
- else if (S_boolean == typeof val) {
291
+ } else if (S_boolean == typeof val) {
295
292
  return true === val ? 1 : 0
296
- }
297
- else {
293
+ } else {
298
294
  return 0
299
295
  }
300
296
  }
301
297
 
302
-
303
298
  // Extract part of an array or string into a new value, from the start
304
299
  // point to the end point. If no end is specified, extract to the
305
300
  // full length of the value. Negative arguments count from the end of
@@ -307,7 +302,7 @@ function size(val: any): number {
307
302
  // is inclusive, and end is *exclusive*.
308
303
  // NOTE: input lists are not mutated by default. Use the mutate
309
304
  // argument to mutate lists in place.
310
- 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 {
311
306
  if (S_number === typeof val) {
312
307
  start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start
313
308
  end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1
@@ -327,21 +322,16 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
327
322
  end = 0
328
323
  }
329
324
  start = 0
330
- }
331
-
332
- else if (null != end) {
325
+ } else if (null != end) {
333
326
  if (end < 0) {
334
327
  end = vlen + end
335
328
  if (end < 0) {
336
329
  end = 0
337
330
  }
338
- }
339
- else if (vlen < end) {
331
+ } else if (vlen < end) {
340
332
  end = vlen
341
333
  }
342
- }
343
-
344
- else {
334
+ } else {
345
335
  end = vlen
346
336
  }
347
337
 
@@ -355,21 +345,17 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
355
345
  for (let i = 0, j = start; j < end; i++, j++) {
356
346
  val[i] = val[j]
357
347
  }
358
- val.length = (end - start)
359
- }
360
- else {
348
+ val.length = end - start
349
+ } else {
361
350
  val = val.slice(start, end) as V
362
351
  }
363
- }
364
- else if (S_string === typeof val) {
352
+ } else if (S_string === typeof val) {
365
353
  val = (val as string).substring(start, end) as V
366
354
  }
367
- }
368
- else {
355
+ } else {
369
356
  if (islist(val)) {
370
357
  val = [] as V
371
- }
372
- else if (S_string === typeof val) {
358
+ } else if (S_string === typeof val) {
373
359
  val = S_MT as V
374
360
  }
375
361
  }
@@ -378,19 +364,16 @@ function slice<V extends any>(val: V, start?: number, end?: number, mutate?: boo
378
364
  return val
379
365
  }
380
366
 
381
-
382
367
  // String padding.
383
368
  function pad(str: any, padding?: number, padchar?: string): string {
384
369
  str = S_string === typeof str ? str : stringify(str)
385
370
  padding = null == padding ? 44 : padding
386
- padchar = null == padchar ? S_SP : ((padchar + S_SP)[0])
371
+ padchar = null == padchar ? S_SP : (padchar + S_SP)[0]
387
372
  return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar)
388
373
  }
389
374
 
390
-
391
375
  // Determine the type of a value as a bit code.
392
376
  function typify(value: any): number {
393
-
394
377
  if (undefined === value) {
395
378
  return T_noval
396
379
  }
@@ -399,41 +382,30 @@ function typify(value: any): number {
399
382
 
400
383
  if (null === value) {
401
384
  return T_scalar | T_null
402
- }
403
- else if (S_number === typestr) {
385
+ } else if (S_number === typestr) {
404
386
  if (Number.isInteger(value)) {
405
387
  return T_scalar | T_number | T_integer
406
- }
407
- else if (isNaN(value)) {
388
+ } else if (isNaN(value)) {
408
389
  return T_noval
409
- }
410
- else {
390
+ } else {
411
391
  return T_scalar | T_number | T_decimal
412
392
  }
413
- }
414
- else if (S_string === typestr) {
393
+ } else if (S_string === typestr) {
415
394
  return T_scalar | T_string
416
- }
417
- else if (S_boolean === typestr) {
395
+ } else if (S_boolean === typestr) {
418
396
  return T_scalar | T_boolean
419
- }
420
- else if (S_function === typestr) {
397
+ } else if (S_function === typestr) {
421
398
  return T_scalar | T_function
422
399
  }
423
400
 
424
401
  // For languages that have symbolic atoms.
425
402
  else if (S_symbol === typestr) {
426
403
  return T_scalar | T_symbol
427
- }
428
-
429
- else if (Array.isArray(value)) {
404
+ } else if (Array.isArray(value)) {
430
405
  return T_node | T_list
431
- }
432
-
433
- else if (S_object === typestr) {
434
-
406
+ } else if (S_object === typestr) {
435
407
  if (value.constructor instanceof Function) {
436
- let cname = value.constructor.name
408
+ const cname = value.constructor.name
437
409
  if ('Object' !== cname && 'Array' !== cname) {
438
410
  return T_node | T_instance
439
411
  }
@@ -446,7 +418,6 @@ function typify(value: any): number {
446
418
  return T_any
447
419
  }
448
420
 
449
-
450
421
  // Get a list element. The key should be an integer, or a string
451
422
  // that can parse to an integer only. Negative integers count from the end of the list.
452
423
  function getelem(val: any, key: any, alt?: any) {
@@ -457,8 +428,8 @@ function getelem(val: any, key: any, alt?: any) {
457
428
  }
458
429
 
459
430
  if (islist(val)) {
460
- let nkey = parseInt(key)
461
- 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)) {
462
433
  if (nkey < 0) {
463
434
  key = val.length + nkey
464
435
  }
@@ -466,14 +437,14 @@ function getelem(val: any, key: any, alt?: any) {
466
437
  }
467
438
  }
468
439
 
469
- if (NONE === out) {
440
+ // null at a slot counts as "no value" — same Group A rule as getprop.
441
+ if (null == out) {
470
442
  return 0 < (T_function & typify(alt)) ? alt() : alt
471
443
  }
472
444
 
473
445
  return out
474
446
  }
475
447
 
476
-
477
448
  // Safely get a property of a node. Undefined arguments return undefined.
478
449
  // If the key is not found, return the alternative value, if any.
479
450
  function getprop(val: any, key: any, alt?: any) {
@@ -487,13 +458,27 @@ function getprop(val: any, key: any, alt?: any) {
487
458
  out = val[key]
488
459
  }
489
460
 
490
- 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) {
491
466
  return alt
492
467
  }
493
468
 
494
469
  return out
495
470
  }
496
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
+ }
497
482
 
498
483
  // Convert different types of keys to string representation.
499
484
  // String keys are returned as is.
@@ -509,42 +494,38 @@ function strkey(key: any = NONE): string {
509
494
 
510
495
  if (0 < (T_string & t)) {
511
496
  return key
512
- }
513
- else if (0 < (T_boolean & t)) {
497
+ } else if (0 < (T_boolean & t)) {
514
498
  return S_MT
515
- }
516
- else if (0 < (T_number & t)) {
499
+ } else if (0 < (T_number & t)) {
517
500
  return key % 1 === 0 ? String(key) : String(Math.floor(key))
518
501
  }
519
502
 
520
503
  return S_MT
521
504
  }
522
505
 
523
-
524
506
  // Sorted keys of a map, or indexes (as strings) of a list.
525
507
  // Root utility - only uses language facilities.
526
508
  function keysof(val: any): string[] {
527
- return !isnode(val) ? [] :
528
- 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)
529
514
  }
530
515
 
531
-
532
516
  // Value of property with name key in node val is defined.
533
517
  // Root utility - only uses language facilities.
534
518
  function haskey(val: any, key: any) {
535
- return NONE !== getprop(val, key)
519
+ // null at a key counts as "no value" — same rule as getprop.
520
+ return null != getprop(val, key)
536
521
  }
537
522
 
538
-
539
523
  // List the sorted keys of a map or list as an array of tuples of the form [key, value].
540
524
  // As with keysof, list indexes are converted to strings.
541
525
  // Root utility - only uses language facilities.
542
- function items(val: any): [string, any][];
543
- function items<T>(val: any, apply: (item: [string, any]) => T): T[];
544
- function items(
545
- val: any,
546
- apply?: (item: [string, any]) => any
547
- ): any[] {
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[] {
548
529
  let out: [string, any][] = keysof(val).map((k: any) => [k, val[k]])
549
530
  if (null != apply) {
550
531
  out = out.map(apply)
@@ -552,7 +533,6 @@ function items(
552
533
  return out
553
534
  }
554
535
 
555
-
556
536
  // To replicate the array spread operator:
557
537
  // a=1, b=[2,3], c=[4,5]
558
538
  // [a,...b,c] -> [1,2,3,[4,5]]
@@ -565,12 +545,11 @@ function flatten(list: any[], depth?: number) {
565
545
  return list.flat(getdef(depth, 1))
566
546
  }
567
547
 
568
-
569
548
  // Filter item values using check function.
570
549
  function filter(val: any, check: (item: [string, any]) => boolean): any[] {
571
- let all = items(val)
572
- let numall = size(all)
573
- let out = []
550
+ const all = items(val)
551
+ const numall = size(all)
552
+ const out = []
574
553
  for (let i = 0; i < numall; i++) {
575
554
  if (check(all[i])) {
576
555
  out.push(all[i][1])
@@ -579,13 +558,15 @@ function filter(val: any, check: (item: [string, any]) => boolean): any[] {
579
558
  return out
580
559
  }
581
560
 
582
-
583
561
  // Escape regular expression.
584
562
  function escre(s: string) {
585
- // s = null == s ? S_MT : s
586
- return replace(s, R_ESCAPE_REGEXP, '\\$&')
563
+ return re_replace(R_ESCAPE_REGEXP, s, '\\$&')
587
564
  }
588
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
+ }
589
570
 
590
571
  // Escape URLs.
591
572
  function escurl(s: string) {
@@ -593,24 +574,89 @@ function escurl(s: string) {
593
574
  return encodeURIComponent(s)
594
575
  }
595
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
+ }
596
645
 
597
646
  // Replace a search string (all), or a regexp, in a source string.
598
647
  function replace(s: string, from: string | RegExp, to: any) {
599
648
  let rs = s
600
- let ts = typify(s)
649
+ const ts = typify(s)
601
650
  if (0 === (T_string & ts)) {
602
651
  rs = stringify(s)
603
- }
604
- else if (0 < ((T_noval | T_null) & ts)) {
652
+ } else if (0 < ((T_noval | T_null) & ts)) {
605
653
  rs = S_MT
606
- }
607
- else {
654
+ } else {
608
655
  rs = stringify(s)
609
656
  }
610
657
  return rs.replace(from, to)
611
658
  }
612
659
 
613
-
614
660
  // Concatenate url part strings, merging sep char as needed.
615
661
  function join(arr: any[], sep?: string, url?: boolean) {
616
662
  const sarr = size(arr)
@@ -619,41 +665,45 @@ function join(arr: any[], sep?: string, url?: boolean) {
619
665
  const out = filter(
620
666
  items(
621
667
  // filter(arr, (n) => null != n[1] && S_MT !== n[1]),
622
- 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]),
623
669
  (n) => {
624
- let i = +n[0]
670
+ const i = +n[0]
625
671
  let s = n[1]
626
672
 
627
673
  if (NONE !== sepre && S_MT !== sepre) {
628
674
  if (url && 0 === i) {
629
- s = replace(s, RegExp(sepre + '+$'), S_MT)
675
+ s = re_replace(re_compile(sepre + '+$'), s, S_MT)
630
676
  return s
631
677
  }
632
678
 
633
679
  if (0 < i) {
634
- s = replace(s, RegExp('^' + sepre + '+'), S_MT)
680
+ s = re_replace(re_compile('^' + sepre + '+'), s, S_MT)
635
681
  }
636
682
 
637
683
  if (i < sarr - 1 || !url) {
638
- s = replace(s, RegExp(sepre + '+$'), S_MT)
684
+ s = re_replace(re_compile(sepre + '+$'), s, S_MT)
639
685
  }
640
686
 
641
- s = replace(s, RegExp('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
642
- '$1' + sepdef + '$2')
687
+ s = re_replace(
688
+ re_compile('([^' + sepre + '])' + sepre + '+([^' + sepre + '])'),
689
+ s,
690
+ '$1' + sepdef + '$2',
691
+ )
643
692
  }
644
693
 
645
694
  return s
646
- }), (n) => S_MT !== n[1])
647
- .join(sepdef)
695
+ },
696
+ ),
697
+ (n) => S_MT !== n[1],
698
+ ).join(sepdef)
648
699
 
649
700
  return out
650
701
  }
651
702
 
652
-
653
703
  // Output JSON in a "standard" format, with 2 space indents, each property on a new line,
654
704
  // and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
655
705
  // In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
656
- function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
706
+ function jsonify(val: any, flags?: { indent?: number; offset?: number }) {
657
707
  let str = S_null
658
708
 
659
709
  if (null != val) {
@@ -667,14 +717,14 @@ function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
667
717
  if (0 < offset) {
668
718
  // Left offset entire indented JSON so that it aligns with surrounding code
669
719
  // indented by offset. Assume first brace is on line with asignment, so not offset.
670
- str = '{\n' +
720
+ str =
721
+ '{\n' +
671
722
  join(
672
- items(
673
- slice(str.split('\n'), 1),
674
- (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
+ )
675
726
  }
676
- }
677
- catch (e: any) {
727
+ } catch {
678
728
  str = '__JSONIFY_FAILED__'
679
729
  }
680
730
  }
@@ -682,7 +732,6 @@ function jsonify(val: any, flags?: { indent?: number, offset?: number }) {
682
732
  return str
683
733
  }
684
734
 
685
-
686
735
  // Safely stringify a value for humans (NOT JSON!).
687
736
  function stringify(val: any, maxlen?: number, pretty?: any): string {
688
737
  let valstr = S_MT
@@ -694,15 +743,10 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
694
743
 
695
744
  if (S_string === typeof val) {
696
745
  valstr = val
697
- }
698
- else {
746
+ } else {
699
747
  try {
700
- valstr = JSON.stringify(val, function(_key: string, val: any) {
701
- if (
702
- val !== null &&
703
- typeof val === "object" &&
704
- !Array.isArray(val)
705
- ) {
748
+ valstr = JSON.stringify(val, function (_key: string, val: any) {
749
+ if (val !== null && typeof val === 'object' && !Array.isArray(val)) {
706
750
  const sortedObj: any = {}
707
751
  items(val, (n) => {
708
752
  sortedObj[n[0]] = val[n[0]]
@@ -711,49 +755,57 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
711
755
  }
712
756
  return val
713
757
  })
714
- valstr = valstr.replace(R_QUOTES, S_MT)
715
- }
716
- catch (err: any) {
758
+ valstr = re_replace(R_QUOTES, valstr, S_MT)
759
+ } catch {
717
760
  valstr = '__STRINGIFY_FAILED__'
718
761
  }
719
762
  }
720
763
 
721
764
  if (null != maxlen && -1 < maxlen) {
722
- let js = valstr.substring(0, maxlen)
723
- 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
724
767
  }
725
768
 
726
769
  if (pretty) {
727
770
  // Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
728
- let c = items(
771
+ const c = items(
729
772
  [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69],
730
- (n) => '\x1b[38;5;' + n[1] + 'm'),
731
- 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
732
779
  for (const ch of valstr) {
733
780
  if (ch === '{' || ch === '[') {
734
- d++; o = c[d % c.length]; t += o + ch
781
+ d++
782
+ o = c[d % c.length]
783
+ t += o + ch
735
784
  } else if (ch === '}' || ch === ']') {
736
- t += o + ch; d--; o = c[d % c.length]
785
+ t += o + ch
786
+ d--
787
+ o = c[d % c.length]
737
788
  } else {
738
789
  t += o + ch
739
790
  }
740
791
  }
741
792
  return t + r
742
-
743
793
  }
744
794
 
745
795
  return valstr
746
796
  }
747
797
 
748
-
749
798
  // Build a human friendly path string.
750
799
  function pathify(val: any, startin?: number, endin?: number) {
751
800
  let pathstr: string | undefined = NONE
752
801
 
753
- let path: any[] | undefined = islist(val) ? val :
754
- S_string == typeof val ? [val] :
755
- S_number == typeof val ? [val] :
756
- 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
757
809
 
758
810
  const start = null == startin ? 0 : -1 < startin ? startin : 0
759
811
  const end = null == endin ? 0 : -1 < endin ? endin : 0
@@ -762,15 +814,17 @@ function pathify(val: any, startin?: number, endin?: number) {
762
814
  path = slice(path, start, path.length - end)
763
815
  if (0 === path.length) {
764
816
  pathstr = '<root>'
765
- }
766
- else {
817
+ } else {
767
818
  pathstr = join(
768
819
  items(
769
- filter(path, (n) => iskey(n[1])), (n) => {
770
- let p = n[1]
771
- return S_number === typeof p ? S_MT + Math.floor(p) :
772
- p.replace(R_DOT, S_MT)
773
- }), S_DT)
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
+ )
774
828
  }
775
829
  }
776
830
 
@@ -781,34 +835,34 @@ function pathify(val: any, startin?: number, endin?: number) {
781
835
  return pathstr
782
836
  }
783
837
 
784
-
785
838
  // Clone a JSON-like data structure.
786
839
  // NOTE: function and instance values are copied, *not* cloned.
787
840
  function clone(val: any): any {
788
841
  const refs: any[] = []
789
842
  const reftype = T_function | T_instance
790
- const replacer: any = (_k: any, v: any) => 0 < (reftype & typify(v)) ?
791
- (refs.push(v), '`$REF:' + (refs.length - 1) + '`') : v
792
- const reviver: any = (_k: any, v: any, m: any) => S_string === typeof v ?
793
- (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
794
847
  const out = NONE === val ? NONE : JSON.parse(JSON.stringify(val, replacer), reviver)
795
848
  return out
796
849
  }
797
850
 
798
-
799
851
  // Define a JSON Object using function arguments.
800
852
  function jm(...kv: any[]): Record<string, any> {
801
853
  const kvsize = size(kv)
802
854
  const o: any = {}
803
855
  for (let i = 0; i < kvsize; i += 2) {
804
- 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
805
860
  k = 'string' === typeof k ? k : stringify(k)
806
- o[k] = getprop(kv, i + 1, null)
861
+ o[k] = i + 1 < kv.length ? kv[i + 1] : null
807
862
  }
808
863
  return o
809
864
  }
810
865
 
811
-
812
866
  // Define a JSON Array using function arguments.
813
867
  function jt(...v: any[]): any[] {
814
868
  const vsize = size(v)
@@ -819,8 +873,7 @@ function jt(...v: any[]): any[] {
819
873
  return a
820
874
  }
821
875
 
822
-
823
- // Safely delete a property from an object or array element.
876
+ // Safely delete a property from an object or array element.
824
877
  // Undefined arguments and invalid keys are ignored.
825
878
  // Returns the (possibly modified) parent.
826
879
  // For objects, the property is deleted using the delete operator.
@@ -834,8 +887,7 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
834
887
  if (ismap(parent)) {
835
888
  key = strkey(key)
836
889
  delete (parent as any)[key]
837
- }
838
- else if (islist(parent)) {
890
+ } else if (islist(parent)) {
839
891
  // Ensure key is an integer.
840
892
  let keyI = +key
841
893
 
@@ -859,7 +911,6 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
859
911
  return parent
860
912
  }
861
913
 
862
-
863
914
  // Safely set a property. Undefined arguments and invalid keys are ignored.
864
915
  // Returns the (possibly modified) parent.
865
916
  // If the parent is a list, and the key is negative, prepend the value.
@@ -874,8 +925,7 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
874
925
  key = S_MT + key
875
926
  const pany = parent as any
876
927
  pany[key] = val
877
- }
878
- else if (islist(parent)) {
928
+ } else if (islist(parent)) {
879
929
  // Ensure key is an integer.
880
930
  let keyI = +key
881
931
 
@@ -901,8 +951,12 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
901
951
  return parent
902
952
  }
903
953
 
904
-
905
954
  // Walk a data structure depth first, applying a function to each value.
955
+ // The `path` argument passed to the before/after callbacks is a single
956
+ // mutable array per depth, shared across all callback invocations for the
957
+ // lifetime of this top-level walk call. Callbacks that need to store the
958
+ // path MUST clone it (e.g. `path.slice()`); the contents will otherwise
959
+ // be overwritten by subsequent visits.
906
960
  function walk(
907
961
  // These arguments are the public interface.
908
962
  val: any,
@@ -919,25 +973,42 @@ function walk(
919
973
  // These areguments are used for recursive state.
920
974
  key?: string | number,
921
975
  parent?: any,
922
- path?: string[]
976
+ path?: string[],
977
+ pool?: string[][],
923
978
  ): any {
979
+ if (NONE === pool) {
980
+ pool = [[]]
981
+ }
924
982
  if (NONE === path) {
925
- path = []
983
+ path = pool[0]
926
984
  }
927
985
 
986
+ const depth = path.length
987
+
928
988
  let out = null == before ? val : before(key, val, parent, path)
929
989
 
930
990
  maxdepth = null != maxdepth && 0 <= maxdepth ? maxdepth : MAXDEPTH
931
- if (0 === maxdepth || (null != path && 0 < maxdepth && maxdepth <= path.length)) {
991
+ if (0 === maxdepth || (0 < maxdepth && maxdepth <= depth)) {
932
992
  return out
933
993
  }
934
994
 
935
995
  if (isnode(out)) {
936
- for (let [ckey, child] of items(out)) {
937
- setprop(out, ckey, walk(
938
- child, before, after, maxdepth, ckey, out,
939
- flatten([getdef(path, []), S_MT + ckey])
940
- ))
996
+ const childDepth = depth + 1
997
+ let childPath = pool[childDepth]
998
+ if (NONE === childPath) {
999
+ childPath = new Array(childDepth)
1000
+ pool[childDepth] = childPath
1001
+ }
1002
+ // Sync prefix [0..depth-1] from the current path. Only needed once per
1003
+ // parent: siblings share the same prefix and will each overwrite slot
1004
+ // [depth] below.
1005
+ for (let i = 0; i < depth; i++) {
1006
+ childPath[i] = path[i]
1007
+ }
1008
+
1009
+ for (const [ckey, child] of items(out)) {
1010
+ childPath[depth] = S_MT + ckey
1011
+ setprop(out, ckey, walk(child, before, after, maxdepth, ckey, out, childPath, pool))
941
1012
  }
942
1013
  }
943
1014
 
@@ -946,7 +1017,6 @@ function walk(
946
1017
  return out
947
1018
  }
948
1019
 
949
-
950
1020
  // Merge a list of values into each other. Later values have
951
1021
  // precedence. Nodes override scalars. Node kinds (list or map)
952
1022
  // override each other, and do *not* merge. The first element is
@@ -966,8 +1036,7 @@ function merge(val: any, maxdepth?: number): any {
966
1036
 
967
1037
  if (0 === lenlist) {
968
1038
  return NONE
969
- }
970
- else if (1 === lenlist) {
1039
+ } else if (1 === lenlist) {
971
1040
  return list[0]
972
1041
  }
973
1042
 
@@ -975,25 +1044,19 @@ function merge(val: any, maxdepth?: number): any {
975
1044
  out = getprop(list, 0, {})
976
1045
 
977
1046
  for (let oI = 1; oI < lenlist; oI++) {
978
- let obj = list[oI]
1047
+ const obj = list[oI]
979
1048
 
980
1049
  if (!isnode(obj)) {
981
1050
  // Nodes win.
982
1051
  out = obj
983
- }
984
- else {
1052
+ } else {
985
1053
  // Current value at path end in overriding node.
986
- let cur: any[] = [out]
1054
+ const cur: any[] = [out]
987
1055
 
988
1056
  // Current value at path end in destination node.
989
- let dst: any[] = [out]
990
-
991
- function before(
992
- key: string | number | undefined,
993
- val: any,
994
- _parent: any,
995
- path: string[]
996
- ) {
1057
+ const dst: any[] = [out]
1058
+
1059
+ function before(key: string | number | undefined, val: any, _parent: any, path: string[]) {
997
1060
  const pI = size(path)
998
1061
 
999
1062
  if (md <= pI) {
@@ -1007,7 +1070,6 @@ function merge(val: any, maxdepth?: number): any {
1007
1070
 
1008
1071
  // Descend into override node - Set up correct target in `after` function.
1009
1072
  else {
1010
-
1011
1073
  // Descend into destination node using same key.
1012
1074
  dst[pI] = 0 < pI ? getprop(dst[pI - 1], key) : dst[pI]
1013
1075
  const tval = dst[pI]
@@ -1038,12 +1100,7 @@ function merge(val: any, maxdepth?: number): any {
1038
1100
  return val
1039
1101
  }
1040
1102
 
1041
- function after(
1042
- key: string | number | undefined,
1043
- _val: any,
1044
- _parent: any,
1045
- path: string[]
1046
- ) {
1103
+ function after(key: string | number | undefined, _val: any, _parent: any, path: string[]) {
1047
1104
  const cI = size(path)
1048
1105
  const target = cur[cI - 1]
1049
1106
  const value = cur[cI]
@@ -1069,20 +1126,24 @@ function merge(val: any, maxdepth?: number): any {
1069
1126
  return out
1070
1127
  }
1071
1128
 
1072
-
1073
1129
  // Set a value using a path. Missing path parts are created.
1074
1130
  // String paths create only maps. Use a string list to create list parts.
1075
1131
  function setpath(
1076
1132
  store: any,
1077
1133
  path: number | string | string[],
1078
1134
  val: any,
1079
- injdef?: Partial<Injection>
1135
+ injdef?: Partial<Injection>,
1080
1136
  ) {
1081
1137
  const pathType = typify(path)
1082
1138
 
1083
- const parts = 0 < (T_list & pathType) ? path :
1084
- 0 < (T_string & pathType) ? (path as string).split(S_DT) :
1085
- 0 < (T_number & pathType) ? [path] : NONE
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
1086
1147
 
1087
1148
  if (NONE === parts) {
1088
1149
  return NONE
@@ -1104,21 +1165,22 @@ function setpath(
1104
1165
 
1105
1166
  if (DELETE === val) {
1106
1167
  delprop(parent, getelem(parts, -1))
1107
- }
1108
- else {
1168
+ } else {
1109
1169
  setprop(parent, getelem(parts, -1), val)
1110
1170
  }
1111
1171
 
1112
1172
  return parent
1113
1173
  }
1114
1174
 
1115
-
1116
1175
  function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
1117
-
1118
1176
  // Operate on a string array.
1119
- const parts = islist(path) ? path :
1120
- 'string' === typeof path ? path.split(S_DT) :
1121
- '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
1122
1184
 
1123
1185
  if (NONE === parts) {
1124
1186
  return NONE
@@ -1134,9 +1196,7 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1134
1196
  // An empty path (incl empty string) just finds the store.
1135
1197
  if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
1136
1198
  val = src
1137
- }
1138
- else if (0 < numparts) {
1139
-
1199
+ } else if (0 < numparts) {
1140
1200
  // Check for $ACTIONs
1141
1201
  if (1 === numparts) {
1142
1202
  val = getprop(store, parts[0])
@@ -1145,7 +1205,7 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1145
1205
  if (!isfunc(val)) {
1146
1206
  val = src
1147
1207
 
1148
- const m = parts[0].match(R_META_PATH)
1208
+ const m = re_find(R_META_PATH, parts[0])
1149
1209
  if (m && injdef && injdef.meta) {
1150
1210
  val = getprop(injdef.meta, m[1])
1151
1211
  parts[0] = m[3]
@@ -1158,25 +1218,21 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1158
1218
 
1159
1219
  if (injdef && S_DKEY === part) {
1160
1220
  part = getprop(injdef, S_key)
1161
- }
1162
- else if (injdef && part.startsWith('$GET:')) {
1221
+ } else if (injdef && part.startsWith('$GET:')) {
1163
1222
  // $GET:path$ -> get store value, use as path part (string)
1164
1223
  part = stringify(getpath(src, slice(part, 5, -1)))
1165
- }
1166
- else if (injdef && part.startsWith('$REF:')) {
1224
+ } else if (injdef && part.startsWith('$REF:')) {
1167
1225
  // $REF:refpath$ -> get spec value, use as path part (string)
1168
1226
  part = stringify(getpath(getprop(store, S_DSPEC), slice(part, 5, -1)))
1169
- }
1170
- else if (injdef && part.startsWith('$META:')) {
1227
+ } else if (injdef && part.startsWith('$META:')) {
1171
1228
  // $META:metapath$ -> get meta value, use as path part (string)
1172
1229
  part = stringify(getpath(getprop(injdef, 'meta'), slice(part, 6, -1)))
1173
1230
  }
1174
1231
 
1175
1232
  // $$ escapes $
1176
- part = part.replace(R_DOUBLE_DOLLAR, '$')
1233
+ part = re_replace(R_DOUBLE_DOLLAR, part, '$')
1177
1234
 
1178
1235
  if (S_MT === part) {
1179
-
1180
1236
  let ascends = 0
1181
1237
  while (S_MT === parts[1 + pI]) {
1182
1238
  ascends++
@@ -1190,26 +1246,22 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1190
1246
 
1191
1247
  if (0 === ascends) {
1192
1248
  val = dparent
1193
- }
1194
- else {
1249
+ } else {
1195
1250
  // const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
1196
1251
  const fullpath = flatten([slice(dpath, 0 - ascends), parts.slice(pI + 1)])
1197
1252
 
1198
1253
  if (ascends <= size(dpath)) {
1199
1254
  val = getpath(store, fullpath)
1200
- }
1201
- else {
1255
+ } else {
1202
1256
  val = NONE
1203
1257
  }
1204
1258
 
1205
1259
  break
1206
1260
  }
1207
- }
1208
- else {
1261
+ } else {
1209
1262
  val = dparent
1210
1263
  }
1211
- }
1212
- else {
1264
+ } else {
1213
1265
  val = getprop(val, part)
1214
1266
  }
1215
1267
  }
@@ -1228,16 +1280,11 @@ function getpath(store: any, path: number | string | string[], injdef?: Partial<
1228
1280
  return val
1229
1281
  }
1230
1282
 
1231
-
1232
1283
  // Inject values from a data store into a node recursively, resolving
1233
1284
  // paths against the store, or current if they are local. The modify
1234
1285
  // argument allows custom modification of the result. The inj
1235
1286
  // (Injection) argument is used to maintain recursive state.
1236
- function inject(
1237
- val: any,
1238
- store: any,
1239
- injdef?: Partial<Injection>,
1240
- ) {
1287
+ function inject(val: any, store: any, injdef?: Partial<Injection>) {
1241
1288
  const valtype = typeof val
1242
1289
  let inj: Injection = injdef as Injection
1243
1290
 
@@ -1265,7 +1312,6 @@ function inject(
1265
1312
 
1266
1313
  // Descend into node.
1267
1314
  if (isnode(val)) {
1268
-
1269
1315
  // Keys are sorted alphanumerically to ensure determinism.
1270
1316
  // Injection transforms ($FOO) are processed *after* other keys.
1271
1317
  // NOTE: the optional digits suffix of the transform can thus be
@@ -1276,11 +1322,10 @@ function inject(
1276
1322
 
1277
1323
  if (ismap(val)) {
1278
1324
  nodekeys = flatten([
1279
- filter(nodekeys, (n => !n[1].includes(S_DS))),
1280
- 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)),
1281
1327
  ])
1282
- }
1283
- else {
1328
+ } else {
1284
1329
  nodekeys = keysof(val)
1285
1330
  }
1286
1331
 
@@ -1289,7 +1334,6 @@ function inject(
1289
1334
  // 2. inj.mode=M_VAL - The child value is injected.
1290
1335
  // 3. inj.mode=M_KEYPOST - Key string is injected again, allowing child mutation.
1291
1336
  for (let nkI = 0; nkI < nodekeys.length; nkI++) {
1292
-
1293
1337
  const childinj = inj.child(nkI, nodekeys)
1294
1338
  const nodekey = childinj.key
1295
1339
  childinj.mode = M_KEYPRE
@@ -1336,17 +1380,11 @@ function inject(
1336
1380
 
1337
1381
  // Custom modification.
1338
1382
  if (inj.modify && SKIP !== val) {
1339
- let mkey = inj.key
1340
- let mparent = inj.parent
1341
- let mval = getprop(mparent, mkey)
1342
-
1343
- inj.modify(
1344
- mval,
1345
- mkey,
1346
- mparent,
1347
- inj,
1348
- store
1349
- )
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)
1350
1388
  }
1351
1389
 
1352
1390
  // console.log('INJ-VAL', val)
@@ -1355,10 +1393,9 @@ function inject(
1355
1393
 
1356
1394
  // Original val reference may no longer be correct.
1357
1395
  // This return value is only used as the top level result.
1358
- return getprop(inj.parent, S_DTOP)
1396
+ return _lookup(inj.parent, S_DTOP)
1359
1397
  }
1360
1398
 
1361
-
1362
1399
  // The transform_* functions are special command inject handlers (see Injector).
1363
1400
 
1364
1401
  // Delete a key from a map or list.
@@ -1367,7 +1404,6 @@ const transform_DELETE: Injector = (inj: Injection) => {
1367
1404
  return NONE
1368
1405
  }
1369
1406
 
1370
-
1371
1407
  // Copy value from source data.
1372
1408
  const transform_COPY: Injector = (inj: Injection, _val: any) => {
1373
1409
  const ijname = 'COPY'
@@ -1376,13 +1412,12 @@ const transform_COPY: Injector = (inj: Injection, _val: any) => {
1376
1412
  return NONE
1377
1413
  }
1378
1414
 
1379
- let out = getprop(inj.dparent, inj.key)
1415
+ const out = _lookup(inj.dparent, inj.key)
1380
1416
  inj.setval(out)
1381
1417
 
1382
1418
  return out
1383
1419
  }
1384
1420
 
1385
-
1386
1421
  // As a value, inject the key of the parent node.
1387
1422
  // As a key, defined the name of the key property in the source object.
1388
1423
  const transform_KEY: Injector = (inj: Injection) => {
@@ -1394,7 +1429,7 @@ const transform_KEY: Injector = (inj: Injection) => {
1394
1429
  }
1395
1430
 
1396
1431
  // Key is defined by $KEY meta property.
1397
- const keyspec = getprop(parent, S_BKEY)
1432
+ const keyspec = _lookup(parent, S_BKEY)
1398
1433
  if (NONE !== keyspec) {
1399
1434
  delprop(parent, S_BKEY)
1400
1435
  return getprop(inj.dparent, keyspec)
@@ -1402,10 +1437,9 @@ const transform_KEY: Injector = (inj: Injection) => {
1402
1437
 
1403
1438
  // Key is defined within general purpose $META object.
1404
1439
  // return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
1405
- return getprop(getprop(parent, S_BANNO), S_KEY, getelem(path, -2))
1440
+ return _lookup(_lookup(parent, S_BANNO), S_KEY) ?? getelem(path, -2)
1406
1441
  }
1407
1442
 
1408
-
1409
1443
  // Annotate node. Does nothing itself, just used by
1410
1444
  // other injectors, and is removed when called.
1411
1445
  const transform_ANNO: Injector = (inj: Injection) => {
@@ -1414,10 +1448,9 @@ const transform_ANNO: Injector = (inj: Injection) => {
1414
1448
  return NONE
1415
1449
  }
1416
1450
 
1417
-
1418
- // Merge a list of objects into the current object.
1451
+ // Merge a list of objects into the current object.
1419
1452
  // Must be a key in an object. The value is merged over the current object.
1420
- // 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`.
1421
1454
  // If the value is the empty string, merge the top level store.
1422
1455
  // Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
1423
1456
  const transform_MERGE: Injector = (inj: Injection) => {
@@ -1450,15 +1483,9 @@ const transform_MERGE: Injector = (inj: Injection) => {
1450
1483
  return out
1451
1484
  }
1452
1485
 
1453
-
1454
1486
  // Convert a node to a list.
1455
1487
  // Format: ['`$EACH`', '`source-path-of-node`', child-template]
1456
- const transform_EACH: Injector = (
1457
- inj: Injection,
1458
- _val: any,
1459
- _ref: string,
1460
- store: any
1461
- ) => {
1488
+ const transform_EACH: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1462
1489
  const ijname = 'EACH'
1463
1490
 
1464
1491
  if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
@@ -1487,24 +1514,28 @@ const transform_EACH: Injector = (
1487
1514
  let tval: any = []
1488
1515
 
1489
1516
  const tkey = getelem(inj.path, -2)
1490
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1517
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1491
1518
 
1492
1519
  // Create clones of the child template for each value of the current soruce.
1493
1520
  if (0 < (T_list & srctype)) {
1494
1521
  tval = items(src, () => clone(child))
1495
- }
1496
- else if (0 < (T_map & srctype)) {
1497
- tval = items(src, (n => merge([
1498
- clone(child),
1499
- // Make a note of the key for $KEY transforms.
1500
- { [S_BANNO]: { KEY: n[0] } }
1501
- ], 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
+ )
1502
1533
  }
1503
1534
 
1504
1535
  let rval = []
1505
1536
 
1506
1537
  if (0 < size(tval)) {
1507
- tcur = null == src ? NONE : Object.values(src)
1538
+ tcur = null == src ? NONE : items(src, (n: [string, any]) => n[1])
1508
1539
 
1509
1540
  const ckey = getelem(inj.path, -2)
1510
1541
 
@@ -1542,16 +1573,10 @@ const transform_EACH: Injector = (
1542
1573
  return rval[0]
1543
1574
  }
1544
1575
 
1545
-
1546
1576
  // Convert a node to a map.
1547
1577
  // Format: { '`$PACK`':['source-path', child-template]}
1548
- const transform_PACK: Injector = (
1549
- inj: Injection,
1550
- _val: any,
1551
- _ref: string,
1552
- store: any
1553
- ) => {
1554
- 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
1555
1580
 
1556
1581
  const ijname = 'EACH'
1557
1582
 
@@ -1583,8 +1608,7 @@ const transform_PACK: Injector = (
1583
1608
  setprop(item[1], S_BANNO, { KEY: item[0] })
1584
1609
  return item[1]
1585
1610
  })
1586
- }
1587
- else {
1611
+ } else {
1588
1612
  src = NONE
1589
1613
  }
1590
1614
  }
@@ -1600,7 +1624,7 @@ const transform_PACK: Injector = (
1600
1624
  const child = getprop(childspec, S_BVAL, childspec)
1601
1625
 
1602
1626
  // Build parallel target object.
1603
- let tval: any = {}
1627
+ const tval: any = {}
1604
1628
 
1605
1629
  items(src, (item: [string, any]) => {
1606
1630
  const srckey = item[0]
@@ -1610,8 +1634,7 @@ const transform_PACK: Injector = (
1610
1634
  if (NONE !== keypath) {
1611
1635
  if (keypath.startsWith('`')) {
1612
1636
  key = inject(keypath, merge([{}, store, { $TOP: srcnode }], 1))
1613
- }
1614
- else {
1637
+ } else {
1615
1638
  key = getpath(srcnode, keypath, inj)
1616
1639
  }
1617
1640
  }
@@ -1622,8 +1645,7 @@ const transform_PACK: Injector = (
1622
1645
  const anno = getprop(srcnode, S_BANNO)
1623
1646
  if (NONE === anno) {
1624
1647
  delprop(tchild, S_BANNO)
1625
- }
1626
- else {
1648
+ } else {
1627
1649
  setprop(tchild, S_BANNO, anno)
1628
1650
  }
1629
1651
  })
@@ -1631,14 +1653,15 @@ const transform_PACK: Injector = (
1631
1653
  let rval = {}
1632
1654
 
1633
1655
  if (!isempty(tval)) {
1634
-
1635
1656
  // Build parallel source object.
1636
- let tsrc: any = {}
1657
+ const tsrc: any = {}
1637
1658
  src.reduce((a: any, n: any, i: any) => {
1638
- let kn = null == keypath ? i :
1639
- keypath.startsWith('`') ?
1640
- inject(keypath, merge([{}, store, { $TOP: n }], 1)) :
1641
- getpath(n, keypath, inj)
1659
+ const kn =
1660
+ null == keypath
1661
+ ? i
1662
+ : keypath.startsWith('`')
1663
+ ? inject(keypath, merge([{}, store, { $TOP: n }], 1))
1664
+ : getpath(n, keypath, inj)
1642
1665
 
1643
1666
  setprop(a, kn, n)
1644
1667
  return a
@@ -1678,16 +1701,10 @@ const transform_PACK: Injector = (
1678
1701
  return NONE
1679
1702
  }
1680
1703
 
1681
-
1682
1704
  // TODO: not found ref should removed key (setprop NONE)
1683
1705
  // Reference original spec (enables recursice transformations)
1684
1706
  // Format: ['`$REF`', '`spec-path`']
1685
- const transform_REF: Injector = (
1686
- inj: Injection,
1687
- val: any,
1688
- _ref: string,
1689
- store: any
1690
- ) => {
1707
+ const transform_REF: Injector = (inj: Injection, val: any, _ref: string, store: any) => {
1691
1708
  const { nodes } = inj
1692
1709
 
1693
1710
  if (M_VAL !== inj.mode) {
@@ -1695,7 +1712,7 @@ const transform_REF: Injector = (
1695
1712
  }
1696
1713
 
1697
1714
  // Get arguments: ['`$REF`', 'ref-path'].
1698
- const refpath = getprop(inj.parent, 1)
1715
+ const refpath = _lookup(inj.parent, 1)
1699
1716
  inj.keyI = size(inj.keys)
1700
1717
 
1701
1718
  // Spec reference.
@@ -1720,12 +1737,12 @@ const transform_REF: Injector = (
1720
1737
  })
1721
1738
  }
1722
1739
 
1723
- let tref = clone(ref)
1740
+ const tref = clone(ref)
1724
1741
 
1725
1742
  const cpath = slice(inj.path, -3)
1726
1743
  const tpath = slice(inj.path, -1)
1727
- let tcur = getpath(store, cpath)
1728
- let tval = getpath(store, tpath)
1744
+ const tcur = getpath(store, cpath)
1745
+ const tval = getpath(store, tpath)
1729
1746
  let rval = NONE
1730
1747
 
1731
1748
  if (!hasSubRef || NONE !== tval) {
@@ -1742,8 +1759,7 @@ const transform_REF: Injector = (
1742
1759
  inject(tref, store, tinj)
1743
1760
 
1744
1761
  rval = tinj.val
1745
- }
1746
- else {
1762
+ } else {
1747
1763
  rval = NONE
1748
1764
  }
1749
1765
 
@@ -1756,13 +1772,7 @@ const transform_REF: Injector = (
1756
1772
  return val
1757
1773
  }
1758
1774
 
1759
-
1760
- const transform_FORMAT: Injector = (
1761
- inj: Injection,
1762
- _val: any,
1763
- _ref: string,
1764
- store: any
1765
- ) => {
1775
+ const transform_FORMAT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1766
1776
  // console.log('FORMAT-START', inj, _val)
1767
1777
 
1768
1778
  // Remove remaining keys to avoid spurious processing.
@@ -1774,24 +1784,24 @@ const transform_FORMAT: Injector = (
1774
1784
 
1775
1785
  // Get arguments: ['`$FORMAT`', 'name', child].
1776
1786
  // TODO: EACH and PACK should accept customm functions too
1777
- const name = getprop(inj.parent, 1)
1778
- const child = getprop(inj.parent, 2)
1787
+ const name = _lookup(inj.parent, 1)
1788
+ const child = _lookup(inj.parent, 2)
1779
1789
 
1780
1790
  // Source data.
1781
1791
  const tkey = getelem(inj.path, -2)
1782
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1792
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1783
1793
 
1784
1794
  const cinj = injectChild(child, store, inj)
1785
1795
  const resolved = cinj.val
1786
1796
 
1787
- let formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
1797
+ const formatter = 0 < (T_function & typify(name)) ? name : getprop(FORMATTER, name)
1788
1798
 
1789
1799
  if (NONE === formatter) {
1790
1800
  inj.errs.push('$FORMAT: unknown format: ' + name + '.')
1791
1801
  return NONE
1792
1802
  }
1793
1803
 
1794
- let out = walk(resolved, formatter)
1804
+ const out = walk(resolved, formatter)
1795
1805
 
1796
1806
  setprop(target, tkey, out)
1797
1807
  // _updateAncestors(inj, target, tkey, out)
@@ -1799,17 +1809,15 @@ const transform_FORMAT: Injector = (
1799
1809
  return out
1800
1810
  }
1801
1811
 
1802
-
1803
1812
  const FORMATTER: Record<string, WalkApply> = {
1804
1813
  identity: (_k: any, v: any) => v,
1805
- upper: (_k: any, v: any) => isnode(v) ? v : ('' + v).toUpperCase(),
1806
- lower: (_k: any, v: any) => isnode(v) ? v : ('' + v).toLowerCase(),
1807
- string: (_k: any, v: any) => isnode(v) ? v : ('' + v),
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),
1808
1817
  number: (_k: any, v: any) => {
1809
1818
  if (isnode(v)) {
1810
1819
  return v
1811
- }
1812
- else {
1820
+ } else {
1813
1821
  let n = Number(v)
1814
1822
  if (isNaN(n)) {
1815
1823
  n = 0
@@ -1820,8 +1828,7 @@ const FORMATTER: Record<string, WalkApply> = {
1820
1828
  integer: (_k: any, v: any) => {
1821
1829
  if (isnode(v)) {
1822
1830
  return v
1823
- }
1824
- else {
1831
+ } else {
1825
1832
  let n = Number(v)
1826
1833
  if (isNaN(n)) {
1827
1834
  n = 0
@@ -1830,17 +1837,15 @@ const FORMATTER: Record<string, WalkApply> = {
1830
1837
  }
1831
1838
  },
1832
1839
  concat: (k: any, v: any) =>
1833
- null == k && islist(v) ? join(items(v, (n => isnode(n[1]) ? S_MT : (S_MT + n[1]))), S_MT) : v
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,
1834
1846
  }
1835
1847
 
1836
-
1837
-
1838
- const transform_APPLY: Injector = (
1839
- inj: Injection,
1840
- _val: any,
1841
- _ref: string,
1842
- store: any
1843
- ) => {
1848
+ const transform_APPLY: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
1844
1849
  const ijname = 'APPLY'
1845
1850
 
1846
1851
  if (!checkPlacement(M_VAL, ijname, T_list, inj)) {
@@ -1855,7 +1860,7 @@ const transform_APPLY: Injector = (
1855
1860
  }
1856
1861
 
1857
1862
  const tkey = getelem(inj.path, -2)
1858
- const target = getelem(inj.nodes, - 2, () => getelem(inj.nodes, -1))
1863
+ const target = getelem(inj.nodes, -2, () => getelem(inj.nodes, -1))
1859
1864
 
1860
1865
  const cinj = injectChild(child, store, inj)
1861
1866
  const resolved = cinj.val
@@ -1867,14 +1872,13 @@ const transform_APPLY: Injector = (
1867
1872
  return out
1868
1873
  }
1869
1874
 
1870
-
1871
1875
  // Transform data using spec.
1872
1876
  // Only operates on static JSON-like data.
1873
1877
  // Arrays are treated as if they are objects with indices as keys.
1874
1878
  function transform(
1875
1879
  data: any, // Source data to transform into new data (original not mutated)
1876
1880
  spec: any, // Transform specification; output follows this shape
1877
- injdef?: Partial<Injection>
1881
+ injdef?: Partial<Injection>,
1878
1882
  ) {
1879
1883
  // Clone the spec so that the clone can be modified in place as the transform result.
1880
1884
  const origspec = spec
@@ -1886,57 +1890,64 @@ function transform(
1886
1890
  const errs = injdef?.errs || []
1887
1891
 
1888
1892
  const extraTransforms: any = {}
1889
- const extraData = null == extra ? NONE : items(extra)
1890
- .reduce((a: any, n: any[]) =>
1891
- (n[0].startsWith(S_DS) ? extraTransforms[n[0]] = n[1] : (a[n[0]] = n[1]), a), {})
1892
-
1893
- const dataClone = merge([
1894
- isempty(extraData) ? NONE : clone(extraData),
1895
- clone(data),
1896
- ])
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)])
1897
1905
 
1898
1906
  // Define a top level store that provides transform operations.
1899
- const store = merge([
1900
- {
1901
- // The inject function recognises this special location for the root of the source data.
1902
- // NOTE: to escape data that contains "`$FOO`" keys at the top level,
1903
- // place that data inside a holding map: { myholder: mydata }.
1904
- $TOP: dataClone,
1905
-
1906
- $SPEC: () => origspec,
1907
-
1908
- // Escape backtick (this also works inside backticks).
1909
- $BT: () => S_BT,
1910
-
1911
- // Escape dollar sign (this also works inside backticks).
1912
- $DS: () => S_DS,
1913
-
1914
- // Insert current date and time as an ISO string.
1915
- $WHEN: () => new Date().toISOString(),
1916
-
1917
- $DELETE: transform_DELETE,
1918
- $COPY: transform_COPY,
1919
- $KEY: transform_KEY,
1920
- $ANNO: transform_ANNO,
1921
- $MERGE: transform_MERGE,
1922
- $EACH: transform_EACH,
1923
- $PACK: transform_PACK,
1924
- $REF: transform_REF,
1925
- $FORMAT: transform_FORMAT,
1926
- $APPLY: transform_APPLY,
1927
- },
1928
-
1929
- // Custom extra transforms, if any.
1930
- extraTransforms,
1931
-
1932
- {
1933
- $ERRS: errs,
1934
- }
1935
- ], 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
+ )
1936
1947
 
1937
1948
  const out = inject(spec, store, injdef)
1938
1949
 
1939
- const generr = (0 < size(errs) && !collect)
1950
+ const generr = 0 < size(errs) && !collect
1940
1951
  if (generr) {
1941
1952
  throw new Error(join(errs, ' | '))
1942
1953
  }
@@ -1944,20 +1955,19 @@ function transform(
1944
1955
  return out
1945
1956
  }
1946
1957
 
1947
-
1948
1958
  // A required string value. NOTE: Rejects empty strings.
1949
1959
  const validate_STRING: Injector = (inj: Injection) => {
1950
- let out = getprop(inj.dparent, inj.key)
1960
+ const out = _lookup(inj.dparent, inj.key)
1951
1961
 
1952
1962
  const t = typify(out)
1953
1963
  if (0 === (T_string & t)) {
1954
- let msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
1964
+ const msg = _invalidTypeMsg(inj.path, S_string, t, out, 'V1010')
1955
1965
  inj.errs.push(msg)
1956
1966
  return NONE
1957
1967
  }
1958
1968
 
1959
1969
  if (S_MT === out) {
1960
- let msg = 'Empty string at ' + pathify(inj.path, 1)
1970
+ const msg = 'Empty string at ' + pathify(inj.path, 1)
1961
1971
  inj.errs.push(msg)
1962
1972
  return NONE
1963
1973
  }
@@ -1965,13 +1975,10 @@ const validate_STRING: Injector = (inj: Injection) => {
1965
1975
  return out
1966
1976
  }
1967
1977
 
1968
-
1969
-
1970
-
1971
1978
  const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
1972
1979
  const tname = slice(ref, 1).toLowerCase()
1973
1980
  const typev = 1 << (31 - TYPENAME.indexOf(tname))
1974
- let out = getprop(inj.dparent, inj.key)
1981
+ const out = _lookup(inj.dparent, inj.key)
1975
1982
 
1976
1983
  const t = typify(out)
1977
1984
 
@@ -1985,15 +1992,12 @@ const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
1985
1992
  return out
1986
1993
  }
1987
1994
 
1988
-
1989
1995
  // Allow any value.
1990
1996
  const validate_ANY: Injector = (inj: Injection) => {
1991
- let out = getprop(inj.dparent, inj.key)
1997
+ const out = _lookup(inj.dparent, inj.key)
1992
1998
  return out
1993
1999
  }
1994
2000
 
1995
-
1996
-
1997
2001
  // Specify child values for map or list.
1998
2002
  // Map syntax: {'`$CHILD`': child-template }
1999
2003
  // List syntax: ['`$CHILD`', child-template ]
@@ -2012,15 +2016,13 @@ const validate_CHILD: Injector = (inj: Injection) => {
2012
2016
 
2013
2017
  if (NONE == tval) {
2014
2018
  tval = {}
2015
- }
2016
- else if (!ismap(tval)) {
2017
- inj.errs.push(_invalidTypeMsg(
2018
- 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')
2019
2021
  return NONE
2020
2022
  }
2021
2023
 
2022
2024
  const ckeys = keysof(tval)
2023
- for (let ckey of ckeys) {
2025
+ for (const ckey of ckeys) {
2024
2026
  setprop(parent, ckey, clone(childtm))
2025
2027
 
2026
2028
  // NOTE: modifying inj! This extends the child value loop in inject.
@@ -2034,14 +2036,13 @@ const validate_CHILD: Injector = (inj: Injection) => {
2034
2036
 
2035
2037
  // List syntax.
2036
2038
  if (M_VAL === mode) {
2037
-
2038
2039
  if (!islist(parent)) {
2039
2040
  // $CHILD was not inside a list.
2040
2041
  inj.errs.push('Invalid $CHILD as value')
2041
2042
  return NONE
2042
2043
  }
2043
2044
 
2044
- const childtm = getprop(parent, 1)
2045
+ const childtm = _lookup(parent, 1)
2045
2046
 
2046
2047
  if (NONE === inj.dparent) {
2047
2048
  // Empty list as default.
@@ -2052,7 +2053,12 @@ const validate_CHILD: Injector = (inj: Injection) => {
2052
2053
 
2053
2054
  if (!islist(inj.dparent)) {
2054
2055
  const msg = _invalidTypeMsg(
2055
- 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
+ )
2056
2062
  inj.errs.push(msg)
2057
2063
  inj.keyI = size(parent)
2058
2064
  return inj.dparent
@@ -2072,26 +2078,22 @@ const validate_CHILD: Injector = (inj: Injection) => {
2072
2078
  return NONE
2073
2079
  }
2074
2080
 
2075
-
2076
2081
  // TODO: implement SOME, ALL
2077
2082
  // FIX: ONE should mean exactly one, not at least one (=SOME)
2078
2083
  // TODO: implement a generate validate_ALT to do all of these
2079
2084
  // Match at least one of the specified shapes.
2080
2085
  // Syntax: ['`$ONE`', alt0, alt1, ...]
2081
- const validate_ONE: Injector = (
2082
- inj: Injection,
2083
- _val: any,
2084
- _ref: string,
2085
- store: any
2086
- ) => {
2086
+ const validate_ONE: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2087
2087
  const { mode, parent, keyI } = inj
2088
2088
 
2089
2089
  // Only operate in val mode, since parent is a list.
2090
2090
  if (M_VAL === mode) {
2091
2091
  if (!islist(parent) || 0 !== keyI) {
2092
- inj.errs.push('The $ONE validator at field ' +
2093
- pathify(inj.path, 1, 1) +
2094
- ' 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
+ )
2095
2097
  return
2096
2098
  }
2097
2099
 
@@ -2103,19 +2105,20 @@ const validate_ONE: Injector = (
2103
2105
  inj.path = slice(inj.path, -1)
2104
2106
  inj.key = getelem(inj.path, -1)
2105
2107
 
2106
- let tvals = slice(parent, 1)
2108
+ const tvals = slice(parent, 1)
2107
2109
  if (0 === size(tvals)) {
2108
- inj.errs.push('The $ONE validator at field ' +
2109
- pathify(inj.path, 1, 1) +
2110
- ' 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
+ )
2111
2115
  return
2112
2116
  }
2113
2117
 
2114
2118
  // See if we can find a match.
2115
- for (let tval of tvals) {
2116
-
2119
+ for (const tval of tvals) {
2117
2120
  // If match, then errs.length = 0
2118
- let terrs: any[] = []
2121
+ const terrs: any[] = []
2119
2122
 
2120
2123
  const vstore = merge([{}, store], 1)
2121
2124
  vstore.$TOP = inj.dparent
@@ -2135,27 +2138,38 @@ const validate_ONE: Injector = (
2135
2138
  }
2136
2139
 
2137
2140
  // There was no match.
2138
- const valdesc =
2139
- replace(join(items(tvals, (n) => stringify(n[1])), ', '),
2140
- 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
+ )
2141
2149
 
2142
- inj.errs.push(_invalidTypeMsg(
2143
- inj.path,
2144
- (1 < size(tvals) ? 'one of ' : '') + valdesc,
2145
- 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
+ )
2146
2159
  }
2147
2160
  }
2148
2161
 
2149
-
2150
2162
  const validate_EXACT: Injector = (inj: Injection) => {
2151
2163
  const { mode, parent, key, keyI } = inj
2152
2164
 
2153
2165
  // Only operate in val mode, since parent is a list.
2154
2166
  if (M_VAL === mode) {
2155
2167
  if (!islist(parent) || 0 !== keyI) {
2156
- inj.errs.push('The $EXACT validator at field ' +
2157
- pathify(inj.path, 1, 1) +
2158
- ' 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
+ )
2159
2173
  return
2160
2174
  }
2161
2175
 
@@ -2168,17 +2182,19 @@ const validate_EXACT: Injector = (inj: Injection) => {
2168
2182
  inj.path = slice(inj.path, 0, -1)
2169
2183
  inj.key = getelem(inj.path, -1)
2170
2184
 
2171
- let tvals = slice(parent, 1)
2185
+ const tvals = slice(parent, 1)
2172
2186
  if (0 === size(tvals)) {
2173
- inj.errs.push('The $EXACT validator at field ' +
2174
- pathify(inj.path, 1, 1) +
2175
- ' 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
+ )
2176
2192
  return
2177
2193
  }
2178
2194
 
2179
2195
  // See if we can find an exact value match.
2180
2196
  let currentstr: string | undefined = undefined
2181
- for (let tval of tvals) {
2197
+ for (const tval of tvals) {
2182
2198
  let exactmatch = tval === inj.dparent
2183
2199
 
2184
2200
  if (!exactmatch && isnode(tval)) {
@@ -2193,31 +2209,35 @@ const validate_EXACT: Injector = (inj: Injection) => {
2193
2209
  }
2194
2210
 
2195
2211
  // There was no match.
2196
- const valdesc =
2197
- replace(join(items(tvals, (n) => stringify(n[1])), ', '),
2198
- R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
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
+ )
2199
2220
 
2200
- inj.errs.push(_invalidTypeMsg(
2201
- inj.path,
2202
- (1 < size(inj.path) ? '' : 'value ') +
2203
- 'exactly equal to ' + (1 === size(tvals) ? '' : 'one of ') + valdesc,
2204
- typify(inj.dparent), inj.dparent, 'V0110'))
2205
- }
2206
- 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 {
2207
2234
  delprop(parent, key)
2208
2235
  }
2209
2236
  }
2210
2237
 
2211
-
2212
2238
  // This is the "modify" argument to inject. Use this to perform
2213
2239
  // generic validation. Runs *after* any special commands.
2214
- const _validation: Modify = (
2215
- pval: any,
2216
- key?: any,
2217
- parent?: any,
2218
- inj?: Injection,
2219
- ) => {
2220
-
2240
+ const _validation: Modify = (pval: any, key?: any, parent?: any, inj?: Injection) => {
2221
2241
  if (NONE === inj) {
2222
2242
  return
2223
2243
  }
@@ -2263,40 +2283,37 @@ const _validation: Modify = (
2263
2283
  // Empty spec object {} means object can be open (any keys).
2264
2284
  if (0 < size(pkeys) && true !== getprop(pval, '`$OPEN`')) {
2265
2285
  const badkeys = []
2266
- for (let ckey of ckeys) {
2267
- 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)) {
2268
2291
  badkeys.push(ckey)
2269
2292
  }
2270
2293
  }
2271
2294
 
2272
2295
  // Closed object, so reject extra keys not in shape.
2273
2296
  if (0 < size(badkeys)) {
2274
- const msg =
2275
- '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, ', ')
2276
2298
  inj.errs.push(msg)
2277
2299
  }
2278
- }
2279
- else {
2300
+ } else {
2280
2301
  // Object is open, so merge in extra keys.
2281
2302
  merge([pval, cval])
2282
2303
  if (isnode(pval)) {
2283
2304
  delprop(pval, '`$OPEN`')
2284
2305
  }
2285
2306
  }
2286
- }
2287
- else if (islist(cval)) {
2307
+ } else if (islist(cval)) {
2288
2308
  if (!islist(pval)) {
2289
2309
  inj.errs.push(_invalidTypeMsg(inj.path, typename(ptype), ctype, cval, 'V0030'))
2290
2310
  }
2291
- }
2292
- else if (exact) {
2311
+ } else if (exact) {
2293
2312
  if (cval !== pval) {
2294
2313
  const pathmsg = 1 < size(inj.path) ? 'at field ' + pathify(inj.path, 1) + S_VIZ : S_MT
2295
- inj.errs.push('Value ' + pathmsg + cval +
2296
- ' should equal ' + pval + S_DT)
2314
+ inj.errs.push('Value ' + pathmsg + cval + ' should equal ' + pval + S_DT)
2297
2315
  }
2298
- }
2299
- else {
2316
+ } else {
2300
2317
  // Spec value was a default, copy over data
2301
2318
  setprop(parent, key, cval)
2302
2319
  }
@@ -2304,8 +2321,6 @@ const _validation: Modify = (
2304
2321
  return
2305
2322
  }
2306
2323
 
2307
-
2308
-
2309
2324
  // Validate a data structure against a shape specification. The shape
2310
2325
  // specification follows the "by example" principle. Plain data in
2311
2326
  // teh shape is treated as default values that also specify the
@@ -2319,51 +2334,54 @@ const _validation: Modify = (
2319
2334
  function validate(
2320
2335
  data: any, // Source data to transform into new data (original not mutated)
2321
2336
  spec: any, // Transform specification; output follows this shape
2322
- injdef?: Partial<Injection>
2337
+ injdef?: Partial<Injection>,
2323
2338
  ) {
2324
2339
  const extra = injdef?.extra
2325
2340
 
2326
2341
  const collect = null != injdef?.errs
2327
2342
  const errs = injdef?.errs || []
2328
2343
 
2329
- const store = merge([
2330
- {
2331
- // Remove the transform commands.
2332
- $DELETE: null,
2333
- $COPY: null,
2334
- $KEY: null,
2335
- $META: null,
2336
- $MERGE: null,
2337
- $EACH: null,
2338
- $PACK: null,
2339
-
2340
- $STRING: validate_STRING,
2341
- $NUMBER: validate_TYPE,
2342
- $INTEGER: validate_TYPE,
2343
- $DECIMAL: validate_TYPE,
2344
- $BOOLEAN: validate_TYPE,
2345
- $NULL: validate_TYPE,
2346
- $NIL: validate_TYPE,
2347
- $MAP: validate_TYPE,
2348
- $LIST: validate_TYPE,
2349
- $FUNCTION: validate_TYPE,
2350
- $INSTANCE: validate_TYPE,
2351
- $ANY: validate_ANY,
2352
- $CHILD: validate_CHILD,
2353
- $ONE: validate_ONE,
2354
- $EXACT: validate_EXACT,
2355
- },
2356
-
2357
- getdef(extra, {}),
2358
-
2359
- // A special top level value to collect errors.
2360
- // NOTE: collecterrs parameter always wins.
2361
- {
2362
- $ERRS: errs,
2363
- }
2364
- ], 1)
2365
-
2366
- 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', {})
2367
2385
  setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false))
2368
2386
 
2369
2387
  const out = transform(data, spec, {
@@ -2374,7 +2392,7 @@ function validate(
2374
2392
  errs,
2375
2393
  })
2376
2394
 
2377
- const generr = (0 < size(errs) && !collect)
2395
+ const generr = 0 < size(errs) && !collect
2378
2396
  if (generr) {
2379
2397
  throw new Error(join(errs, ' | '))
2380
2398
  }
@@ -2382,10 +2400,9 @@ function validate(
2382
2400
  return out
2383
2401
  }
2384
2402
 
2385
-
2386
2403
  const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2387
2404
  if (M_KEYPRE === inj.mode) {
2388
- const terms = getprop(inj.parent, inj.key)
2405
+ const terms = _lookup(inj.parent, inj.key)
2389
2406
 
2390
2407
  const ppath = slice(inj.path, -1)
2391
2408
  const point = getpath(store, ppath)
@@ -2393,8 +2410,8 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2393
2410
  const vstore = merge([{}, store], 1)
2394
2411
  vstore.$TOP = point
2395
2412
 
2396
- for (let term of terms) {
2397
- let terrs: any[] = []
2413
+ for (const term of terms) {
2414
+ const terrs: any[] = []
2398
2415
 
2399
2416
  validate(point, term, {
2400
2417
  extra: vstore,
@@ -2404,7 +2421,8 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2404
2421
 
2405
2422
  if (0 != size(terrs)) {
2406
2423
  inj.errs.push(
2407
- 'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2424
+ 'AND:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms),
2425
+ )
2408
2426
  }
2409
2427
  }
2410
2428
 
@@ -2414,10 +2432,9 @@ const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: an
2414
2432
  }
2415
2433
  }
2416
2434
 
2417
-
2418
2435
  const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2419
2436
  if (M_KEYPRE === inj.mode) {
2420
- const terms = getprop(inj.parent, inj.key)
2437
+ const terms = _lookup(inj.parent, inj.key)
2421
2438
 
2422
2439
  const ppath = slice(inj.path, -1)
2423
2440
  const point = getpath(store, ppath)
@@ -2425,8 +2442,8 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
2425
2442
  const vstore = merge([{}, store], 1)
2426
2443
  vstore.$TOP = point
2427
2444
 
2428
- for (let term of terms) {
2429
- let terrs: any[] = []
2445
+ for (const term of terms) {
2446
+ const terrs: any[] = []
2430
2447
 
2431
2448
  validate(point, term, {
2432
2449
  extra: vstore,
@@ -2443,15 +2460,13 @@ const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any
2443
2460
  }
2444
2461
  }
2445
2462
 
2446
- inj.errs.push(
2447
- 'OR:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2463
+ inj.errs.push('OR:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(terms))
2448
2464
  }
2449
2465
  }
2450
2466
 
2451
-
2452
2467
  const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
2453
2468
  if (M_KEYPRE === inj.mode) {
2454
- const term = getprop(inj.parent, inj.key)
2469
+ const term = _lookup(inj.parent, inj.key)
2455
2470
 
2456
2471
  const ppath = slice(inj.path, -1)
2457
2472
  const point = getpath(store, ppath)
@@ -2459,7 +2474,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2459
2474
  const vstore = merge([{}, store], 1)
2460
2475
  vstore.$TOP = point
2461
2476
 
2462
- let terrs: any[] = []
2477
+ const terrs: any[] = []
2463
2478
 
2464
2479
  validate(point, term, {
2465
2480
  extra: vstore,
@@ -2468,8 +2483,7 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2468
2483
  })
2469
2484
 
2470
2485
  if (0 == size(terrs)) {
2471
- inj.errs.push(
2472
- 'NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
2486
+ inj.errs.push('NOT:' + pathify(ppath) + S_VIZ + stringify(point) + ' fail:' + stringify(term))
2473
2487
  }
2474
2488
 
2475
2489
  const gkey = getelem(inj.path, -2)
@@ -2478,10 +2492,9 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
2478
2492
  }
2479
2493
  }
2480
2494
 
2481
-
2482
2495
  const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
2483
2496
  if (M_KEYPRE === inj.mode) {
2484
- const term = getprop(inj.parent, inj.key)
2497
+ const term = _lookup(inj.parent, inj.key)
2485
2498
  // const src = getprop(store, inj.base, store)
2486
2499
  const gkey = getelem(inj.path, -2)
2487
2500
 
@@ -2494,17 +2507,13 @@ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any
2494
2507
 
2495
2508
  if ('$GT' === ref && point > term) {
2496
2509
  pass = true
2497
- }
2498
- else if ('$LT' === ref && point < term) {
2510
+ } else if ('$LT' === ref && point < term) {
2499
2511
  pass = true
2500
- }
2501
- else if ('$GTE' === ref && point >= term) {
2512
+ } else if ('$GTE' === ref && point >= term) {
2502
2513
  pass = true
2503
- }
2504
- else if ('$LTE' === ref && point <= term) {
2514
+ } else if ('$LTE' === ref && point <= term) {
2505
2515
  pass = true
2506
- }
2507
- else if ('$LIKE' === ref && stringify(point).match(RegExp(term))) {
2516
+ } else if ('$LIKE' === ref && re_test(re_compile(term), stringify(point))) {
2508
2517
  pass = true
2509
2518
  }
2510
2519
 
@@ -2512,17 +2521,23 @@ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any
2512
2521
  // Update spec to match found value so that _validate does not complain.
2513
2522
  const gp = getelem(inj.nodes, -2)
2514
2523
  setprop(gp, gkey, point)
2515
- }
2516
- else {
2517
- inj.errs.push('CMP: ' + pathify(ppath) + S_VIZ + stringify(point) +
2518
- ' 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
+ )
2519
2535
  }
2520
2536
  }
2521
2537
 
2522
2538
  return NONE
2523
2539
  }
2524
2540
 
2525
-
2526
2541
  // Select children from a top-level object that match a MongoDB-style query.
2527
2542
  // Supports $and, $or, and equality comparisons.
2528
2543
  // For arrays, children are elements; for objects, children are values.
@@ -2533,12 +2548,11 @@ function select(children: any, query: any): any[] {
2533
2548
  }
2534
2549
 
2535
2550
  if (ismap(children)) {
2536
- children = items(children, n => {
2551
+ children = items(children, (n) => {
2537
2552
  setprop(n[1], S_DKEY, n[0])
2538
2553
  return n[1]
2539
2554
  })
2540
- }
2541
- else {
2555
+ } else {
2542
2556
  children = items(children, (n) => (setprop(n[1], S_DKEY, +n[0]), n[1]))
2543
2557
  }
2544
2558
 
@@ -2555,7 +2569,7 @@ function select(children: any, query: any): any[] {
2555
2569
  $GTE: select_CMP,
2556
2570
  $LTE: select_CMP,
2557
2571
  $LIKE: select_CMP,
2558
- }
2572
+ },
2559
2573
  }
2560
2574
 
2561
2575
  const q = clone(query)
@@ -2580,26 +2594,25 @@ function select(children: any, query: any): any[] {
2580
2594
  return results
2581
2595
  }
2582
2596
 
2583
-
2584
2597
  // Injection state used for recursive injection into JSON - like data structures.
2585
2598
  class Injection {
2586
- mode: InjectMode // Injection mode: M_KEYPRE, M_VAL, M_KEYPOST.
2587
- full: boolean // Transform escape was full key name.
2588
- keyI: number // Index of parent key in list of parent keys.
2589
- keys: string[] // List of parent keys.
2590
- key: string // Current parent key.
2591
- val: any // Current child value.
2592
- parent: any // Current parent (in transform specification).
2593
- path: string[] // Path to current node.
2594
- nodes: any[] // Stack of ancestor nodes.
2595
- handler: Injector // Custom handler for injections.
2596
- 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.
2597
2610
  meta: Record<string, any> // Custom meta data. NOTE: do not merge, values must remain as-is.
2598
- dparent: any // Current data parent node (contains current data value).
2599
- dpath: string[] // Current data value path
2600
- base?: string // Base key for data in store, if any.
2601
- modify?: Modify // Modify injection output.
2602
- 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.
2603
2616
  extra?: any
2604
2617
 
2605
2618
  constructor(val: any, parent: any) {
@@ -2622,42 +2635,56 @@ class Injection {
2622
2635
  this.meta = {}
2623
2636
  }
2624
2637
 
2625
-
2626
2638
  toString(prefix?: string) {
2627
- return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
2639
+ return (
2640
+ 'INJ' +
2641
+ (null == prefix ? '' : S_FS + prefix) +
2642
+ S_CN +
2628
2643
  pad(pathify(this.path, 1)) +
2629
- MODENAME[this.mode] + (this.full ? '/full' : '') + S_CN +
2630
- 'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
2631
- ' p=' + stringify(this.parent, -1, 1) +
2632
- ' m=' + stringify(this.meta, -1, 1) +
2633
- ' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
2634
- ' 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
+ )
2635
2666
  }
2636
2667
 
2637
-
2638
2668
  descend() {
2639
2669
  this.meta.__d++
2640
2670
  const parentkey = getelem(this.path, -2)
2641
2671
 
2642
2672
  // Resolve current node in store for local paths.
2643
2673
  if (NONE === this.dparent) {
2644
-
2645
2674
  // Even if there's no data, dpath should continue to match path, so that
2646
2675
  // relative paths work properly.
2647
2676
  if (1 < size(this.dpath)) {
2648
2677
  this.dpath = flatten([this.dpath, parentkey])
2649
2678
  }
2650
- }
2651
- else {
2679
+ } else {
2652
2680
  // this.dparent is the containing node of the current store value.
2653
2681
  if (null != parentkey) {
2654
2682
  this.dparent = getprop(this.dparent, parentkey)
2655
2683
 
2656
- let lastpart = getelem(this.dpath, -1)
2684
+ const lastpart = getelem(this.dpath, -1)
2657
2685
  if (lastpart === '$:' + parentkey) {
2658
2686
  this.dpath = slice(this.dpath, -1)
2659
- }
2660
- else {
2687
+ } else {
2661
2688
  this.dpath = flatten([this.dpath, parentkey])
2662
2689
  }
2663
2690
  }
@@ -2667,7 +2694,6 @@ class Injection {
2667
2694
  return this.dparent
2668
2695
  }
2669
2696
 
2670
-
2671
2697
  child(keyI: number, keys: string[]) {
2672
2698
  const key = strkey(keys[keyI])
2673
2699
  const val = this.val
@@ -2694,20 +2720,17 @@ class Injection {
2694
2720
  return cinj
2695
2721
  }
2696
2722
 
2697
-
2698
2723
  setval(val: any, ancestor?: number) {
2699
2724
  let parent = NONE
2700
2725
  if (null == ancestor || ancestor < 2) {
2701
- parent = NONE === val ?
2702
- this.parent = delprop(this.parent, this.key) :
2703
- setprop(this.parent, this.key, val)
2704
- }
2705
- else {
2726
+ parent =
2727
+ NONE === val
2728
+ ? (this.parent = delprop(this.parent, this.key))
2729
+ : setprop(this.parent, this.key, val)
2730
+ } else {
2706
2731
  const aval = getelem(this.nodes, 0 - ancestor)
2707
2732
  const akey = getelem(this.path, 0 - ancestor)
2708
- parent = NONE === val ?
2709
- delprop(aval, akey) :
2710
- setprop(aval, akey, val)
2733
+ parent = NONE === val ? delprop(aval, akey) : setprop(aval, akey, val)
2711
2734
  }
2712
2735
 
2713
2736
  // console.log('SETVAL', val, this.key, this.parent)
@@ -2715,42 +2738,36 @@ class Injection {
2715
2738
  }
2716
2739
  }
2717
2740
 
2718
-
2719
2741
  // Internal utilities
2720
2742
  // ==================
2721
2743
 
2722
-
2723
2744
  // // Update all references to target in inj.nodes.
2724
2745
  // function _updateAncestors(_inj: Injection, target: any, tkey: any, tval: any) {
2725
2746
  // // SetProp is sufficient in TypeScript as target reference remains consistent even for lists.
2726
2747
  // setprop(target, tkey, tval)
2727
2748
  // }
2728
2749
 
2729
-
2730
2750
  // Build a type validation error message.
2731
2751
  function _invalidTypeMsg(path: any, needtype: string, vt: number, v: any, _whence?: string) {
2732
- let vs = null == v ? 'no value' : stringify(v)
2733
-
2734
- return 'Expected ' +
2735
- (1 < size(path) ? ('field ' + pathify(path, 1) + ' to be ') : '') +
2736
- needtype + ', but found ' +
2737
- (null != v ? typename(vt) + S_VIZ : '') + vs +
2738
-
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 +
2739
2761
  // Uncomment to help debug validation errors.
2740
2762
  // ' [' + _whence + ']' +
2741
2763
 
2742
2764
  '.'
2765
+ )
2743
2766
  }
2744
2767
 
2745
-
2746
2768
  // Default inject handler for transforms. If the path resolves to a function,
2747
2769
  // call the function passing the injection inj. This is how transforms operate.
2748
- const _injecthandler: Injector = (
2749
- inj: Injection,
2750
- val: any,
2751
- ref: string,
2752
- store: any
2753
- ): any => {
2770
+ const _injecthandler: Injector = (inj: Injection, val: any, ref: string, store: any): any => {
2754
2771
  let out = val
2755
2772
  const iscmd = isfunc(val) && (NONE === ref || ref.startsWith(S_DS))
2756
2773
 
@@ -2769,37 +2786,28 @@ const _injecthandler: Injector = (
2769
2786
  return out
2770
2787
  }
2771
2788
 
2772
-
2773
- const _validatehandler: Injector = (
2774
- inj: Injection,
2775
- val: any,
2776
- ref: string,
2777
- store: any
2778
- ): any => {
2789
+ const _validatehandler: Injector = (inj: Injection, val: any, ref: string, store: any): any => {
2779
2790
  let out = val
2780
2791
 
2781
- const m = ref.match(R_META_PATH)
2792
+ const m = re_find(R_META_PATH, ref)
2782
2793
  const ismetapath = null != m
2783
2794
 
2784
2795
  if (ismetapath) {
2785
2796
  if ('=' === m[2]) {
2786
2797
  inj.setval([S_BEXACT, val])
2787
- }
2788
- else {
2798
+ } else {
2789
2799
  inj.setval(val)
2790
2800
  }
2791
2801
  inj.keyI = -1
2792
2802
 
2793
2803
  out = SKIP
2794
- }
2795
- else {
2804
+ } else {
2796
2805
  out = _injecthandler(inj, val, ref, store)
2797
2806
  }
2798
2807
 
2799
2808
  return out
2800
2809
  }
2801
2810
 
2802
-
2803
2811
  // Inject values from a data store into a string. Not a public utility - used by
2804
2812
  // `inject`. Inject are marked with `path` where path is resolved
2805
2813
  // with getpath against the store or current (if defined)
@@ -2809,11 +2817,7 @@ const _validatehandler: Injector = (
2809
2817
  // upper case letters only, and 999 is any digits, which are
2810
2818
  // discarded. This syntax specifies the name of a transform, and
2811
2819
  // optionally allows transforms to be ordered by alphanumeric sorting.
2812
- function _injectstr(
2813
- val: string,
2814
- store: any,
2815
- inj?: Injection
2816
- ): any {
2820
+ function _injectstr(val: string, store: any, inj?: Injection): any {
2817
2821
  // Can't inject into non-strings
2818
2822
  if (S_string !== typeof val || S_MT === val) {
2819
2823
  return S_MT
@@ -2822,7 +2826,7 @@ function _injectstr(
2822
2826
  let out: any = val
2823
2827
 
2824
2828
  // Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
2825
- const m = val.match(R_INJECTION_FULL)
2829
+ const m = re_find(R_INJECTION_FULL, val)
2826
2830
 
2827
2831
  // Full string of the val is an injection.
2828
2832
  if (m) {
@@ -2833,20 +2837,18 @@ function _injectstr(
2833
2837
 
2834
2838
  // Special escapes inside injection.
2835
2839
  if (3 < size(pathref)) {
2836
- 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)
2837
2841
  }
2838
2842
 
2839
2843
  // Get the extracted path reference.
2840
2844
  out = getpath(store, pathref, inj)
2841
- }
2842
-
2843
- else {
2845
+ } else {
2844
2846
  // Check for injections within the string.
2845
2847
  const partial = (_m: string, ref: string) => {
2846
2848
  // Special escapes inside injection.
2847
2849
 
2848
2850
  if (3 < size(ref)) {
2849
- 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)
2850
2852
  }
2851
2853
 
2852
2854
  if (inj) {
@@ -2859,7 +2861,7 @@ function _injectstr(
2859
2861
  return NONE === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
2860
2862
  }
2861
2863
 
2862
- out = val.replace(R_INJECTION_PARTIAL, partial)
2864
+ out = re_replace(R_INJECTION_PARTIAL, val, (m) => partial('', m[1]))
2863
2865
 
2864
2866
  // Also call the inj handler on the entire string, providing the
2865
2867
  // option for custom injection.
@@ -2872,11 +2874,9 @@ function _injectstr(
2872
2874
  return out
2873
2875
  }
2874
2876
 
2875
-
2876
2877
  // Handler Utilities
2877
2878
  // =================
2878
2879
 
2879
-
2880
2880
  const MODENAME: any = {
2881
2881
  [M_VAL]: 'val',
2882
2882
  [M_KEYPRE]: 'key:pre',
@@ -2893,28 +2893,44 @@ function checkPlacement(
2893
2893
  modes: InjectMode,
2894
2894
  ijname: string,
2895
2895
  parentTypes: number,
2896
- inj: Injection
2896
+ inj: Injection,
2897
2897
  ): boolean {
2898
2898
  if (0 === (modes & inj.mode)) {
2899
- inj.errs.push('$' + ijname + ': invalid placement as ' + PLACEMENT[inj.mode] +
2900
- ', expected: ' + join(items(
2901
- [M_KEYPRE, M_KEYPOST, M_VAL].filter(m => modes & m),
2902
- (n: any) => PLACEMENT[n[1]]), ',') + '.')
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
+ )
2903
2914
  return false
2904
2915
  }
2905
2916
  if (!isempty(parentTypes)) {
2906
2917
  const ptype = typify(inj.parent)
2907
2918
  if (0 === (parentTypes & ptype)) {
2908
- inj.errs.push('$' + ijname + ': invalid placement in parent ' + typename(ptype) +
2909
- ', 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
+ )
2910
2928
  return false
2911
-
2912
2929
  }
2913
2930
  }
2914
2931
  return true
2915
2932
  }
2916
2933
 
2917
-
2918
2934
  // function injectorArgs(argTypes: number[], inj: Injection): any {
2919
2935
  function injectorArgs(argTypes: number[], args: any[]): any {
2920
2936
  const numargs = size(argTypes)
@@ -2925,9 +2941,16 @@ function injectorArgs(argTypes: number[], args: any[]): any {
2925
2941
  const arg = args[argI]
2926
2942
  const argType = typify(arg)
2927
2943
  if (0 === (argTypes[argI] & argType)) {
2928
- found[0] = 'invalid argument: ' + stringify(arg, 22) +
2929
- ' (' + typename(argType) + ' at position ' + (1 + argI) +
2930
- ') is not of type: ' + typename(argTypes[argI]) + '.'
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
+ '.'
2931
2954
  break
2932
2955
  }
2933
2956
  found[1 + argI] = arg
@@ -2935,7 +2958,6 @@ function injectorArgs(argTypes: number[], args: any[]): any {
2935
2958
  return found
2936
2959
  }
2937
2960
 
2938
-
2939
2961
  function injectChild(child: any, store: any, inj: Injection): Injection {
2940
2962
  let cinj = inj
2941
2963
 
@@ -2945,8 +2967,7 @@ function injectChild(child: any, store: any, inj: Injection): Injection {
2945
2967
  cinj = inj.prior.prior.child(inj.prior.keyI, inj.prior.keys)
2946
2968
  cinj.val = child
2947
2969
  setprop(cinj.parent, inj.prior.key, child)
2948
- }
2949
- else {
2970
+ } else {
2950
2971
  cinj = inj.prior.child(inj.keyI, inj.keys)
2951
2972
  cinj.val = child
2952
2973
  setprop(cinj.parent, inj.key, child)
@@ -2959,7 +2980,6 @@ function injectChild(child: any, store: any, inj: Injection): Injection {
2959
2980
  return cinj
2960
2981
  }
2961
2982
 
2962
-
2963
2983
  class StructUtility {
2964
2984
  clone = clone
2965
2985
  delprop = delprop
@@ -3066,13 +3086,16 @@ export {
3066
3086
  typename,
3067
3087
  validate,
3068
3088
  walk,
3069
-
3089
+ re_compile,
3090
+ re_find,
3091
+ re_find_all,
3092
+ re_replace,
3093
+ re_test,
3094
+ re_escape,
3070
3095
  SKIP,
3071
3096
  DELETE,
3072
-
3073
3097
  jm,
3074
3098
  jt,
3075
-
3076
3099
  T_any,
3077
3100
  T_noval,
3078
3101
  T_boolean,
@@ -3088,20 +3111,13 @@ export {
3088
3111
  T_instance,
3089
3112
  T_scalar,
3090
3113
  T_node,
3091
-
3092
3114
  M_KEYPRE,
3093
3115
  M_KEYPOST,
3094
3116
  M_VAL,
3095
-
3096
3117
  MODENAME,
3097
-
3098
3118
  checkPlacement,
3099
3119
  injectorArgs,
3100
3120
  injectChild,
3101
3121
  }
3102
3122
 
3103
- export type {
3104
- Injection,
3105
- Injector,
3106
- WalkApply
3107
- }
3123
+ export type { Injection, Injector, WalkApply }