@voxgig/struct 0.3.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -1
- package/dist/StructUtility.js +469 -143
- package/dist/StructUtility.js.map +1 -1
- package/package.json +1 -1
- package/src/StructUtility.ts +573 -180
package/src/StructUtility.ts
CHANGED
|
@@ -103,7 +103,7 @@ const NONE = undefined
|
|
|
103
103
|
const SKIP = { '`$SKIP`': true }
|
|
104
104
|
const DELETE = { '`$DELETE`': true }
|
|
105
105
|
|
|
106
|
-
const R_INTEGER_KEY = /^[-0-9]
|
|
106
|
+
const R_INTEGER_KEY = /^(0|-?[1-9][0-9]*)$/
|
|
107
107
|
const R_ESCAPE_REGEXP = /[.*+?^${}()|[\]\\]/g // Chars that need escaping in regexp.
|
|
108
108
|
const R_QUOTES = /"/g
|
|
109
109
|
const R_DOT = /\./g
|
|
@@ -112,6 +112,7 @@ const R_META_PATH = /^([^$]+)\$([=~])(.+)$/
|
|
|
112
112
|
const R_DOUBLE_DOLLAR = /\$\$/g
|
|
113
113
|
const R_TRANSFORM_NAME = /`\$([A-Z]+)`/g
|
|
114
114
|
const R_INJECTION_FULL = /^`(\$[A-Z]+|[^`]*)[0-9]*`$/
|
|
115
|
+
const R_CHECKER_NAME = /^\$[A-Z]+$/
|
|
115
116
|
const R_BT_ESCAPE = /\$BT/g
|
|
116
117
|
const R_DS_ESCAPE = /\$DS/g
|
|
117
118
|
const R_INJECTION_PARTIAL = /`([^`]+)`/g
|
|
@@ -334,8 +335,43 @@ function typify(value: any): number {
|
|
|
334
335
|
return T_any
|
|
335
336
|
}
|
|
336
337
|
|
|
337
|
-
//
|
|
338
|
-
|
|
338
|
+
// A list key's index: an integral number, or the string String(n) prints, else NaN.
|
|
339
|
+
function _listkey(key: any): number {
|
|
340
|
+
if (S_number === typeof key) {
|
|
341
|
+
return Number.isInteger(key) ? key + 0 : NaN
|
|
342
|
+
}
|
|
343
|
+
return S_string === typeof key && R_INTEGER_KEY.test(key) ? +key : NaN
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// The stored value; a plain map yields own properties only, never __proto__.
|
|
347
|
+
function _readprop(val: any, key: any): any {
|
|
348
|
+
if (islist(val)) {
|
|
349
|
+
const keyI = _listkey(key)
|
|
350
|
+
return 0 <= keyI ? val[keyI] : NONE
|
|
351
|
+
}
|
|
352
|
+
if (S_string !== typeof key && S_number !== typeof key) {
|
|
353
|
+
return NONE
|
|
354
|
+
}
|
|
355
|
+
if (0 < (T_instance & typify(val))) {
|
|
356
|
+
return (val as any)[key]
|
|
357
|
+
}
|
|
358
|
+
return Object.prototype.hasOwnProperty.call(val, key) ? (val as any)[key] : NONE
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function _setown(obj: any, key: string, val: any) {
|
|
362
|
+
if ('__proto__' === key) {
|
|
363
|
+
Object.defineProperty(obj, key, {
|
|
364
|
+
value: val,
|
|
365
|
+
enumerable: true,
|
|
366
|
+
writable: true,
|
|
367
|
+
configurable: true,
|
|
368
|
+
})
|
|
369
|
+
} else {
|
|
370
|
+
obj[key] = val
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Get a list element; a negative integer key counts from the end.
|
|
339
375
|
function getelem(val: any, key: any, alt?: any) {
|
|
340
376
|
let out = NONE
|
|
341
377
|
|
|
@@ -344,12 +380,12 @@ function getelem(val: any, key: any, alt?: any) {
|
|
|
344
380
|
}
|
|
345
381
|
|
|
346
382
|
if (islist(val)) {
|
|
347
|
-
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
out = val[
|
|
383
|
+
let keyI = _listkey(key)
|
|
384
|
+
if (keyI < 0) {
|
|
385
|
+
keyI = val.length + keyI
|
|
386
|
+
}
|
|
387
|
+
if (0 <= keyI) {
|
|
388
|
+
out = val[keyI]
|
|
353
389
|
}
|
|
354
390
|
}
|
|
355
391
|
|
|
@@ -371,7 +407,7 @@ function getprop(val: any, key: any, alt?: any) {
|
|
|
371
407
|
}
|
|
372
408
|
|
|
373
409
|
if (isnode(val)) {
|
|
374
|
-
out = val
|
|
410
|
+
out = _readprop(val, key)
|
|
375
411
|
}
|
|
376
412
|
|
|
377
413
|
// JSON null at a key is treated as "no value" for the default-substitution
|
|
@@ -392,7 +428,7 @@ function getprop(val: any, key: any, alt?: any) {
|
|
|
392
428
|
// null as absent (Group A) per UNDEF_SPEC.md.
|
|
393
429
|
function _lookup(val: any, key: any): any {
|
|
394
430
|
if (NONE === val || NONE === key) return NONE
|
|
395
|
-
if (isnode(val)) return val
|
|
431
|
+
if (isnode(val)) return _readprop(val, key)
|
|
396
432
|
return NONE
|
|
397
433
|
}
|
|
398
434
|
|
|
@@ -789,7 +825,7 @@ function jm(...kv: any[]): Record<string, any> {
|
|
|
789
825
|
// with value null" from "slot beyond end of kv".
|
|
790
826
|
let k = i < kv.length ? kv[i] : '$KEY' + i
|
|
791
827
|
k = 'string' === typeof k ? k : stringify(k)
|
|
792
|
-
o
|
|
828
|
+
_setown(o, k, i + 1 < kv.length ? kv[i + 1] : null)
|
|
793
829
|
}
|
|
794
830
|
return o
|
|
795
831
|
}
|
|
@@ -819,14 +855,12 @@ function delprop<PARENT>(parent: PARENT, key: any): PARENT {
|
|
|
819
855
|
key = strkey(key)
|
|
820
856
|
delete (parent as any)[key]
|
|
821
857
|
} else if (islist(parent)) {
|
|
822
|
-
|
|
858
|
+
const keyI = _listkey(key)
|
|
823
859
|
|
|
824
860
|
if (isNaN(keyI)) {
|
|
825
861
|
return parent
|
|
826
862
|
}
|
|
827
863
|
|
|
828
|
-
keyI = Math.floor(keyI)
|
|
829
|
-
|
|
830
864
|
// Delete list element at position keyI, shifting later elements down.
|
|
831
865
|
const psize = size(parent)
|
|
832
866
|
if (0 <= keyI && keyI < psize) {
|
|
@@ -858,18 +892,14 @@ function setprop<PARENT>(parent: PARENT, key: any, val: any): PARENT {
|
|
|
858
892
|
}
|
|
859
893
|
|
|
860
894
|
if (ismap(parent)) {
|
|
861
|
-
|
|
862
|
-
const pany = parent as any
|
|
863
|
-
pany[key] = val
|
|
895
|
+
_setown(parent, S_MT + key, val)
|
|
864
896
|
} else if (islist(parent)) {
|
|
865
|
-
|
|
897
|
+
const keyI = _listkey(key)
|
|
866
898
|
|
|
867
899
|
if (isNaN(keyI)) {
|
|
868
900
|
return parent
|
|
869
901
|
}
|
|
870
902
|
|
|
871
|
-
keyI = Math.floor(keyI)
|
|
872
|
-
|
|
873
903
|
// Set or append value at position keyI, or append if keyI out of bounds.
|
|
874
904
|
if (0 <= keyI) {
|
|
875
905
|
parent[slice(keyI, 0, size(parent) + 1)] = val
|
|
@@ -1097,13 +1127,23 @@ function setpath(
|
|
|
1097
1127
|
}
|
|
1098
1128
|
|
|
1099
1129
|
function getpath(store: any, path: number | string | string[], injdef?: Partial<Injection>) {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1130
|
+
let parts: any = NONE
|
|
1131
|
+
if (islist(path)) {
|
|
1132
|
+
parts = []
|
|
1133
|
+
for (const part of path) {
|
|
1134
|
+
if (S_number === typeof part) {
|
|
1135
|
+
parts.push(S_MT + part)
|
|
1136
|
+
} else if (S_string === typeof part) {
|
|
1137
|
+
parts.push(part)
|
|
1138
|
+
} else {
|
|
1139
|
+
return NONE
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
} else if (S_string === typeof path) {
|
|
1143
|
+
parts = (path as string).split(S_DT)
|
|
1144
|
+
} else if (S_number === typeof path) {
|
|
1145
|
+
parts = [S_MT + path]
|
|
1146
|
+
}
|
|
1107
1147
|
|
|
1108
1148
|
if (NONE === parts) {
|
|
1109
1149
|
return NONE
|
|
@@ -1598,9 +1638,7 @@ const transform_PACK: Injector = (inj: Injection, _val: any, _ref: string, store
|
|
|
1598
1638
|
return NONE
|
|
1599
1639
|
}
|
|
1600
1640
|
|
|
1601
|
-
const transform_REF: Injector = (inj: Injection,
|
|
1602
|
-
const { nodes } = inj
|
|
1603
|
-
|
|
1641
|
+
const transform_REF: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
1604
1642
|
if (M_VAL !== inj.mode) {
|
|
1605
1643
|
return NONE
|
|
1606
1644
|
}
|
|
@@ -1614,7 +1652,6 @@ const transform_REF: Injector = (inj: Injection, val: any, _ref: string, store:
|
|
|
1614
1652
|
const dpath = slice(inj.path, 1)
|
|
1615
1653
|
const ref = getpath(spec, refpath, {
|
|
1616
1654
|
dpath,
|
|
1617
|
-
// dparent: getpath(spec, inj.path.slice(1))
|
|
1618
1655
|
dparent: getpath(spec, dpath),
|
|
1619
1656
|
})
|
|
1620
1657
|
|
|
@@ -1628,39 +1665,23 @@ const transform_REF: Injector = (inj: Injection, val: any, _ref: string, store:
|
|
|
1628
1665
|
})
|
|
1629
1666
|
}
|
|
1630
1667
|
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
const
|
|
1634
|
-
const tpath = slice(inj.path, -1)
|
|
1635
|
-
const tcur = getpath(store, cpath)
|
|
1636
|
-
const tval = getpath(store, tpath)
|
|
1637
|
-
let rval = NONE
|
|
1638
|
-
|
|
1639
|
-
if (!hasSubRef || NONE !== tval) {
|
|
1640
|
-
const tinj = inj.child(0, [getelem(tpath, -1)])
|
|
1641
|
-
|
|
1642
|
-
tinj.path = tpath
|
|
1643
|
-
tinj.nodes = slice(inj.nodes, -1)
|
|
1644
|
-
tinj.parent = getelem(nodes, -2)
|
|
1645
|
-
tinj.val = tref
|
|
1646
|
-
|
|
1647
|
-
tinj.dpath = flatten([cpath])
|
|
1648
|
-
tinj.dparent = tcur
|
|
1649
|
-
|
|
1650
|
-
inject(tref, store, tinj)
|
|
1651
|
-
|
|
1652
|
-
rval = tinj.val
|
|
1653
|
-
} else {
|
|
1654
|
-
rval = NONE
|
|
1655
|
-
}
|
|
1668
|
+
// A recursive reference stops where the data does.
|
|
1669
|
+
const tval = getpath(store, slice(inj.path, -1))
|
|
1670
|
+
const rval = !hasSubRef || NONE !== tval ? clone(ref) : NONE
|
|
1656
1671
|
|
|
1672
|
+
// The enclosing loop injects the referenced spec in this list's place, so
|
|
1673
|
+
// neither this pass nor this element leaves a value for modify to check.
|
|
1657
1674
|
const grandparent = inj.setval(rval, 2)
|
|
1658
1675
|
|
|
1659
|
-
|
|
1660
|
-
|
|
1676
|
+
const prior = inj.prior
|
|
1677
|
+
if (prior) {
|
|
1678
|
+
prior.modify = NONE
|
|
1679
|
+
if (islist(grandparent) || NONE !== rval) {
|
|
1680
|
+
prior.keyI--
|
|
1681
|
+
}
|
|
1661
1682
|
}
|
|
1662
1683
|
|
|
1663
|
-
return
|
|
1684
|
+
return SKIP
|
|
1664
1685
|
}
|
|
1665
1686
|
|
|
1666
1687
|
const transform_FORMAT: Injector = (inj: Injection, _val: any, _ref: string, store: any) => {
|
|
@@ -1860,7 +1881,9 @@ const validate_STRING: Injector = (inj: Injection) => {
|
|
|
1860
1881
|
|
|
1861
1882
|
const validate_TYPE: Injector = (inj: Injection, _val: any, ref: string) => {
|
|
1862
1883
|
const tname = slice(ref, 1).toLowerCase()
|
|
1863
|
-
|
|
1884
|
+
|
|
1885
|
+
// $NIL is absent or null.
|
|
1886
|
+
const typev = (1 << (31 - TYPENAME.indexOf(tname))) | (S_nil === tname ? T_null : 0)
|
|
1864
1887
|
const out = _lookup(inj.dparent, inj.key)
|
|
1865
1888
|
|
|
1866
1889
|
const t = typify(out)
|
|
@@ -1897,7 +1920,7 @@ const validate_CHILD: Injector = (inj: Injection) => {
|
|
|
1897
1920
|
if (NONE == tval) {
|
|
1898
1921
|
tval = {}
|
|
1899
1922
|
} else if (!ismap(tval)) {
|
|
1900
|
-
|
|
1923
|
+
// _validation reports the mismatch, once.
|
|
1901
1924
|
return NONE
|
|
1902
1925
|
}
|
|
1903
1926
|
|
|
@@ -1927,14 +1950,6 @@ const validate_CHILD: Injector = (inj: Injection) => {
|
|
|
1927
1950
|
}
|
|
1928
1951
|
|
|
1929
1952
|
if (!islist(inj.dparent)) {
|
|
1930
|
-
const msg = _invalidTypeMsg(
|
|
1931
|
-
slice(inj.path, -1),
|
|
1932
|
-
S_list,
|
|
1933
|
-
typify(inj.dparent),
|
|
1934
|
-
inj.dparent,
|
|
1935
|
-
'V0230',
|
|
1936
|
-
)
|
|
1937
|
-
inj.errs.push(msg)
|
|
1938
1953
|
inj.keyI = size(parent)
|
|
1939
1954
|
return inj.dparent
|
|
1940
1955
|
}
|
|
@@ -1964,56 +1979,42 @@ const validate_CHILD: Injector = (inj: Injection) => {
|
|
|
1964
1979
|
return NONE
|
|
1965
1980
|
}
|
|
1966
1981
|
|
|
1967
|
-
const
|
|
1968
|
-
const { mode, parent, keyI } = inj
|
|
1982
|
+
const validate_EXACT: Injector = (inj: Injection) => {
|
|
1983
|
+
const { mode, parent, key, keyI } = inj
|
|
1969
1984
|
|
|
1970
1985
|
// Only operate in val mode, since parent is a list.
|
|
1971
1986
|
if (M_VAL === mode) {
|
|
1972
1987
|
if (!islist(parent) || 0 !== keyI) {
|
|
1973
1988
|
inj.errs.push(
|
|
1974
|
-
'The $
|
|
1989
|
+
'The $EXACT validator at field ' +
|
|
1975
1990
|
pathify(inj.path, 1, 1) +
|
|
1976
1991
|
' must be the first element of an array.',
|
|
1977
1992
|
)
|
|
1978
1993
|
return
|
|
1979
1994
|
}
|
|
1980
1995
|
|
|
1996
|
+
const cval = _slotval(inj)
|
|
1997
|
+
|
|
1981
1998
|
inj.keyI = size(inj.keys)
|
|
1982
1999
|
|
|
1983
|
-
//
|
|
1984
|
-
inj.setval(
|
|
2000
|
+
// Replace [$EXACT, ...] with the data value; SKIP keeps inject off the detached list.
|
|
2001
|
+
inj.setval(cval, 2)
|
|
1985
2002
|
|
|
1986
|
-
|
|
1987
|
-
// getprop(inj.parent, inj.key), and inj.parent is still this list.
|
|
1988
|
-
inj.path = slice(inj.path, -1)
|
|
2003
|
+
inj.path = slice(inj.path, 0, -1)
|
|
1989
2004
|
|
|
1990
2005
|
const tvals = slice(parent, 1)
|
|
1991
2006
|
if (0 === size(tvals)) {
|
|
1992
2007
|
inj.errs.push(
|
|
1993
|
-
'The $
|
|
2008
|
+
'The $EXACT validator at field ' +
|
|
1994
2009
|
pathify(inj.path, 1, 1) +
|
|
1995
2010
|
' must have at least one argument.',
|
|
1996
2011
|
)
|
|
1997
|
-
return
|
|
2012
|
+
return SKIP
|
|
1998
2013
|
}
|
|
1999
2014
|
|
|
2000
2015
|
for (const tval of tvals) {
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
const vstore = merge([{}, store], 1)
|
|
2004
|
-
vstore.$TOP = inj.dparent
|
|
2005
|
-
|
|
2006
|
-
const vcurrent = validate(inj.dparent, tval, {
|
|
2007
|
-
extra: vstore,
|
|
2008
|
-
errs: terrs,
|
|
2009
|
-
meta: inj.meta,
|
|
2010
|
-
})
|
|
2011
|
-
|
|
2012
|
-
inj.setval(vcurrent, -2)
|
|
2013
|
-
|
|
2014
|
-
// Accept current value if there was a match
|
|
2015
|
-
if (0 === size(terrs)) {
|
|
2016
|
-
return
|
|
2016
|
+
if (_deepequal(tval, cval)) {
|
|
2017
|
+
return SKIP
|
|
2017
2018
|
}
|
|
2018
2019
|
}
|
|
2019
2020
|
|
|
@@ -2029,62 +2030,94 @@ const validate_ONE: Injector = (inj: Injection, _val: any, _ref: string, store:
|
|
|
2029
2030
|
inj.errs.push(
|
|
2030
2031
|
_invalidTypeMsg(
|
|
2031
2032
|
inj.path,
|
|
2032
|
-
(1 < size(
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2033
|
+
(1 < size(inj.path) ? '' : 'value ') +
|
|
2034
|
+
'exactly equal to ' +
|
|
2035
|
+
(1 === size(tvals) ? '' : 'one of ') +
|
|
2036
|
+
valdesc,
|
|
2037
|
+
typify(cval),
|
|
2038
|
+
cval,
|
|
2039
|
+
'V0110',
|
|
2036
2040
|
),
|
|
2037
2041
|
)
|
|
2042
|
+
|
|
2043
|
+
return SKIP
|
|
2044
|
+
} else {
|
|
2045
|
+
delprop(parent, key)
|
|
2038
2046
|
}
|
|
2039
2047
|
}
|
|
2040
2048
|
|
|
2041
|
-
|
|
2042
|
-
|
|
2049
|
+
// Count the specs that admit the value: ['`$NOF`', count, spec, ...], where
|
|
2050
|
+
// count is an integer, or a map holding min, max or both.
|
|
2051
|
+
const validate_NOF: Injector = (inj: Injection, _val: any, _ref: string, store: any) =>
|
|
2052
|
+
_validatecount(inj, store, '$NOF', true)
|
|
2043
2053
|
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
inj.errs.push(
|
|
2048
|
-
'The $EXACT validator at field ' +
|
|
2049
|
-
pathify(inj.path, 1, 1) +
|
|
2050
|
-
' must be the first element of an array.',
|
|
2051
|
-
)
|
|
2052
|
-
return
|
|
2053
|
-
}
|
|
2054
|
+
// Every spec admits the value: ['`$ALL`', spec, ...].
|
|
2055
|
+
const validate_ALL: Injector = (inj: Injection, _val: any, _ref: string, store: any) =>
|
|
2056
|
+
_validatecount(inj, store, '$ALL', false)
|
|
2054
2057
|
|
|
2055
|
-
|
|
2058
|
+
// The counting list directives. Without a count, every spec has to admit.
|
|
2059
|
+
function _validatecount(inj: Injection, store: any, name: string, counted: boolean): any {
|
|
2060
|
+
const { mode, parent, keyI } = inj
|
|
2056
2061
|
|
|
2057
|
-
|
|
2058
|
-
|
|
2062
|
+
if (M_VAL !== mode) {
|
|
2063
|
+
return NONE
|
|
2064
|
+
}
|
|
2059
2065
|
|
|
2060
|
-
|
|
2061
|
-
inj.
|
|
2066
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
2067
|
+
inj.errs.push(
|
|
2068
|
+
'The ' +
|
|
2069
|
+
name +
|
|
2070
|
+
' validator at field ' +
|
|
2071
|
+
pathify(inj.path, 1, 1) +
|
|
2072
|
+
' must be the first element of an array.',
|
|
2073
|
+
)
|
|
2074
|
+
return NONE
|
|
2075
|
+
}
|
|
2062
2076
|
|
|
2063
|
-
|
|
2064
|
-
if (0 === size(tvals)) {
|
|
2065
|
-
inj.errs.push(
|
|
2066
|
-
'The $EXACT validator at field ' +
|
|
2067
|
-
pathify(inj.path, 1, 1) +
|
|
2068
|
-
' must have at least one argument.',
|
|
2069
|
-
)
|
|
2070
|
-
return
|
|
2071
|
-
}
|
|
2077
|
+
const cval = _slotval(inj)
|
|
2072
2078
|
|
|
2073
|
-
|
|
2074
|
-
for (const tval of tvals) {
|
|
2075
|
-
let exactmatch = tval === inj.dparent
|
|
2079
|
+
inj.keyI = size(inj.keys)
|
|
2076
2080
|
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
exactmatch = tvalstr === currentstr
|
|
2081
|
-
}
|
|
2081
|
+
// Replace the directive's list with the data value. Each return is SKIP, so
|
|
2082
|
+
// that inject leaves the detached list alone and validates nothing in it.
|
|
2083
|
+
inj.setval(cval, 2)
|
|
2082
2084
|
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2085
|
+
inj.path = slice(inj.path, -1)
|
|
2086
|
+
|
|
2087
|
+
const count = counted ? _lookup(parent, 1) : NONE
|
|
2088
|
+
const tvals = slice(parent, counted ? 2 : 1)
|
|
2089
|
+
const numtvals = size(tvals)
|
|
2090
|
+
const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1)
|
|
2091
|
+
|
|
2092
|
+
if (0 === numtvals) {
|
|
2093
|
+
inj.errs.push(
|
|
2094
|
+
field + (counted ? ' must have a count and' : ' must have') + ' at least one argument.',
|
|
2095
|
+
)
|
|
2096
|
+
return SKIP
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
const [lo, hi, countdesc] = counted ? _nofcount(count, numtvals) : [numtvals, numtvals, 'all']
|
|
2100
|
+
|
|
2101
|
+
if (NONE === countdesc) {
|
|
2102
|
+
inj.errs.push(field + ' has an invalid count: ' + stringify(count) + '.')
|
|
2103
|
+
return SKIP
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
let admitted = 0
|
|
2107
|
+
for (let tI = 0; tI < numtvals; tI++) {
|
|
2108
|
+
const left = numtvals - tI
|
|
2109
|
+
|
|
2110
|
+
// Stop once the specs left cannot change the verdict.
|
|
2111
|
+
if (hi < admitted || admitted + left < lo || (lo <= admitted && admitted + left <= hi)) {
|
|
2112
|
+
break
|
|
2086
2113
|
}
|
|
2087
2114
|
|
|
2115
|
+
if (_admits(cval, tvals[tI], inj, store)) {
|
|
2116
|
+
admitted++
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
if (admitted < lo || hi < admitted) {
|
|
2088
2121
|
const valdesc = replace(
|
|
2089
2122
|
join(
|
|
2090
2123
|
items(tvals, (n) => stringify(n[1])),
|
|
@@ -2095,20 +2128,200 @@ const validate_EXACT: Injector = (inj: Injection) => {
|
|
|
2095
2128
|
)
|
|
2096
2129
|
|
|
2097
2130
|
inj.errs.push(
|
|
2098
|
-
_invalidTypeMsg(
|
|
2099
|
-
inj.path,
|
|
2100
|
-
(1 < size(inj.path) ? '' : 'value ') +
|
|
2101
|
-
'exactly equal to ' +
|
|
2102
|
-
(1 === size(tvals) ? '' : 'one of ') +
|
|
2103
|
-
valdesc,
|
|
2104
|
-
typify(inj.dparent),
|
|
2105
|
-
inj.dparent,
|
|
2106
|
-
'V0110',
|
|
2107
|
-
),
|
|
2131
|
+
_invalidTypeMsg(inj.path, countdesc + ' of ' + valdesc, typify(cval), cval, 'V0310'),
|
|
2108
2132
|
)
|
|
2109
|
-
} else {
|
|
2110
|
-
delprop(parent, key)
|
|
2111
2133
|
}
|
|
2134
|
+
|
|
2135
|
+
return SKIP
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
// The value's length is within a bound: ['`$LEN`', bound, spec?], where the
|
|
2139
|
+
// bound is as for a $NOF count. A string's length is in code points.
|
|
2140
|
+
const validate_LEN: Injector = (inj: Injection, _val: any, _ref: string, store: any) =>
|
|
2141
|
+
_validatebound(inj, store, '$LEN')
|
|
2142
|
+
|
|
2143
|
+
// The value is a number within a bound: ['`$RANGE`', bound, spec?], where the
|
|
2144
|
+
// bound holds min or gt, max or lt, or one of each.
|
|
2145
|
+
const validate_RANGE: Injector = (inj: Injection, _val: any, _ref: string, store: any) =>
|
|
2146
|
+
_validatebound(inj, store, '$RANGE')
|
|
2147
|
+
|
|
2148
|
+
const validate_LIKE: Injector = (inj: Injection, _val: any, _ref: string, store: any) =>
|
|
2149
|
+
_validatebound(inj, store, '$LIKE')
|
|
2150
|
+
|
|
2151
|
+
// The bounding list directives: a bound or pattern, then at most one spec that also admits.
|
|
2152
|
+
function _validatebound(inj: Injection, store: any, name: string): any {
|
|
2153
|
+
const { mode, parent, keyI } = inj
|
|
2154
|
+
|
|
2155
|
+
if (M_VAL !== mode) {
|
|
2156
|
+
return NONE
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
if (!islist(parent) || 0 !== keyI) {
|
|
2160
|
+
inj.errs.push(
|
|
2161
|
+
'The ' +
|
|
2162
|
+
name +
|
|
2163
|
+
' validator at field ' +
|
|
2164
|
+
pathify(inj.path, 1, 1) +
|
|
2165
|
+
' must be the first element of an array.',
|
|
2166
|
+
)
|
|
2167
|
+
return NONE
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
const cval = _slotval(inj)
|
|
2171
|
+
|
|
2172
|
+
inj.keyI = size(inj.keys)
|
|
2173
|
+
|
|
2174
|
+
// As for the counting directives: SKIP keeps inject off the detached list.
|
|
2175
|
+
inj.setval(cval, 2)
|
|
2176
|
+
|
|
2177
|
+
inj.path = slice(inj.path, -1)
|
|
2178
|
+
|
|
2179
|
+
const field = 'The ' + name + ' validator at field ' + pathify(inj.path, 1)
|
|
2180
|
+
const nargs = size(parent) - 1
|
|
2181
|
+
const islen = '$LEN' === name
|
|
2182
|
+
const islike = '$LIKE' === name
|
|
2183
|
+
const argname = islike ? 'pattern' : 'bound'
|
|
2184
|
+
|
|
2185
|
+
if (nargs < 1 || 2 < nargs) {
|
|
2186
|
+
inj.errs.push(field + ' must have a ' + argname + ' and at most one spec.')
|
|
2187
|
+
return SKIP
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
const bound = _lookup(parent, 1)
|
|
2191
|
+
const like = islike ? _likere(bound) : NONE
|
|
2192
|
+
const [lo, hi, desc] = islen
|
|
2193
|
+
? _lenbound(bound)
|
|
2194
|
+
: [0, 0, islike ? (NONE === like ? NONE : 'like /' + bound + '/') : _rangedesc(bound)]
|
|
2195
|
+
|
|
2196
|
+
if (NONE === desc) {
|
|
2197
|
+
inj.errs.push(field + ' has an invalid ' + argname + ': ' + stringify(bound) + '.')
|
|
2198
|
+
return SKIP
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
const ctype = typify(cval)
|
|
2202
|
+
|
|
2203
|
+
if (2 === nargs && !_admits(cval, _lookup(parent, 2), inj, store)) {
|
|
2204
|
+
const specdesc = replace(
|
|
2205
|
+
stringify(_lookup(parent, 2)),
|
|
2206
|
+
R_TRANSFORM_NAME,
|
|
2207
|
+
(_m: any, p1: string) => p1.toLowerCase(),
|
|
2208
|
+
)
|
|
2209
|
+
inj.errs.push(_invalidTypeMsg(inj.path, specdesc, ctype, cval, 'V0410'))
|
|
2210
|
+
return SKIP
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
if (0 === ((islen ? T_string | T_list | T_map : islike ? T_string : T_number) & ctype)) {
|
|
2214
|
+
const needtype = islen ? 'string, list or map' : islike ? S_string : S_number
|
|
2215
|
+
inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0420'))
|
|
2216
|
+
return SKIP
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
const len = islen
|
|
2220
|
+
? S_string === typeof cval
|
|
2221
|
+
? Array.from(cval as string).length
|
|
2222
|
+
: size(cval)
|
|
2223
|
+
: 0
|
|
2224
|
+
const inbound = islen
|
|
2225
|
+
? lo <= len && (-1 === hi || len <= hi)
|
|
2226
|
+
: islike
|
|
2227
|
+
? re_test(like as RegExp, cval)
|
|
2228
|
+
: _inrange(bound, cval)
|
|
2229
|
+
|
|
2230
|
+
if (!inbound) {
|
|
2231
|
+
const needtype = (1 < size(inj.path) ? '' : 'value ') + desc
|
|
2232
|
+
inj.errs.push(_invalidTypeMsg(inj.path, needtype, ctype, cval, 'V0430'))
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
return SKIP
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
// The bounds of a $LEN bound, with -1 for no maximum, and the words for it,
|
|
2239
|
+
// which are absent when the bound is invalid.
|
|
2240
|
+
function _lenbound(bound: any): [number, number, string | undefined] {
|
|
2241
|
+
const [lo, hi, countdesc] = _nofcount(bound, -1)
|
|
2242
|
+
|
|
2243
|
+
if (NONE === countdesc) {
|
|
2244
|
+
return [0, 0, NONE]
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
const desc =
|
|
2248
|
+
'of length ' +
|
|
2249
|
+
(lo === hi ? lo : -1 === hi ? 'at least ' + lo : 0 === lo ? 'at most ' + hi : lo + ' to ' + hi)
|
|
2250
|
+
|
|
2251
|
+
return [lo, hi, desc]
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
// A $LIKE pattern compiled by the port's own regex engine; absent unless it compiles.
|
|
2255
|
+
function _likere(pattern: any): RegExp | undefined {
|
|
2256
|
+
if (S_string !== typeof pattern) {
|
|
2257
|
+
return NONE
|
|
2258
|
+
}
|
|
2259
|
+
try {
|
|
2260
|
+
return re_compile(pattern)
|
|
2261
|
+
} catch {
|
|
2262
|
+
return NONE
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
// The words for a $RANGE bound, which are absent when the bound is invalid.
|
|
2267
|
+
function _rangedesc(bound: any): string | undefined {
|
|
2268
|
+
if (!ismap(bound)) {
|
|
2269
|
+
return NONE
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
const min = _lookup(bound, 'min')
|
|
2273
|
+
const max = _lookup(bound, 'max')
|
|
2274
|
+
const gt = _lookup(bound, 'gt')
|
|
2275
|
+
const lt = _lookup(bound, 'lt')
|
|
2276
|
+
const lower = NONE === min ? gt : min
|
|
2277
|
+
const upper = NONE === max ? lt : max
|
|
2278
|
+
const isnum = (n: any) => NONE === n || 0 < (T_number & typify(n))
|
|
2279
|
+
|
|
2280
|
+
const valid =
|
|
2281
|
+
0 < size(bound) &&
|
|
2282
|
+
size(bound) === [min, max, gt, lt].filter((n) => NONE !== n).length &&
|
|
2283
|
+
(NONE === min || NONE === gt) &&
|
|
2284
|
+
(NONE === max || NONE === lt) &&
|
|
2285
|
+
isnum(min) &&
|
|
2286
|
+
isnum(max) &&
|
|
2287
|
+
isnum(gt) &&
|
|
2288
|
+
isnum(lt) &&
|
|
2289
|
+
(NONE === lower || NONE === upper || lower <= upper)
|
|
2290
|
+
|
|
2291
|
+
if (!valid) {
|
|
2292
|
+
return NONE
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
return join(
|
|
2296
|
+
[
|
|
2297
|
+
NONE !== min
|
|
2298
|
+
? 'at least ' + stringify(min)
|
|
2299
|
+
: NONE !== gt
|
|
2300
|
+
? 'greater than ' + stringify(gt)
|
|
2301
|
+
: NONE,
|
|
2302
|
+
NONE !== max
|
|
2303
|
+
? 'at most ' + stringify(max)
|
|
2304
|
+
: NONE !== lt
|
|
2305
|
+
? 'less than ' + stringify(lt)
|
|
2306
|
+
: NONE,
|
|
2307
|
+
],
|
|
2308
|
+
' and ',
|
|
2309
|
+
)
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
// A number within a valid $RANGE bound.
|
|
2313
|
+
function _inrange(bound: any, val: number): boolean {
|
|
2314
|
+
const min = _lookup(bound, 'min')
|
|
2315
|
+
const max = _lookup(bound, 'max')
|
|
2316
|
+
const gt = _lookup(bound, 'gt')
|
|
2317
|
+
const lt = _lookup(bound, 'lt')
|
|
2318
|
+
|
|
2319
|
+
return (
|
|
2320
|
+
(NONE === min || min <= val) &&
|
|
2321
|
+
(NONE === max || val <= max) &&
|
|
2322
|
+
(NONE === gt || gt < val) &&
|
|
2323
|
+
(NONE === lt || val < lt)
|
|
2324
|
+
)
|
|
2112
2325
|
}
|
|
2113
2326
|
|
|
2114
2327
|
// This is the "modify" argument to inject. Use this to perform
|
|
@@ -2228,8 +2441,12 @@ function validate(
|
|
|
2228
2441
|
$INSTANCE: validate_TYPE,
|
|
2229
2442
|
$ANY: validate_ANY,
|
|
2230
2443
|
$CHILD: validate_CHILD,
|
|
2231
|
-
$ONE: validate_ONE,
|
|
2232
2444
|
$EXACT: validate_EXACT,
|
|
2445
|
+
$NOF: validate_NOF,
|
|
2446
|
+
$ALL: validate_ALL,
|
|
2447
|
+
$LEN: validate_LEN,
|
|
2448
|
+
$RANGE: validate_RANGE,
|
|
2449
|
+
$LIKE: validate_LIKE,
|
|
2233
2450
|
},
|
|
2234
2451
|
|
|
2235
2452
|
getdef(extra, {}),
|
|
@@ -2246,13 +2463,20 @@ function validate(
|
|
|
2246
2463
|
const meta = getprop(injdef, 'meta', {})
|
|
2247
2464
|
setprop(meta, S_BEXACT, getprop(meta, S_BEXACT, false))
|
|
2248
2465
|
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
errs
|
|
2255
|
-
|
|
2466
|
+
// A spec that names an unknown checker validates nothing.
|
|
2467
|
+
const numerrs = size(errs)
|
|
2468
|
+
_checknames(spec, store, [], errs)
|
|
2469
|
+
|
|
2470
|
+
const out =
|
|
2471
|
+
numerrs < size(errs)
|
|
2472
|
+
? clone(data)
|
|
2473
|
+
: transform(data, spec, {
|
|
2474
|
+
meta,
|
|
2475
|
+
extra: store,
|
|
2476
|
+
modify: _validation,
|
|
2477
|
+
handler: _validatehandler,
|
|
2478
|
+
errs,
|
|
2479
|
+
})
|
|
2256
2480
|
|
|
2257
2481
|
const generr = 0 < size(errs) && !collect
|
|
2258
2482
|
if (generr) {
|
|
@@ -2355,6 +2579,11 @@ const select_NOT: Injector = (inj: Injection, _val: any, _ref: string, store: an
|
|
|
2355
2579
|
}
|
|
2356
2580
|
|
|
2357
2581
|
const select_CMP: Injector = (inj: Injection, _val: any, ref: string, store: any) => {
|
|
2582
|
+
// The list form of $LIKE is validate's checker; the key form is this operator.
|
|
2583
|
+
if (M_VAL === inj.mode && '$LIKE' === ref) {
|
|
2584
|
+
return validate_LIKE(inj, _val, ref, store)
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2358
2587
|
if (M_KEYPRE === inj.mode) {
|
|
2359
2588
|
const term = _lookup(inj.parent, inj.key)
|
|
2360
2589
|
const gkey = getelem(inj.path, -2)
|
|
@@ -2429,12 +2658,7 @@ function select(children: any, query: any): any[] {
|
|
|
2429
2658
|
|
|
2430
2659
|
const q = clone(query)
|
|
2431
2660
|
|
|
2432
|
-
|
|
2433
|
-
if (ismap(v)) {
|
|
2434
|
-
setprop(v, '`$OPEN`', getprop(v, '`$OPEN`', true))
|
|
2435
|
-
}
|
|
2436
|
-
return v
|
|
2437
|
-
})
|
|
2661
|
+
_openquery(q)
|
|
2438
2662
|
|
|
2439
2663
|
for (const child of children) {
|
|
2440
2664
|
injdef.errs = []
|
|
@@ -2449,6 +2673,27 @@ function select(children: any, query: any): any[] {
|
|
|
2449
2673
|
return results
|
|
2450
2674
|
}
|
|
2451
2675
|
|
|
2676
|
+
// Open every map in a select query, so a child may hold keys the query does
|
|
2677
|
+
// not name, but leave a directive's literals as written: what $EXACT
|
|
2678
|
+
// compares, and the count or bound of $NOF, $LEN and $RANGE.
|
|
2679
|
+
function _openquery(q: any): void {
|
|
2680
|
+
if (ismap(q)) {
|
|
2681
|
+
setprop(q, '`$OPEN`', getprop(q, '`$OPEN`', true))
|
|
2682
|
+
for (const key of keysof(q)) {
|
|
2683
|
+
_openquery(q[key])
|
|
2684
|
+
}
|
|
2685
|
+
} else if (islist(q)) {
|
|
2686
|
+
const head = q[0]
|
|
2687
|
+
const exact = '`$EXACT`' === head
|
|
2688
|
+
const bounded = '`$NOF`' === head || '`$LEN`' === head || '`$RANGE`' === head
|
|
2689
|
+
for (let i = 0; i < size(q); i++) {
|
|
2690
|
+
if (0 === i || !(exact || (bounded && 1 === i))) {
|
|
2691
|
+
_openquery(q[i])
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
|
|
2452
2697
|
// Injection state used for recursive injection into JSON - like data structures.
|
|
2453
2698
|
class Injection {
|
|
2454
2699
|
mode: InjectMode // Injection mode: M_KEYPRE, M_VAL, M_KEYPOST.
|
|
@@ -2612,6 +2857,167 @@ function _invalidTypeMsg(path: any, needtype: string, vt: number, v: any, _whenc
|
|
|
2612
2857
|
)
|
|
2613
2858
|
}
|
|
2614
2859
|
|
|
2860
|
+
// Names a validate spec may use besides its checkers: transform's that it keeps, and $OPEN.
|
|
2861
|
+
const VALIDATE_NAMES = [
|
|
2862
|
+
'$TOP',
|
|
2863
|
+
'$SPEC',
|
|
2864
|
+
'$BT',
|
|
2865
|
+
'$DS',
|
|
2866
|
+
'$WHEN',
|
|
2867
|
+
'$ANNO',
|
|
2868
|
+
'$REF',
|
|
2869
|
+
'$FORMAT',
|
|
2870
|
+
'$APPLY',
|
|
2871
|
+
'$OPEN',
|
|
2872
|
+
]
|
|
2873
|
+
|
|
2874
|
+
// Report each checker the spec names that validate lacks; $EXACT's arguments are literals.
|
|
2875
|
+
function _checknames(spec: any, store: any, path: string[], errs: any[]): void {
|
|
2876
|
+
const name = _refname(spec)
|
|
2877
|
+
if (NONE !== name) {
|
|
2878
|
+
if (_unknownname(name, store)) {
|
|
2879
|
+
errs.push('Unknown validator ' + name + ' at field ' + pathify(path) + '.')
|
|
2880
|
+
}
|
|
2881
|
+
return
|
|
2882
|
+
}
|
|
2883
|
+
|
|
2884
|
+
if (islist(spec) && '$EXACT' === _refname(spec[0])) {
|
|
2885
|
+
return
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
for (const key of keysof(spec)) {
|
|
2889
|
+
if (ismap(spec)) {
|
|
2890
|
+
_checknames(key, store, path, errs)
|
|
2891
|
+
}
|
|
2892
|
+
_checknames(spec[key], store, flatten([path, key]), errs)
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
// The name a whole-string reference gives, when it is a checker name in any case.
|
|
2897
|
+
function _refname(val: any): string | undefined {
|
|
2898
|
+
const m = S_string === typeof val ? re_find(R_INJECTION_FULL, val) : null
|
|
2899
|
+
return null != m && re_test(R_CHECKER_NAME, m[1].toUpperCase()) ? m[1] : NONE
|
|
2900
|
+
}
|
|
2901
|
+
|
|
2902
|
+
// Unknown: undefined and upper case, or a defined name in another case. Null disables a name.
|
|
2903
|
+
function _unknownname(name: string, store: any): boolean {
|
|
2904
|
+
const known = (n: string) => VALIDATE_NAMES.includes(n) || null != _lookup(store, n)
|
|
2905
|
+
const upper = name.toUpperCase()
|
|
2906
|
+
return !known(name) && (upper === name || known(upper))
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
// The data value at a list directive's slot. The directive's own dparent was
|
|
2910
|
+
// read with getprop, which reports a stored null as absent.
|
|
2911
|
+
function _slotval(inj: Injection): any {
|
|
2912
|
+
const prior = inj.prior
|
|
2913
|
+
return NONE === prior ? inj.dparent : _lookup(prior.dparent, prior.key)
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
// JSON equality: numbers by value, lists in order, maps by key in any order.
|
|
2917
|
+
function _deepequal(a: any, b: any): boolean {
|
|
2918
|
+
if (a === b) {
|
|
2919
|
+
return true
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
const atype = typify(a)
|
|
2923
|
+
if (atype !== typify(b) || 0 === ((T_list | T_map) & atype)) {
|
|
2924
|
+
return false
|
|
2925
|
+
}
|
|
2926
|
+
|
|
2927
|
+
const akeys = keysof(a)
|
|
2928
|
+
const bkeys = keysof(b)
|
|
2929
|
+
if (size(akeys) !== size(bkeys)) {
|
|
2930
|
+
return false
|
|
2931
|
+
}
|
|
2932
|
+
|
|
2933
|
+
for (let kI = 0; kI < size(akeys); kI++) {
|
|
2934
|
+
const k = akeys[kI]
|
|
2935
|
+
if (k !== bkeys[kI] || !_deepequal(a[k], b[k])) {
|
|
2936
|
+
return false
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
|
|
2940
|
+
return true
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
// Every member val has is in out, unchanged. A member only out has is a
|
|
2944
|
+
// default the spec supplied, and is ignored.
|
|
2945
|
+
function _keeps(val: any, out: any): boolean {
|
|
2946
|
+
if (NONE === val) {
|
|
2947
|
+
return true
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
if (0 === ((T_list | T_map) & typify(val)) || islist(val) !== islist(out) || !isnode(out)) {
|
|
2951
|
+
return _deepequal(val, out)
|
|
2952
|
+
}
|
|
2953
|
+
|
|
2954
|
+
for (const k of keysof(val)) {
|
|
2955
|
+
if (!_keeps(val[k], _lookup(out, k))) {
|
|
2956
|
+
return false
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
|
|
2960
|
+
return true
|
|
2961
|
+
}
|
|
2962
|
+
|
|
2963
|
+
// A spec admits a value when validating the value against it finds no error
|
|
2964
|
+
// and gives back the value as it is.
|
|
2965
|
+
function _admits(val: any, spec: any, inj: Injection, store: any): boolean {
|
|
2966
|
+
const terrs: any[] = []
|
|
2967
|
+
|
|
2968
|
+
const vstore = merge([{}, store], 1)
|
|
2969
|
+
vstore.$TOP = val
|
|
2970
|
+
|
|
2971
|
+
const out = validate(val, spec, {
|
|
2972
|
+
extra: vstore,
|
|
2973
|
+
errs: terrs,
|
|
2974
|
+
meta: inj.meta,
|
|
2975
|
+
})
|
|
2976
|
+
|
|
2977
|
+
return 0 === size(terrs) && _keeps(val, out)
|
|
2978
|
+
}
|
|
2979
|
+
|
|
2980
|
+
// The bounds of a $NOF count, and the words for it, which are absent when
|
|
2981
|
+
// the count is invalid. Without a max, the bound is the number of specs.
|
|
2982
|
+
function _nofcount(count: any, numtvals: number): [number, number, string | undefined] {
|
|
2983
|
+
const isnat = (n: any) => 0 < (T_integer & typify(n)) && 0 <= n
|
|
2984
|
+
|
|
2985
|
+
if (isnat(count)) {
|
|
2986
|
+
return [count, count, 0 === count ? 'none' : 'exactly ' + count]
|
|
2987
|
+
}
|
|
2988
|
+
|
|
2989
|
+
const min = _lookup(count, 'min')
|
|
2990
|
+
const max = _lookup(count, 'max')
|
|
2991
|
+
|
|
2992
|
+
const valid =
|
|
2993
|
+
ismap(count) &&
|
|
2994
|
+
0 < size(count) &&
|
|
2995
|
+
size(count) === (NONE === min ? 0 : 1) + (NONE === max ? 0 : 1) &&
|
|
2996
|
+
(NONE === min || isnat(min)) &&
|
|
2997
|
+
(NONE === max || isnat(max)) &&
|
|
2998
|
+
(NONE === min || NONE === max || min <= max)
|
|
2999
|
+
|
|
3000
|
+
if (!valid) {
|
|
3001
|
+
return [0, 0, NONE]
|
|
3002
|
+
}
|
|
3003
|
+
|
|
3004
|
+
const lo = NONE === min ? 0 : min
|
|
3005
|
+
const hi = NONE === max ? numtvals : max
|
|
3006
|
+
|
|
3007
|
+
const desc =
|
|
3008
|
+
NONE === max
|
|
3009
|
+
? 'at least ' + lo
|
|
3010
|
+
: 0 === hi
|
|
3011
|
+
? 'none'
|
|
3012
|
+
: NONE === min
|
|
3013
|
+
? 'at most ' + hi
|
|
3014
|
+
: lo === hi
|
|
3015
|
+
? 'exactly ' + lo
|
|
3016
|
+
: lo + ' to ' + hi
|
|
3017
|
+
|
|
3018
|
+
return [lo, hi, desc]
|
|
3019
|
+
}
|
|
3020
|
+
|
|
2615
3021
|
// Default inject handler for transforms. If the path resolves to a function,
|
|
2616
3022
|
// call the function passing the injection inj. This is how transforms operate.
|
|
2617
3023
|
const _injecthandler: Injector = (inj: Injection, val: any, ref: string, store: any): any => {
|
|
@@ -2913,19 +3319,6 @@ function condenseCmp(a: string, b: string): number {
|
|
|
2913
3319
|
return a.length - i - (b.length - j)
|
|
2914
3320
|
}
|
|
2915
3321
|
|
|
2916
|
-
function condenseSet(obj: any, key: string, val: any) {
|
|
2917
|
-
if ('__proto__' === key) {
|
|
2918
|
-
Object.defineProperty(obj, key, {
|
|
2919
|
-
value: val,
|
|
2920
|
-
enumerable: true,
|
|
2921
|
-
writable: true,
|
|
2922
|
-
configurable: true,
|
|
2923
|
-
})
|
|
2924
|
-
} else {
|
|
2925
|
-
obj[key] = val
|
|
2926
|
-
}
|
|
2927
|
-
}
|
|
2928
|
-
|
|
2929
3322
|
function iscondensed(val: any): boolean {
|
|
2930
3323
|
// The version must MATCH, not merely be numeric: a future profile would
|
|
2931
3324
|
// otherwise be decoded under this one's schema.
|
|
@@ -2990,8 +3383,8 @@ function condense(val: any): any {
|
|
|
2990
3383
|
const build = (n: any): number => {
|
|
2991
3384
|
if (ismap(n)) {
|
|
2992
3385
|
const enc: any[] = [CONDENSE_MAP]
|
|
2993
|
-
// keysof
|
|
2994
|
-
const keys = keysof(n)
|
|
3386
|
+
// Code-point order, not keysof's code-unit order, so the key ids ascend for bisection.
|
|
3387
|
+
const keys = keysof(n).sort(condenseCmp)
|
|
2995
3388
|
for (let i = 0; i < keys.length; i++) {
|
|
2996
3389
|
enc.push(symid[keys[i]])
|
|
2997
3390
|
enc.push(build((n as any)[keys[i]]))
|
|
@@ -3054,7 +3447,7 @@ function condenseSymId(cond: any, key: string): number {
|
|
|
3054
3447
|
// The node index of `key` inside node `idx`, or -1. Touches only this node.
|
|
3055
3448
|
function condenseChild(cond: any, idx: number, key: any): number {
|
|
3056
3449
|
const enc = (cond as any)[S_condnode][idx]
|
|
3057
|
-
if (null == enc) {
|
|
3450
|
+
if (null == enc || (S_string !== typeof key && S_number !== typeof key)) {
|
|
3058
3451
|
return -1
|
|
3059
3452
|
}
|
|
3060
3453
|
|
|
@@ -3137,7 +3530,7 @@ function condenseMaterialise(cond: any, idx: number): any {
|
|
|
3137
3530
|
if (CONDENSE_MAP === kind) {
|
|
3138
3531
|
const out: any = {}
|
|
3139
3532
|
for (let j = 1; j < enc.length; j += 2) {
|
|
3140
|
-
|
|
3533
|
+
_setown(out, sym[enc[j]], rebuild(enc[j + 1]))
|
|
3141
3534
|
}
|
|
3142
3535
|
return out
|
|
3143
3536
|
}
|