@voxgig/struct 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/struct.d.ts +51 -35
- package/dist/struct.js +678 -301
- package/dist/struct.js.map +1 -1
- package/package.json +3 -3
- package/src/struct.ts +836 -369
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
|
|
63
|
-
const
|
|
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,
|
|
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,
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
256
|
+
else if (null != end) {
|
|
257
|
+
if (end < 0) {
|
|
258
|
+
end = vlen + end
|
|
295
259
|
if (end < 0) {
|
|
296
|
-
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
|
-
|
|
307
|
-
end = val.length
|
|
263
|
+
else if (vlen < end) {
|
|
264
|
+
end = vlen
|
|
308
265
|
}
|
|
266
|
+
}
|
|
309
267
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
268
|
+
else {
|
|
269
|
+
end = vlen
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (vlen < start) {
|
|
273
|
+
start = vlen
|
|
274
|
+
}
|
|
313
275
|
|
|
314
|
-
|
|
315
|
-
|
|
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
|
}
|
|
@@ -322,9 +296,9 @@ function slice<V extends any>(val: V, start: number, end?: number): V {
|
|
|
322
296
|
|
|
323
297
|
|
|
324
298
|
function pad(str: any, padding?: number, padchar?: string): string {
|
|
325
|
-
str = stringify(str)
|
|
299
|
+
str = S_string === typeof str ? str : stringify(str)
|
|
326
300
|
padding = null == padding ? 44 : padding
|
|
327
|
-
padchar = null == padchar ?
|
|
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(
|
|
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) =>
|
|
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(
|
|
425
|
+
return s.replace(R_ESCAPE_REGEXP, '\\$&')
|
|
453
426
|
}
|
|
454
427
|
|
|
455
428
|
|
|
@@ -463,11 +436,38 @@ 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 &&
|
|
467
|
-
.map((s, i) => 0 === i ? s.replace(
|
|
468
|
-
s.replace(
|
|
469
|
-
|
|
470
|
-
|
|
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, flags?: { indent?: number, offset?: number }) {
|
|
453
|
+
let str = S_null
|
|
454
|
+
if (null != val) {
|
|
455
|
+
const indent = getprop(flags, 'indent', 2)
|
|
456
|
+
str = JSON.stringify(val, null, indent)
|
|
457
|
+
if (UNDEF === str) {
|
|
458
|
+
str = S_null
|
|
459
|
+
}
|
|
460
|
+
const offset = getprop(flags, 'offset', 0)
|
|
461
|
+
if (0 < offset) {
|
|
462
|
+
// Left offset entire indented JSON so that it aligns with surrounding code
|
|
463
|
+
// indented by offset.
|
|
464
|
+
str = '{\n' + str.split('\n').slice(1)
|
|
465
|
+
.map(n => pad(n, 0 - offset - size(n)))
|
|
466
|
+
.join('\n')
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return str
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
|
|
@@ -480,35 +480,39 @@ function stringify(val: any, maxlen?: number, pretty?: any): string {
|
|
|
480
480
|
return pretty ? '<>' : valstr
|
|
481
481
|
}
|
|
482
482
|
|
|
483
|
-
|
|
484
|
-
valstr =
|
|
485
|
-
if (
|
|
486
|
-
val !== null &&
|
|
487
|
-
typeof val === "object" &&
|
|
488
|
-
!Array.isArray(val)
|
|
489
|
-
) {
|
|
490
|
-
const sortedObj: any = {}
|
|
491
|
-
for (const k of Object.keys(val).sort()) {
|
|
492
|
-
sortedObj[k] = val[k]
|
|
493
|
-
}
|
|
494
|
-
return sortedObj
|
|
495
|
-
}
|
|
496
|
-
return val
|
|
497
|
-
})
|
|
483
|
+
if (S_string === typeof val) {
|
|
484
|
+
valstr = val
|
|
498
485
|
}
|
|
499
|
-
|
|
500
|
-
|
|
486
|
+
else {
|
|
487
|
+
try {
|
|
488
|
+
valstr = JSON.stringify(val, function(_key: string, val: any) {
|
|
489
|
+
if (
|
|
490
|
+
val !== null &&
|
|
491
|
+
typeof val === "object" &&
|
|
492
|
+
!Array.isArray(val)
|
|
493
|
+
) {
|
|
494
|
+
const sortedObj: any = {}
|
|
495
|
+
for (const k of Object.keys(val).sort()) {
|
|
496
|
+
sortedObj[k] = val[k]
|
|
497
|
+
}
|
|
498
|
+
return sortedObj
|
|
499
|
+
}
|
|
500
|
+
return val
|
|
501
|
+
})
|
|
502
|
+
valstr = valstr.replace(R_QUOTES, S_MT)
|
|
503
|
+
}
|
|
504
|
+
catch (err: any) {
|
|
505
|
+
valstr = S_MT + val
|
|
506
|
+
}
|
|
501
507
|
}
|
|
502
508
|
|
|
503
|
-
valstr = S_string !== typeof valstr ? S_MT + valstr : valstr
|
|
504
|
-
valstr = valstr.replace(/"/g, '')
|
|
505
|
-
|
|
506
509
|
if (null != maxlen && -1 < maxlen) {
|
|
507
510
|
let js = valstr.substring(0, maxlen)
|
|
508
511
|
valstr = maxlen < valstr.length ? (js.substring(0, maxlen - 3) + '...') : valstr
|
|
509
512
|
}
|
|
510
513
|
|
|
511
514
|
if (pretty) {
|
|
515
|
+
// Indicate deeper JSON levels with different terminal colors (simplistic wrt strings).
|
|
512
516
|
let c = [81, 118, 213, 39, 208, 201, 45, 190, 129, 51, 160, 121, 226, 33, 207, 69]
|
|
513
517
|
.map(n => `\x1b[38;5;${n}m`),
|
|
514
518
|
r = '\x1b[0m', d = 0, o = c[0], t = o
|
|
@@ -551,8 +555,8 @@ function pathify(val: any, startin?: number, endin?: number) {
|
|
|
551
555
|
// .filter((p: any, t: any) => (t = typeof p, S_string === t || S_number === t))
|
|
552
556
|
.filter((p: any) => iskey(p))
|
|
553
557
|
.map((p: any) =>
|
|
554
|
-
|
|
555
|
-
p.replace(
|
|
558
|
+
S_number === typeof p ? S_MT + Math.floor(p) :
|
|
559
|
+
p.replace(R_DOT, S_MT))
|
|
556
560
|
.join(S_DT)
|
|
557
561
|
}
|
|
558
562
|
}
|
|
@@ -572,18 +576,53 @@ function clone(val: any): any {
|
|
|
572
576
|
const replacer: any = (_k: any, v: any) => S_function === typeof v ?
|
|
573
577
|
(refs.push(v), '`$FUNCTION:' + (refs.length - 1) + '`') : v
|
|
574
578
|
const reviver: any = (_k: any, v: any, m: any) => S_string === typeof v ?
|
|
575
|
-
(m = v.match(
|
|
579
|
+
(m = v.match(R_FUNCTION_REF), m ? refs[m[1]] : v) : v
|
|
576
580
|
return UNDEF === val ? UNDEF : JSON.parse(JSON.stringify(val, replacer), reviver)
|
|
577
581
|
}
|
|
578
582
|
|
|
579
583
|
|
|
584
|
+
// Safely delete a property from an object or array element.
|
|
585
|
+
// Undefined arguments and invalid keys are ignored.
|
|
586
|
+
// Returns the (possibly modified) parent.
|
|
587
|
+
// For objects, the property is deleted using the delete operator.
|
|
588
|
+
// For arrays, the element at the index is removed and remaining elements are shifted down.
|
|
589
|
+
function delprop<PARENT>(parent: PARENT, key: any): PARENT {
|
|
590
|
+
if (!iskey(key)) {
|
|
591
|
+
return parent
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (ismap(parent)) {
|
|
595
|
+
// key = S_MT + key
|
|
596
|
+
key = strkey(key)
|
|
597
|
+
delete (parent as any)[key]
|
|
598
|
+
}
|
|
599
|
+
else if (islist(parent)) {
|
|
600
|
+
// Ensure key is an integer.
|
|
601
|
+
let keyI = +key
|
|
602
|
+
|
|
603
|
+
if (isNaN(keyI)) {
|
|
604
|
+
return parent
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
keyI = Math.floor(keyI)
|
|
608
|
+
|
|
609
|
+
// Delete list element at position keyI, shifting later elements down.
|
|
610
|
+
if (0 <= keyI && keyI < parent.length) {
|
|
611
|
+
for (let pI = keyI; pI < parent.length - 1; pI++) {
|
|
612
|
+
parent[pI] = parent[pI + 1]
|
|
613
|
+
}
|
|
614
|
+
parent.length = parent.length - 1
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return parent
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
|
|
580
622
|
// Safely set a property. Undefined arguments and invalid keys are ignored.
|
|
581
623
|
// Returns the (possibly modified) parent.
|
|
582
|
-
// If the value is undefined the key will be deleted from the parent.
|
|
583
624
|
// If the parent is a list, and the key is negative, prepend the value.
|
|
584
625
|
// 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
626
|
function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
588
627
|
if (!iskey(key)) {
|
|
589
628
|
return parent
|
|
@@ -591,12 +630,8 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
|
591
630
|
|
|
592
631
|
if (ismap(parent)) {
|
|
593
632
|
key = S_MT + key
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
else {
|
|
598
|
-
(parent as any)[key] = val
|
|
599
|
-
}
|
|
633
|
+
const pany = parent as any
|
|
634
|
+
pany[key] = val
|
|
600
635
|
}
|
|
601
636
|
else if (islist(parent)) {
|
|
602
637
|
// Ensure key is an integer.
|
|
@@ -608,18 +643,8 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
|
608
643
|
|
|
609
644
|
keyI = Math.floor(keyI)
|
|
610
645
|
|
|
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
646
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
622
|
-
|
|
647
|
+
if (0 <= keyI) {
|
|
623
648
|
parent[parent.length < keyI ? parent.length : keyI] = val
|
|
624
649
|
}
|
|
625
650
|
|
|
@@ -695,7 +720,7 @@ function merge(val: any): any {
|
|
|
695
720
|
}
|
|
696
721
|
else {
|
|
697
722
|
// Node stack. walking down the current obj.
|
|
698
|
-
let cur = [out]
|
|
723
|
+
let cur: any[] = [out]
|
|
699
724
|
let cI = 0
|
|
700
725
|
|
|
701
726
|
function merger(
|
|
@@ -713,7 +738,7 @@ function merge(val: any): any {
|
|
|
713
738
|
let lenpath = path.length
|
|
714
739
|
cI = lenpath - 1
|
|
715
740
|
if (UNDEF === cur[cI]) {
|
|
716
|
-
cur[cI] = getpath(slice(path, 0, lenpath - 1)
|
|
741
|
+
cur[cI] = getpath(out, slice(path, 0, lenpath - 1))
|
|
717
742
|
}
|
|
718
743
|
|
|
719
744
|
// Create node if needed.
|
|
@@ -746,68 +771,118 @@ function merge(val: any): any {
|
|
|
746
771
|
}
|
|
747
772
|
|
|
748
773
|
|
|
749
|
-
|
|
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) {
|
|
774
|
+
function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
|
|
758
775
|
|
|
759
776
|
// Operate on a string array.
|
|
760
|
-
const parts = islist(path) ? path :
|
|
777
|
+
const parts = islist(path) ? path :
|
|
778
|
+
'string' === typeof path ? path.split(S_DT) :
|
|
779
|
+
'number' === typeof path ? [strkey(path)] : UNDEF
|
|
761
780
|
|
|
762
781
|
if (UNDEF === parts) {
|
|
763
782
|
return UNDEF
|
|
764
783
|
}
|
|
765
784
|
|
|
766
|
-
let root = store
|
|
785
|
+
// let root = store
|
|
767
786
|
let val = store
|
|
768
|
-
const base = getprop(
|
|
787
|
+
const base = getprop(injdef, S_base)
|
|
788
|
+
const src = getprop(store, base, store)
|
|
789
|
+
const numparts = size(parts)
|
|
790
|
+
const dparent = getprop(injdef, 'dparent')
|
|
769
791
|
|
|
770
792
|
// An empty path (incl empty string) just finds the store.
|
|
771
|
-
if (null == path || null == store || (1 ===
|
|
772
|
-
|
|
773
|
-
val = getprop(store, base, store)
|
|
793
|
+
if (null == path || null == store || (1 === numparts && S_MT === parts[0])) {
|
|
794
|
+
val = src
|
|
774
795
|
}
|
|
775
|
-
else if (0 <
|
|
776
|
-
let pI = 0
|
|
796
|
+
else if (0 < numparts) {
|
|
777
797
|
|
|
778
|
-
//
|
|
779
|
-
if (
|
|
780
|
-
|
|
781
|
-
root = current
|
|
798
|
+
// Check for $ACTIONs
|
|
799
|
+
if (1 === numparts) {
|
|
800
|
+
val = getprop(store, parts[0])
|
|
782
801
|
}
|
|
783
802
|
|
|
784
|
-
|
|
785
|
-
|
|
803
|
+
if (!isfunc(val)) {
|
|
804
|
+
val = src
|
|
805
|
+
|
|
806
|
+
const m = parts[0].match(R_META_PATH)
|
|
807
|
+
if (m && injdef && injdef.meta) {
|
|
808
|
+
val = getprop(injdef.meta, m[1])
|
|
809
|
+
parts[0] = m[3]
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
const dpath = getprop(injdef, 'dpath')
|
|
786
813
|
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
getprop(getprop(root, base), part) :
|
|
790
|
-
first
|
|
814
|
+
for (let pI = 0; UNDEF !== val && pI < parts.length; pI++) {
|
|
815
|
+
let part = parts[pI]
|
|
791
816
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
817
|
+
if (injdef && S_DKEY === part) {
|
|
818
|
+
part = getprop(injdef, S_key)
|
|
819
|
+
}
|
|
820
|
+
else if (injdef && part.startsWith('$GET:')) {
|
|
821
|
+
// $GET:path$ -> get store value, use as path part (string)
|
|
822
|
+
part = stringify(getpath(src, part.substring(5, part.length - 1)))
|
|
823
|
+
}
|
|
824
|
+
else if (injdef && part.startsWith('$REF:')) {
|
|
825
|
+
// $REF:refpath$ -> get spec value, use as path part (string)
|
|
826
|
+
part = stringify(getpath(getprop(store, S_DSPEC), part.substring(5, part.length - 1)))
|
|
827
|
+
}
|
|
828
|
+
else if (injdef && part.startsWith('$META:')) {
|
|
829
|
+
// $META:metapath$ -> get meta value, use as path part (string)
|
|
830
|
+
part = stringify(getpath(getprop(injdef, 'meta'), part.substring(6, part.length - 1)))
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// $$ escapes $
|
|
834
|
+
part = part.replace(R_DOUBLE_DOLLAR, '$')
|
|
835
|
+
|
|
836
|
+
if (S_MT === part) {
|
|
837
|
+
|
|
838
|
+
let ascends = 0
|
|
839
|
+
while (S_MT === parts[1 + pI]) {
|
|
840
|
+
ascends++
|
|
841
|
+
pI++
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
if (injdef && 0 < ascends) {
|
|
845
|
+
if (pI === parts.length - 1) {
|
|
846
|
+
ascends--
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
if (0 === ascends) {
|
|
850
|
+
val = dparent
|
|
851
|
+
}
|
|
852
|
+
else {
|
|
853
|
+
const fullpath = slice(dpath, 0 - ascends).concat(parts.slice(pI + 1))
|
|
854
|
+
|
|
855
|
+
if (ascends <= size(dpath)) {
|
|
856
|
+
val = getpath(store, fullpath)
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
val = UNDEF
|
|
860
|
+
}
|
|
861
|
+
break
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
else {
|
|
865
|
+
val = dparent
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
val = getprop(val, part)
|
|
870
|
+
}
|
|
797
871
|
}
|
|
798
872
|
}
|
|
799
873
|
}
|
|
800
874
|
|
|
801
875
|
// Inj may provide a custom handler to modify found value.
|
|
802
|
-
|
|
876
|
+
const handler = getprop(injdef, 'handler')
|
|
877
|
+
if (null != injdef && isfunc(handler)) {
|
|
803
878
|
const ref = pathify(path)
|
|
804
|
-
val =
|
|
879
|
+
val = handler(injdef, val, ref, store)
|
|
805
880
|
}
|
|
806
881
|
|
|
807
882
|
return val
|
|
808
883
|
}
|
|
809
884
|
|
|
810
|
-
|
|
885
|
+
|
|
811
886
|
|
|
812
887
|
// Inject values from a data store into a node recursively, resolving
|
|
813
888
|
// paths against the store, or current if they are local. THe modify
|
|
@@ -816,36 +891,29 @@ function getpath(path: string | string[], store: any, current?: any, inj?: Injec
|
|
|
816
891
|
function inject(
|
|
817
892
|
val: any,
|
|
818
893
|
store: any,
|
|
819
|
-
|
|
820
|
-
current?: any,
|
|
821
|
-
inj?: Injection,
|
|
894
|
+
injdef?: Partial<Injection>,
|
|
822
895
|
) {
|
|
823
896
|
const valtype = typeof val
|
|
897
|
+
let inj: Injection = injdef as Injection
|
|
824
898
|
|
|
825
899
|
// Create state if at root of injection. The input value is placed
|
|
826
900
|
// inside a virtual parent holder to simplify edge cases.
|
|
827
|
-
if (UNDEF ===
|
|
828
|
-
|
|
901
|
+
if (UNDEF === injdef || null == injdef.mode) {
|
|
829
902
|
// Set up state assuming we are starting in the virtual parent.
|
|
830
|
-
inj = new Injection(val, { [S_DTOP]: val }
|
|
831
|
-
inj.
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
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)
|
|
903
|
+
inj = new Injection(val, { [S_DTOP]: val })
|
|
904
|
+
inj.dparent = store
|
|
905
|
+
inj.errs = getprop(store, S_DERRS, [])
|
|
906
|
+
inj.meta.__d = 0
|
|
907
|
+
|
|
908
|
+
if (UNDEF !== injdef) {
|
|
909
|
+
inj.modify = null == injdef.modify ? inj.modify : injdef.modify
|
|
910
|
+
inj.extra = null == injdef.extra ? inj.extra : injdef.extra
|
|
911
|
+
inj.meta = null == injdef.meta ? inj.meta : injdef.meta
|
|
912
|
+
inj.handler = null == injdef.handler ? inj.handler : injdef.handler
|
|
913
|
+
}
|
|
846
914
|
}
|
|
847
915
|
|
|
848
|
-
|
|
916
|
+
inj.descend()
|
|
849
917
|
|
|
850
918
|
// Descend into node.
|
|
851
919
|
if (isnode(val)) {
|
|
@@ -857,7 +925,7 @@ function inject(
|
|
|
857
925
|
let nodekeys = ismap(val) ? [
|
|
858
926
|
...Object.keys(val).filter(k => !k.includes(S_DS)).sort(),
|
|
859
927
|
...Object.keys(val).filter(k => k.includes(S_DS)).sort(),
|
|
860
|
-
] : val.map((_n: any, i: number) => i)
|
|
928
|
+
] : (val as any).map((_n: any, i: number) => i)
|
|
861
929
|
|
|
862
930
|
|
|
863
931
|
// Each child key-value pair is processed in three injection phases:
|
|
@@ -871,7 +939,7 @@ function inject(
|
|
|
871
939
|
childinj.mode = S_MKEYPRE
|
|
872
940
|
|
|
873
941
|
// Peform the key:pre mode injection on the child key.
|
|
874
|
-
const prekey = _injectstr(nodekey, store,
|
|
942
|
+
const prekey = _injectstr(nodekey, store, childinj)
|
|
875
943
|
|
|
876
944
|
// The injection may modify child processing.
|
|
877
945
|
nkI = childinj.keyI
|
|
@@ -884,7 +952,7 @@ function inject(
|
|
|
884
952
|
|
|
885
953
|
// Perform the val mode injection on the child value.
|
|
886
954
|
// NOTE: return value is not used.
|
|
887
|
-
inject(childinj.val, store,
|
|
955
|
+
inject(childinj.val, store, childinj)
|
|
888
956
|
|
|
889
957
|
// The injection may modify child processing.
|
|
890
958
|
nkI = childinj.keyI
|
|
@@ -892,7 +960,7 @@ function inject(
|
|
|
892
960
|
|
|
893
961
|
// Peform the key:post mode injection on the child key.
|
|
894
962
|
childinj.mode = S_MKEYPOST as InjectMode
|
|
895
|
-
_injectstr(nodekey, store,
|
|
963
|
+
_injectstr(nodekey, store, childinj)
|
|
896
964
|
|
|
897
965
|
// The injection may modify child processing.
|
|
898
966
|
nkI = childinj.keyI
|
|
@@ -904,21 +972,27 @@ function inject(
|
|
|
904
972
|
// Inject paths into string scalars.
|
|
905
973
|
else if (S_string === valtype) {
|
|
906
974
|
inj.mode = S_MVAL as InjectMode
|
|
907
|
-
val = _injectstr(val, store,
|
|
908
|
-
|
|
975
|
+
val = _injectstr(val, store, inj)
|
|
976
|
+
if (SKIP !== val) {
|
|
977
|
+
inj.setval(val)
|
|
978
|
+
}
|
|
909
979
|
}
|
|
910
980
|
|
|
981
|
+
// console.log('INJECT-M0 ', val, '' + inj)
|
|
982
|
+
|
|
911
983
|
// Custom modification.
|
|
912
|
-
if (modify) {
|
|
984
|
+
if (inj.modify && SKIP !== val) {
|
|
913
985
|
let mkey = inj.key
|
|
914
986
|
let mparent = inj.parent
|
|
915
987
|
let mval = getprop(mparent, mkey)
|
|
916
|
-
|
|
988
|
+
|
|
989
|
+
// console.log('INJECT-M1 ' + inj)
|
|
990
|
+
|
|
991
|
+
inj.modify(
|
|
917
992
|
mval,
|
|
918
993
|
mkey,
|
|
919
994
|
mparent,
|
|
920
995
|
inj,
|
|
921
|
-
current,
|
|
922
996
|
store
|
|
923
997
|
)
|
|
924
998
|
}
|
|
@@ -935,19 +1009,18 @@ function inject(
|
|
|
935
1009
|
|
|
936
1010
|
// Delete a key from a map or list.
|
|
937
1011
|
const transform_DELETE: Injector = (inj: Injection) => {
|
|
938
|
-
// _setparentprop(inj, UNDEF)
|
|
939
1012
|
inj.setval(UNDEF)
|
|
940
1013
|
return UNDEF
|
|
941
1014
|
}
|
|
942
1015
|
|
|
943
1016
|
|
|
944
1017
|
// Copy value from source data.
|
|
945
|
-
const transform_COPY: Injector = (inj: Injection, _val: any
|
|
1018
|
+
const transform_COPY: Injector = (inj: Injection, _val: any) => {
|
|
946
1019
|
const { mode, key } = inj
|
|
947
1020
|
|
|
948
1021
|
let out = key
|
|
949
1022
|
if (!mode.startsWith(S_MKEY)) {
|
|
950
|
-
out = getprop(
|
|
1023
|
+
out = getprop(inj.dparent, key)
|
|
951
1024
|
inj.setval(out)
|
|
952
1025
|
}
|
|
953
1026
|
|
|
@@ -957,7 +1030,7 @@ const transform_COPY: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
957
1030
|
|
|
958
1031
|
// As a value, inject the key of the parent node.
|
|
959
1032
|
// As a key, defined the name of the key property in the source object.
|
|
960
|
-
const transform_KEY: Injector = (inj: Injection
|
|
1033
|
+
const transform_KEY: Injector = (inj: Injection) => {
|
|
961
1034
|
const { mode, path, parent } = inj
|
|
962
1035
|
|
|
963
1036
|
// Do nothing in val mode.
|
|
@@ -966,22 +1039,22 @@ const transform_KEY: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
966
1039
|
}
|
|
967
1040
|
|
|
968
1041
|
// Key is defined by $KEY meta property.
|
|
969
|
-
const keyspec = getprop(parent,
|
|
1042
|
+
const keyspec = getprop(parent, S_BKEY)
|
|
970
1043
|
if (UNDEF !== keyspec) {
|
|
971
|
-
|
|
972
|
-
return getprop(
|
|
1044
|
+
delprop(parent, S_BKEY)
|
|
1045
|
+
return getprop(inj.dparent, keyspec)
|
|
973
1046
|
}
|
|
974
1047
|
|
|
975
1048
|
// Key is defined within general purpose $META object.
|
|
976
|
-
return getprop(getprop(parent,
|
|
1049
|
+
return getprop(getprop(parent, S_BANNO), S_KEY, getprop(path, path.length - 2))
|
|
977
1050
|
}
|
|
978
1051
|
|
|
979
1052
|
|
|
980
|
-
//
|
|
1053
|
+
// Annotatea node. Does nothing itself, just used by
|
|
981
1054
|
// other injectors, and is removed when called.
|
|
982
|
-
const
|
|
1055
|
+
const transform_ANNO: Injector = (inj: Injection) => {
|
|
983
1056
|
const { parent } = inj
|
|
984
|
-
|
|
1057
|
+
delprop(parent, S_BANNO)
|
|
985
1058
|
return UNDEF
|
|
986
1059
|
}
|
|
987
1060
|
|
|
@@ -991,18 +1064,22 @@ const transform_META: Injector = (inj: Injection) => {
|
|
|
991
1064
|
// If the value is an array, the elements are first merged using `merge`.
|
|
992
1065
|
// If the value is the empty string, merge the top level store.
|
|
993
1066
|
// Format: { '`$MERGE`': '`source-path`' | ['`source-paths`', ...] }
|
|
994
|
-
const transform_MERGE: Injector = (
|
|
995
|
-
inj: Injection, _val: any, current: any
|
|
996
|
-
) => {
|
|
1067
|
+
const transform_MERGE: Injector = (inj: Injection) => {
|
|
997
1068
|
const { mode, key, parent } = inj
|
|
998
1069
|
|
|
999
|
-
|
|
1070
|
+
// Ensures $MERGE is removed from parent list (val mode).
|
|
1071
|
+
let out: any = UNDEF
|
|
1072
|
+
|
|
1073
|
+
if (S_MKEYPRE === mode) {
|
|
1074
|
+
out = key
|
|
1075
|
+
}
|
|
1000
1076
|
|
|
1001
1077
|
// Operate after child values have been transformed.
|
|
1002
|
-
if (S_MKEYPOST === mode) {
|
|
1078
|
+
else if (S_MKEYPOST === mode) {
|
|
1079
|
+
out = key
|
|
1003
1080
|
|
|
1004
1081
|
let args = getprop(parent, key)
|
|
1005
|
-
args =
|
|
1082
|
+
args = Array.isArray(args) ? args : [args]
|
|
1006
1083
|
|
|
1007
1084
|
// Remove the $MERGE command from a parent map.
|
|
1008
1085
|
inj.setval(UNDEF)
|
|
@@ -1013,11 +1090,10 @@ const transform_MERGE: Injector = (
|
|
|
1013
1090
|
|
|
1014
1091
|
merge(mergelist)
|
|
1015
1092
|
|
|
1016
|
-
return key
|
|
1093
|
+
// return key
|
|
1017
1094
|
}
|
|
1018
1095
|
|
|
1019
|
-
|
|
1020
|
-
return UNDEF
|
|
1096
|
+
return out
|
|
1021
1097
|
}
|
|
1022
1098
|
|
|
1023
1099
|
|
|
@@ -1026,7 +1102,6 @@ const transform_MERGE: Injector = (
|
|
|
1026
1102
|
const transform_EACH: Injector = (
|
|
1027
1103
|
inj: Injection,
|
|
1028
1104
|
_val: any,
|
|
1029
|
-
current: any,
|
|
1030
1105
|
_ref: string,
|
|
1031
1106
|
store: any
|
|
1032
1107
|
) => {
|
|
@@ -1046,7 +1121,8 @@ const transform_EACH: Injector = (
|
|
|
1046
1121
|
|
|
1047
1122
|
// Source data.
|
|
1048
1123
|
const srcstore = getprop(store, inj.base, store)
|
|
1049
|
-
|
|
1124
|
+
|
|
1125
|
+
const src = getpath(srcstore, srcpath, inj)
|
|
1050
1126
|
|
|
1051
1127
|
// Create parallel data structures:
|
|
1052
1128
|
// source entries :: child templates
|
|
@@ -1065,7 +1141,7 @@ const transform_EACH: Injector = (
|
|
|
1065
1141
|
...clone(child),
|
|
1066
1142
|
|
|
1067
1143
|
// Make a note of the key for $KEY transforms.
|
|
1068
|
-
[
|
|
1144
|
+
[S_BANNO]: { KEY: n[0] }
|
|
1069
1145
|
}))
|
|
1070
1146
|
}
|
|
1071
1147
|
|
|
@@ -1074,24 +1150,36 @@ const transform_EACH: Injector = (
|
|
|
1074
1150
|
if (0 < size(tval)) {
|
|
1075
1151
|
tcur = null == src ? UNDEF : Object.values(src)
|
|
1076
1152
|
|
|
1153
|
+
const ckey = getelem(inj.path, -2)
|
|
1154
|
+
|
|
1077
1155
|
const tpath = slice(inj.path, -1)
|
|
1156
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey]
|
|
1157
|
+
|
|
1078
1158
|
|
|
1079
1159
|
// Parent structure.
|
|
1080
|
-
|
|
1160
|
+
|
|
1161
|
+
// const ckey = getelem(cpath, -1)
|
|
1081
1162
|
tcur = { [ckey]: tcur }
|
|
1082
1163
|
|
|
1083
1164
|
if (1 < tpath.length) {
|
|
1084
1165
|
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1166
|
+
// const pkey = getelem(cpath, -2, S_DTOP)
|
|
1085
1167
|
tcur = { [pkey]: tcur }
|
|
1168
|
+
dpath.push('$:' + pkey)
|
|
1086
1169
|
}
|
|
1087
1170
|
|
|
1088
1171
|
const tinj = inj.child(0, [ckey])
|
|
1089
1172
|
tinj.path = tpath
|
|
1090
1173
|
tinj.nodes = slice(inj.nodes, -1)
|
|
1091
|
-
|
|
1174
|
+
|
|
1175
|
+
tinj.parent = getelem(tinj.nodes, -1)
|
|
1176
|
+
setprop(tinj.parent, ckey, tval)
|
|
1177
|
+
|
|
1092
1178
|
tinj.val = tval
|
|
1179
|
+
tinj.dpath = dpath
|
|
1180
|
+
tinj.dparent = tcur
|
|
1093
1181
|
|
|
1094
|
-
inject(tval, store,
|
|
1182
|
+
inject(tval, store, tinj)
|
|
1095
1183
|
rval = tinj.val
|
|
1096
1184
|
}
|
|
1097
1185
|
|
|
@@ -1107,7 +1195,6 @@ const transform_EACH: Injector = (
|
|
|
1107
1195
|
const transform_PACK: Injector = (
|
|
1108
1196
|
inj: Injection,
|
|
1109
1197
|
_val: any,
|
|
1110
|
-
current: any,
|
|
1111
1198
|
_ref: string,
|
|
1112
1199
|
store: any
|
|
1113
1200
|
) => {
|
|
@@ -1124,19 +1211,20 @@ const transform_PACK: Injector = (
|
|
|
1124
1211
|
const child = clone(args[1]) // Child template.
|
|
1125
1212
|
|
|
1126
1213
|
// Find key and target node.
|
|
1127
|
-
const keyprop = child[
|
|
1214
|
+
const keyprop = child[S_BKEY]
|
|
1128
1215
|
const tkey = getelem(path, -2)
|
|
1129
1216
|
const target = nodes[path.length - 2] || nodes[path.length - 1]
|
|
1130
1217
|
|
|
1131
1218
|
// Source data
|
|
1132
1219
|
const srcstore = getprop(store, inj.base, store)
|
|
1133
|
-
|
|
1220
|
+
|
|
1221
|
+
let src = getpath(srcstore, srcpath, inj)
|
|
1134
1222
|
|
|
1135
1223
|
// Prepare source as a list.
|
|
1136
1224
|
src = islist(src) ? src :
|
|
1137
1225
|
ismap(src) ? Object.entries(src)
|
|
1138
1226
|
.reduce((a: any[], n: any) =>
|
|
1139
|
-
(n[1][
|
|
1227
|
+
(n[1][S_BANNO] = { KEY: n[0] }, a.push(n[1]), a), []) :
|
|
1140
1228
|
UNDEF
|
|
1141
1229
|
|
|
1142
1230
|
if (null == src) {
|
|
@@ -1144,9 +1232,9 @@ const transform_PACK: Injector = (
|
|
|
1144
1232
|
}
|
|
1145
1233
|
|
|
1146
1234
|
// Get key if specified.
|
|
1147
|
-
let childkey: PropKey | undefined = getprop(child,
|
|
1235
|
+
let childkey: PropKey | undefined = getprop(child, S_BKEY)
|
|
1148
1236
|
let keyname = UNDEF === childkey ? keyprop : childkey
|
|
1149
|
-
|
|
1237
|
+
delprop(child, S_BKEY)
|
|
1150
1238
|
|
|
1151
1239
|
// Build parallel target object.
|
|
1152
1240
|
let tval: any = {}
|
|
@@ -1154,7 +1242,13 @@ const transform_PACK: Injector = (
|
|
|
1154
1242
|
let kn = getprop(n, keyname)
|
|
1155
1243
|
setprop(a, kn, clone(child))
|
|
1156
1244
|
const nchild = getprop(a, kn)
|
|
1157
|
-
|
|
1245
|
+
const mval = getprop(n, S_BANNO)
|
|
1246
|
+
if (UNDEF === mval) {
|
|
1247
|
+
delprop(nchild, S_BANNO)
|
|
1248
|
+
}
|
|
1249
|
+
else {
|
|
1250
|
+
setprop(nchild, S_BANNO, mval)
|
|
1251
|
+
}
|
|
1158
1252
|
return a
|
|
1159
1253
|
}, tval)
|
|
1160
1254
|
|
|
@@ -1173,20 +1267,28 @@ const transform_PACK: Injector = (
|
|
|
1173
1267
|
const tpath = slice(inj.path, -1)
|
|
1174
1268
|
|
|
1175
1269
|
const ckey = getelem(inj.path, -2)
|
|
1270
|
+
const dpath = [S_DTOP, ...srcpath.split(S_DT), '$:' + ckey]
|
|
1271
|
+
|
|
1176
1272
|
tcur = { [ckey]: tcur }
|
|
1177
1273
|
|
|
1178
1274
|
if (1 < tpath.length) {
|
|
1179
1275
|
const pkey = getelem(inj.path, -3, S_DTOP)
|
|
1180
1276
|
tcur = { [pkey]: tcur }
|
|
1277
|
+
dpath.push('$:' + pkey)
|
|
1181
1278
|
}
|
|
1182
1279
|
|
|
1183
1280
|
const tinj = inj.child(0, [ckey])
|
|
1184
1281
|
tinj.path = tpath
|
|
1185
1282
|
tinj.nodes = slice(inj.nodes, -1)
|
|
1186
|
-
|
|
1283
|
+
|
|
1284
|
+
// tinj.parent = tcur
|
|
1285
|
+
tinj.parent = getelem(tinj.nodes, -1)
|
|
1187
1286
|
tinj.val = tval
|
|
1188
1287
|
|
|
1189
|
-
|
|
1288
|
+
tinj.dpath = dpath
|
|
1289
|
+
tinj.dparent = tcur
|
|
1290
|
+
|
|
1291
|
+
inject(tval, store, tinj)
|
|
1190
1292
|
rval = tinj.val
|
|
1191
1293
|
}
|
|
1192
1294
|
|
|
@@ -1197,16 +1299,16 @@ const transform_PACK: Injector = (
|
|
|
1197
1299
|
}
|
|
1198
1300
|
|
|
1199
1301
|
|
|
1302
|
+
// TODO: not found ref should removed key (setprop UNDEF)
|
|
1200
1303
|
// Reference original spec (enables recursice transformations)
|
|
1201
1304
|
// Format: ['`$REF`', '`spec-path`']
|
|
1202
1305
|
const transform_REF: Injector = (
|
|
1203
1306
|
inj: Injection,
|
|
1204
1307
|
val: any,
|
|
1205
|
-
_current: any,
|
|
1206
1308
|
_ref: string,
|
|
1207
1309
|
store: any
|
|
1208
1310
|
) => {
|
|
1209
|
-
const { nodes
|
|
1311
|
+
const { nodes } = inj
|
|
1210
1312
|
|
|
1211
1313
|
if (S_MVAL !== inj.mode) {
|
|
1212
1314
|
return UNDEF
|
|
@@ -1218,7 +1320,12 @@ const transform_REF: Injector = (
|
|
|
1218
1320
|
|
|
1219
1321
|
// Spec reference.
|
|
1220
1322
|
const spec = getprop(store, S_DSPEC)()
|
|
1221
|
-
|
|
1323
|
+
|
|
1324
|
+
const ref = getpath(spec, refpath, {
|
|
1325
|
+
// TODO: test relative refs
|
|
1326
|
+
dpath: inj.path.slice(1),
|
|
1327
|
+
dparent: getpath(spec, inj.path.slice(1))
|
|
1328
|
+
})
|
|
1222
1329
|
|
|
1223
1330
|
let hasSubRef = false
|
|
1224
1331
|
if (isnode(ref)) {
|
|
@@ -1234,8 +1341,8 @@ const transform_REF: Injector = (
|
|
|
1234
1341
|
|
|
1235
1342
|
const cpath = slice(inj.path, -3)
|
|
1236
1343
|
const tpath = slice(inj.path, -1)
|
|
1237
|
-
let tcur = getpath(
|
|
1238
|
-
let tval = getpath(
|
|
1344
|
+
let tcur = getpath(store, cpath)
|
|
1345
|
+
let tval = getpath(store, tpath)
|
|
1239
1346
|
let rval = UNDEF
|
|
1240
1347
|
|
|
1241
1348
|
if (!hasSubRef || UNDEF !== tval) {
|
|
@@ -1246,7 +1353,10 @@ const transform_REF: Injector = (
|
|
|
1246
1353
|
tinj.parent = getelem(nodes, -2)
|
|
1247
1354
|
tinj.val = tref
|
|
1248
1355
|
|
|
1249
|
-
|
|
1356
|
+
tinj.dpath = [...cpath]
|
|
1357
|
+
tinj.dparent = tcur
|
|
1358
|
+
|
|
1359
|
+
inject(tref, store, tinj)
|
|
1250
1360
|
|
|
1251
1361
|
rval = tinj.val
|
|
1252
1362
|
}
|
|
@@ -1270,13 +1380,17 @@ const transform_REF: Injector = (
|
|
|
1270
1380
|
function transform(
|
|
1271
1381
|
data: any, // Source data to transform into new data (original not mutated)
|
|
1272
1382
|
spec: any, // Transform specification; output follows this shape
|
|
1273
|
-
extra?: any, // Additional store of data and transforms.
|
|
1274
|
-
modify?: Modify // Optionally modify individual values.
|
|
1383
|
+
// extra?: any, // Additional store of data and transforms.
|
|
1384
|
+
// modify?: Modify // Optionally modify individual values.
|
|
1385
|
+
injdef?: Partial<Injection>
|
|
1275
1386
|
) {
|
|
1276
1387
|
// Clone the spec so that the clone can be modified in place as the transform result.
|
|
1277
1388
|
const origspec = spec
|
|
1278
1389
|
spec = clone(origspec)
|
|
1279
1390
|
|
|
1391
|
+
const extra = injdef?.extra
|
|
1392
|
+
// const modify = injdef?.modify
|
|
1393
|
+
|
|
1280
1394
|
const extraTransforms: any = {}
|
|
1281
1395
|
const extraData = null == extra ? UNDEF : items(extra)
|
|
1282
1396
|
.reduce((a: any, n: any[]) =>
|
|
@@ -1309,7 +1423,7 @@ function transform(
|
|
|
1309
1423
|
$DELETE: transform_DELETE,
|
|
1310
1424
|
$COPY: transform_COPY,
|
|
1311
1425
|
$KEY: transform_KEY,
|
|
1312
|
-
$
|
|
1426
|
+
$ANNO: transform_ANNO,
|
|
1313
1427
|
$MERGE: transform_MERGE,
|
|
1314
1428
|
$EACH: transform_EACH,
|
|
1315
1429
|
$PACK: transform_PACK,
|
|
@@ -1319,14 +1433,15 @@ function transform(
|
|
|
1319
1433
|
...extraTransforms,
|
|
1320
1434
|
}
|
|
1321
1435
|
|
|
1322
|
-
const out = inject(spec, store, modify,
|
|
1436
|
+
// const out = inject(spec, store, { modify, extra })
|
|
1437
|
+
const out = inject(spec, store, injdef)
|
|
1323
1438
|
return out
|
|
1324
1439
|
}
|
|
1325
1440
|
|
|
1326
1441
|
|
|
1327
1442
|
// A required string value. NOTE: Rejects empty strings.
|
|
1328
|
-
const validate_STRING: Injector = (inj: Injection
|
|
1329
|
-
let out = getprop(
|
|
1443
|
+
const validate_STRING: Injector = (inj: Injection) => {
|
|
1444
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1330
1445
|
|
|
1331
1446
|
const t = typify(out)
|
|
1332
1447
|
if (S_string !== t) {
|
|
@@ -1346,8 +1461,8 @@ const validate_STRING: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1346
1461
|
|
|
1347
1462
|
|
|
1348
1463
|
// A required number value (int or float).
|
|
1349
|
-
const validate_NUMBER: Injector = (inj: Injection
|
|
1350
|
-
let out = getprop(
|
|
1464
|
+
const validate_NUMBER: Injector = (inj: Injection) => {
|
|
1465
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1351
1466
|
|
|
1352
1467
|
const t = typify(out)
|
|
1353
1468
|
if (S_number !== t) {
|
|
@@ -1360,8 +1475,8 @@ const validate_NUMBER: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1360
1475
|
|
|
1361
1476
|
|
|
1362
1477
|
// A required boolean value.
|
|
1363
|
-
const validate_BOOLEAN: Injector = (inj: Injection
|
|
1364
|
-
let out = getprop(
|
|
1478
|
+
const validate_BOOLEAN: Injector = (inj: Injection) => {
|
|
1479
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1365
1480
|
|
|
1366
1481
|
const t = typify(out)
|
|
1367
1482
|
if (S_boolean !== t) {
|
|
@@ -1374,8 +1489,8 @@ const validate_BOOLEAN: Injector = (inj: Injection, _val: any, current: any) =>
|
|
|
1374
1489
|
|
|
1375
1490
|
|
|
1376
1491
|
// A required object (map) value (contents not validated).
|
|
1377
|
-
const validate_OBJECT: Injector = (inj: Injection
|
|
1378
|
-
let out = getprop(
|
|
1492
|
+
const validate_OBJECT: Injector = (inj: Injection) => {
|
|
1493
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1379
1494
|
|
|
1380
1495
|
const t = typify(out)
|
|
1381
1496
|
if (t !== S_object) {
|
|
@@ -1388,8 +1503,8 @@ const validate_OBJECT: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1388
1503
|
|
|
1389
1504
|
|
|
1390
1505
|
// A required array (list) value (contents not validated).
|
|
1391
|
-
const validate_ARRAY: Injector = (inj: Injection
|
|
1392
|
-
let out = getprop(
|
|
1506
|
+
const validate_ARRAY: Injector = (inj: Injection) => {
|
|
1507
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1393
1508
|
|
|
1394
1509
|
const t = typify(out)
|
|
1395
1510
|
if (t !== S_array) {
|
|
@@ -1402,8 +1517,8 @@ const validate_ARRAY: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1402
1517
|
|
|
1403
1518
|
|
|
1404
1519
|
// A required function value.
|
|
1405
|
-
const validate_FUNCTION: Injector = (inj: Injection
|
|
1406
|
-
let out = getprop(
|
|
1520
|
+
const validate_FUNCTION: Injector = (inj: Injection) => {
|
|
1521
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1407
1522
|
|
|
1408
1523
|
const t = typify(out)
|
|
1409
1524
|
if (S_function !== t) {
|
|
@@ -1416,8 +1531,9 @@ const validate_FUNCTION: Injector = (inj: Injection, _val: any, current: any) =>
|
|
|
1416
1531
|
|
|
1417
1532
|
|
|
1418
1533
|
// Allow any value.
|
|
1419
|
-
const validate_ANY: Injector = (inj: Injection
|
|
1420
|
-
|
|
1534
|
+
const validate_ANY: Injector = (inj: Injection) => {
|
|
1535
|
+
let out = getprop(inj.dparent, inj.key)
|
|
1536
|
+
return out
|
|
1421
1537
|
}
|
|
1422
1538
|
|
|
1423
1539
|
|
|
@@ -1425,7 +1541,7 @@ const validate_ANY: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1425
1541
|
// Specify child values for map or list.
|
|
1426
1542
|
// Map syntax: {'`$CHILD`': child-template }
|
|
1427
1543
|
// List syntax: ['`$CHILD`', child-template ]
|
|
1428
|
-
const validate_CHILD: Injector = (inj: Injection
|
|
1544
|
+
const validate_CHILD: Injector = (inj: Injection) => {
|
|
1429
1545
|
const { mode, key, parent, keys, path } = inj
|
|
1430
1546
|
|
|
1431
1547
|
// Setup data structures for validation by cloning child template.
|
|
@@ -1436,7 +1552,7 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1436
1552
|
|
|
1437
1553
|
// Get corresponding current object.
|
|
1438
1554
|
const pkey = getprop(path, path.length - 2)
|
|
1439
|
-
let tval = getprop(
|
|
1555
|
+
let tval = getprop(inj.dparent, pkey)
|
|
1440
1556
|
|
|
1441
1557
|
if (UNDEF == tval) {
|
|
1442
1558
|
tval = {}
|
|
@@ -1456,7 +1572,6 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1456
1572
|
}
|
|
1457
1573
|
|
|
1458
1574
|
// Remove $CHILD to cleanup ouput.
|
|
1459
|
-
// _setparentprop(inj, UNDEF)
|
|
1460
1575
|
inj.setval(UNDEF)
|
|
1461
1576
|
return UNDEF
|
|
1462
1577
|
}
|
|
@@ -1472,27 +1587,28 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1472
1587
|
|
|
1473
1588
|
const childtm = getprop(parent, 1)
|
|
1474
1589
|
|
|
1475
|
-
if (UNDEF ===
|
|
1590
|
+
if (UNDEF === inj.dparent) {
|
|
1476
1591
|
// Empty list as default.
|
|
1477
1592
|
parent.length = 0
|
|
1478
1593
|
return UNDEF
|
|
1479
1594
|
}
|
|
1480
1595
|
|
|
1481
|
-
if (!islist(
|
|
1596
|
+
if (!islist(inj.dparent)) {
|
|
1482
1597
|
const msg = _invalidTypeMsg(
|
|
1483
|
-
slice(inj.path, -1), S_array, typify(
|
|
1598
|
+
slice(inj.path, -1), S_array, typify(inj.dparent), inj.dparent, 'V0230')
|
|
1484
1599
|
inj.errs.push(msg)
|
|
1485
1600
|
inj.keyI = parent.length
|
|
1486
|
-
return
|
|
1601
|
+
return inj.dparent
|
|
1487
1602
|
}
|
|
1488
1603
|
|
|
1489
1604
|
// Clone children abd reset inj key index.
|
|
1490
1605
|
// The inject child loop will now iterate over the cloned children,
|
|
1491
1606
|
// validating them againt the current list values.
|
|
1492
|
-
|
|
1493
|
-
|
|
1607
|
+
|
|
1608
|
+
inj.dparent.map((_n, i) => parent[i] = clone(childtm))
|
|
1609
|
+
parent.length = inj.dparent.length
|
|
1494
1610
|
inj.keyI = 0
|
|
1495
|
-
const out = getprop(
|
|
1611
|
+
const out = getprop(inj.dparent, 0)
|
|
1496
1612
|
return out
|
|
1497
1613
|
}
|
|
1498
1614
|
|
|
@@ -1505,11 +1621,10 @@ const validate_CHILD: Injector = (inj: Injection, _val: any, current: any) => {
|
|
|
1505
1621
|
const validate_ONE: Injector = (
|
|
1506
1622
|
inj: Injection,
|
|
1507
1623
|
_val: any,
|
|
1508
|
-
current: any,
|
|
1509
1624
|
_ref: string,
|
|
1510
1625
|
store: any
|
|
1511
1626
|
) => {
|
|
1512
|
-
const { mode, parent,
|
|
1627
|
+
const { mode, parent, keyI } = inj
|
|
1513
1628
|
|
|
1514
1629
|
// Only operate in val mode, since parent is a list.
|
|
1515
1630
|
if (S_MVAL === mode) {
|
|
@@ -1523,7 +1638,7 @@ const validate_ONE: Injector = (
|
|
|
1523
1638
|
inj.keyI = inj.keys.length
|
|
1524
1639
|
|
|
1525
1640
|
// Clean up structure, replacing [$ONE, ...] with current
|
|
1526
|
-
|
|
1641
|
+
inj.setval(inj.dparent, 2)
|
|
1527
1642
|
|
|
1528
1643
|
inj.path = slice(inj.path, -1)
|
|
1529
1644
|
inj.key = getelem(inj.path, -1)
|
|
@@ -1543,10 +1658,15 @@ const validate_ONE: Injector = (
|
|
|
1543
1658
|
let terrs: any[] = []
|
|
1544
1659
|
|
|
1545
1660
|
const vstore = { ...store }
|
|
1546
|
-
vstore.$TOP =
|
|
1547
|
-
|
|
1661
|
+
vstore.$TOP = inj.dparent
|
|
1662
|
+
|
|
1663
|
+
const vcurrent = validate(inj.dparent, tval, {
|
|
1664
|
+
extra: vstore,
|
|
1665
|
+
errs: terrs,
|
|
1666
|
+
meta: inj.meta,
|
|
1667
|
+
})
|
|
1548
1668
|
|
|
1549
|
-
|
|
1669
|
+
inj.setval(vcurrent, -2)
|
|
1550
1670
|
|
|
1551
1671
|
// Accept current value if there was a match
|
|
1552
1672
|
if (0 === terrs.length) {
|
|
@@ -1559,24 +1679,18 @@ const validate_ONE: Injector = (
|
|
|
1559
1679
|
const valdesc = tvals
|
|
1560
1680
|
.map((v: any) => stringify(v))
|
|
1561
1681
|
.join(', ')
|
|
1562
|
-
.replace(
|
|
1682
|
+
.replace(R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
|
|
1563
1683
|
|
|
1564
1684
|
inj.errs.push(_invalidTypeMsg(
|
|
1565
1685
|
inj.path,
|
|
1566
1686
|
(1 < tvals.length ? 'one of ' : '') + valdesc,
|
|
1567
|
-
typify(
|
|
1687
|
+
typify(inj.dparent), inj.dparent, 'V0210'))
|
|
1568
1688
|
}
|
|
1569
1689
|
}
|
|
1570
1690
|
|
|
1571
1691
|
|
|
1572
|
-
const validate_EXACT: Injector = (
|
|
1573
|
-
|
|
1574
|
-
_val: any,
|
|
1575
|
-
current: any,
|
|
1576
|
-
_ref: string,
|
|
1577
|
-
_store: any
|
|
1578
|
-
) => {
|
|
1579
|
-
const { mode, parent, key, keyI, path, nodes } = inj
|
|
1692
|
+
const validate_EXACT: Injector = (inj: Injection) => {
|
|
1693
|
+
const { mode, parent, key, keyI } = inj
|
|
1580
1694
|
|
|
1581
1695
|
// Only operate in val mode, since parent is a list.
|
|
1582
1696
|
if (S_MVAL === mode) {
|
|
@@ -1589,8 +1703,8 @@ const validate_EXACT: Injector = (
|
|
|
1589
1703
|
|
|
1590
1704
|
inj.keyI = inj.keys.length
|
|
1591
1705
|
|
|
1592
|
-
// Clean up structure, replacing [$EXACT, ...] with current
|
|
1593
|
-
inj.setval(
|
|
1706
|
+
// Clean up structure, replacing [$EXACT, ...] with current data parent
|
|
1707
|
+
inj.setval(inj.dparent, 2)
|
|
1594
1708
|
|
|
1595
1709
|
inj.path = slice(inj.path, 0, inj.path.length - 1)
|
|
1596
1710
|
inj.key = getelem(inj.path, -1)
|
|
@@ -1606,10 +1720,10 @@ const validate_EXACT: Injector = (
|
|
|
1606
1720
|
// See if we can find an exact value match.
|
|
1607
1721
|
let currentstr: string | undefined = undefined
|
|
1608
1722
|
for (let tval of tvals) {
|
|
1609
|
-
let exactmatch = tval ===
|
|
1723
|
+
let exactmatch = tval === inj.dparent
|
|
1610
1724
|
|
|
1611
1725
|
if (!exactmatch && isnode(tval)) {
|
|
1612
|
-
currentstr = undefined === currentstr ? stringify(
|
|
1726
|
+
currentstr = undefined === currentstr ? stringify(inj.dparent) : currentstr
|
|
1613
1727
|
const tvalstr = stringify(tval)
|
|
1614
1728
|
exactmatch = tvalstr === currentstr
|
|
1615
1729
|
}
|
|
@@ -1622,16 +1736,16 @@ const validate_EXACT: Injector = (
|
|
|
1622
1736
|
const valdesc = tvals
|
|
1623
1737
|
.map((v: any) => stringify(v))
|
|
1624
1738
|
.join(', ')
|
|
1625
|
-
.replace(
|
|
1739
|
+
.replace(R_TRANSFORM_NAME, (_m: any, p1: string) => p1.toLowerCase())
|
|
1626
1740
|
|
|
1627
1741
|
inj.errs.push(_invalidTypeMsg(
|
|
1628
1742
|
inj.path,
|
|
1629
1743
|
(1 < inj.path.length ? '' : 'value ') +
|
|
1630
1744
|
'exactly equal to ' + (1 === tvals.length ? '' : 'one of ') + valdesc,
|
|
1631
|
-
typify(
|
|
1745
|
+
typify(inj.dparent), inj.dparent, 'V0110'))
|
|
1632
1746
|
}
|
|
1633
1747
|
else {
|
|
1634
|
-
|
|
1748
|
+
delprop(parent, key)
|
|
1635
1749
|
}
|
|
1636
1750
|
}
|
|
1637
1751
|
|
|
@@ -1643,18 +1757,24 @@ const _validation: Modify = (
|
|
|
1643
1757
|
key?: any,
|
|
1644
1758
|
parent?: any,
|
|
1645
1759
|
inj?: Injection,
|
|
1646
|
-
current?: any,
|
|
1647
|
-
_store?: any
|
|
1648
1760
|
) => {
|
|
1649
1761
|
|
|
1650
1762
|
if (UNDEF === inj) {
|
|
1651
1763
|
return
|
|
1652
1764
|
}
|
|
1653
1765
|
|
|
1766
|
+
if (SKIP === pval) {
|
|
1767
|
+
return
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
// select needs exact matches
|
|
1771
|
+
const exact = getprop(inj.meta, '`$EXACT`')
|
|
1772
|
+
|
|
1654
1773
|
// Current val to verify.
|
|
1655
|
-
const cval = getprop(
|
|
1774
|
+
const cval = getprop(inj.dparent, key)
|
|
1656
1775
|
|
|
1657
|
-
if (UNDEF === cval || UNDEF === inj) {
|
|
1776
|
+
// if (UNDEF === cval || UNDEF === inj) {
|
|
1777
|
+
if (UNDEF === inj || (!exact && UNDEF === cval)) {
|
|
1658
1778
|
return
|
|
1659
1779
|
}
|
|
1660
1780
|
|
|
@@ -1667,6 +1787,9 @@ const _validation: Modify = (
|
|
|
1667
1787
|
|
|
1668
1788
|
const ctype = typify(cval)
|
|
1669
1789
|
|
|
1790
|
+
// console.log('VALID-A', pval, ptype, cval, ctype)
|
|
1791
|
+
|
|
1792
|
+
|
|
1670
1793
|
// Type mismatch.
|
|
1671
1794
|
if (ptype !== ctype && UNDEF !== pval) {
|
|
1672
1795
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0010'))
|
|
@@ -1702,7 +1825,7 @@ const _validation: Modify = (
|
|
|
1702
1825
|
// Object is open, so merge in extra keys.
|
|
1703
1826
|
merge([pval, cval])
|
|
1704
1827
|
if (isnode(pval)) {
|
|
1705
|
-
|
|
1828
|
+
delprop(pval, '`$OPEN`')
|
|
1706
1829
|
}
|
|
1707
1830
|
}
|
|
1708
1831
|
}
|
|
@@ -1711,6 +1834,14 @@ const _validation: Modify = (
|
|
|
1711
1834
|
inj.errs.push(_invalidTypeMsg(inj.path, ptype, ctype, cval, 'V0030'))
|
|
1712
1835
|
}
|
|
1713
1836
|
}
|
|
1837
|
+
else if (exact) {
|
|
1838
|
+
// else if (inj.meta['`$EXACT`']) {
|
|
1839
|
+
// console.log('VALID-X', cval, pval)
|
|
1840
|
+
|
|
1841
|
+
if (cval !== pval) {
|
|
1842
|
+
inj.errs.push('Value ' + cval + ' should equal ' + pval)
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1714
1845
|
else {
|
|
1715
1846
|
// Spec value was a default, copy over data
|
|
1716
1847
|
setprop(parent, key, cval)
|
|
@@ -1734,13 +1865,12 @@ const _validation: Modify = (
|
|
|
1734
1865
|
function validate(
|
|
1735
1866
|
data: any, // Source data to transform into new data (original not mutated)
|
|
1736
1867
|
spec: any, // Transform specification; output follows this shape
|
|
1737
|
-
|
|
1738
|
-
extra?: any, // Additional custom checks
|
|
1739
|
-
|
|
1740
|
-
// Optionally modify individual values.
|
|
1741
|
-
collecterrs?: any,
|
|
1868
|
+
injdef?: Partial<Injection>
|
|
1742
1869
|
) {
|
|
1743
|
-
const
|
|
1870
|
+
const extra = injdef?.extra
|
|
1871
|
+
|
|
1872
|
+
const collect = null != injdef?.errs
|
|
1873
|
+
const errs = injdef?.errs || []
|
|
1744
1874
|
|
|
1745
1875
|
const store = {
|
|
1746
1876
|
// Remove the transform commands.
|
|
@@ -1770,9 +1900,14 @@ function validate(
|
|
|
1770
1900
|
$ERRS: errs,
|
|
1771
1901
|
}
|
|
1772
1902
|
|
|
1773
|
-
const out = transform(data, spec,
|
|
1903
|
+
const out = transform(data, spec, {
|
|
1904
|
+
meta: injdef?.meta,
|
|
1905
|
+
extra: store,
|
|
1906
|
+
modify: _validation,
|
|
1907
|
+
handler: _validatehandler
|
|
1908
|
+
})
|
|
1774
1909
|
|
|
1775
|
-
const generr = (0 < errs.length &&
|
|
1910
|
+
const generr = (0 < errs.length && !collect)
|
|
1776
1911
|
if (generr) {
|
|
1777
1912
|
throw new Error('Invalid data: ' + errs.join(' | '))
|
|
1778
1913
|
}
|
|
@@ -1781,15 +1916,314 @@ function validate(
|
|
|
1781
1916
|
}
|
|
1782
1917
|
|
|
1783
1918
|
|
|
1784
|
-
|
|
1785
|
-
|
|
1919
|
+
const select_AND: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
1920
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1921
|
+
const terms = getprop(inj.parent, inj.key)
|
|
1922
|
+
// const src = getprop(store, inj.base, store)
|
|
1923
|
+
|
|
1924
|
+
const ppath = slice(inj.path, -1)
|
|
1925
|
+
const point = getpath(store, ppath)
|
|
1926
|
+
|
|
1927
|
+
const vstore = { ...store }
|
|
1928
|
+
vstore.$TOP = point
|
|
1929
|
+
|
|
1930
|
+
for (let term of terms) {
|
|
1931
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1932
|
+
|
|
1933
|
+
let terrs: any[] = []
|
|
1934
|
+
|
|
1935
|
+
validate(point, term, {
|
|
1936
|
+
extra: vstore,
|
|
1937
|
+
errs: terrs,
|
|
1938
|
+
meta: inj.meta,
|
|
1939
|
+
})
|
|
1940
|
+
|
|
1941
|
+
if (0 != terrs.length) {
|
|
1942
|
+
inj.errs.push(
|
|
1943
|
+
'AND:' + pathify(ppath) + ': ' + stringify(point) + ' fail:' + stringify(terms))
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
const gkey = getelem(inj.path, -2)
|
|
1948
|
+
const gp = getelem(inj.nodes, -2)
|
|
1949
|
+
setprop(gp, gkey, point)
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
|
|
1954
|
+
const select_OR: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
1955
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1956
|
+
const terms = getprop(inj.parent, inj.key)
|
|
1957
|
+
// const src = getprop(store, inj.base, store)
|
|
1958
|
+
|
|
1959
|
+
const ppath = slice(inj.path, -1)
|
|
1960
|
+
const point = getpath(store, ppath)
|
|
1961
|
+
// console.log('OR:', ppath, point)
|
|
1962
|
+
|
|
1963
|
+
const vstore = { ...store }
|
|
1964
|
+
vstore.$TOP = point
|
|
1965
|
+
|
|
1966
|
+
for (let term of terms) {
|
|
1967
|
+
// console.log('OR-TERM:', term)
|
|
1968
|
+
// setprop(term, '`$OPEN`', getprop(term, '`$OPEN`', true))
|
|
1969
|
+
|
|
1970
|
+
let terrs: any[] = []
|
|
1971
|
+
|
|
1972
|
+
validate(point, term, {
|
|
1973
|
+
extra: vstore,
|
|
1974
|
+
errs: terrs,
|
|
1975
|
+
meta: inj.meta,
|
|
1976
|
+
})
|
|
1977
|
+
|
|
1978
|
+
// console.log('OR-ERRS:', terrs)
|
|
1979
|
+
|
|
1980
|
+
if (0 === terrs.length) {
|
|
1981
|
+
const gkey = getelem(inj.path, -2)
|
|
1982
|
+
const gp = getelem(inj.nodes, -2)
|
|
1983
|
+
setprop(gp, gkey, point)
|
|
1984
|
+
|
|
1985
|
+
// console.log('OR-NODES:' + inj, inj.nodes)
|
|
1986
|
+
return
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
inj.errs.push(
|
|
1991
|
+
'OR:' + pathify(ppath) + ': ' + stringify(point) + ' fail:' + stringify(terms))
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
|
|
1997
|
+
if (S_MKEYPRE === inj.mode) {
|
|
1998
|
+
const term = getprop(inj.parent, inj.key)
|
|
1999
|
+
// const src = getprop(store, inj.base, store)
|
|
2000
|
+
const gkey = getelem(inj.path, -2)
|
|
2001
|
+
|
|
2002
|
+
// const tval = getprop(src, gkey)
|
|
2003
|
+
|
|
2004
|
+
const ppath = slice(inj.path, -1)
|
|
2005
|
+
const point = getpath(store, ppath)
|
|
2006
|
+
|
|
2007
|
+
let pass = false
|
|
2008
|
+
|
|
2009
|
+
if ('$GT' === ref && point > term) {
|
|
2010
|
+
pass = true
|
|
2011
|
+
}
|
|
2012
|
+
else if ('$LT' === ref && point < term) {
|
|
2013
|
+
pass = true
|
|
2014
|
+
}
|
|
2015
|
+
else if ('$GTE' === ref && point >= term) {
|
|
2016
|
+
pass = true
|
|
2017
|
+
}
|
|
2018
|
+
else if ('$LTE' === ref && point <= term) {
|
|
2019
|
+
pass = true
|
|
2020
|
+
}
|
|
1786
2021
|
|
|
2022
|
+
if (pass) {
|
|
2023
|
+
// Update spec to match found value so that _validate does not complain.
|
|
2024
|
+
const gp = getelem(inj.nodes, -2)
|
|
2025
|
+
setprop(gp, gkey, point)
|
|
2026
|
+
}
|
|
2027
|
+
else {
|
|
2028
|
+
inj.errs.push('CMP: ' + pathify(ppath) + ': ' + stringify(point) +
|
|
2029
|
+
' fail:' + ref + ' ' + stringify(term))
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
1787
2032
|
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
//
|
|
2033
|
+
return UNDEF
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
|
|
2037
|
+
// Select children from a top-level object that match a MongoDB-style query.
|
|
2038
|
+
// Supports $and, $or, and equality comparisons.
|
|
2039
|
+
// For arrays, children are elements; for objects, children are values.
|
|
2040
|
+
// TODO: swap arg order for consistency
|
|
2041
|
+
function select(query: any, children: any): any[] {
|
|
2042
|
+
if (!isnode(children)) {
|
|
2043
|
+
return []
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
if (ismap(children)) {
|
|
2047
|
+
children = items(children).map(n => (n[1][S_DKEY] = n[0], n[1]))
|
|
2048
|
+
}
|
|
2049
|
+
else {
|
|
2050
|
+
children = (children as any[]).map((n, i) => ((ismap(n) ? n[S_DKEY] = i : null), n))
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
const results: any[] = []
|
|
2054
|
+
const injdef = {
|
|
2055
|
+
errs: [],
|
|
2056
|
+
meta: { '`$EXACT`': true },
|
|
2057
|
+
extra: {
|
|
2058
|
+
$AND: select_AND,
|
|
2059
|
+
$OR: select_OR,
|
|
2060
|
+
$GT: select_CMP,
|
|
2061
|
+
$LT: select_CMP,
|
|
2062
|
+
$GTE: select_CMP,
|
|
2063
|
+
$LTE: select_CMP,
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
const q = clone(query)
|
|
2068
|
+
|
|
2069
|
+
walk(q, (_k: PropKey | undefined, v: any) => {
|
|
2070
|
+
if (ismap(v)) {
|
|
2071
|
+
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true))
|
|
2072
|
+
}
|
|
2073
|
+
return v
|
|
2074
|
+
})
|
|
2075
|
+
|
|
2076
|
+
for (const child of children) {
|
|
2077
|
+
// console.log('CHILD', child, q)
|
|
2078
|
+
|
|
2079
|
+
injdef.errs = []
|
|
2080
|
+
|
|
2081
|
+
validate(child, clone(q), injdef)
|
|
2082
|
+
|
|
2083
|
+
// console.log('CHILD-ERRS', injdef.errs)
|
|
2084
|
+
|
|
2085
|
+
if (0 === size(injdef.errs)) {
|
|
2086
|
+
results.push(child)
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
return results
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
// Injection state used for recursive injection into JSON - like data structures.
|
|
2096
|
+
class Injection {
|
|
2097
|
+
mode: InjectMode // Injection mode: key:pre, val, key:post.
|
|
2098
|
+
full: boolean // Transform escape was full key name.
|
|
2099
|
+
keyI: number // Index of parent key in list of parent keys.
|
|
2100
|
+
keys: string[] // List of parent keys.
|
|
2101
|
+
key: string // Current parent key.
|
|
2102
|
+
val: any // Current child value.
|
|
2103
|
+
parent: any // Current parent (in transform specification).
|
|
2104
|
+
path: string[] // Path to current node.
|
|
2105
|
+
nodes: any[] // Stack of ancestor nodes.
|
|
2106
|
+
handler: Injector // Custom handler for injections.
|
|
2107
|
+
errs: any[] // Error collector.
|
|
2108
|
+
meta: Record<string, any> // Custom meta data.
|
|
2109
|
+
dparent: any // Current data parent node (contains current data value).
|
|
2110
|
+
dpath: string[] // Current data value path
|
|
2111
|
+
base?: string // Base key for data in store, if any.
|
|
2112
|
+
modify?: Modify // Modify injection output.
|
|
2113
|
+
prior?: Injection // Parent (aka prior) injection.
|
|
2114
|
+
extra?: any
|
|
2115
|
+
|
|
2116
|
+
constructor(val: any, parent: any) {
|
|
2117
|
+
this.val = val
|
|
2118
|
+
this.parent = parent
|
|
2119
|
+
this.errs = []
|
|
2120
|
+
|
|
2121
|
+
this.dparent = UNDEF
|
|
2122
|
+
this.dpath = [S_DTOP]
|
|
2123
|
+
|
|
2124
|
+
this.mode = S_MVAL as InjectMode
|
|
2125
|
+
this.full = false
|
|
2126
|
+
this.keyI = 0
|
|
2127
|
+
this.keys = [S_DTOP]
|
|
2128
|
+
this.key = S_DTOP
|
|
2129
|
+
this.path = [S_DTOP]
|
|
2130
|
+
this.nodes = [parent]
|
|
2131
|
+
this.handler = _injecthandler
|
|
2132
|
+
this.base = S_DTOP
|
|
2133
|
+
this.meta = {}
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
|
|
2137
|
+
toString(prefix?: string) {
|
|
2138
|
+
return 'INJ' + (null == prefix ? '' : S_FS + prefix) + S_CN +
|
|
2139
|
+
pad(pathify(this.path, 1)) +
|
|
2140
|
+
this.mode + (this.full ? '/full' : '') + S_CN +
|
|
2141
|
+
'key=' + this.keyI + S_FS + this.key + S_FS + S_OS + this.keys + S_CS +
|
|
2142
|
+
' p=' + stringify(this.parent, -1, 1) +
|
|
2143
|
+
' m=' + stringify(this.meta, -1, 1) +
|
|
2144
|
+
' d/' + pathify(this.dpath, 1) + '=' + stringify(this.dparent, -1, 1) +
|
|
2145
|
+
' r=' + stringify(this.nodes[0]?.[S_DTOP], -1, 1)
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
|
|
2149
|
+
descend() {
|
|
2150
|
+
this.meta.__d++
|
|
2151
|
+
const parentkey = getelem(this.path, -2)
|
|
2152
|
+
|
|
2153
|
+
// Resolve current node in store for local paths.
|
|
2154
|
+
if (UNDEF === this.dparent) {
|
|
2155
|
+
|
|
2156
|
+
// Even if there's no data, dpath should continue to match path, so that
|
|
2157
|
+
// relative paths work properly.
|
|
2158
|
+
if (1 < this.dpath.length) {
|
|
2159
|
+
this.dpath = [...this.dpath, parentkey]
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
else {
|
|
2163
|
+
// this.dparent is the containing node of the current store value.
|
|
2164
|
+
if (null != parentkey) {
|
|
2165
|
+
this.dparent = getprop(this.dparent, parentkey)
|
|
2166
|
+
|
|
2167
|
+
let lastpart = getelem(this.dpath, -1)
|
|
2168
|
+
if (lastpart === '$:' + parentkey) {
|
|
2169
|
+
this.dpath = slice(this.dpath, -1)
|
|
2170
|
+
}
|
|
2171
|
+
else {
|
|
2172
|
+
this.dpath = [...this.dpath, parentkey]
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
return this.dparent
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
child(keyI: number, keys: string[]) {
|
|
2182
|
+
const key = strkey(keys[keyI])
|
|
2183
|
+
const val = this.val
|
|
2184
|
+
|
|
2185
|
+
const cinj = new Injection(getprop(val, key), val)
|
|
2186
|
+
cinj.keyI = keyI
|
|
2187
|
+
cinj.keys = keys
|
|
2188
|
+
cinj.key = key
|
|
2189
|
+
|
|
2190
|
+
cinj.path = [...(this.path || []), key]
|
|
2191
|
+
cinj.nodes = [...(this.nodes || []), val]
|
|
2192
|
+
|
|
2193
|
+
cinj.mode = this.mode
|
|
2194
|
+
cinj.handler = this.handler
|
|
2195
|
+
cinj.modify = this.modify
|
|
2196
|
+
cinj.base = this.base
|
|
2197
|
+
cinj.meta = this.meta
|
|
2198
|
+
cinj.errs = this.errs
|
|
2199
|
+
cinj.prior = this
|
|
2200
|
+
|
|
2201
|
+
cinj.dpath = [...this.dpath]
|
|
2202
|
+
cinj.dparent = this.dparent
|
|
2203
|
+
|
|
2204
|
+
return cinj
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
|
|
2208
|
+
setval(val: any, ancestor?: number) {
|
|
2209
|
+
if (null == ancestor || ancestor < 2) {
|
|
2210
|
+
return UNDEF === val ?
|
|
2211
|
+
delprop(this.parent, this.key) :
|
|
2212
|
+
setprop(this.parent, this.key, val)
|
|
2213
|
+
}
|
|
2214
|
+
else {
|
|
2215
|
+
const aval = getelem(this.nodes, 0 - ancestor)
|
|
2216
|
+
const akey = getelem(this.path, 0 - ancestor)
|
|
2217
|
+
return UNDEF === val ?
|
|
2218
|
+
delprop(aval, akey) :
|
|
2219
|
+
setprop(aval, akey, val)
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
// Internal utilities
|
|
2226
|
+
// ==================
|
|
1793
2227
|
|
|
1794
2228
|
|
|
1795
2229
|
// Update all references to target in inj.nodes.
|
|
@@ -1820,7 +2254,6 @@ function _invalidTypeMsg(path: any, needtype: string, vt: string, v: any, _whenc
|
|
|
1820
2254
|
const _injecthandler: Injector = (
|
|
1821
2255
|
inj: Injection,
|
|
1822
2256
|
val: any,
|
|
1823
|
-
current: any,
|
|
1824
2257
|
ref: string,
|
|
1825
2258
|
store: any
|
|
1826
2259
|
): any => {
|
|
@@ -1829,12 +2262,11 @@ const _injecthandler: Injector = (
|
|
|
1829
2262
|
|
|
1830
2263
|
// Only call val function if it is a special command ($NAME format).
|
|
1831
2264
|
if (iscmd) {
|
|
1832
|
-
out = (val as Injector)(inj, val,
|
|
2265
|
+
out = (val as Injector)(inj, val, ref, store)
|
|
1833
2266
|
}
|
|
1834
2267
|
|
|
1835
2268
|
// Update parent with value. Ensures references remain in node tree.
|
|
1836
2269
|
else if (S_MVAL === inj.mode && inj.full) {
|
|
1837
|
-
// _setparentprop(inj, val)
|
|
1838
2270
|
inj.setval(val)
|
|
1839
2271
|
}
|
|
1840
2272
|
|
|
@@ -1842,6 +2274,36 @@ const _injecthandler: Injector = (
|
|
|
1842
2274
|
}
|
|
1843
2275
|
|
|
1844
2276
|
|
|
2277
|
+
const _validatehandler: Injector = (
|
|
2278
|
+
inj: Injection,
|
|
2279
|
+
val: any,
|
|
2280
|
+
ref: string,
|
|
2281
|
+
store: any
|
|
2282
|
+
): any => {
|
|
2283
|
+
let out = val
|
|
2284
|
+
|
|
2285
|
+
const m = ref.match(R_META_PATH)
|
|
2286
|
+
const ismetapath = null != m
|
|
2287
|
+
|
|
2288
|
+
if (ismetapath) {
|
|
2289
|
+
if ('=' === m[2]) {
|
|
2290
|
+
inj.setval(['`$EXACT`', val])
|
|
2291
|
+
}
|
|
2292
|
+
else {
|
|
2293
|
+
inj.setval(val)
|
|
2294
|
+
}
|
|
2295
|
+
inj.keyI = -1
|
|
2296
|
+
|
|
2297
|
+
out = SKIP
|
|
2298
|
+
}
|
|
2299
|
+
else {
|
|
2300
|
+
out = _injecthandler(inj, val, ref, store)
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
return out
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
|
|
1845
2307
|
// Inject values from a data store into a string. Not a public utility - used by
|
|
1846
2308
|
// `inject`. Inject are marked with `path` where path is resolved
|
|
1847
2309
|
// with getpath against the store or current (if defined)
|
|
@@ -1854,7 +2316,6 @@ const _injecthandler: Injector = (
|
|
|
1854
2316
|
function _injectstr(
|
|
1855
2317
|
val: string,
|
|
1856
2318
|
store: any,
|
|
1857
|
-
current?: any,
|
|
1858
2319
|
inj?: Injection
|
|
1859
2320
|
): any {
|
|
1860
2321
|
// Can't inject into non-strings
|
|
@@ -1865,7 +2326,7 @@ function _injectstr(
|
|
|
1865
2326
|
let out: any = val
|
|
1866
2327
|
|
|
1867
2328
|
// Pattern examples: "`a.b.c`", "`$NAME`", "`$NAME1`"
|
|
1868
|
-
const m = val.match(
|
|
2329
|
+
const m = val.match(R_INJECTION_FULL)
|
|
1869
2330
|
|
|
1870
2331
|
// Full string of the val is an injection.
|
|
1871
2332
|
if (m) {
|
|
@@ -1875,34 +2336,35 @@ function _injectstr(
|
|
|
1875
2336
|
let pathref = m[1]
|
|
1876
2337
|
|
|
1877
2338
|
// Special escapes inside injection.
|
|
1878
|
-
pathref =
|
|
1879
|
-
|
|
2339
|
+
pathref = 3 < pathref.length ?
|
|
2340
|
+
pathref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) :
|
|
2341
|
+
pathref
|
|
1880
2342
|
|
|
1881
2343
|
// Get the extracted path reference.
|
|
1882
|
-
out = getpath(
|
|
2344
|
+
out = getpath(store, pathref, inj)
|
|
1883
2345
|
}
|
|
1884
2346
|
|
|
1885
2347
|
else {
|
|
1886
2348
|
// Check for injections within the string.
|
|
1887
2349
|
const partial = (_m: string, ref: string) => {
|
|
1888
2350
|
// Special escapes inside injection.
|
|
1889
|
-
ref = 3 < ref.length ? ref.replace(
|
|
2351
|
+
ref = 3 < ref.length ? ref.replace(R_BT_ESCAPE, S_BT).replace(R_DS_ESCAPE, S_DS) : ref
|
|
1890
2352
|
if (inj) {
|
|
1891
2353
|
inj.full = false
|
|
1892
2354
|
}
|
|
1893
|
-
const found = getpath(
|
|
2355
|
+
const found = getpath(store, ref, inj)
|
|
1894
2356
|
|
|
1895
2357
|
// Ensure inject value is a string.
|
|
1896
2358
|
return UNDEF === found ? S_MT : S_string === typeof found ? found : JSON.stringify(found)
|
|
1897
2359
|
}
|
|
1898
2360
|
|
|
1899
|
-
out = val.replace(
|
|
2361
|
+
out = val.replace(R_INJECTION_PARTIAL, partial)
|
|
1900
2362
|
|
|
1901
2363
|
// Also call the inj handler on the entire string, providing the
|
|
1902
2364
|
// option for custom injection.
|
|
1903
2365
|
if (null != inj && isfunc(inj.handler)) {
|
|
1904
2366
|
inj.full = true
|
|
1905
|
-
out = inj.handler(inj, out,
|
|
2367
|
+
out = inj.handler(inj, out, val, store)
|
|
1906
2368
|
}
|
|
1907
2369
|
}
|
|
1908
2370
|
|
|
@@ -1910,9 +2372,9 @@ function _injectstr(
|
|
|
1910
2372
|
}
|
|
1911
2373
|
|
|
1912
2374
|
|
|
1913
|
-
// Define a class to mirror the JavaScript implementation
|
|
1914
2375
|
class StructUtility {
|
|
1915
2376
|
clone = clone
|
|
2377
|
+
delprop = delprop
|
|
1916
2378
|
escre = escre
|
|
1917
2379
|
escurl = escurl
|
|
1918
2380
|
getelem = getelem
|
|
@@ -1928,10 +2390,12 @@ class StructUtility {
|
|
|
1928
2390
|
isnode = isnode
|
|
1929
2391
|
items = items
|
|
1930
2392
|
joinurl = joinurl
|
|
2393
|
+
jsonify = jsonify
|
|
1931
2394
|
keysof = keysof
|
|
1932
2395
|
merge = merge
|
|
1933
2396
|
pad = pad
|
|
1934
2397
|
pathify = pathify
|
|
2398
|
+
select = select
|
|
1935
2399
|
setprop = setprop
|
|
1936
2400
|
size = size
|
|
1937
2401
|
slice = slice
|
|
@@ -1946,6 +2410,7 @@ class StructUtility {
|
|
|
1946
2410
|
export {
|
|
1947
2411
|
StructUtility,
|
|
1948
2412
|
clone,
|
|
2413
|
+
delprop,
|
|
1949
2414
|
escre,
|
|
1950
2415
|
escurl,
|
|
1951
2416
|
getelem,
|
|
@@ -1961,10 +2426,12 @@ export {
|
|
|
1961
2426
|
isnode,
|
|
1962
2427
|
items,
|
|
1963
2428
|
joinurl,
|
|
2429
|
+
jsonify,
|
|
1964
2430
|
keysof,
|
|
1965
2431
|
merge,
|
|
1966
2432
|
pad,
|
|
1967
2433
|
pathify,
|
|
2434
|
+
select,
|
|
1968
2435
|
setprop,
|
|
1969
2436
|
size,
|
|
1970
2437
|
slice,
|