json-p3 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/json-p3.cjs.js +218 -42
- package/dist/json-p3.esm.js +218 -42
- package/dist/json-p3.iife.js +218 -42
- package/dist/json-p3.iife.min.js +1 -1
- package/dist/json-p3.iife.min.js.map +1 -1
- package/dist/path/environment.d.ts +18 -4
- package/dist/path/expression.d.ts +1 -0
- package/dist/path/extra/expression.d.ts +6 -0
- package/dist/path/extra/index.d.ts +0 -0
- package/dist/path/extra/selectors.d.ts +16 -0
- package/dist/path/lex.d.ts +6 -3
- package/dist/path/parse.d.ts +2 -0
- package/dist/path/selectors.d.ts +3 -1
- package/dist/path/token.d.ts +2 -0
- package/dist/path/types.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/json-p3.iife.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* json-p3 version 1.
|
|
2
|
+
* json-p3 version 1.2.0
|
|
3
3
|
* https://github.com/jg-rp/json-p3
|
|
4
4
|
*
|
|
5
5
|
* MIT License
|
|
@@ -863,12 +863,13 @@ var json_p3 = (function (exports) {
|
|
|
863
863
|
this.left = left;
|
|
864
864
|
this.operator = operator;
|
|
865
865
|
this.right = right;
|
|
866
|
+
this.logical = operator === "&&" || operator === "||";
|
|
866
867
|
}
|
|
867
868
|
evaluate(context) {
|
|
868
869
|
let left = this.left.evaluate(context);
|
|
869
|
-
if (left instanceof JSONPathNodeList && left.nodes.length === 1) left = left.nodes[0].value;
|
|
870
|
+
if (!this.logical && left instanceof JSONPathNodeList && left.nodes.length === 1) left = left.nodes[0].value;
|
|
870
871
|
let right = this.right.evaluate(context);
|
|
871
|
-
if (right instanceof JSONPathNodeList && right.nodes.length === 1) right = right.nodes[0].value;
|
|
872
|
+
if (!this.logical && right instanceof JSONPathNodeList && right.nodes.length === 1) right = right.nodes[0].value;
|
|
872
873
|
if (this.operator === "&&") {
|
|
873
874
|
return isTruthy(left) && isTruthy(right);
|
|
874
875
|
}
|
|
@@ -878,7 +879,7 @@ var json_p3 = (function (exports) {
|
|
|
878
879
|
return compare(left, this.operator, right);
|
|
879
880
|
}
|
|
880
881
|
toString() {
|
|
881
|
-
if (this.
|
|
882
|
+
if (this.logical) {
|
|
882
883
|
return `(${this.left.toString()} ${this.operator} ${this.right.toString()})`;
|
|
883
884
|
}
|
|
884
885
|
return `${this.left.toString()} ${this.operator} ${this.right.toString()}`;
|
|
@@ -1182,6 +1183,8 @@ var json_p3 = (function (exports) {
|
|
|
1182
1183
|
TokenKind["GE"] = "TOKEN_GE";
|
|
1183
1184
|
TokenKind["GT"] = "TOKEN_GT";
|
|
1184
1185
|
TokenKind["INDEX"] = "TOKEN_INDEX";
|
|
1186
|
+
TokenKind["KEY"] = "TOKEN_KEY";
|
|
1187
|
+
TokenKind["KEYS"] = "TOKEN_KEYS";
|
|
1185
1188
|
TokenKind["LBRACKET"] = "TOKEN_LBRACKET";
|
|
1186
1189
|
TokenKind["LE"] = "TOKEN_LE";
|
|
1187
1190
|
TokenKind["LG"] = "TOKEN_LG";
|
|
@@ -1251,6 +1254,9 @@ var json_p3 = (function (exports) {
|
|
|
1251
1254
|
}
|
|
1252
1255
|
}
|
|
1253
1256
|
|
|
1257
|
+
/** A lexer that accepts additional, non-standard tokens. */
|
|
1258
|
+
|
|
1259
|
+
|
|
1254
1260
|
// These regular expressions are to be used with Lexer.acceptMatchRun(),
|
|
1255
1261
|
// which expects the sticky flag to be set.
|
|
1256
1262
|
const exponentPattern = /e[+-]?\d+/y;
|
|
@@ -1290,7 +1296,8 @@ var json_p3 = (function (exports) {
|
|
|
1290
1296
|
/**
|
|
1291
1297
|
* @param path - A JSONPath query.
|
|
1292
1298
|
*/
|
|
1293
|
-
constructor(path) {
|
|
1299
|
+
constructor(environment, path) {
|
|
1300
|
+
this.environment = environment;
|
|
1294
1301
|
this.path = path;
|
|
1295
1302
|
}
|
|
1296
1303
|
get pos() {
|
|
@@ -1390,8 +1397,8 @@ var json_p3 = (function (exports) {
|
|
|
1390
1397
|
* @returns A two-tuple containing a lexer for _path_ and an array to populate
|
|
1391
1398
|
* with tokens.
|
|
1392
1399
|
*/
|
|
1393
|
-
function lex(path) {
|
|
1394
|
-
const lexer = new Lexer(path);
|
|
1400
|
+
function lex(environment, path) {
|
|
1401
|
+
const lexer = new Lexer(environment, path);
|
|
1395
1402
|
return [lexer, lexer.tokens];
|
|
1396
1403
|
}
|
|
1397
1404
|
|
|
@@ -1400,8 +1407,8 @@ var json_p3 = (function (exports) {
|
|
|
1400
1407
|
* @param path - A JSONPath query.
|
|
1401
1408
|
* @returns Tokens to be parsed by the parser.
|
|
1402
1409
|
*/
|
|
1403
|
-
function tokenize(path) {
|
|
1404
|
-
const [lexer, tokens] = lex(path);
|
|
1410
|
+
function tokenize(environment, path) {
|
|
1411
|
+
const [lexer, tokens] = lex(environment, path);
|
|
1405
1412
|
lexer.run();
|
|
1406
1413
|
if (tokens.length && tokens[tokens.length - 1].kind === TokenKind.ERROR) {
|
|
1407
1414
|
throw new JSONPathSyntaxError(tokens[tokens.length - 1].value, tokens[tokens.length - 1]);
|
|
@@ -1454,6 +1461,16 @@ var json_p3 = (function (exports) {
|
|
|
1454
1461
|
* @returns -
|
|
1455
1462
|
*/
|
|
1456
1463
|
function lexDescendantSelection(l) {
|
|
1464
|
+
if (l.acceptMatchRun(namePattern)) {
|
|
1465
|
+
// Shorthand name
|
|
1466
|
+
l.emit(TokenKind.NAME);
|
|
1467
|
+
return lexSegment;
|
|
1468
|
+
}
|
|
1469
|
+
if (!l.environment.strict && l.acceptMatchRun(l.environment.keysPattern)) {
|
|
1470
|
+
// Non-standard keys selector
|
|
1471
|
+
l.emit(TokenKind.KEYS);
|
|
1472
|
+
return lexSegment;
|
|
1473
|
+
}
|
|
1457
1474
|
const ch = l.next();
|
|
1458
1475
|
switch (ch) {
|
|
1459
1476
|
case "":
|
|
@@ -1467,13 +1484,8 @@ var json_p3 = (function (exports) {
|
|
|
1467
1484
|
return lexInsideBracketedSelection;
|
|
1468
1485
|
default:
|
|
1469
1486
|
l.backup();
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
return lexSegment;
|
|
1473
|
-
} else {
|
|
1474
|
-
l.error(`unexpected descendent selection token '${ch}'`);
|
|
1475
|
-
return null;
|
|
1476
|
-
}
|
|
1487
|
+
l.error(`unexpected descendent selection token '${ch}'`);
|
|
1488
|
+
return null;
|
|
1477
1489
|
}
|
|
1478
1490
|
}
|
|
1479
1491
|
function lexDotSelector(l) {
|
|
@@ -1482,23 +1494,34 @@ var json_p3 = (function (exports) {
|
|
|
1482
1494
|
l.error("unexpected whitespace after dot");
|
|
1483
1495
|
return null;
|
|
1484
1496
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
l.emit(TokenKind.WILD);
|
|
1497
|
+
if (!l.environment.strict && l.acceptMatchRun(l.environment.keysPattern)) {
|
|
1498
|
+
l.emit(TokenKind.KEYS);
|
|
1488
1499
|
return lexSegment;
|
|
1489
1500
|
}
|
|
1490
|
-
l.backup();
|
|
1491
1501
|
if (l.acceptMatchRun(namePattern)) {
|
|
1492
1502
|
l.emit(TokenKind.NAME);
|
|
1493
1503
|
return lexSegment;
|
|
1494
|
-
} else {
|
|
1495
|
-
l.error(`unexpected shorthand selector '${ch}'`);
|
|
1496
|
-
return null;
|
|
1497
1504
|
}
|
|
1505
|
+
const ch = l.next();
|
|
1506
|
+
if (ch === "*") {
|
|
1507
|
+
l.emit(TokenKind.WILD);
|
|
1508
|
+
return lexSegment;
|
|
1509
|
+
}
|
|
1510
|
+
l.backup();
|
|
1511
|
+
l.error(`unexpected shorthand selector '${ch}'`);
|
|
1512
|
+
return null;
|
|
1498
1513
|
}
|
|
1499
1514
|
function lexInsideBracketedSelection(l) {
|
|
1500
1515
|
for (;;) {
|
|
1501
1516
|
l.ignoreWhitespace();
|
|
1517
|
+
if (l.acceptMatchRun(indexPattern)) {
|
|
1518
|
+
l.emit(TokenKind.INDEX);
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
if (!l.environment.strict && l.acceptMatchRun(l.environment.keysPattern)) {
|
|
1522
|
+
l.emit(TokenKind.KEYS);
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1502
1525
|
const ch = l.next();
|
|
1503
1526
|
switch (ch) {
|
|
1504
1527
|
case "]":
|
|
@@ -1527,10 +1550,6 @@ var json_p3 = (function (exports) {
|
|
|
1527
1550
|
return lexDoubleQuoteStringInsideBracketSelection;
|
|
1528
1551
|
default:
|
|
1529
1552
|
l.backup();
|
|
1530
|
-
if (l.acceptMatchRun(indexPattern)) {
|
|
1531
|
-
l.emit(TokenKind.INDEX);
|
|
1532
|
-
continue;
|
|
1533
|
-
}
|
|
1534
1553
|
l.error(`unexpected token '${ch}' in bracketed selection`);
|
|
1535
1554
|
return null;
|
|
1536
1555
|
}
|
|
@@ -1544,6 +1563,8 @@ var json_p3 = (function (exports) {
|
|
|
1544
1563
|
const ch = l.next();
|
|
1545
1564
|
switch (ch) {
|
|
1546
1565
|
case "":
|
|
1566
|
+
l.error("unclosed bracketed selection");
|
|
1567
|
+
return null;
|
|
1547
1568
|
case "]":
|
|
1548
1569
|
l.filterLevel -= 1;
|
|
1549
1570
|
if (l.parenStack.length === 1) {
|
|
@@ -1585,6 +1606,9 @@ var json_p3 = (function (exports) {
|
|
|
1585
1606
|
case "@":
|
|
1586
1607
|
l.emit(TokenKind.CURRENT);
|
|
1587
1608
|
return lexSegment;
|
|
1609
|
+
case "#":
|
|
1610
|
+
l.emit(TokenKind.KEY);
|
|
1611
|
+
return lexSegment;
|
|
1588
1612
|
case ".":
|
|
1589
1613
|
l.backup();
|
|
1590
1614
|
return lexSegment;
|
|
@@ -1962,12 +1986,22 @@ var json_p3 = (function (exports) {
|
|
|
1962
1986
|
}
|
|
1963
1987
|
resolve(nodes) {
|
|
1964
1988
|
const rv = [];
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1989
|
+
if (this.environment.nondeterministic) {
|
|
1990
|
+
for (const root of nodes) {
|
|
1991
|
+
for (const node of this.nondeterministicVisitor(root)) {
|
|
1992
|
+
rv.push(node);
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
} else {
|
|
1996
|
+
for (const node of nodes) {
|
|
1997
|
+
rv.push(node);
|
|
1998
|
+
for (const _node of this.visitor(node)) {
|
|
1999
|
+
rv.push(_node);
|
|
2000
|
+
}
|
|
1969
2001
|
}
|
|
1970
2002
|
}
|
|
2003
|
+
|
|
2004
|
+
// console.log(JSON.stringify(rv.map((n: any) => n.value)));
|
|
1971
2005
|
return this.selector.resolve(rv);
|
|
1972
2006
|
}
|
|
1973
2007
|
*lazyResolve(nodes) {
|
|
@@ -2020,7 +2054,7 @@ var json_p3 = (function (exports) {
|
|
|
2020
2054
|
toString() {
|
|
2021
2055
|
return `..${this.selector.toString()}`;
|
|
2022
2056
|
}
|
|
2023
|
-
|
|
2057
|
+
visitor(node) {
|
|
2024
2058
|
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
2025
2059
|
if (depth >= this.environment.maxRecursionDepth) {
|
|
2026
2060
|
throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
|
|
@@ -2031,7 +2065,7 @@ var json_p3 = (function (exports) {
|
|
|
2031
2065
|
for (let i = 0; i < node.value.length; i++) {
|
|
2032
2066
|
const _node = new JSONPathNode(node.value[i], node.location.concat(i), node.root);
|
|
2033
2067
|
rv.push(_node);
|
|
2034
|
-
for (const __node of this.
|
|
2068
|
+
for (const __node of this.visitor(_node, depth + 1)) {
|
|
2035
2069
|
rv.push(__node);
|
|
2036
2070
|
}
|
|
2037
2071
|
}
|
|
@@ -2039,13 +2073,52 @@ var json_p3 = (function (exports) {
|
|
|
2039
2073
|
for (const [key, value] of this.environment.entries(node.value)) {
|
|
2040
2074
|
const _node = new JSONPathNode(value, node.location.concat(key), node.root);
|
|
2041
2075
|
rv.push(_node);
|
|
2042
|
-
for (const __node of this.
|
|
2076
|
+
for (const __node of this.visitor(_node, depth + 1)) {
|
|
2043
2077
|
rv.push(__node);
|
|
2044
2078
|
}
|
|
2045
2079
|
}
|
|
2046
2080
|
}
|
|
2047
2081
|
return rv;
|
|
2048
2082
|
}
|
|
2083
|
+
nondeterministicVisitor(root) {
|
|
2084
|
+
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
2085
|
+
const rv = [root];
|
|
2086
|
+
let queue = this.nondeterministicChildren(root).map(node => [node, depth]);
|
|
2087
|
+
while (queue.length) {
|
|
2088
|
+
const [node, _depth] = queue.shift();
|
|
2089
|
+
rv.push(node);
|
|
2090
|
+
if (_depth >= this.environment.maxRecursionDepth) {
|
|
2091
|
+
throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
// Visit child nodes now or queue them for later?
|
|
2095
|
+
const visitChildren = Math.random() < 0.5;
|
|
2096
|
+
for (const child of this.nondeterministicChildren(node)) {
|
|
2097
|
+
if (visitChildren) {
|
|
2098
|
+
rv.push(child);
|
|
2099
|
+
const grandchildren = this.nondeterministicChildren(child).map(n => [n, _depth + 2]);
|
|
2100
|
+
queue = interleave(queue, grandchildren);
|
|
2101
|
+
} else {
|
|
2102
|
+
queue.push([child, _depth + 1]);
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
return rv;
|
|
2107
|
+
}
|
|
2108
|
+
nondeterministicChildren(node) {
|
|
2109
|
+
const _rv = [];
|
|
2110
|
+
if (node.value instanceof String) return _rv;
|
|
2111
|
+
if (isArray(node.value)) {
|
|
2112
|
+
for (let i = 0; i < node.value.length; i++) {
|
|
2113
|
+
_rv.push(new JSONPathNode(node.value[i], node.location.concat(i), node.root));
|
|
2114
|
+
}
|
|
2115
|
+
} else if (isObject(node.value)) {
|
|
2116
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
2117
|
+
_rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return _rv;
|
|
2121
|
+
}
|
|
2049
2122
|
}
|
|
2050
2123
|
class FilterSelector extends JSONPathSelector {
|
|
2051
2124
|
constructor(environment, token, expression) {
|
|
@@ -2066,7 +2139,8 @@ var json_p3 = (function (exports) {
|
|
|
2066
2139
|
const filterContext = {
|
|
2067
2140
|
environment: this.environment,
|
|
2068
2141
|
currentValue: value,
|
|
2069
|
-
rootValue: node.root
|
|
2142
|
+
rootValue: node.root,
|
|
2143
|
+
currentKey: i
|
|
2070
2144
|
};
|
|
2071
2145
|
if (this.expression.evaluate(filterContext)) {
|
|
2072
2146
|
rv.push(new JSONPathNode(value, node.location.concat(i), node.root));
|
|
@@ -2077,7 +2151,8 @@ var json_p3 = (function (exports) {
|
|
|
2077
2151
|
const filterContext = {
|
|
2078
2152
|
environment: this.environment,
|
|
2079
2153
|
currentValue: value,
|
|
2080
|
-
rootValue: node.root
|
|
2154
|
+
rootValue: node.root,
|
|
2155
|
+
currentKey: key
|
|
2081
2156
|
};
|
|
2082
2157
|
if (this.expression.evaluate(filterContext)) {
|
|
2083
2158
|
rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
@@ -2099,7 +2174,8 @@ var json_p3 = (function (exports) {
|
|
|
2099
2174
|
environment: this.environment,
|
|
2100
2175
|
currentValue: value,
|
|
2101
2176
|
rootValue: node.root,
|
|
2102
|
-
lazy: true
|
|
2177
|
+
lazy: true,
|
|
2178
|
+
currentKey: i
|
|
2103
2179
|
};
|
|
2104
2180
|
if (this.expression.evaluate(filterContext)) {
|
|
2105
2181
|
yield new JSONPathNode(value, node.location.concat(i), node.root);
|
|
@@ -2111,7 +2187,8 @@ var json_p3 = (function (exports) {
|
|
|
2111
2187
|
environment: this.environment,
|
|
2112
2188
|
currentValue: value,
|
|
2113
2189
|
rootValue: node.root,
|
|
2114
|
-
lazy: true
|
|
2190
|
+
lazy: true,
|
|
2191
|
+
currentKey: key
|
|
2115
2192
|
};
|
|
2116
2193
|
if (this.expression.evaluate(filterContext)) {
|
|
2117
2194
|
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
@@ -2154,6 +2231,41 @@ var json_p3 = (function (exports) {
|
|
|
2154
2231
|
}
|
|
2155
2232
|
}
|
|
2156
2233
|
|
|
2234
|
+
/**
|
|
2235
|
+
* Randomly interleave elements from two arrays while maintaining relative
|
|
2236
|
+
* order of each input array.
|
|
2237
|
+
*
|
|
2238
|
+
* If _arrayA_ is empty, _arrayB_ is returned, and vice versa.
|
|
2239
|
+
*/
|
|
2240
|
+
function interleave(arrayA, arrayB) {
|
|
2241
|
+
if (arrayA.length === 0) {
|
|
2242
|
+
return arrayB;
|
|
2243
|
+
}
|
|
2244
|
+
if (arrayB.length === 0) {
|
|
2245
|
+
return arrayA;
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
// An array of iterators
|
|
2249
|
+
const iterators = [];
|
|
2250
|
+
const itA = arrayA[Symbol.iterator]();
|
|
2251
|
+
const itB = arrayB[Symbol.iterator]();
|
|
2252
|
+
for (let i = 0; i < arrayA.length; i++) {
|
|
2253
|
+
iterators.push(itA);
|
|
2254
|
+
}
|
|
2255
|
+
for (let i = 0; i < arrayB.length; i++) {
|
|
2256
|
+
iterators.push(itB);
|
|
2257
|
+
}
|
|
2258
|
+
shuffle(iterators);
|
|
2259
|
+
return iterators.map(it => it.next().value);
|
|
2260
|
+
}
|
|
2261
|
+
function shuffle(entries) {
|
|
2262
|
+
for (let i = entries.length - 1; i > 0; i--) {
|
|
2263
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
2264
|
+
[entries[i], entries[j]] = [entries[j], entries[i]];
|
|
2265
|
+
}
|
|
2266
|
+
return entries;
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2157
2269
|
var selectors = /*#__PURE__*/Object.freeze({
|
|
2158
2270
|
__proto__: null,
|
|
2159
2271
|
BracketedSelection: BracketedSelection,
|
|
@@ -2237,6 +2349,57 @@ var json_p3 = (function (exports) {
|
|
|
2237
2349
|
}
|
|
2238
2350
|
}
|
|
2239
2351
|
|
|
2352
|
+
class CurrentKey extends FilterExpression {
|
|
2353
|
+
evaluate(context) {
|
|
2354
|
+
return context.currentKey ?? Nothing;
|
|
2355
|
+
}
|
|
2356
|
+
toString() {
|
|
2357
|
+
return "#";
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
/**
|
|
2362
|
+
* Object property name selector or array index selector.
|
|
2363
|
+
*/
|
|
2364
|
+
class KeysSelector extends JSONPathSelector {
|
|
2365
|
+
constructor(environment, token) {
|
|
2366
|
+
let shorthand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
2367
|
+
super(environment, token);
|
|
2368
|
+
this.environment = environment;
|
|
2369
|
+
this.token = token;
|
|
2370
|
+
this.shorthand = shorthand;
|
|
2371
|
+
}
|
|
2372
|
+
resolve(nodes) {
|
|
2373
|
+
const rv = [];
|
|
2374
|
+
for (const node of nodes) {
|
|
2375
|
+
if (node.value instanceof String || isArray(node.value)) continue;
|
|
2376
|
+
if (isObject(node.value)) {
|
|
2377
|
+
let i = 0;
|
|
2378
|
+
for (const [key, _] of this.environment.entries(node.value)) {
|
|
2379
|
+
rv.push(new JSONPathNode(key, node.location.concat("[~]", `[${i}]`), node.root));
|
|
2380
|
+
i++;
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
return rv;
|
|
2385
|
+
}
|
|
2386
|
+
*lazyResolve(nodes) {
|
|
2387
|
+
for (const node of nodes) {
|
|
2388
|
+
if (node.value instanceof String || isArray(node.value)) continue;
|
|
2389
|
+
if (isObject(node.value)) {
|
|
2390
|
+
let i = 0;
|
|
2391
|
+
for (const [key, _] of this.environment.entries(node.value)) {
|
|
2392
|
+
yield new JSONPathNode(key, node.location.concat("[~]", `[${i}]`), node.root);
|
|
2393
|
+
i++;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
toString() {
|
|
2399
|
+
return this.shorthand ? "[~]" : "~";
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2240
2403
|
const PRECEDENCE_LOWEST = 1;
|
|
2241
2404
|
const PRECEDENCE_LOGICAL_OR = 4;
|
|
2242
2405
|
const PRECEDENCE_LOGICAL_AND = 5;
|
|
@@ -2252,7 +2415,7 @@ var json_p3 = (function (exports) {
|
|
|
2252
2415
|
class Parser {
|
|
2253
2416
|
constructor(environment) {
|
|
2254
2417
|
this.environment = environment;
|
|
2255
|
-
this.tokenMap = new Map([[TokenKind.FALSE, this.parseBoolean], [TokenKind.NUMBER, this.parseNumber], [TokenKind.LPAREN, this.parseGroupedExpression], [TokenKind.NOT, this.parsePrefixExpression], [TokenKind.NULL, this.parseNull], [TokenKind.ROOT, this.parseRootQuery], [TokenKind.CURRENT, this.parseRelativeQuery], [TokenKind.SINGLE_QUOTE_STRING, this.parseString], [TokenKind.DOUBLE_QUOTE_STRING, this.parseString], [TokenKind.TRUE, this.parseBoolean], [TokenKind.FUNCTION, this.parseFunction]]);
|
|
2418
|
+
this.tokenMap = new Map([[TokenKind.FALSE, this.parseBoolean], [TokenKind.NUMBER, this.parseNumber], [TokenKind.LPAREN, this.parseGroupedExpression], [TokenKind.NOT, this.parsePrefixExpression], [TokenKind.NULL, this.parseNull], [TokenKind.ROOT, this.parseRootQuery], [TokenKind.CURRENT, this.parseRelativeQuery], [TokenKind.SINGLE_QUOTE_STRING, this.parseString], [TokenKind.DOUBLE_QUOTE_STRING, this.parseString], [TokenKind.TRUE, this.parseBoolean], [TokenKind.FUNCTION, this.parseFunction], [TokenKind.KEY, this.parseCurrentKey], [TokenKind.KEY, this.parseCurrentKey]]);
|
|
2256
2419
|
}
|
|
2257
2420
|
parse(stream) {
|
|
2258
2421
|
if (stream.current.kind === TokenKind.ROOT) stream.next();
|
|
@@ -2284,6 +2447,8 @@ var json_p3 = (function (exports) {
|
|
|
2284
2447
|
return new NameSelector(this.environment, stream.current, stream.current.value, true);
|
|
2285
2448
|
case TokenKind.WILD:
|
|
2286
2449
|
return new WildcardSelector(this.environment, stream.current, true);
|
|
2450
|
+
case TokenKind.KEYS:
|
|
2451
|
+
return new KeysSelector(this.environment, stream.current, true);
|
|
2287
2452
|
case TokenKind.DDOT:
|
|
2288
2453
|
{
|
|
2289
2454
|
const segmentToken = stream.current;
|
|
@@ -2377,6 +2542,9 @@ var json_p3 = (function (exports) {
|
|
|
2377
2542
|
case TokenKind.WILD:
|
|
2378
2543
|
items.push(new WildcardSelector(this.environment, stream.current));
|
|
2379
2544
|
break;
|
|
2545
|
+
case TokenKind.KEYS:
|
|
2546
|
+
items.push(new KeysSelector(this.environment, stream.current));
|
|
2547
|
+
break;
|
|
2380
2548
|
case TokenKind.EOF:
|
|
2381
2549
|
throw new JSONPathSyntaxError("unexpected end of query", stream.current);
|
|
2382
2550
|
default:
|
|
@@ -2457,6 +2625,9 @@ var json_p3 = (function (exports) {
|
|
|
2457
2625
|
const tok = stream.next();
|
|
2458
2626
|
return new RelativeQuery(tok, new JSONPath(this.environment, this.parsePath(stream, true)));
|
|
2459
2627
|
}
|
|
2628
|
+
parseCurrentKey(stream) {
|
|
2629
|
+
return new CurrentKey(stream.current);
|
|
2630
|
+
}
|
|
2460
2631
|
parseFunction(stream) {
|
|
2461
2632
|
const args = [];
|
|
2462
2633
|
const tok = stream.next();
|
|
@@ -2573,6 +2744,10 @@ var json_p3 = (function (exports) {
|
|
|
2573
2744
|
* If `true`, enable nondeterministic ordering when iterating JSON object data.
|
|
2574
2745
|
*/
|
|
2575
2746
|
|
|
2747
|
+
/**
|
|
2748
|
+
* The pattern to use for the non-standard _keys selector_.
|
|
2749
|
+
*/
|
|
2750
|
+
|
|
2576
2751
|
/**
|
|
2577
2752
|
* A map of function names to objects implementing the {@link FilterFunction}
|
|
2578
2753
|
* interface. You are free to set or delete custom filter functions directly.
|
|
@@ -2588,6 +2763,7 @@ var json_p3 = (function (exports) {
|
|
|
2588
2763
|
this.minIntIndex = options.maxIntIndex ?? -Math.pow(2, 53) - 1;
|
|
2589
2764
|
this.maxRecursionDepth = options.maxRecursionDepth ?? 50;
|
|
2590
2765
|
this.nondeterministic = options.nondeterministic ?? false;
|
|
2766
|
+
this.keysPattern = options.keysPattern ?? /~/y;
|
|
2591
2767
|
this.parser = new Parser(this);
|
|
2592
2768
|
this.setupFilterFunctions();
|
|
2593
2769
|
}
|
|
@@ -2597,7 +2773,7 @@ var json_p3 = (function (exports) {
|
|
|
2597
2773
|
* @returns A new {@link JSONPath} object, bound to this environment.
|
|
2598
2774
|
*/
|
|
2599
2775
|
compile(path) {
|
|
2600
|
-
return new JSONPath(this, this.parser.parse(new TokenStream(tokenize(path))));
|
|
2776
|
+
return new JSONPath(this, this.parser.parse(new TokenStream(tokenize(this, path))));
|
|
2601
2777
|
}
|
|
2602
2778
|
|
|
2603
2779
|
/**
|
|
@@ -2679,7 +2855,7 @@ var json_p3 = (function (exports) {
|
|
|
2679
2855
|
for (const [typ, arg, idx] of func.argTypes.map((t, i) => [t, args[i], i])) {
|
|
2680
2856
|
switch (typ) {
|
|
2681
2857
|
case FunctionExpressionType.ValueType:
|
|
2682
|
-
if (!(arg instanceof FilterExpressionLiteral || arg instanceof JSONPathQuery && arg.path.singularQuery() || arg instanceof FunctionExtension && this.functionRegister.get(arg.name)?.returnType === FunctionExpressionType.ValueType)) {
|
|
2858
|
+
if (!(arg instanceof FilterExpressionLiteral || arg instanceof CurrentKey || arg instanceof JSONPathQuery && arg.path.singularQuery() || arg instanceof FunctionExtension && this.functionRegister.get(arg.name)?.returnType === FunctionExpressionType.ValueType)) {
|
|
2683
2859
|
throw new JSONPathTypeError(`${token.value}() argument ${idx} must be of ValueType`, arg.token);
|
|
2684
2860
|
}
|
|
2685
2861
|
break;
|
|
@@ -3307,7 +3483,7 @@ var json_p3 = (function (exports) {
|
|
|
3307
3483
|
apply: apply
|
|
3308
3484
|
});
|
|
3309
3485
|
|
|
3310
|
-
const version = "1.
|
|
3486
|
+
const version = "1.2.0";
|
|
3311
3487
|
|
|
3312
3488
|
exports.DEFAULT_ENVIRONMENT = DEFAULT_ENVIRONMENT;
|
|
3313
3489
|
exports.FunctionExpressionType = FunctionExpressionType;
|