@voxgig/struct 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/struct.ts CHANGED
@@ -59,8 +59,10 @@ const S_MVAL = 'val'
59
59
  const S_MKEY = 'key'
60
60
 
61
61
  // Special keys.
62
- const S_DKEY = '`$KEY`'
63
- const S_DMETA = '`$META`'
62
+ const S_BKEY = '`$KEY`'
63
+ const S_BANNO = '`$ANNO`'
64
+
65
+ const S_DKEY = '$KEY'
64
66
  const S_DTOP = '$TOP'
65
67
  const S_DERRS = '$ERRS'
66
68
  const S_DSPEC = '$SPEC'
@@ -74,22 +76,49 @@ const S_number = 'number'
74
76
  const S_object = 'object'
75
77
  const S_string = 'string'
76
78
  const S_null = 'null'
79
+ const S_key = 'key'
77
80
  const S_MT = ''
78
81
  const S_BT = '`'
79
82
  const S_DS = '$'
80
83
  const S_DT = '.'
81
84
  const S_CN = ':'
82
85
  const S_FS = '/'
86
+ const S_OS = '['
87
+ const S_CS = ']'
88
+ const S_SP = ' '
83
89
  const S_KEY = 'KEY'
84
90
 
85
91
 
86
92
  // The standard undefined value for this language.
87
93
  const UNDEF = undefined
88
94
 
95
+ // Private marker to indicate a skippable value.
96
+ const SKIP = { '`$SKIP`': true }
97
+
98
+ // Regular expression constants
99
+ const R_INTEGER_KEY = /^[-0-9]+$/ // Match integer keys (including <0).
100
+ const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g // Chars that need escaping in regexp.
101
+ const R_TRAILING_SLASH = /\/+$/ // Trailing slashes in URLs.
102
+ const R_LEADING_TRAILING_SLASH = /([^\/])\/+/ // Multiple slashes in URL middle.
103
+ const R_LEADING_SLASH = /^\/+/ // Leading slashes in URLs.
104
+ const R_QUOTES = /"/g // Double quotes for removal.
105
+ const R_DOT = /\./g // Dots in path strings.
106
+ const R_FUNCTION_REF = /^`\$FUNCTION:([0-9]+)`$/ // Function reference in clone.
107
+ const R_META_PATH = /^([^$]+)\$([=~])(.+)$/ // Meta path syntax.
108
+ const R_DOUBLE_DOLLAR = /\$\$/g // Double dollar escape sequence.
109
+ const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g // Transform command names.
110
+ const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/ // Full string injection pattern.
111
+ const R_BT_ESCAPE = /\$BT/g // Backtick escape sequence.
112
+ const R_DS_ESCAPE = /\$DS/g // Dollar sign escape sequence.
113
+ const R_INJECTION_PARTIAL = /`([^`]+)`/g // Partial string injection pattern.
114
+
89
115
 
90
116
  // Keys are strings for maps, or integers for lists.
91
117
  type PropKey = string | number
92
118
 
119
+ // Type that can be indexed by both string and number keys.
120
+ type Indexable = { [key: string]: any } & { [key: number]: any }
121
+
93
122
 
94
123
  // For each key in a node (map or list), perform value injections in
95
124
  // three phases: on key value, before child, and then on key value again.
@@ -101,111 +130,25 @@ type InjectMode = 'key:pre' | 'key:post' | 'val'
101
130
  // - `a.b.c`: insert value at {a:{b:{c:1}}}
102
131
  // - `$FOO`: apply transform FOO
103
132
  type Injector = (
104
- inj: Injection, // Injection state.
133
+ inj: Injection, // Injection state.
105
134
  val: any, // Injection value specification.
106
- current: any, // Current source parent value.
107
135
  ref: string, // Original injection reference string.
108
136
  store: any, // Current source root value.
109
137
  ) => any
110
138
 
111
139
 
112
- // Injection state used for recursive injection into JSON - like data structures.
113
- class Injection {
114
- mode: InjectMode // Injection mode: key:pre, val, key:post.
115
- full: boolean // Transform escape was full key name.
116
- keyI: number // Index of parent key in list of parent keys.
117
- keys: string[] // List of parent keys.
118
- key: string // Current parent key.
119
- val: any // Current child value.
120
- parent: any // Current parent (in transform specification).
121
- path: string[] // Path to current node.
122
- nodes: any[] // Stack of ancestor nodes.
123
- handler: Injector // Custom handler for injections.
124
- errs: any[] // Error collector.
125
- meta: Record<string, any> // Custom meta data.
126
- base?: string // Base key for data in store, if any.
127
- modify?: Modify // Modify injection output.
128
- prior?: Injection
129
-
130
- constructor(val: any, parent: any, modify?: Modify, errs?: any[]) {
131
- this.val = val
132
- this.parent = parent
133
-
134
- this.mode = S_MVAL as InjectMode
135
- this.full = false
136
- this.keyI = 0
137
- this.keys = [S_DTOP]
138
- this.key = S_DTOP
139
- this.path = [S_DTOP]
140
- this.nodes = [parent]
141
- this.handler = _injecthandler
142
- this.base = S_DTOP
143
- this.modify = modify
144
- this.errs = errs || []
145
- this.meta = {}
146
- }
147
-
148
- toString(prefix?: string) {
149
- return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
150
- pad(pathify(this.path)) +
151
- this.mode + (this.full ? '/full' : '') + S_CN +
152
- 'key=' + this.keyI + S_FS + this.key + S_FS + this.keys +
153
- ' p=' + stringify(this.parent, -1, 1) +
154
- ' m=' + stringify(this.meta, -1, 1) +
155
- ' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1)
156
- }
157
-
158
- child(keyI: number, keys: string[]) {
159
- const key = strkey(keys[keyI])
160
- const val = this.val
161
-
162
- const cinj = new Injection(
163
- getprop(val, key),
164
- val,
165
- this.modify,
166
- this.errs
167
- )
168
- cinj.keyI = keyI
169
- cinj.keys = keys
170
- cinj.key = key
171
-
172
- cinj.path = [...(this.path || []), key]
173
- cinj.nodes = [...(this.nodes || []), val]
174
-
175
- cinj.mode = this.mode
176
- cinj.handler = this.handler
177
- cinj.base = this.base
178
- cinj.meta = this.meta
179
- cinj.prior = this
180
-
181
- return cinj
182
- }
183
-
184
- setval(val: any, ancestor?: number) {
185
- if (null == ancestor || ancestor < 2) {
186
- return setprop(this.parent, this.key, val)
187
- }
188
- else {
189
- return setprop(getelem(this.nodes, 0 - ancestor), getelem(this.path, 0 - ancestor), val)
190
- }
191
- }
192
- }
193
-
194
-
195
140
  // Apply a custom modification to injections.
196
141
  type Modify = (
197
142
  val: any, // Value.
198
143
  key?: PropKey, // Value key, if any,
199
144
  parent?: any, // Parent node, if any.
200
- inj?: Injection, // Injection state, if any.
201
- current?: any, // Current value in store (matches path).
145
+ inj?: Injection, // Injection state, if any.
202
146
  store?: any, // Store, if any
203
147
  ) => void
204
148
 
205
149
 
206
150
  // Function applied to each node and leaf when walking a node structure depth first.
207
- // NOTE: For {a:{b:1}} the call sequence args will be:
208
- // b, 1, {b:1}, [a,b]
151
+ // For {a:{b:1}} the call sequence args will be: b, 1, {b:1}, [a,b].
209
152
  type WalkApply = (
210
153
  // Map keys are strings, list keys are numbers, top key is UNDEF
211
154
  key: string | number | undefined,
@@ -215,26 +158,29 @@ type WalkApply = (
215
158
  ) => any
216
159
 
217
160
 
161
+
218
162
  // Value is a node - defined, and a map (hash) or list (array).
219
- function isnode(val: any) {
163
+ // NOTE: typescript
164
+ // things
165
+ function isnode(val: any): val is Indexable {
220
166
  return null != val && S_object == typeof val
221
167
  }
222
168
 
223
169
 
224
170
  // Value is a defined map (hash) with string keys.
225
- function ismap(val: any) {
171
+ function ismap(val: any): val is { [key: string]: any } {
226
172
  return null != val && S_object == typeof val && !Array.isArray(val)
227
173
  }
228
174
 
229
175
 
230
176
  // Value is a defined list (array) with integer keys (indexes).
231
- function islist(val: any) {
177
+ function islist(val: any): val is any[] {
232
178
  return Array.isArray(val)
233
179
  }
234
180
 
235
181
 
236
182
  // Value is a defined string (non-empty) or integer key.
237
- function iskey(key: any) {
183
+ function iskey(key: any): key is PropKey {
238
184
  const keytype = typeof key
239
185
  return (S_string === keytype && S_MT !== key) || S_number === keytype
240
186
  }
@@ -249,11 +195,13 @@ function isempty(val: any) {
249
195
 
250
196
 
251
197
  // Value is a function.
252
- function isfunc(val: any) {
198
+ function isfunc(val: any): val is Function {
253
199
  return S_function === typeof val
254
200
  }
255
201
 
256
202
 
203
+ // The integer size of the value. For arrays and strings, the length,
204
+ // for numbers, the integer part, for boolean, true is 1 and falso 0, for all other values, 0.
257
205
  function size(val: any): number {
258
206
  if (islist(val)) {
259
207
  return val.length
@@ -279,40 +227,66 @@ function size(val: any): number {
279
227
  }
280
228
 
281
229
 
282
- function slice<V extends any>(val: V, start: number, end?: number): V {
283
- if (islist(val)) {
284
- const vlen = size(val)
285
- if (null != start) {
286
- if (start < 0) {
287
- end = vlen + start
288
- if (end < 0) {
289
- end = 0
290
- }
291
- start = 0
230
+ // Extract part of an array or string into a new value, from the start point to the end point.
231
+ // If no end is specified, extract to the full length of the value. Negative arguments count
232
+ // from the end of the value. For numbers, perform min and max bounding, where start is
233
+ // inclusive, and end is *exclusive*.
234
+ function slice<V extends any>(val: V, start?: number, end?: number): V {
235
+ if (S_number === typeof val) {
236
+ start = null == start || S_number !== typeof start ? Number.MIN_SAFE_INTEGER : start
237
+ end = (null == end || S_number !== typeof end ? Number.MAX_SAFE_INTEGER : end) - 1
238
+ return Math.min(Math.max(val as number, start), end) as V
239
+ }
240
+
241
+ const vlen = size(val)
242
+
243
+ if (null != end && null == start) {
244
+ start = 0
245
+ }
246
+
247
+ if (null != start) {
248
+ if (start < 0) {
249
+ end = vlen + start
250
+ if (end < 0) {
251
+ end = 0
292
252
  }
253
+ start = 0
254
+ }
293
255
 
294
- else if (null != end) {
256
+ else if (null != end) {
257
+ if (end < 0) {
258
+ end = vlen + end
295
259
  if (end < 0) {
296
- end = vlen + end
297
- if (end < 0) {
298
- end = 0
299
- }
300
- }
301
- else if (vlen < end) {
302
- end = val.length
260
+ end = 0
303
261
  }
304
262
  }
305
-
306
- else {
307
- end = val.length
263
+ else if (vlen < end) {
264
+ end = vlen
308
265
  }
266
+ }
309
267
 
310
- if (vlen < start) {
311
- start = vlen
312
- }
268
+ else {
269
+ end = vlen
270
+ }
271
+
272
+ if (vlen < start) {
273
+ start = vlen
274
+ }
313
275
 
314
- if (-1 < start && start <= end && end <= vlen) {
315
- return val.slice(start, end) as V
276
+ if (-1 < start && start <= end && end <= vlen) {
277
+ if (islist(val)) {
278
+ val = val.slice(start, end) as V
279
+ }
280
+ else if (S_string === typeof val) {
281
+ val = (val as string).substring(start, end) as V
282
+ }
283
+ }
284
+ else {
285
+ if (islist(val)) {
286
+ val = [] as V
287
+ }
288
+ else if (S_string === typeof val) {
289
+ val = S_MT as V
316
290
  }
317
291
  }
318
292
  }
@@ -324,7 +298,7 @@ function slice<V extends any>(val: V, start: number, end?: number): V {
324
298
  function pad(str: any, padding?: number, padchar?: string): string {
325
299
  str = stringify(str)
326
300
  padding = null == padding ? 44 : padding
327
- padchar = null == padchar ? ' ' : ((padchar + ' ')[0])
301
+ padchar = null == padchar ? S_SP : ((padchar + S_SP)[0])
328
302
  return -1 < padding ? str.padEnd(padding, padchar) : str.padStart(0 - padding, padchar)
329
303
  }
330
304
 
@@ -362,7 +336,7 @@ function getelem(val: any, key: any, alt?: any) {
362
336
 
363
337
  if (islist(val)) {
364
338
  let nkey = parseInt(key)
365
- if (Number.isInteger(nkey) && ('' + key).match(/^[-0-9]+$/)) {
339
+ if (Number.isInteger(nkey) && ('' + key).match(R_INTEGER_KEY)) {
366
340
  if (nkey < 0) {
367
341
  key = val.length + nkey
368
342
  }
@@ -380,7 +354,6 @@ function getelem(val: any, key: any, alt?: any) {
380
354
 
381
355
  // Safely get a property of a node. Undefined arguments return undefined.
382
356
  // If the key is not found, return the alternative value, if any.
383
- // TODO: for arrays. make -i work, such that -1 => last element
384
357
  function getprop(val: any, key: any, alt?: any) {
385
358
  let out = alt
386
359
 
@@ -429,7 +402,7 @@ function strkey(key: any = UNDEF): string {
429
402
  // Sorted keys of a map, or indexes of a list.
430
403
  function keysof(val: any): string[] {
431
404
  return !isnode(val) ? [] :
432
- ismap(val) ? Object.keys(val).sort() : val.map((_n: any, i: number) => '' + i)
405
+ ismap(val) ? Object.keys(val).sort() : (val as any).map((_n: any, i: number) => S_MT + i)
433
406
  }
434
407
 
435
408
 
@@ -449,7 +422,7 @@ function items(val: any): [number | string, any][] {
449
422
  // Escape regular expression.
450
423
  function escre(s: string) {
451
424
  s = null == s ? S_MT : s
452
- return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
425
+ return s.replace(R_ESCAPE_REGEXP, '\\$&')
453
426
  }
454
427
 
455
428
 
@@ -463,11 +436,29 @@ function escurl(s: string) {
463
436
  // Concatenate url part strings, merging forward slashes as needed.
464
437
  function joinurl(sarr: any[]) {
465
438
  return sarr
466
- .filter(s => null != s && '' !== s)
467
- .map((s, i) => 0 === i ? s.replace(/\/+$/, '') :
468
- s.replace(/([^\/])\/+/, '$1/').replace(/^\/+/, '').replace(/\/+$/, ''))
469
- .filter(s => '' !== s)
470
- .join('/')
439
+ .filter(s => null != s && S_MT !== s)
440
+ .map((s, i) => 0 === i ? s.replace(R_TRAILING_SLASH, S_MT) :
441
+ s.replace(R_LEADING_TRAILING_SLASH, '$1/')
442
+ .replace(R_LEADING_SLASH, S_MT)
443
+ .replace(R_TRAILING_SLASH, S_MT))
444
+ .filter(s => S_MT !== s)
445
+ .join(S_FS)
446
+ }
447
+
448
+
449
+ // Output JSON in a "standard" format, with 2 space indents, each property on a new line,
450
+ // and spaces after {[: and before ]}. Any "wierd" values (NaN, etc) are output as null.
451
+ // In general, the behaivor of of JavaScript's JSON.stringify(val,null,2) is followed.
452
+ function jsonify(val: any) {
453
+ let str = S_null
454
+ if (null != val) {
455
+ str = JSON.stringify(val, null, 2)
456
+ if (UNDEF === str) {
457
+ str = S_null
458
+ }
459
+ }
460
+
461
+ return str
471
462
  }
472
463
 
473
464
 
@@ -501,7 +492,7 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
501
492
  }
502
493
 
503
494
  valstr = S_string !== typeof valstr ? S_MT + valstr : valstr
504
- valstr = valstr.replace(/"/g, '')
495
+ valstr = valstr.replace(R_QUOTES, S_MT)
505
496
 
506
497
  if (null != maxlen && -1 < maxlen) {
507
498
  let js = valstr.substring(0, maxlen)
@@ -509,6 +500,7 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
509
500
  }
510
501
 
511
502
  if (pretty) {
503
+ // Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
512
504
  let c = [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69]
513
505
  .map(n => `\x1b[38;5;${n}m`),
514
506
  r = '\x1b[0m', d = 0, o = c[0], t = o
@@ -551,8 +543,8 @@ function pathify(val: any, startin?: number, endin?: number) {
551
543
  // .filter((p: any, t: any) => (t = typeof p, S_string === t || S_number === t))
552
544
  .filter((p: any) => iskey(p))
553
545
  .map((p: any) =>
554
- 'number' === typeof p ? S_MT + Math.floor(p) :
555
- p.replace(/\./g, S_MT))
546
+ S_number === typeof p ? S_MT + Math.floor(p) :
547
+ p.replace(R_DOT, S_MT))
556
548
  .join(S_DT)
557
549
  }
558
550
  }
@@ -572,18 +564,53 @@ function clone(val: any): any {
572
564
  const replacer: any = (_k: any, v: any) => S_function === typeof v ?
573
565
  (refs.push(v), '`$FUNCTION:' + (refs.length - 1) + '`') : v
574
566
  const reviver: any = (_k: any, v: any, m: any) => S_string === typeof v ?
575
- (m = v.match(/^`\$FUNCTION:([0-9]+)`$/), m ? refs[m[1]] : v) : v
567
+ (m = v.match(R_FUNCTION_REF), m ? refs[m[1]] : v) : v
576
568
  return UNDEF === val ? UNDEF : JSON.parse(JSON.stringify(val, replacer), reviver)
577
569
  }
578
570
 
579
571
 
572
+ // Safely delete a property from an object or array element.
573
+ // Undefined arguments and invalid keys are ignored.
574
+ // Returns the (possibly modified) parent.
575
+ // For objects, the property is deleted using the delete operator.
576
+ // For arrays, the element at the index is removed and remaining elements are shifted down.
577
+ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
578
+ if (!iskey(key)) {
579
+ return parent
580
+ }
581
+
582
+ if (ismap(parent)) {
583
+ // key = S_MT + key
584
+ key = strkey(key)
585
+ delete (parent as any)[key]
586
+ }
587
+ else if (islist(parent)) {
588
+ // Ensure key is an integer.
589
+ let keyI = +key
590
+
591
+ if (isNaN(keyI)) {
592
+ return parent
593
+ }
594
+
595
+ keyI = Math.floor(keyI)
596
+
597
+ // Delete list element at position keyI, shifting later elements down.
598
+ if (0 <= keyI && keyI < parent.length) {
599
+ for (let pI = keyI; pI < parent.length - 1; pI++) {
600
+ parent[pI] = parent[pI + 1]
601
+ }
602
+ parent.length = parent.length - 1
603
+ }
604
+ }
605
+
606
+ return parent
607
+ }
608
+
609
+
580
610
  // Safely set a property. Undefined arguments and invalid keys are ignored.
581
611
  // Returns the (possibly modified) parent.
582
- // If the value is undefined the key will be deleted from the parent.
583
612
  // If the parent is a list, and the key is negative, prepend the value.
584
613
  // NOTE: If the key is above the list size, append the value; below, prepend.
585
- // If the value is undefined, remove the list element at index key, and shift the
586
- // remaining elements down. These rules avoid "holes" in the list.
587
614
  function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
588
615
  if (!iskey(key)) {
589
616
  return parent
@@ -591,12 +618,8 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
591
618
 
592
619
  if (ismap(parent)) {
593
620
  key = S_MT + key
594
- if (UNDEF === val) {
595
- delete (parent as any)[key]
596
- }
597
- else {
598
- (parent as any)[key] = val
599
- }
621
+ const pany = parent as any
622
+ pany[key] = val
600
623
  }
601
624
  else if (islist(parent)) {
602
625
  // Ensure key is an integer.
@@ -608,18 +631,8 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
608
631
 
609
632
  keyI = Math.floor(keyI)
610
633
 
611
- // Delete list element at position keyI, shifting later elements down.
612
- if (UNDEF === val) {
613
- if (0 <= keyI && keyI < parent.length) {
614
- for (let pI = keyI; pI < parent.length - 1; pI++) {
615
- parent[pI] = parent[pI + 1]
616
- }
617
- parent.length = parent.length - 1
618
- }
619
- }
620
-
621
634
  // Set or append value at position keyI, or append if keyI out of bounds.
622
- else if (0 <= keyI) {
635
+ if (0 <= keyI) {
623
636
  parent[parent.length < keyI ? parent.length : keyI] = val
624
637
  }
625
638
 
@@ -695,7 +708,7 @@ function merge(val: any): any {
695
708
  }
696
709
  else {
697
710
  // Node stack. walking down the current obj.
698
- let cur = [out]
711
+ let cur: any[] = [out]
699
712
  let cI = 0
700
713
 
701
714
  function merger(
@@ -713,7 +726,7 @@ function merge(val: any): any {
713
726
  let lenpath = path.length
714
727
  cI = lenpath - 1
715
728
  if (UNDEF === cur[cI]) {
716
- cur[cI] = getpath(slice(path, 0, lenpath - 1), out)
729
+ cur[cI] = getpath(out, slice(path, 0, lenpath - 1))
717
730
  }
718
731
 
719
732
  // Create node if needed.
@@ -746,68 +759,118 @@ function merge(val: any): any {
746
759
  }
747
760
 
748
761
 
749
- // Get a value deep inside a node using a key path. For example the
750
- // path `a.b` gets the value 1 from {a:{b:1}}. The path can specified
751
- // as a dotted string, or a string array. If the path starts with a
752
- // dot (or the first element is ''), the path is considered local, and
753
- // resolved against the `current` argument, if defined. Integer path
754
- // parts are used as array indexes. The inj argument allows for
755
- // custom handling when called from `inject` or `transform`.
756
- // TODO: change sig to be consistent with getprop
757
- function getpath(path: string | string[], store: any, current?: any, inj?: Injection) {
762
+ function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
758
763
 
759
764
  // Operate on a string array.
760
- const parts = islist(path) ? path : S_string === typeof path ? path.split(S_DT) : UNDEF
765
+ const parts = islist(path) ? path :
766
+ 'string' === typeof path ? path.split(S_DT) :
767
+ 'number' === typeof path ? [strkey(path)] : UNDEF
761
768
 
762
769
  if (UNDEF === parts) {
763
770
  return UNDEF
764
771
  }
765
772
 
766
- let root = store
773
+ // let root = store
767
774
  let val = store
768
- const base = getprop(inj, S_base)
775
+ const base = getprop(injdef, S_base)
776
+ const src = getprop(store, base, store)
777
+ const numparts = size(parts)
778
+ const dparent = getprop(injdef, 'dparent')
769
779
 
770
780
  // An empty path (incl empty string) just finds the store.
771
- if (null == path || null == store || (1 === parts.length && S_MT === parts[0])) {
772
- // The actual store data may be in a store sub property, defined by inj.base.
773
- val = getprop(store, base, store)
781
+ if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
782
+ val = src
774
783
  }
775
- else if (0 < parts.length) {
776
- let pI = 0
784
+ else if (0 < numparts) {
777
785
 
778
- // Relative path uses `current` argument.
779
- if (S_MT === parts[0]) {
780
- pI = 1
781
- root = current
786
+ // Check for $ACTIONs
787
+ if (1 === numparts) {
788
+ val = getprop(store, parts[0])
782
789
  }
783
790
 
784
- let part = pI < parts.length ? parts[pI] : UNDEF
785
- let first: any = S_MT === part ? current : getprop(root, part)
791
+ if (!isfunc(val)) {
792
+ val = src
793
+
794
+ const m = parts[0].match(R_META_PATH)
795
+ if (m && injdef && injdef.meta) {
796
+ val = getprop(injdef.meta, m[1])
797
+ parts[0] = m[3]
798
+ }
799
+
800
+ const dpath = getprop(injdef, 'dpath')
801
+
802
+ for (let pI = 0; UNDEF !== val && pI < parts.length; pI++) {
803
+ let part = parts[pI]
804
+
805
+ if (injdef && S_DKEY === part) {
806
+ part = getprop(injdef, S_key)
807
+ }
808
+ else if (injdef && part.startsWith('$GET:')) {
809
+ // $GET:path$ -> get store value, use as path part (string)
810
+ part = stringify(getpath(src, part.substring(5, part.length - 1)))
811
+ }
812
+ else if (injdef && part.startsWith('$REF:')) {
813
+ // $REF:refpath$ -> get spec value, use as path part (string)
814
+ part = stringify(getpath(getprop(store, S_DSPEC), part.substring(5, part.length - 1)))
815
+ }
816
+ else if (injdef && part.startsWith('$META:')) {
817
+ // $META:metapath$ -> get meta value, use as path part (string)
818
+ part = stringify(getpath(getprop(injdef, 'meta'), part.substring(6, part.length - 1)))
819
+ }
820
+
821
+ // $$ escapes $
822
+ part = part.replace(R_DOUBLE_DOLLAR, '$')
823
+
824
+ if (S_MT === part) {
786
825
 
787
- // At top level, check inj.base, if provided
788
- val = (UNDEF === first && 0 === pI) ?
789
- getprop(getprop(root, base), part) :
790
- first
826
+ let ascends = 0
827
+ while (S_MT === parts[1 + pI]) {
828
+ ascends++
829
+ pI++
830
+ }
791
831
 
792
- // Move along the path, trying to descend into the store.
793
- for (pI++; UNDEF !== val && pI < parts.length; pI++) {
794
- part = parts[pI]
795
- if (S_MT !== part) {
796
- val = getprop(val, parts[pI])
832
+ if (injdef && 0 < ascends) {
833
+ if (pI === parts.length - 1) {
834
+ ascends--
835
+ }
836
+
837
+ if (0 === ascends) {
838
+ val = dparent
839
+ }
840
+ else {
841
+ const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
842
+
843
+ if (ascends <= size(dpath)) {
844
+ val = getpath(store, fullpath)
845
+ }
846
+ else {
847
+ val = UNDEF
848
+ }
849
+ break
850
+ }
851
+ }
852
+ else {
853
+ val = dparent
854
+ }
855
+ }
856
+ else {
857
+ val = getprop(val, part)
858
+ }
797
859
  }
798
860
  }
799
861
  }
800
862
 
801
863
  // Inj may provide a custom handler to modify found value.
802
- if (null != inj && isfunc(inj.handler)) {
864
+ const handler = getprop(injdef, 'handler')
865
+ if (null != injdef && isfunc(handler)) {
803
866
  const ref = pathify(path)
804
- val = inj.handler(inj, val, current, ref, store)
867
+ val = handler(injdef, val, ref, store)
805
868
  }
806
869
 
807
870
  return val
808
871
  }
809
872
 
810
- // let cI = 0
873
+
811
874
 
812
875
  // Inject values from a data store into a node recursively, resolving
813
876
  // paths against the store, or current if they are local. THe modify
@@ -816,36 +879,29 @@ function getpath(path: string | string[], store: any, current?: any, inj?: Injec
816
879
  function inject(
817
880
  val: any,
818
881
  store: any,
819
- modify?: Modify,
820
- current?: any,
821
- inj?: Injection,
882
+ injdef?: Partial<Injection>,
822
883
  ) {
823
884
  const valtype = typeof val
885
+ let inj: Injection = injdef as Injection
824
886
 
825
887
  // Create state if at root of injection. The input value is placed
826
888
  // inside a virtual parent holder to simplify edge cases.
827
- if (UNDEF === inj) {
828
-
889
+ if (UNDEF === injdef || null == injdef.mode) {
829
890
  // Set up state assuming we are starting in the virtual parent.
830
- inj = new Injection(val, { [S_DTOP]: val }, modify, getprop(store, S_DERRS))
831
- inj.meta.d = 0
832
- }
833
-
834
- inj.meta.d++
835
-
836
- // Resolve current node in store for local paths.
837
- if (UNDEF === current) {
838
- // TODO: check store base prop ($TOP)
839
- current = { $TOP: store }
840
- }
841
- else {
842
- // A provided current is expected to be the containing node of the associated
843
- // store value.
844
- const parentkey = getelem(inj.path, -2)
845
- current = null == parentkey ? current : getprop(current, parentkey)
891
+ inj = new Injection(val, { [S_DTOP]: val })
892
+ inj.dparent = store
893
+ inj.errs = getprop(store, S_DERRS, [])
894
+ inj.meta.__d = 0
895
+
896
+ if (UNDEF !== injdef) {
897
+ inj.modify = null == injdef.modify ? inj.modify : injdef.modify
898
+ inj.extra = null == injdef.extra ? inj.extra : injdef.extra
899
+ inj.meta = null == injdef.meta ? inj.meta : injdef.meta
900
+ inj.handler = null == injdef.handler ? inj.handler : injdef.handler
901
+ }
846
902
  }
847
903
 
848
- // console.log('' + inj + ' c=' + stringify(current, -1, 1))
904
+ inj.descend()
849
905
 
850
906
  // Descend into node.
851
907
  if (isnode(val)) {
@@ -857,7 +913,7 @@ function inject(
857
913
  let nodekeys = ismap(val) ? [
858
914
  ...Object.keys(val).filter(k => !k.includes(S_DS)).sort(),
859
915
  ...Object.keys(val).filter(k => k.includes(S_DS)).sort(),
860
- ] : val.map((_n: any, i: number) => i)
916
+ ] : (val as any).map((_n: any, i: number) => i)
861
917
 
862
918
 
863
919
  // Each child key-value pair is processed in three injection phases:
@@ -871,7 +927,7 @@ function inject(
871
927
  childinj.mode = S_MKEYPRE
872
928
 
873
929
  // Peform the key:pre mode injection on the child key.
874
- const prekey = _injectstr(nodekey, store, current, childinj)
930
+ const prekey = _injectstr(nodekey, store, childinj)
875
931
 
876
932
  // The injection may modify child processing.
877
933
  nkI = childinj.keyI
@@ -884,7 +940,7 @@ function inject(
884
940
 
885
941
  // Perform the val mode injection on the child value.
886
942
  // NOTE: return value is not used.
887
- inject(childinj.val, store, modify, current, childinj)
943
+ inject(childinj.val, store, childinj)
888
944
 
889
945
  // The injection may modify child processing.
890
946
  nkI = childinj.keyI
@@ -892,7 +948,7 @@ function inject(
892
948
 
893
949
  // Peform the key:post mode injection on the child key.
894
950
  childinj.mode = S_MKEYPOST as InjectMode
895
- _injectstr(nodekey, store, current, childinj)
951
+ _injectstr(nodekey, store, childinj)
896
952
 
897
953
  // The injection may modify child processing.
898
954
  nkI = childinj.keyI
@@ -904,21 +960,27 @@ function inject(
904
960
  // Inject paths into string scalars.
905
961
  else if (S_string === valtype) {
906
962
  inj.mode = S_MVAL as InjectMode
907
- val = _injectstr(val, store, current, inj)
908
- setprop(inj.parent, inj.key, val)
963
+ val = _injectstr(val, store, inj)
964
+ if (SKIP !== val) {
965
+ inj.setval(val)
966
+ }
909
967
  }
910
968
 
969
+ // console.log('INJECT-M0 ', val, '' + inj)
970
+
911
971
  // Custom modification.
912
- if (modify) {
972
+ if (inj.modify && SKIP !== val) {
913
973
  let mkey = inj.key
914
974
  let mparent = inj.parent
915
975
  let mval = getprop(mparent, mkey)
916
- modify(
976
+
977
+ // console.log('INJECT-M1 ' + inj)
978
+
979
+ inj.modify(
917
980
  mval,
918
981
  mkey,
919
982
  mparent,
920
983
  inj,
921
- current,
922
984
  store
923
985
  )
924
986
  }
@@ -935,19 +997,18 @@ function inject(
935
997
 
936
998
  // Delete a key from a map or list.
937
999
  const transform_DELETE: Injector = (inj: Injection) => {
938
- // _setparentprop(inj, UNDEF)
939
1000
  inj.setval(UNDEF)
940
1001
  return UNDEF
941
1002
  }
942
1003
 
943
1004
 
944
1005
  // Copy value from source data.
945
- const transform_COPY: Injector = (inj: Injection, _val: any, current: any) => {
1006
+ const transform_COPY: Injector = (inj: Injection, _val: any) => {
946
1007
  const { mode, key } = inj
947
1008
 
948
1009
  let out = key
949
1010
  if (!mode.startsWith(S_MKEY)) {
950
- out = getprop(current, key)
1011
+ out = getprop(inj.dparent, key)
951
1012
  inj.setval(out)
952
1013
  }
953
1014
 
@@ -957,7 +1018,7 @@ const transform_COPY: Injector = (inj: Injection, _val: any, current: any) => {
957
1018
 
958
1019
  // As a value, inject the key of the parent node.
959
1020
  // As a key, defined the name of the key property in the source object.
960
- const transform_KEY: Injector = (inj: Injection, _val: any, current: any) => {
1021
+ const transform_KEY: Injector = (inj: Injection) => {
961
1022
  const { mode, path, parent } = inj
962
1023
 
963
1024
  // Do nothing in val mode.
@@ -966,22 +1027,22 @@ const transform_KEY: Injector = (inj: Injection, _val: any, current: any) => {
966
1027
  }
967
1028
 
968
1029
  // Key is defined by $KEY meta property.
969
- const keyspec = getprop(parent, S_DKEY)
1030
+ const keyspec = getprop(parent, S_BKEY)
970
1031
  if (UNDEF !== keyspec) {
971
- setprop(parent, S_DKEY, UNDEF)
972
- return getprop(current, keyspec)
1032
+ delprop(parent, S_BKEY)
1033
+ return getprop(inj.dparent, keyspec)
973
1034
  }
974
1035
 
975
1036
  // Key is defined within general purpose $META object.
976
- return getprop(getprop(parent, S_DMETA), S_KEY, getprop(path, path.length - 2))
1037
+ return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
977
1038
  }
978
1039
 
979
1040
 
980
- // Store meta data about a node. Does nothing itself, just used by
1041
+ // Annotatea node. Does nothing itself, just used by
981
1042
  // other injectors, and is removed when called.
982
- const transform_META: Injector = (inj: Injection) => {
1043
+ const transform_ANNO: Injector = (inj: Injection) => {
983
1044
  const { parent } = inj
984
- setprop(parent, S_DMETA, UNDEF)
1045
+ delprop(parent, S_BANNO)
985
1046
  return UNDEF
986
1047
  }
987
1048
 
@@ -991,18 +1052,22 @@ const transform_META: Injector = (inj: Injection) => {
991
1052
  // If the value is an array, the elements are first merged using `merge`.
992
1053
  // If the value is the empty string, merge the top level store.
993
1054
  // Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
994
- const transform_MERGE: Injector = (
995
- inj: Injection, _val: any, current: any
996
- ) => {
1055
+ const transform_MERGE: Injector = (inj: Injection) => {
997
1056
  const { mode, key, parent } = inj
998
1057
 
999
- if (S_MKEYPRE === mode) { return key }
1058
+ // Ensures $MERGE is removed from parent list (val mode).
1059
+ let out: any = UNDEF
1060
+
1061
+ if (S_MKEYPRE === mode) {
1062
+ out = key
1063
+ }
1000
1064
 
1001
1065
  // Operate after child values have been transformed.
1002
- if (S_MKEYPOST === mode) {
1066
+ else if (S_MKEYPOST === mode) {
1067
+ out = key
1003
1068
 
1004
1069
  let args = getprop(parent, key)
1005
- args = S_MT === args ? [current.$TOP] : Array.isArray(args) ? args : [args]
1070
+ args = Array.isArray(args) ? args : [args]
1006
1071
 
1007
1072
  // Remove the $MERGE command from a parent map.
1008
1073
  inj.setval(UNDEF)
@@ -1013,11 +1078,10 @@ const transform_MERGE: Injector = (
1013
1078
 
1014
1079
  merge(mergelist)
1015
1080
 
1016
- return key
1081
+ // return key
1017
1082
  }
1018
1083
 
1019
- // Ensures $MERGE is removed from parent list.
1020
- return UNDEF
1084
+ return out
1021
1085
  }
1022
1086
 
1023
1087
 
@@ -1026,7 +1090,6 @@ const transform_MERGE: Injector = (
1026
1090
  const transform_EACH: Injector = (
1027
1091
  inj: Injection,
1028
1092
  _val: any,
1029
- current: any,
1030
1093
  _ref: string,
1031
1094
  store: any
1032
1095
  ) => {
@@ -1046,7 +1109,8 @@ const transform_EACH: Injector = (
1046
1109
 
1047
1110
  // Source data.
1048
1111
  const srcstore = getprop(store, inj.base, store)
1049
- const src = getpath(srcpath, srcstore, current)
1112
+
1113
+ const src = getpath(srcstore, srcpath, inj)
1050
1114
 
1051
1115
  // Create parallel data structures:
1052
1116
  // source entries :: child templates
@@ -1065,7 +1129,7 @@ const transform_EACH: Injector = (
1065
1129
  ...clone(child),
1066
1130
 
1067
1131
  // Make a note of the key for $KEY transforms.
1068
- [S_DMETA]: { KEY: n[0] }
1132
+ [S_BANNO]: { KEY: n[0] }
1069
1133
  }))
1070
1134
  }
1071
1135
 
@@ -1074,24 +1138,36 @@ const transform_EACH: Injector = (
1074
1138
  if (0 < size(tval)) {
1075
1139
  tcur = null == src ? UNDEF : Object.values(src)
1076
1140
 
1141
+ const ckey = getelem(inj.path, -2)
1142
+
1077
1143
  const tpath = slice(inj.path, -1)
1144
+ const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey]
1145
+
1078
1146
 
1079
1147
  // Parent structure.
1080
- const ckey = getelem(inj.path, -2)
1148
+
1149
+ // const ckey = getelem(cpath, -1)
1081
1150
  tcur = { [ckey]: tcur }
1082
1151
 
1083
1152
  if (1 < tpath.length) {
1084
1153
  const pkey = getelem(inj.path, -3, S_DTOP)
1154
+ // const pkey = getelem(cpath, -2, S_DTOP)
1085
1155
  tcur = { [pkey]: tcur }
1156
+ dpath.push('$:' + pkey)
1086
1157
  }
1087
1158
 
1088
1159
  const tinj = inj.child(0, [ckey])
1089
1160
  tinj.path = tpath
1090
1161
  tinj.nodes = slice(inj.nodes, -1)
1091
- tinj.parent = tcur
1162
+
1163
+ tinj.parent = getelem(tinj.nodes, -1)
1164
+ setprop(tinj.parent, ckey, tval)
1165
+
1092
1166
  tinj.val = tval
1167
+ tinj.dpath = dpath
1168
+ tinj.dparent = tcur
1093
1169
 
1094
- inject(tval, store, inj.modify, tcur, tinj)
1170
+ inject(tval, store, tinj)
1095
1171
  rval = tinj.val
1096
1172
  }
1097
1173
 
@@ -1107,7 +1183,6 @@ const transform_EACH: Injector = (
1107
1183
  const transform_PACK: Injector = (
1108
1184
  inj: Injection,
1109
1185
  _val: any,
1110
- current: any,
1111
1186
  _ref: string,
1112
1187
  store: any
1113
1188
  ) => {
@@ -1124,19 +1199,20 @@ const transform_PACK: Injector = (
1124
1199
  const child = clone(args[1]) // Child template.
1125
1200
 
1126
1201
  // Find key and target node.
1127
- const keyprop = child[S_DKEY]
1202
+ const keyprop = child[S_BKEY]
1128
1203
  const tkey = getelem(path, -2)
1129
1204
  const target = nodes[path.length - 2] || nodes[path.length - 1]
1130
1205
 
1131
1206
  // Source data
1132
1207
  const srcstore = getprop(store, inj.base, store)
1133
- let src = getpath(srcpath, srcstore, current)
1208
+
1209
+ let src = getpath(srcstore, srcpath, inj)
1134
1210
 
1135
1211
  // Prepare source as a list.
1136
1212
  src = islist(src) ? src :
1137
1213
  ismap(src) ? Object.entries(src)
1138
1214
  .reduce((a: any[], n: any) =>
1139
- (n[1][S_DMETA] = { KEY: n[0] }, a.push(n[1]), a), []) :
1215
+ (n[1][S_BANNO] = { KEY: n[0] }, a.push(n[1]), a), []) :
1140
1216
  UNDEF
1141
1217
 
1142
1218
  if (null == src) {
@@ -1144,9 +1220,9 @@ const transform_PACK: Injector = (
1144
1220
  }
1145
1221
 
1146
1222
  // Get key if specified.
1147
- let childkey: PropKey | undefined = getprop(child, S_DKEY)
1223
+ let childkey: PropKey | undefined = getprop(child, S_BKEY)
1148
1224
  let keyname = UNDEF === childkey ? keyprop : childkey
1149
- setprop(child, S_DKEY, UNDEF)
1225
+ delprop(child, S_BKEY)
1150
1226
 
1151
1227
  // Build parallel target object.
1152
1228
  let tval: any = {}
@@ -1154,7 +1230,13 @@ const transform_PACK: Injector = (
1154
1230
  let kn = getprop(n, keyname)
1155
1231
  setprop(a, kn, clone(child))
1156
1232
  const nchild = getprop(a, kn)
1157
- setprop(nchild, S_DMETA, getprop(n, S_DMETA))
1233
+ const mval = getprop(n, S_BANNO)
1234
+ if (UNDEF === mval) {
1235
+ delprop(nchild, S_BANNO)
1236
+ }
1237
+ else {
1238
+ setprop(nchild, S_BANNO, mval)
1239
+ }
1158
1240
  return a
1159
1241
  }, tval)
1160
1242
 
@@ -1173,20 +1255,28 @@ const transform_PACK: Injector = (
1173
1255
  const tpath = slice(inj.path, -1)
1174
1256
 
1175
1257
  const ckey = getelem(inj.path, -2)
1258
+ const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey]
1259
+
1176
1260
  tcur = { [ckey]: tcur }
1177
1261
 
1178
1262
  if (1 < tpath.length) {
1179
1263
  const pkey = getelem(inj.path, -3, S_DTOP)
1180
1264
  tcur = { [pkey]: tcur }
1265
+ dpath.push('$:' + pkey)
1181
1266
  }
1182
1267
 
1183
1268
  const tinj = inj.child(0, [ckey])
1184
1269
  tinj.path = tpath
1185
1270
  tinj.nodes = slice(inj.nodes, -1)
1186
- tinj.parent = tcur
1271
+
1272
+ // tinj.parent = tcur
1273
+ tinj.parent = getelem(tinj.nodes, -1)
1187
1274
  tinj.val = tval
1188
1275
 
1189
- inject(tval, store, inj.modify, tcur, tinj)
1276
+ tinj.dpath = dpath
1277
+ tinj.dparent = tcur
1278
+
1279
+ inject(tval, store, tinj)
1190
1280
  rval = tinj.val
1191
1281
  }
1192
1282
 
@@ -1197,16 +1287,16 @@ const transform_PACK: Injector = (
1197
1287
  }
1198
1288
 
1199
1289
 
1290
+ // TODO: not found ref should removed key (setprop UNDEF)
1200
1291
  // Reference original spec (enables recursice transformations)
1201
1292
  // Format: ['`$REF`', '`spec-path`']
1202
1293
  const transform_REF: Injector = (
1203
1294
  inj: Injection,
1204
1295
  val: any,
1205
- _current: any,
1206
1296
  _ref: string,
1207
1297
  store: any
1208
1298
  ) => {
1209
- const { nodes, modify } = inj
1299
+ const { nodes } = inj
1210
1300
 
1211
1301
  if (S_MVAL !== inj.mode) {
1212
1302
  return UNDEF
@@ -1218,7 +1308,12 @@ const transform_REF: Injector = (
1218
1308
 
1219
1309
  // Spec reference.
1220
1310
  const spec = getprop(store, S_DSPEC)()
1221
- const ref = getpath(refpath, spec)
1311
+
1312
+ const ref = getpath(spec, refpath, {
1313
+ // TODO: test relative refs
1314
+ dpath: inj.path.slice(1),
1315
+ dparent: getpath(spec, inj.path.slice(1))
1316
+ })
1222
1317
 
1223
1318
  let hasSubRef = false
1224
1319
  if (isnode(ref)) {
@@ -1234,8 +1329,8 @@ const transform_REF: Injector = (
1234
1329
 
1235
1330
  const cpath = slice(inj.path, -3)
1236
1331
  const tpath = slice(inj.path, -1)
1237
- let tcur = getpath(cpath, store)
1238
- let tval = getpath(tpath, store)
1332
+ let tcur = getpath(store, cpath)
1333
+ let tval = getpath(store, tpath)
1239
1334
  let rval = UNDEF
1240
1335
 
1241
1336
  if (!hasSubRef || UNDEF !== tval) {
@@ -1246,7 +1341,10 @@ const transform_REF: Injector = (
1246
1341
  tinj.parent = getelem(nodes, -2)
1247
1342
  tinj.val = tref
1248
1343
 
1249
- inject(tref, store, modify, tcur, tinj)
1344
+ tinj.dpath = [...cpath]
1345
+ tinj.dparent = tcur
1346
+
1347
+ inject(tref, store, tinj)
1250
1348
 
1251
1349
  rval = tinj.val
1252
1350
  }
@@ -1270,13 +1368,17 @@ const transform_REF: Injector = (
1270
1368
  function transform(
1271
1369
  data: any, // Source data to transform into new data (original not mutated)
1272
1370
  spec: any, // Transform specification; output follows this shape
1273
- extra?: any, // Additional store of data and transforms.
1274
- modify?: Modify // Optionally modify individual values.
1371
+ // extra?: any, // Additional store of data and transforms.
1372
+ // modify?: Modify // Optionally modify individual values.
1373
+ injdef?: Partial<Injection>
1275
1374
  ) {
1276
1375
  // Clone the spec so that the clone can be modified in place as the transform result.
1277
1376
  const origspec = spec
1278
1377
  spec = clone(origspec)
1279
1378
 
1379
+ const extra = injdef?.extra
1380
+ // const modify = injdef?.modify
1381
+
1280
1382
  const extraTransforms: any = {}
1281
1383
  const extraData = null == extra ? UNDEF : items(extra)
1282
1384
  .reduce((a: any, n: any[]) =>
@@ -1309,7 +1411,7 @@ function transform(
1309
1411
  $DELETE: transform_DELETE,
1310
1412
  $COPY: transform_COPY,
1311
1413
  $KEY: transform_KEY,
1312
- $META: transform_META,
1414
+ $ANNO: transform_ANNO,
1313
1415
  $MERGE: transform_MERGE,
1314
1416
  $EACH: transform_EACH,
1315
1417
  $PACK: transform_PACK,
@@ -1319,14 +1421,15 @@ function transform(
1319
1421
  ...extraTransforms,
1320
1422
  }
1321
1423
 
1322
- const out = inject(spec, store, modify, store)
1424
+ // const out = inject(spec, store, { modify, extra })
1425
+ const out = inject(spec, store, injdef)
1323
1426
  return out
1324
1427
  }
1325
1428
 
1326
1429
 
1327
1430
  // A required string value. NOTE: Rejects empty strings.
1328
- const validate_STRING: Injector = (inj: Injection, _val: any, current: any) => {
1329
- let out = getprop(current, inj.key)
1431
+ const validate_STRING: Injector = (inj: Injection) => {
1432
+ let out = getprop(inj.dparent, inj.key)
1330
1433
 
1331
1434
  const t = typify(out)
1332
1435
  if (S_string !== t) {
@@ -1346,8 +1449,8 @@ const validate_STRING: Injector = (inj: Injection, _val: any, current: any) => {
1346
1449
 
1347
1450
 
1348
1451
  // A required number value (int or float).
1349
- const validate_NUMBER: Injector = (inj: Injection, _val: any, current: any) => {
1350
- let out = getprop(current, inj.key)
1452
+ const validate_NUMBER: Injector = (inj: Injection) => {
1453
+ let out = getprop(inj.dparent, inj.key)
1351
1454
 
1352
1455
  const t = typify(out)
1353
1456
  if (S_number !== t) {
@@ -1360,8 +1463,8 @@ const validate_NUMBER: Injector = (inj: Injection, _val: any, current: any) => {
1360
1463
 
1361
1464
 
1362
1465
  // A required boolean value.
1363
- const validate_BOOLEAN: Injector = (inj: Injection, _val: any, current: any) => {
1364
- let out = getprop(current, inj.key)
1466
+ const validate_BOOLEAN: Injector = (inj: Injection) => {
1467
+ let out = getprop(inj.dparent, inj.key)
1365
1468
 
1366
1469
  const t = typify(out)
1367
1470
  if (S_boolean !== t) {
@@ -1374,8 +1477,8 @@ const validate_BOOLEAN: Injector = (inj: Injection, _val: any, current: any) =>
1374
1477
 
1375
1478
 
1376
1479
  // A required object (map) value (contents not validated).
1377
- const validate_OBJECT: Injector = (inj: Injection, _val: any, current: any) => {
1378
- let out = getprop(current, inj.key)
1480
+ const validate_OBJECT: Injector = (inj: Injection) => {
1481
+ let out = getprop(inj.dparent, inj.key)
1379
1482
 
1380
1483
  const t = typify(out)
1381
1484
  if (t !== S_object) {
@@ -1388,8 +1491,8 @@ const validate_OBJECT: Injector = (inj: Injection, _val: any, current: any) => {
1388
1491
 
1389
1492
 
1390
1493
  // A required array (list) value (contents not validated).
1391
- const validate_ARRAY: Injector = (inj: Injection, _val: any, current: any) => {
1392
- let out = getprop(current, inj.key)
1494
+ const validate_ARRAY: Injector = (inj: Injection) => {
1495
+ let out = getprop(inj.dparent, inj.key)
1393
1496
 
1394
1497
  const t = typify(out)
1395
1498
  if (t !== S_array) {
@@ -1402,8 +1505,8 @@ const validate_ARRAY: Injector = (inj: Injection, _val: any, current: any) => {
1402
1505
 
1403
1506
 
1404
1507
  // A required function value.
1405
- const validate_FUNCTION: Injector = (inj: Injection, _val: any, current: any) => {
1406
- let out = getprop(current, inj.key)
1508
+ const validate_FUNCTION: Injector = (inj: Injection) => {
1509
+ let out = getprop(inj.dparent, inj.key)
1407
1510
 
1408
1511
  const t = typify(out)
1409
1512
  if (S_function !== t) {
@@ -1416,8 +1519,9 @@ const validate_FUNCTION: Injector = (inj: Injection, _val: any, current: any) =>
1416
1519
 
1417
1520
 
1418
1521
  // Allow any value.
1419
- const validate_ANY: Injector = (inj: Injection, _val: any, current: any) => {
1420
- return getprop(current, inj.key)
1522
+ const validate_ANY: Injector = (inj: Injection) => {
1523
+ let out = getprop(inj.dparent, inj.key)
1524
+ return out
1421
1525
  }
1422
1526
 
1423
1527
 
@@ -1425,7 +1529,7 @@ const validate_ANY: Injector = (inj: Injection, _val: any, current: any) => {
1425
1529
  // Specify child values for map or list.
1426
1530
  // Map syntax: {'`$CHILD`': child-template }
1427
1531
  // List syntax: ['`$CHILD`', child-template ]
1428
- const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
1532
+ const validate_CHILD: Injector = (inj: Injection) => {
1429
1533
  const { mode, key, parent, keys, path } = inj
1430
1534
 
1431
1535
  // Setup data structures for validation by cloning child template.
@@ -1436,7 +1540,7 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
1436
1540
 
1437
1541
  // Get corresponding current object.
1438
1542
  const pkey = getprop(path, path.length - 2)
1439
- let tval = getprop(current, pkey)
1543
+ let tval = getprop(inj.dparent, pkey)
1440
1544
 
1441
1545
  if (UNDEF == tval) {
1442
1546
  tval = {}
@@ -1456,7 +1560,6 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
1456
1560
  }
1457
1561
 
1458
1562
  // Remove $CHILD to cleanup ouput.
1459
- // _setparentprop(inj, UNDEF)
1460
1563
  inj.setval(UNDEF)
1461
1564
  return UNDEF
1462
1565
  }
@@ -1472,27 +1575,28 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
1472
1575
 
1473
1576
  const childtm = getprop(parent, 1)
1474
1577
 
1475
- if (UNDEF === current) {
1578
+ if (UNDEF === inj.dparent) {
1476
1579
  // Empty list as default.
1477
1580
  parent.length = 0
1478
1581
  return UNDEF
1479
1582
  }
1480
1583
 
1481
- if (!islist(current)) {
1584
+ if (!islist(inj.dparent)) {
1482
1585
  const msg = _invalidTypeMsg(
1483
- slice(inj.path, -1), S_array, typify(current), current, 'V0230')
1586
+ slice(inj.path, -1), S_array, typify(inj.dparent), inj.dparent, 'V0230')
1484
1587
  inj.errs.push(msg)
1485
1588
  inj.keyI = parent.length
1486
- return current
1589
+ return inj.dparent
1487
1590
  }
1488
1591
 
1489
1592
  // Clone children abd reset inj key index.
1490
1593
  // The inject child loop will now iterate over the cloned children,
1491
1594
  // validating them againt the current list values.
1492
- current.map((_n, i) => parent[i] = clone(childtm))
1493
- parent.length = current.length
1595
+
1596
+ inj.dparent.map((_n, i) => parent[i] = clone(childtm))
1597
+ parent.length = inj.dparent.length
1494
1598
  inj.keyI = 0
1495
- const out = getprop(current, 0)
1599
+ const out = getprop(inj.dparent, 0)
1496
1600
  return out
1497
1601
  }
1498
1602
 
@@ -1505,11 +1609,10 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
1505
1609
  const validate_ONE: Injector = (
1506
1610
  inj: Injection,
1507
1611
  _val: any,
1508
- current: any,
1509
1612
  _ref: string,
1510
1613
  store: any
1511
1614
  ) => {
1512
- const { mode, parent, path, keyI, nodes } = inj
1615
+ const { mode, parent, keyI } = inj
1513
1616
 
1514
1617
  // Only operate in val mode, since parent is a list.
1515
1618
  if (S_MVAL === mode) {
@@ -1523,7 +1626,7 @@ const validate_ONE: Injector = (
1523
1626
  inj.keyI = inj.keys.length
1524
1627
 
1525
1628
  // Clean up structure, replacing [$ONE, ...] with current
1526
- const grandparent = inj.setval(current, 2)
1629
+ inj.setval(inj.dparent, 2)
1527
1630
 
1528
1631
  inj.path = slice(inj.path, -1)
1529
1632
  inj.key = getelem(inj.path, -1)
@@ -1543,10 +1646,15 @@ const validate_ONE: Injector = (
1543
1646
  let terrs: any[] = []
1544
1647
 
1545
1648
  const vstore = { ...store }
1546
- vstore.$TOP = current
1547
- const vcurrent = validate(current, tval, vstore, terrs)
1649
+ vstore.$TOP = inj.dparent
1650
+
1651
+ const vcurrent = validate(inj.dparent, tval, {
1652
+ extra: vstore,
1653
+ errs: terrs,
1654
+ meta: inj.meta,
1655
+ })
1548
1656
 
1549
- setprop(grandparent, getelem(path, -2), vcurrent)
1657
+ inj.setval(vcurrent, -2)
1550
1658
 
1551
1659
  // Accept current value if there was a match
1552
1660
  if (0 === terrs.length) {
@@ -1559,24 +1667,18 @@ const validate_ONE: Injector = (
1559
1667
  const valdesc = tvals
1560
1668
  .map((v: any) => stringify(v))
1561
1669
  .join(', ')
1562
- .replace(/`\$([A-Z]+)`/g, (_m: any, p1: string) => p1.toLowerCase())
1670
+ .replace(R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
1563
1671
 
1564
1672
  inj.errs.push(_invalidTypeMsg(
1565
1673
  inj.path,
1566
1674
  (1 < tvals.length ? 'one of ' : '') + valdesc,
1567
- typify(current), current, 'V0210'))
1675
+ typify(inj.dparent), inj.dparent, 'V0210'))
1568
1676
  }
1569
1677
  }
1570
1678
 
1571
1679
 
1572
- const validate_EXACT: Injector = (
1573
- inj: Injection,
1574
- _val: any,
1575
- current: any,
1576
- _ref: string,
1577
- _store: any
1578
- ) => {
1579
- const { mode, parent, key, keyI, path, nodes } = inj
1680
+ const validate_EXACT: Injector = (inj: Injection) => {
1681
+ const { mode, parent, key, keyI } = inj
1580
1682
 
1581
1683
  // Only operate in val mode, since parent is a list.
1582
1684
  if (S_MVAL === mode) {
@@ -1589,8 +1691,8 @@ const validate_EXACT: Injector = (
1589
1691
 
1590
1692
  inj.keyI = inj.keys.length
1591
1693
 
1592
- // Clean up structure, replacing [$EXACT, ...] with current
1593
- inj.setval(current, 2)
1694
+ // Clean up structure, replacing [$EXACT, ...] with current data parent
1695
+ inj.setval(inj.dparent, 2)
1594
1696
 
1595
1697
  inj.path = slice(inj.path, 0, inj.path.length - 1)
1596
1698
  inj.key = getelem(inj.path, -1)
@@ -1606,10 +1708,10 @@ const validate_EXACT: Injector = (
1606
1708
  // See if we can find an exact value match.
1607
1709
  let currentstr: string | undefined = undefined
1608
1710
  for (let tval of tvals) {
1609
- let exactmatch = tval === current
1711
+ let exactmatch = tval === inj.dparent
1610
1712
 
1611
1713
  if (!exactmatch && isnode(tval)) {
1612
- currentstr = undefined === currentstr ? stringify(current) : currentstr
1714
+ currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr
1613
1715
  const tvalstr = stringify(tval)
1614
1716
  exactmatch = tvalstr === currentstr
1615
1717
  }
@@ -1622,16 +1724,16 @@ const validate_EXACT: Injector = (
1622
1724
  const valdesc = tvals
1623
1725
  .map((v: any) => stringify(v))
1624
1726
  .join(', ')
1625
- .replace(/`\$([A-Z]+)`/g, (_m: any, p1: string) => p1.toLowerCase())
1727
+ .replace(R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
1626
1728
 
1627
1729
  inj.errs.push(_invalidTypeMsg(
1628
1730
  inj.path,
1629
1731
  (1 < inj.path.length ? '' : 'value ') +
1630
1732
  'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc,
1631
- typify(current), current, 'V0110'))
1733
+ typify(inj.dparent), inj.dparent, 'V0110'))
1632
1734
  }
1633
1735
  else {
1634
- setprop(parent, key, UNDEF)
1736
+ delprop(parent, key)
1635
1737
  }
1636
1738
  }
1637
1739
 
@@ -1643,18 +1745,24 @@ const _validation: Modify = (
1643
1745
  key?: any,
1644
1746
  parent?: any,
1645
1747
  inj?: Injection,
1646
- current?: any,
1647
- _store?: any
1648
1748
  ) => {
1649
1749
 
1650
1750
  if (UNDEF === inj) {
1651
1751
  return
1652
1752
  }
1653
1753
 
1754
+ if (SKIP === pval) {
1755
+ return
1756
+ }
1757
+
1758
+ // select needs exact matches
1759
+ const exact = getprop(inj.meta, '`$EXACT`')
1760
+
1654
1761
  // Current val to verify.
1655
- const cval = getprop(current, key)
1762
+ const cval = getprop(inj.dparent, key)
1656
1763
 
1657
- if (UNDEF === cval || UNDEF === inj) {
1764
+ // if (UNDEF === cval || UNDEF === inj) {
1765
+ if (UNDEF === inj || (!exact && UNDEF === cval)) {
1658
1766
  return
1659
1767
  }
1660
1768
 
@@ -1667,6 +1775,9 @@ const _validation: Modify = (
1667
1775
 
1668
1776
  const ctype = typify(cval)
1669
1777
 
1778
+ // console.log('VALID-A', pval, ptype, cval, ctype)
1779
+
1780
+
1670
1781
  // Type mismatch.
1671
1782
  if (ptype !== ctype && UNDEF !== pval) {
1672
1783
  inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0010'))
@@ -1702,7 +1813,7 @@ const _validation: Modify = (
1702
1813
  // Object is open, so merge in extra keys.
1703
1814
  merge([pval, cval])
1704
1815
  if (isnode(pval)) {
1705
- setprop(pval, '`$OPEN`', UNDEF)
1816
+ delprop(pval, '`$OPEN`')
1706
1817
  }
1707
1818
  }
1708
1819
  }
@@ -1711,6 +1822,14 @@ const _validation: Modify = (
1711
1822
  inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0030'))
1712
1823
  }
1713
1824
  }
1825
+ else if (exact) {
1826
+ // else if (inj.meta['`$EXACT`']) {
1827
+ // console.log('VALID-X', cval, pval)
1828
+
1829
+ if (cval !== pval) {
1830
+ inj.errs.push('Value ' + cval + ' should equal ' + pval)
1831
+ }
1832
+ }
1714
1833
  else {
1715
1834
  // Spec value was a default, copy over data
1716
1835
  setprop(parent, key, cval)
@@ -1734,13 +1853,12 @@ const _validation: Modify = (
1734
1853
  function validate(
1735
1854
  data: any, // Source data to transform into new data (original not mutated)
1736
1855
  spec: any, // Transform specification; output follows this shape
1737
-
1738
- extra?: any, // Additional custom checks
1739
-
1740
- // Optionally modify individual values.
1741
- collecterrs?: any,
1856
+ injdef?: Partial<Injection>
1742
1857
  ) {
1743
- const errs = null == collecterrs ? [] : collecterrs
1858
+ const extra = injdef?.extra
1859
+
1860
+ const collect = null != injdef?.errs
1861
+ const errs = injdef?.errs || []
1744
1862
 
1745
1863
  const store = {
1746
1864
  // Remove the transform commands.
@@ -1770,9 +1888,14 @@ function validate(
1770
1888
  $ERRS: errs,
1771
1889
  }
1772
1890
 
1773
- const out = transform(data, spec, store, _validation)
1891
+ const out = transform(data, spec, {
1892
+ meta: injdef?.meta,
1893
+ extra: store,
1894
+ modify: _validation,
1895
+ handler: _validatehandler
1896
+ })
1774
1897
 
1775
- const generr = (0 < errs.length && null == collecterrs)
1898
+ const generr = (0 < errs.length && !collect)
1776
1899
  if (generr) {
1777
1900
  throw new Error('Invalid data: ' + errs.join(' | '))
1778
1901
  }
@@ -1781,15 +1904,280 @@ function validate(
1781
1904
  }
1782
1905
 
1783
1906
 
1784
- // Internal utilities
1785
- // ==================
1907
+ const select_AND: Injector = (inj: Injection, val: any, _ref: string, store: any) => {
1908
+ if (S_MKEYPRE === inj.mode) {
1909
+ const terms = getprop(inj.parent, inj.key)
1910
+ const src = getprop(store, inj.base, store)
1911
+
1912
+ for (let term of terms) {
1913
+ // setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
1914
+
1915
+ let terrs: any[] = []
1916
+
1917
+ validate(src, term, {
1918
+ extra: store,
1919
+ errs: terrs,
1920
+ meta: inj.meta,
1921
+ })
1922
+
1923
+ if (0 != terrs.length) {
1924
+ inj.errs.push('AND:' + stringify(val) + ' fail:' + stringify(term))
1925
+ }
1926
+ }
1927
+ }
1928
+ }
1929
+
1930
+
1931
+ const select_OR: Injector = (inj: Injection, val: any, _ref: string, store: any) => {
1932
+ if (S_MKEYPRE === inj.mode) {
1933
+ const terms = getprop(inj.parent, inj.key)
1934
+ const src = getprop(store, inj.base, store)
1935
+
1936
+ for (let term of terms) {
1937
+ // setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
1938
+
1939
+ let terrs: any[] = []
1940
+
1941
+ validate(src, term, {
1942
+ extra: store,
1943
+ errs: terrs,
1944
+ meta: inj.meta,
1945
+ })
1946
+
1947
+ if (0 === terrs.length) {
1948
+ return
1949
+ }
1950
+ }
1951
+
1952
+ inj.errs.push('OR:' + stringify(val) + ' fail:' + stringify(terms))
1953
+ }
1954
+ }
1955
+
1956
+
1957
+ const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
1958
+ if (S_MKEYPRE === inj.mode) {
1959
+ const term = getprop(inj.parent, inj.key)
1960
+ const src = getprop(store, inj.base, store)
1961
+ const gkey = getelem(inj.path, -2)
1962
+
1963
+ const tval = getprop(src, gkey)
1964
+ let pass = false
1965
+
1966
+ if ('$GT' === ref && tval > term) {
1967
+ pass = true
1968
+ }
1969
+ else if ('$LT' === ref && tval < term) {
1970
+ pass = true
1971
+ }
1972
+ else if ('$GTE' === ref && tval >= term) {
1973
+ pass = true
1974
+ }
1975
+ else if ('$LTE' === ref && tval <= term) {
1976
+ pass = true
1977
+ }
1978
+
1979
+ if (pass) {
1980
+ // Update spec to match found value so that _validate does not complain.
1981
+ const gp = getelem(inj.nodes, -2)
1982
+ setprop(gp, gkey, tval)
1983
+ }
1984
+ else {
1985
+ inj.errs.push('CMP: fail:' + ref + ' ' + stringify(term))
1986
+ }
1987
+ }
1988
+
1989
+ return UNDEF
1990
+ }
1991
+
1992
+
1993
+ // Select children from a top-level object that match a MongoDB-style query.
1994
+ // Supports $and, $or, and equality comparisons.
1995
+ // For arrays, children are elements; for objects, children are values.
1996
+ // TODO: swap arg order for consistency
1997
+ function select(query: any, children: any): any[] {
1998
+ if (!isnode(children)) {
1999
+ return []
2000
+ }
2001
+
2002
+ if (ismap(children)) {
2003
+ children = items(children).map(n => (n[1][S_DKEY] = n[0], n[1]))
2004
+ }
2005
+ else {
2006
+ children = (children as any[]).map((n, i) => ((ismap(n) ? n[S_DKEY] = i : null), n))
2007
+ }
2008
+
2009
+ const results: any[] = []
2010
+ const injdef = {
2011
+ errs: [],
2012
+ meta: { '`$EXACT`': true },
2013
+ extra: {
2014
+ $AND: select_AND,
2015
+ $OR: select_OR,
2016
+ $GT: select_CMP,
2017
+ $LT: select_CMP,
2018
+ $GTE: select_CMP,
2019
+ $LTE: select_CMP,
2020
+ }
2021
+ }
2022
+
2023
+ const q = clone(query)
2024
+
2025
+ walk(q, (_k: PropKey | undefined, v: any) => {
2026
+ if (ismap(v)) {
2027
+ setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true))
2028
+ }
2029
+ return v
2030
+ })
2031
+
2032
+ for (const child of children) {
2033
+ // console.log('CHILD', child, q)
2034
+
2035
+ injdef.errs = []
2036
+
2037
+ validate(child, clone(q), injdef)
2038
+
2039
+ if (0 === size(injdef.errs)) {
2040
+ results.push(child)
2041
+ }
2042
+ }
2043
+
2044
+ return results
2045
+ }
2046
+
2047
+
2048
+
2049
+ // Injection state used for recursive injection into JSON - like data structures.
2050
+ class Injection {
2051
+ mode: InjectMode // Injection mode: key:pre, val, key:post.
2052
+ full: boolean // Transform escape was full key name.
2053
+ keyI: number // Index of parent key in list of parent keys.
2054
+ keys: string[] // List of parent keys.
2055
+ key: string // Current parent key.
2056
+ val: any // Current child value.
2057
+ parent: any // Current parent (in transform specification).
2058
+ path: string[] // Path to current node.
2059
+ nodes: any[] // Stack of ancestor nodes.
2060
+ handler: Injector // Custom handler for injections.
2061
+ errs: any[] // Error collector.
2062
+ meta: Record<string, any> // Custom meta data.
2063
+ dparent: any // Current data parent node (contains current data value).
2064
+ dpath: string[] // Current data value path
2065
+ base?: string // Base key for data in store, if any.
2066
+ modify?: Modify // Modify injection output.
2067
+ prior?: Injection // Parent (aka prior) injection.
2068
+ extra?: any
2069
+
2070
+ constructor(val: any, parent: any) {
2071
+ this.val = val
2072
+ this.parent = parent
2073
+ this.errs = []
2074
+
2075
+ this.dparent = UNDEF
2076
+ this.dpath = [S_DTOP]
2077
+
2078
+ this.mode = S_MVAL as InjectMode
2079
+ this.full = false
2080
+ this.keyI = 0
2081
+ this.keys = [S_DTOP]
2082
+ this.key = S_DTOP
2083
+ this.path = [S_DTOP]
2084
+ this.nodes = [parent]
2085
+ this.handler = _injecthandler
2086
+ this.base = S_DTOP
2087
+ this.meta = {}
2088
+ }
2089
+
2090
+
2091
+ toString(prefix?: string) {
2092
+ return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
2093
+ pad(pathify(this.path, 1)) +
2094
+ this.mode + (this.full ? '/full' : '') + S_CN +
2095
+ 'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
2096
+ ' p=' + stringify(this.parent, -1, 1) +
2097
+ ' m=' + stringify(this.meta, -1, 1) +
2098
+ ' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
2099
+ ' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1)
2100
+ }
2101
+
2102
+
2103
+ descend() {
2104
+ this.meta.__d++
2105
+ const parentkey = getelem(this.path, -2)
2106
+
2107
+ // Resolve current node in store for local paths.
2108
+ if (UNDEF === this.dparent) {
2109
+
2110
+ // Even if there's no data, dpath should continue to match path, so that
2111
+ // relative paths work properly.
2112
+ if (1 < this.dpath.length) {
2113
+ this.dpath = [...this.dpath, parentkey]
2114
+ }
2115
+ }
2116
+ else {
2117
+ // this.dparent is the containing node of the current store value.
2118
+ if (null != parentkey) {
2119
+ this.dparent = getprop(this.dparent, parentkey)
2120
+
2121
+ let lastpart = getelem(this.dpath, -1)
2122
+ if (lastpart === '$:' + parentkey) {
2123
+ this.dpath = slice(this.dpath, -1)
2124
+ }
2125
+ else {
2126
+ this.dpath = [...this.dpath, parentkey]
2127
+ }
2128
+ }
2129
+ }
2130
+
2131
+ return this.dparent
2132
+ }
2133
+
2134
+
2135
+ child(keyI: number, keys: string[]) {
2136
+ const key = strkey(keys[keyI])
2137
+ const val = this.val
2138
+
2139
+ const cinj = new Injection(getprop(val, key), val)
2140
+ cinj.keyI = keyI
2141
+ cinj.keys = keys
2142
+ cinj.key = key
2143
+
2144
+ cinj.path = [...(this.path || []), key]
2145
+ cinj.nodes = [...(this.nodes || []), val]
2146
+
2147
+ cinj.mode = this.mode
2148
+ cinj.handler = this.handler
2149
+ cinj.modify = this.modify
2150
+ cinj.base = this.base
2151
+ cinj.meta = this.meta
2152
+ cinj.errs = this.errs
2153
+ cinj.prior = this
2154
+
2155
+ cinj.dpath = [...this.dpath]
2156
+ cinj.dparent = this.dparent
2157
+
2158
+ return cinj
2159
+ }
2160
+
2161
+
2162
+ setval(val: any, ancestor?: number) {
2163
+ if (null == ancestor || ancestor < 2) {
2164
+ return UNDEF === val ?
2165
+ delprop(this.parent, this.key) :
2166
+ setprop(this.parent, this.key, val)
2167
+ }
2168
+ else {
2169
+ const aval = getelem(this.nodes, 0 - ancestor)
2170
+ const akey = getelem(this.path, 0 - ancestor)
2171
+ return UNDEF === val ?
2172
+ delprop(aval, akey) :
2173
+ setprop(aval, akey, val)
2174
+ }
2175
+ }
2176
+ }
1786
2177
 
1787
2178
 
1788
- // Set inj.key property of inj.parent node, ensuring reference consistency
1789
- // when needed by implementation language.
1790
- // function _setparentprop(inj: Injection, val: any) {
1791
- // setprop(inj.parent, inj.key, val)
1792
- // }
2179
+ // Internal utilities
2180
+ // ==================
1793
2181
 
1794
2182
 
1795
2183
  // Update all references to target in inj.nodes.
@@ -1820,7 +2208,6 @@ function _invalidTypeMsg(path: any, needtype: string, vt: string, v: any, _whenc
1820
2208
  const _injecthandler: Injector = (
1821
2209
  inj: Injection,
1822
2210
  val: any,
1823
- current: any,
1824
2211
  ref: string,
1825
2212
  store: any
1826
2213
  ): any => {
@@ -1829,12 +2216,11 @@ const _injecthandler: Injector = (
1829
2216
 
1830
2217
  // Only call val function if it is a special command ($NAME format).
1831
2218
  if (iscmd) {
1832
- out = (val as Injector)(inj, val, current, ref, store)
2219
+ out = (val as Injector)(inj, val, ref, store)
1833
2220
  }
1834
2221
 
1835
2222
  // Update parent with value. Ensures references remain in node tree.
1836
2223
  else if (S_MVAL === inj.mode && inj.full) {
1837
- // _setparentprop(inj, val)
1838
2224
  inj.setval(val)
1839
2225
  }
1840
2226
 
@@ -1842,6 +2228,36 @@ const _injecthandler: Injector = (
1842
2228
  }
1843
2229
 
1844
2230
 
2231
+ const _validatehandler: Injector = (
2232
+ inj: Injection,
2233
+ val: any,
2234
+ ref: string,
2235
+ store: any
2236
+ ): any => {
2237
+ let out = val
2238
+
2239
+ const m = ref.match(R_META_PATH)
2240
+ const ismetapath = null != m
2241
+
2242
+ if (ismetapath) {
2243
+ if ('=' === m[2]) {
2244
+ inj.setval(['`$EXACT`', val])
2245
+ }
2246
+ else {
2247
+ inj.setval(val)
2248
+ }
2249
+ inj.keyI = -1
2250
+
2251
+ out = SKIP
2252
+ }
2253
+ else {
2254
+ out = _injecthandler(inj, val, ref, store)
2255
+ }
2256
+
2257
+ return out
2258
+ }
2259
+
2260
+
1845
2261
  // Inject values from a data store into a string. Not a public utility - used by
1846
2262
  // `inject`. Inject are marked with `path` where path is resolved
1847
2263
  // with getpath against the store or current (if defined)
@@ -1854,7 +2270,6 @@ const _injecthandler: Injector = (
1854
2270
  function _injectstr(
1855
2271
  val: string,
1856
2272
  store: any,
1857
- current?: any,
1858
2273
  inj?: Injection
1859
2274
  ): any {
1860
2275
  // Can't inject into non-strings
@@ -1865,7 +2280,7 @@ function _injectstr(
1865
2280
  let out: any = val
1866
2281
 
1867
2282
  // Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
1868
- const m = val.match(/^`(\$[A-Z]+|[^`]+)[0-9]*`$/)
2283
+ const m = val.match(R_INJECTION_FULL)
1869
2284
 
1870
2285
  // Full string of the val is an injection.
1871
2286
  if (m) {
@@ -1875,34 +2290,35 @@ function _injectstr(
1875
2290
  let pathref = m[1]
1876
2291
 
1877
2292
  // Special escapes inside injection.
1878
- pathref =
1879
- 3 < pathref.length ? pathref.replace(/\$BT/g, S_BT).replace(/\$DS/g, S_DS) : pathref
2293
+ pathref = 3 < pathref.length ?
2294
+ pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) :
2295
+ pathref
1880
2296
 
1881
2297
  // Get the extracted path reference.
1882
- out = getpath(pathref, store, current, inj)
2298
+ out = getpath(store, pathref, inj)
1883
2299
  }
1884
2300
 
1885
2301
  else {
1886
2302
  // Check for injections within the string.
1887
2303
  const partial = (_m: string, ref: string) => {
1888
2304
  // Special escapes inside injection.
1889
- ref = 3 < ref.length ? ref.replace(/\$BT/g, S_BT).replace(/\$DS/g, S_DS) : ref
2305
+ ref = 3 < ref.length ? ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) : ref
1890
2306
  if (inj) {
1891
2307
  inj.full = false
1892
2308
  }
1893
- const found = getpath(ref, store, current, inj)
2309
+ const found = getpath(store, ref, inj)
1894
2310
 
1895
2311
  // Ensure inject value is a string.
1896
2312
  return UNDEF === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
1897
2313
  }
1898
2314
 
1899
- out = val.replace(/`([^`]+)`/g, partial)
2315
+ out = val.replace(R_INJECTION_PARTIAL, partial)
1900
2316
 
1901
2317
  // Also call the inj handler on the entire string, providing the
1902
2318
  // option for custom injection.
1903
2319
  if (null != inj && isfunc(inj.handler)) {
1904
2320
  inj.full = true
1905
- out = inj.handler(inj, out, current, val, store)
2321
+ out = inj.handler(inj, out, val, store)
1906
2322
  }
1907
2323
  }
1908
2324
 
@@ -1910,9 +2326,9 @@ function _injectstr(
1910
2326
  }
1911
2327
 
1912
2328
 
1913
- // Define a class to mirror the JavaScript implementation
1914
2329
  class StructUtility {
1915
2330
  clone = clone
2331
+ delprop = delprop
1916
2332
  escre = escre
1917
2333
  escurl = escurl
1918
2334
  getelem = getelem
@@ -1928,10 +2344,12 @@ class StructUtility {
1928
2344
  isnode = isnode
1929
2345
  items = items
1930
2346
  joinurl = joinurl
2347
+ jsonify = jsonify
1931
2348
  keysof = keysof
1932
2349
  merge = merge
1933
2350
  pad = pad
1934
2351
  pathify = pathify
2352
+ select = select
1935
2353
  setprop = setprop
1936
2354
  size = size
1937
2355
  slice = slice
@@ -1946,6 +2364,7 @@ class StructUtility {
1946
2364
  export {
1947
2365
  StructUtility,
1948
2366
  clone,
2367
+ delprop,
1949
2368
  escre,
1950
2369
  escurl,
1951
2370
  getelem,
@@ -1961,10 +2380,12 @@ export {
1961
2380
  isnode,
1962
2381
  items,
1963
2382
  joinurl,
2383
+ jsonify,
1964
2384
  keysof,
1965
2385
  merge,
1966
2386
  pad,
1967
2387
  pathify,
2388
+ select,
1968
2389
  setprop,
1969
2390
  size,
1970
2391
  slice,