json-p3 1.3.5 → 2.0.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/index.d.ts +1 -1
- package/dist/json-p3.cjs.js +321 -396
- package/dist/json-p3.esm.js +321 -396
- package/dist/json-p3.iife.js +321 -396
- 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 +4 -4
- package/dist/path/expression.d.ts +6 -6
- package/dist/path/extra/selectors.d.ts +8 -10
- package/dist/path/index.d.ts +5 -3
- package/dist/path/parse.d.ts +6 -5
- package/dist/path/path.d.ts +5 -5
- package/dist/path/segments.d.ts +38 -0
- package/dist/path/selectors.d.ts +16 -41
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/json-p3.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* json-p3 version
|
|
2
|
+
* json-p3 version 2.0.0
|
|
3
3
|
* https://github.com/jg-rp/json-p3
|
|
4
4
|
*
|
|
5
5
|
* MIT License
|
|
@@ -917,14 +917,14 @@ class LogicalExpression extends FilterExpression {
|
|
|
917
917
|
/**
|
|
918
918
|
* Base class for relative and absolute JSONPath query expressions.
|
|
919
919
|
*/
|
|
920
|
-
class
|
|
920
|
+
class FilterQuery extends FilterExpression {
|
|
921
921
|
constructor(token, path) {
|
|
922
922
|
super(token);
|
|
923
923
|
this.token = token;
|
|
924
924
|
this.path = path;
|
|
925
925
|
}
|
|
926
926
|
}
|
|
927
|
-
class RelativeQuery extends
|
|
927
|
+
class RelativeQuery extends FilterQuery {
|
|
928
928
|
evaluate(context) {
|
|
929
929
|
return context.lazy ? new JSONPathNodeList(Array.from(this.path.lazyQuery(context.currentValue))) : this.path.query(context.currentValue);
|
|
930
930
|
}
|
|
@@ -932,7 +932,7 @@ class RelativeQuery extends JSONPathQuery {
|
|
|
932
932
|
return `@${this.path.toString().slice(1)}`;
|
|
933
933
|
}
|
|
934
934
|
}
|
|
935
|
-
class RootQuery extends
|
|
935
|
+
class RootQuery extends FilterQuery {
|
|
936
936
|
evaluate(context) {
|
|
937
937
|
return context.lazy ? new JSONPathNodeList(Array.from(this.path.lazyQuery(context.rootValue))) : this.path.query(context.rootValue);
|
|
938
938
|
}
|
|
@@ -1026,9 +1026,9 @@ var expression = /*#__PURE__*/Object.freeze({
|
|
|
1026
1026
|
BooleanLiteral: BooleanLiteral,
|
|
1027
1027
|
FilterExpression: FilterExpression,
|
|
1028
1028
|
FilterExpressionLiteral: FilterExpressionLiteral,
|
|
1029
|
+
FilterQuery: FilterQuery,
|
|
1029
1030
|
FunctionExtension: FunctionExtension,
|
|
1030
1031
|
InfixExpression: InfixExpression,
|
|
1031
|
-
JSONPathQuery: JSONPathQuery,
|
|
1032
1032
|
LogicalExpression: LogicalExpression,
|
|
1033
1033
|
NullLiteral: NullLiteral,
|
|
1034
1034
|
NumberLiteral: NumberLiteral,
|
|
@@ -3129,11 +3129,11 @@ class JSONPathSelector {
|
|
|
3129
3129
|
}
|
|
3130
3130
|
|
|
3131
3131
|
/**
|
|
3132
|
-
* @param
|
|
3132
|
+
* @param node - Nodes matched by preceding selectors.
|
|
3133
3133
|
*/
|
|
3134
3134
|
|
|
3135
3135
|
/**
|
|
3136
|
-
* @param
|
|
3136
|
+
* @param node - Nodes matched by preceding selectors.
|
|
3137
3137
|
*/
|
|
3138
3138
|
|
|
3139
3139
|
/**
|
|
@@ -3145,31 +3145,26 @@ class JSONPathSelector {
|
|
|
3145
3145
|
* Shorthand and quoted name selector.
|
|
3146
3146
|
*/
|
|
3147
3147
|
class NameSelector extends JSONPathSelector {
|
|
3148
|
-
constructor(environment, token, name
|
|
3148
|
+
constructor(environment, token, name) {
|
|
3149
3149
|
super(environment, token);
|
|
3150
3150
|
this.environment = environment;
|
|
3151
3151
|
this.token = token;
|
|
3152
3152
|
this.name = name;
|
|
3153
|
-
this.shorthand = shorthand;
|
|
3154
3153
|
}
|
|
3155
|
-
resolve(
|
|
3154
|
+
resolve(node) {
|
|
3156
3155
|
const rv = [];
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
rv.push(new JSONPathNode(node.value[this.name], node.location.concat(this.name), node.root));
|
|
3160
|
-
}
|
|
3156
|
+
if (!isArray(node.value) && hasStringKey(node.value, this.name)) {
|
|
3157
|
+
rv.push(new JSONPathNode(node.value[this.name], node.location.concat(this.name), node.root));
|
|
3161
3158
|
}
|
|
3162
3159
|
return rv;
|
|
3163
3160
|
}
|
|
3164
|
-
*lazyResolve(
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
yield new JSONPathNode(node.value[this.name], node.location.concat(this.name), node.root);
|
|
3168
|
-
}
|
|
3161
|
+
*lazyResolve(node) {
|
|
3162
|
+
if (!isArray(node.value) && hasStringKey(node.value, this.name)) {
|
|
3163
|
+
yield new JSONPathNode(node.value[this.name], node.location.concat(this.name), node.root);
|
|
3169
3164
|
}
|
|
3170
3165
|
}
|
|
3171
3166
|
toString() {
|
|
3172
|
-
return
|
|
3167
|
+
return `'${this.name}'`;
|
|
3173
3168
|
}
|
|
3174
3169
|
}
|
|
3175
3170
|
|
|
@@ -3186,25 +3181,21 @@ class IndexSelector extends JSONPathSelector {
|
|
|
3186
3181
|
throw new JSONPathIndexError("index out of range", this.token);
|
|
3187
3182
|
}
|
|
3188
3183
|
}
|
|
3189
|
-
resolve(
|
|
3184
|
+
resolve(node) {
|
|
3190
3185
|
const rv = [];
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
rv.push(new JSONPathNode(node.value[normIndex], node.location.concat(normIndex), node.root));
|
|
3196
|
-
}
|
|
3186
|
+
if (isArray(node.value)) {
|
|
3187
|
+
const normIndex = this.normalizedIndex(node.value.length);
|
|
3188
|
+
if (normIndex in node.value) {
|
|
3189
|
+
rv.push(new JSONPathNode(node.value[normIndex], node.location.concat(normIndex), node.root));
|
|
3197
3190
|
}
|
|
3198
3191
|
}
|
|
3199
3192
|
return rv;
|
|
3200
3193
|
}
|
|
3201
|
-
*lazyResolve(
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
yield new JSONPathNode(node.value[normIndex], node.location.concat(normIndex), node.root);
|
|
3207
|
-
}
|
|
3194
|
+
*lazyResolve(node) {
|
|
3195
|
+
if (isArray(node.value)) {
|
|
3196
|
+
const normIndex = this.normalizedIndex(node.value.length);
|
|
3197
|
+
if (normIndex in node.value) {
|
|
3198
|
+
yield new JSONPathNode(node.value[normIndex], node.location.concat(normIndex), node.root);
|
|
3208
3199
|
}
|
|
3209
3200
|
}
|
|
3210
3201
|
}
|
|
@@ -3226,19 +3217,16 @@ class SliceSelector extends JSONPathSelector {
|
|
|
3226
3217
|
this.step = step;
|
|
3227
3218
|
this.checkRange(start, stop, step);
|
|
3228
3219
|
}
|
|
3229
|
-
resolve(
|
|
3220
|
+
resolve(node) {
|
|
3230
3221
|
const rv = [];
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
rv.push(new JSONPathNode(value, node.location.concat(i), node.root));
|
|
3235
|
-
}
|
|
3222
|
+
if (!isArray(node.value)) return rv;
|
|
3223
|
+
for (const [i, value] of this.slice(node.value, this.start, this.stop, this.step)) {
|
|
3224
|
+
rv.push(new JSONPathNode(value, node.location.concat(i), node.root));
|
|
3236
3225
|
}
|
|
3237
3226
|
return rv;
|
|
3238
3227
|
}
|
|
3239
|
-
*lazyResolve(
|
|
3240
|
-
|
|
3241
|
-
if (!isArray(node.value)) continue;
|
|
3228
|
+
*lazyResolve(node) {
|
|
3229
|
+
if (isArray(node.value)) {
|
|
3242
3230
|
for (const [i, value] of this.lazySlice(node.value, this.start, this.stop, this.step)) {
|
|
3243
3231
|
yield new JSONPathNode(value, node.location.concat(i), node.root);
|
|
3244
3232
|
}
|
|
@@ -3344,281 +3332,175 @@ class SliceSelector extends JSONPathSelector {
|
|
|
3344
3332
|
}
|
|
3345
3333
|
class WildcardSelector extends JSONPathSelector {
|
|
3346
3334
|
constructor(environment, token) {
|
|
3347
|
-
let shorthand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
3348
3335
|
super(environment, token);
|
|
3349
3336
|
this.environment = environment;
|
|
3350
3337
|
this.token = token;
|
|
3351
|
-
this.shorthand = shorthand;
|
|
3352
3338
|
}
|
|
3353
|
-
resolve(
|
|
3339
|
+
resolve(node) {
|
|
3354
3340
|
const rv = [];
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
3364
|
-
}
|
|
3341
|
+
if (node.value instanceof String) return rv;
|
|
3342
|
+
if (isArray(node.value)) {
|
|
3343
|
+
for (let i = 0; i < node.value.length; i++) {
|
|
3344
|
+
rv.push(new JSONPathNode(node.value[i], node.location.concat(i), node.root));
|
|
3345
|
+
}
|
|
3346
|
+
} else if (isObject(node.value)) {
|
|
3347
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3348
|
+
rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
3365
3349
|
}
|
|
3366
3350
|
}
|
|
3367
3351
|
return rv;
|
|
3368
3352
|
}
|
|
3369
|
-
*lazyResolve(
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3378
|
-
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3379
|
-
}
|
|
3353
|
+
*lazyResolve(node) {
|
|
3354
|
+
if (isArray(node.value)) {
|
|
3355
|
+
for (let i = 0; i < node.value.length; i++) {
|
|
3356
|
+
yield new JSONPathNode(node.value[i], node.location.concat(i), node.root);
|
|
3357
|
+
}
|
|
3358
|
+
} else if (isObject(node.value) && !isString(node.value)) {
|
|
3359
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3360
|
+
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3380
3361
|
}
|
|
3381
3362
|
}
|
|
3382
3363
|
}
|
|
3383
3364
|
toString() {
|
|
3384
|
-
return
|
|
3365
|
+
return "*";
|
|
3385
3366
|
}
|
|
3386
3367
|
}
|
|
3387
|
-
class
|
|
3388
|
-
constructor(environment, token,
|
|
3368
|
+
class FilterSelector extends JSONPathSelector {
|
|
3369
|
+
constructor(environment, token, expression) {
|
|
3389
3370
|
super(environment, token);
|
|
3390
3371
|
this.environment = environment;
|
|
3391
3372
|
this.token = token;
|
|
3392
|
-
this.
|
|
3393
|
-
}
|
|
3394
|
-
resolve(nodes) {
|
|
3395
|
-
const rv = [];
|
|
3396
|
-
if (this.environment.nondeterministic) {
|
|
3397
|
-
for (const root of nodes) {
|
|
3398
|
-
for (const node of this.nondeterministicVisitor(root)) {
|
|
3399
|
-
rv.push(node);
|
|
3400
|
-
}
|
|
3401
|
-
}
|
|
3402
|
-
} else {
|
|
3403
|
-
for (const node of nodes) {
|
|
3404
|
-
rv.push(node);
|
|
3405
|
-
for (const _node of this.visitor(node)) {
|
|
3406
|
-
rv.push(_node);
|
|
3407
|
-
}
|
|
3408
|
-
}
|
|
3409
|
-
}
|
|
3410
|
-
return this.selector.resolve(rv);
|
|
3411
|
-
}
|
|
3412
|
-
*lazyResolve(nodes) {
|
|
3413
|
-
yield* this.selector.lazyResolve(this._lazyResolve(nodes));
|
|
3414
|
-
}
|
|
3415
|
-
|
|
3416
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
3417
|
-
*_lazyResolve(nodes) {
|
|
3418
|
-
for (const _node of nodes) {
|
|
3419
|
-
const stack = [{
|
|
3420
|
-
node: _node,
|
|
3421
|
-
depth: 0
|
|
3422
|
-
}];
|
|
3423
|
-
yield _node;
|
|
3424
|
-
while (stack.length) {
|
|
3425
|
-
const {
|
|
3426
|
-
node: currentNode,
|
|
3427
|
-
depth
|
|
3428
|
-
} = stack.pop();
|
|
3429
|
-
if (depth >= this.environment.maxRecursionDepth) {
|
|
3430
|
-
throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
|
|
3431
|
-
}
|
|
3432
|
-
if (currentNode.value instanceof String) continue;
|
|
3433
|
-
if (isArray(currentNode.value)) {
|
|
3434
|
-
for (let i = 0; i < currentNode.value.length; i++) {
|
|
3435
|
-
const __node = new JSONPathNode(currentNode.value[i], currentNode.location.concat(i), currentNode.root);
|
|
3436
|
-
yield __node;
|
|
3437
|
-
if (isObject(__node.value)) {
|
|
3438
|
-
stack.push({
|
|
3439
|
-
node: __node,
|
|
3440
|
-
depth: depth + 1
|
|
3441
|
-
});
|
|
3442
|
-
}
|
|
3443
|
-
}
|
|
3444
|
-
} else if (isObject(currentNode.value)) {
|
|
3445
|
-
for (const [key, value] of this.environment.entries(currentNode.value)) {
|
|
3446
|
-
const __node = new JSONPathNode(value, currentNode.location.concat(key), currentNode.root);
|
|
3447
|
-
yield __node;
|
|
3448
|
-
if (isObject(__node.value)) {
|
|
3449
|
-
stack.push({
|
|
3450
|
-
node: __node,
|
|
3451
|
-
depth: depth + 1
|
|
3452
|
-
});
|
|
3453
|
-
}
|
|
3454
|
-
}
|
|
3455
|
-
}
|
|
3456
|
-
}
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
|
-
toString() {
|
|
3460
|
-
return `..${this.selector.toString()}`;
|
|
3373
|
+
this.expression = expression;
|
|
3461
3374
|
}
|
|
3462
|
-
|
|
3463
|
-
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
3464
|
-
if (depth >= this.environment.maxRecursionDepth) {
|
|
3465
|
-
throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
|
|
3466
|
-
}
|
|
3375
|
+
resolve(node) {
|
|
3467
3376
|
const rv = [];
|
|
3468
3377
|
if (node.value instanceof String) return rv;
|
|
3469
3378
|
if (isArray(node.value)) {
|
|
3470
3379
|
for (let i = 0; i < node.value.length; i++) {
|
|
3471
|
-
const
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3380
|
+
const value = node.value[i];
|
|
3381
|
+
const filterContext = {
|
|
3382
|
+
environment: this.environment,
|
|
3383
|
+
currentValue: value,
|
|
3384
|
+
rootValue: node.root,
|
|
3385
|
+
currentKey: i
|
|
3386
|
+
};
|
|
3387
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3388
|
+
rv.push(new JSONPathNode(value, node.location.concat(i), node.root));
|
|
3475
3389
|
}
|
|
3476
3390
|
}
|
|
3477
3391
|
} else if (isObject(node.value)) {
|
|
3478
3392
|
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3479
|
-
const
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
}
|
|
3488
|
-
nondeterministicVisitor(root) {
|
|
3489
|
-
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
3490
|
-
const rv = [root];
|
|
3491
|
-
let queue = this.nondeterministicChildren(root).map(node => [node, depth]);
|
|
3492
|
-
while (queue.length) {
|
|
3493
|
-
const [node, _depth] = queue.shift();
|
|
3494
|
-
rv.push(node);
|
|
3495
|
-
if (_depth >= this.environment.maxRecursionDepth) {
|
|
3496
|
-
throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
|
|
3497
|
-
}
|
|
3498
|
-
|
|
3499
|
-
// Visit child nodes now or queue them for later?
|
|
3500
|
-
const visitChildren = Math.random() < 0.5;
|
|
3501
|
-
for (const child of this.nondeterministicChildren(node)) {
|
|
3502
|
-
if (visitChildren) {
|
|
3503
|
-
rv.push(child);
|
|
3504
|
-
const grandchildren = this.nondeterministicChildren(child).map(n => [n, _depth + 2]);
|
|
3505
|
-
queue = interleave(queue, grandchildren);
|
|
3506
|
-
} else {
|
|
3507
|
-
queue.push([child, _depth + 1]);
|
|
3393
|
+
const filterContext = {
|
|
3394
|
+
environment: this.environment,
|
|
3395
|
+
currentValue: value,
|
|
3396
|
+
rootValue: node.root,
|
|
3397
|
+
currentKey: key
|
|
3398
|
+
};
|
|
3399
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3400
|
+
rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
3508
3401
|
}
|
|
3509
3402
|
}
|
|
3510
3403
|
}
|
|
3511
3404
|
return rv;
|
|
3512
3405
|
}
|
|
3513
|
-
|
|
3514
|
-
const _rv = [];
|
|
3515
|
-
if (node.value instanceof String) return _rv;
|
|
3406
|
+
*lazyResolve(node) {
|
|
3516
3407
|
if (isArray(node.value)) {
|
|
3517
3408
|
for (let i = 0; i < node.value.length; i++) {
|
|
3518
|
-
|
|
3409
|
+
const value = node.value[i];
|
|
3410
|
+
const filterContext = {
|
|
3411
|
+
environment: this.environment,
|
|
3412
|
+
currentValue: value,
|
|
3413
|
+
rootValue: node.root,
|
|
3414
|
+
lazy: true,
|
|
3415
|
+
currentKey: i
|
|
3416
|
+
};
|
|
3417
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3418
|
+
yield new JSONPathNode(value, node.location.concat(i), node.root);
|
|
3419
|
+
}
|
|
3519
3420
|
}
|
|
3520
|
-
} else if (isObject(node.value)) {
|
|
3421
|
+
} else if (isObject(node.value) && !isString(node.value)) {
|
|
3521
3422
|
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3522
|
-
|
|
3423
|
+
const filterContext = {
|
|
3424
|
+
environment: this.environment,
|
|
3425
|
+
currentValue: value,
|
|
3426
|
+
rootValue: node.root,
|
|
3427
|
+
lazy: true,
|
|
3428
|
+
currentKey: key
|
|
3429
|
+
};
|
|
3430
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3431
|
+
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3432
|
+
}
|
|
3523
3433
|
}
|
|
3524
3434
|
}
|
|
3525
|
-
|
|
3435
|
+
}
|
|
3436
|
+
toString() {
|
|
3437
|
+
return `?${this.expression.toString()}`;
|
|
3526
3438
|
}
|
|
3527
3439
|
}
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3440
|
+
|
|
3441
|
+
var selectors = /*#__PURE__*/Object.freeze({
|
|
3442
|
+
__proto__: null,
|
|
3443
|
+
FilterSelector: FilterSelector,
|
|
3444
|
+
IndexSelector: IndexSelector,
|
|
3445
|
+
JSONPathSelector: JSONPathSelector,
|
|
3446
|
+
NameSelector: NameSelector,
|
|
3447
|
+
SliceSelector: SliceSelector,
|
|
3448
|
+
WildcardSelector: WildcardSelector
|
|
3449
|
+
});
|
|
3450
|
+
|
|
3451
|
+
/** Base class for all JSONPath segments. Both shorthand and bracketed. */
|
|
3452
|
+
class JSONPathSegment {
|
|
3453
|
+
constructor(environment, token, selectors) {
|
|
3531
3454
|
this.environment = environment;
|
|
3532
3455
|
this.token = token;
|
|
3533
|
-
this.
|
|
3456
|
+
this.selectors = selectors;
|
|
3534
3457
|
}
|
|
3535
3458
|
|
|
3536
|
-
|
|
3459
|
+
/**
|
|
3460
|
+
* @param nodes - Nodes matched by preceding segments.
|
|
3461
|
+
*/
|
|
3462
|
+
|
|
3463
|
+
/**
|
|
3464
|
+
* @param nodes - Nodes matched by preceding segments.
|
|
3465
|
+
*/
|
|
3466
|
+
|
|
3467
|
+
/**
|
|
3468
|
+
* Return a canonical string representation of this segment.
|
|
3469
|
+
*/
|
|
3470
|
+
}
|
|
3471
|
+
|
|
3472
|
+
/** The child selection segment. */
|
|
3473
|
+
class ChildSegment extends JSONPathSegment {
|
|
3537
3474
|
resolve(nodes) {
|
|
3538
3475
|
const rv = [];
|
|
3539
3476
|
for (const node of nodes) {
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
for (let i = 0; i < node.value.length; i++) {
|
|
3543
|
-
const value = node.value[i];
|
|
3544
|
-
const filterContext = {
|
|
3545
|
-
environment: this.environment,
|
|
3546
|
-
currentValue: value,
|
|
3547
|
-
rootValue: node.root,
|
|
3548
|
-
currentKey: i
|
|
3549
|
-
};
|
|
3550
|
-
if (this.expression.evaluate(filterContext)) {
|
|
3551
|
-
rv.push(new JSONPathNode(value, node.location.concat(i), node.root));
|
|
3552
|
-
}
|
|
3553
|
-
}
|
|
3554
|
-
} else if (isObject(node.value)) {
|
|
3555
|
-
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3556
|
-
const filterContext = {
|
|
3557
|
-
environment: this.environment,
|
|
3558
|
-
currentValue: value,
|
|
3559
|
-
rootValue: node.root,
|
|
3560
|
-
currentKey: key
|
|
3561
|
-
};
|
|
3562
|
-
if (this.expression.evaluate(filterContext)) {
|
|
3563
|
-
rv.push(new JSONPathNode(value, node.location.concat(key), node.root));
|
|
3564
|
-
}
|
|
3565
|
-
}
|
|
3477
|
+
for (const selector of this.selectors) {
|
|
3478
|
+
rv.push(...selector.resolve(node));
|
|
3566
3479
|
}
|
|
3567
3480
|
}
|
|
3568
3481
|
return rv;
|
|
3569
3482
|
}
|
|
3570
|
-
|
|
3571
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
3572
3483
|
*lazyResolve(nodes) {
|
|
3573
3484
|
for (const node of nodes) {
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
for (let i = 0; i < node.value.length; i++) {
|
|
3577
|
-
const value = node.value[i];
|
|
3578
|
-
const filterContext = {
|
|
3579
|
-
environment: this.environment,
|
|
3580
|
-
currentValue: value,
|
|
3581
|
-
rootValue: node.root,
|
|
3582
|
-
lazy: true,
|
|
3583
|
-
currentKey: i
|
|
3584
|
-
};
|
|
3585
|
-
if (this.expression.evaluate(filterContext)) {
|
|
3586
|
-
yield new JSONPathNode(value, node.location.concat(i), node.root);
|
|
3587
|
-
}
|
|
3588
|
-
}
|
|
3589
|
-
} else if (isObject(node.value)) {
|
|
3590
|
-
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3591
|
-
const filterContext = {
|
|
3592
|
-
environment: this.environment,
|
|
3593
|
-
currentValue: value,
|
|
3594
|
-
rootValue: node.root,
|
|
3595
|
-
lazy: true,
|
|
3596
|
-
currentKey: key
|
|
3597
|
-
};
|
|
3598
|
-
if (this.expression.evaluate(filterContext)) {
|
|
3599
|
-
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3600
|
-
}
|
|
3601
|
-
}
|
|
3485
|
+
for (const selector of this.selectors) {
|
|
3486
|
+
yield* selector.resolve(node);
|
|
3602
3487
|
}
|
|
3603
3488
|
}
|
|
3604
3489
|
}
|
|
3605
3490
|
toString() {
|
|
3606
|
-
return
|
|
3491
|
+
return `[${this.selectors.map(s => s.toString()).join(", ")}]`;
|
|
3607
3492
|
}
|
|
3608
3493
|
}
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
this.environment = environment;
|
|
3613
|
-
this.token = token;
|
|
3614
|
-
this.items = items;
|
|
3615
|
-
}
|
|
3494
|
+
|
|
3495
|
+
/** The recursive descent segment. */
|
|
3496
|
+
class DescendantSegment extends JSONPathSegment {
|
|
3616
3497
|
resolve(nodes) {
|
|
3617
3498
|
const rv = [];
|
|
3499
|
+
const visitor = (this.environment.nondeterministic ? this.nondeterministicVisit : this.visit).bind(this);
|
|
3618
3500
|
for (const node of nodes) {
|
|
3619
|
-
for (const
|
|
3620
|
-
for (const
|
|
3621
|
-
rv.push(_node);
|
|
3501
|
+
for (const _node of visitor(node)) {
|
|
3502
|
+
for (const selector of this.selectors) {
|
|
3503
|
+
rv.push(...selector.resolve(_node));
|
|
3622
3504
|
}
|
|
3623
3505
|
}
|
|
3624
3506
|
}
|
|
@@ -3626,13 +3508,75 @@ class BracketedSelection extends JSONPathSelector {
|
|
|
3626
3508
|
}
|
|
3627
3509
|
*lazyResolve(nodes) {
|
|
3628
3510
|
for (const node of nodes) {
|
|
3629
|
-
for (const
|
|
3630
|
-
|
|
3511
|
+
for (const _node of this.visit(node)) {
|
|
3512
|
+
for (const selector of this.selectors) {
|
|
3513
|
+
yield* selector.resolve(_node);
|
|
3514
|
+
}
|
|
3631
3515
|
}
|
|
3632
3516
|
}
|
|
3633
3517
|
}
|
|
3634
3518
|
toString() {
|
|
3635
|
-
return
|
|
3519
|
+
return `..[${this.selectors.map(s => s.toString()).join(", ")}]`;
|
|
3520
|
+
}
|
|
3521
|
+
visit(node) {
|
|
3522
|
+
var _this = this;
|
|
3523
|
+
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
3524
|
+
return function* () {
|
|
3525
|
+
if (depth >= _this.environment.maxRecursionDepth) {
|
|
3526
|
+
throw new JSONPathRecursionLimitError("recursion limit reached", _this.token);
|
|
3527
|
+
}
|
|
3528
|
+
yield node;
|
|
3529
|
+
if (isArray(node.value)) {
|
|
3530
|
+
for (let i = 0; i < node.value.length; i++) {
|
|
3531
|
+
const _node = new JSONPathNode(node.value[i], node.location.concat(i), node.root);
|
|
3532
|
+
yield* _this.visit(_node, depth + 1);
|
|
3533
|
+
}
|
|
3534
|
+
} else if (isObject(node.value)) {
|
|
3535
|
+
for (const [key, value] of _this.environment.entries(node.value)) {
|
|
3536
|
+
const _node = new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3537
|
+
yield* _this.visit(_node, depth + 1);
|
|
3538
|
+
}
|
|
3539
|
+
}
|
|
3540
|
+
}();
|
|
3541
|
+
}
|
|
3542
|
+
nondeterministicVisit(root) {
|
|
3543
|
+
var _this2 = this;
|
|
3544
|
+
let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
3545
|
+
return function* () {
|
|
3546
|
+
let queue = Array.from(_this2.nondeterministicChildren(root)).map(node => [node, depth]);
|
|
3547
|
+
yield root;
|
|
3548
|
+
while (queue.length) {
|
|
3549
|
+
const [node, _depth] = queue.shift();
|
|
3550
|
+
yield node;
|
|
3551
|
+
if (_depth >= _this2.environment.maxRecursionDepth) {
|
|
3552
|
+
throw new JSONPathRecursionLimitError("recursion limit reached", _this2.token);
|
|
3553
|
+
}
|
|
3554
|
+
|
|
3555
|
+
// Visit child nodes now or queue them for later?
|
|
3556
|
+
const visitChildren = Math.random() < 0.5;
|
|
3557
|
+
for (const child of _this2.nondeterministicChildren(node)) {
|
|
3558
|
+
if (visitChildren) {
|
|
3559
|
+
yield child;
|
|
3560
|
+
const grandchildren = Array.from(_this2.nondeterministicChildren(child)).map(n => [n, _depth + 2]);
|
|
3561
|
+
queue = interleave(queue, grandchildren);
|
|
3562
|
+
} else {
|
|
3563
|
+
queue.push([child, _depth + 1]);
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
}();
|
|
3568
|
+
}
|
|
3569
|
+
*nondeterministicChildren(node) {
|
|
3570
|
+
if (isString(node.value)) return;
|
|
3571
|
+
if (isArray(node.value)) {
|
|
3572
|
+
for (let i = 0; i < node.value.length; i++) {
|
|
3573
|
+
yield new JSONPathNode(node.value[i], node.location.concat(i), node.root);
|
|
3574
|
+
}
|
|
3575
|
+
} else if (isObject(node.value)) {
|
|
3576
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3577
|
+
yield new JSONPathNode(value, node.location.concat(key), node.root);
|
|
3578
|
+
}
|
|
3579
|
+
}
|
|
3636
3580
|
}
|
|
3637
3581
|
}
|
|
3638
3582
|
|
|
@@ -3671,30 +3615,18 @@ function shuffle(entries) {
|
|
|
3671
3615
|
return entries;
|
|
3672
3616
|
}
|
|
3673
3617
|
|
|
3674
|
-
var selectors = /*#__PURE__*/Object.freeze({
|
|
3675
|
-
__proto__: null,
|
|
3676
|
-
BracketedSelection: BracketedSelection,
|
|
3677
|
-
FilterSelector: FilterSelector,
|
|
3678
|
-
IndexSelector: IndexSelector,
|
|
3679
|
-
JSONPathSelector: JSONPathSelector,
|
|
3680
|
-
NameSelector: NameSelector,
|
|
3681
|
-
RecursiveDescentSegment: RecursiveDescentSegment,
|
|
3682
|
-
SliceSelector: SliceSelector,
|
|
3683
|
-
WildcardSelector: WildcardSelector
|
|
3684
|
-
});
|
|
3685
|
-
|
|
3686
3618
|
/**
|
|
3687
3619
|
*
|
|
3688
3620
|
*/
|
|
3689
|
-
class
|
|
3621
|
+
class JSONPathQuery {
|
|
3690
3622
|
/**
|
|
3691
3623
|
*
|
|
3692
3624
|
* @param environment -
|
|
3693
|
-
* @param
|
|
3625
|
+
* @param segments -
|
|
3694
3626
|
*/
|
|
3695
|
-
constructor(environment,
|
|
3627
|
+
constructor(environment, segments) {
|
|
3696
3628
|
this.environment = environment;
|
|
3697
|
-
this.
|
|
3629
|
+
this.segments = segments;
|
|
3698
3630
|
}
|
|
3699
3631
|
|
|
3700
3632
|
/**
|
|
@@ -3704,8 +3636,8 @@ class JSONPath {
|
|
|
3704
3636
|
*/
|
|
3705
3637
|
query(value) {
|
|
3706
3638
|
let nodes = [new JSONPathNode(value, [], value)];
|
|
3707
|
-
for (const
|
|
3708
|
-
nodes =
|
|
3639
|
+
for (const segment of this.segments) {
|
|
3640
|
+
nodes = segment.resolve(nodes);
|
|
3709
3641
|
}
|
|
3710
3642
|
return new JSONPathNodeList(nodes);
|
|
3711
3643
|
}
|
|
@@ -3717,8 +3649,8 @@ class JSONPath {
|
|
|
3717
3649
|
*/
|
|
3718
3650
|
lazyQuery(value) {
|
|
3719
3651
|
let nodes = [new JSONPathNode(value, [], value)][Symbol.iterator]();
|
|
3720
|
-
for (const
|
|
3721
|
-
nodes =
|
|
3652
|
+
for (const segment of this.segments) {
|
|
3653
|
+
nodes = segment.lazyResolve(nodes);
|
|
3722
3654
|
}
|
|
3723
3655
|
return nodes;
|
|
3724
3656
|
}
|
|
@@ -3742,12 +3674,14 @@ class JSONPath {
|
|
|
3742
3674
|
*
|
|
3743
3675
|
*/
|
|
3744
3676
|
toString() {
|
|
3745
|
-
return `$${this.
|
|
3677
|
+
return `$${this.segments.map(s => s.toString()).join("")}`;
|
|
3746
3678
|
}
|
|
3747
3679
|
singularQuery() {
|
|
3748
|
-
for (const
|
|
3749
|
-
if (
|
|
3750
|
-
if (
|
|
3680
|
+
for (const segment of this.segments) {
|
|
3681
|
+
if (segment instanceof DescendantSegment) return false;
|
|
3682
|
+
if (segment.selectors.length === 1 && (segment.selectors[0] instanceof NameSelector || segment.selectors[0] instanceof IndexSelector)) {
|
|
3683
|
+
continue;
|
|
3684
|
+
}
|
|
3751
3685
|
return false;
|
|
3752
3686
|
}
|
|
3753
3687
|
return true;
|
|
@@ -3765,33 +3699,26 @@ class CurrentKey extends FilterExpression {
|
|
|
3765
3699
|
|
|
3766
3700
|
class KeySelector extends JSONPathSelector {
|
|
3767
3701
|
constructor(environment, token, key) {
|
|
3768
|
-
let shorthand = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
3769
3702
|
super(environment, token);
|
|
3770
3703
|
this.environment = environment;
|
|
3771
3704
|
this.token = token;
|
|
3772
3705
|
this.key = key;
|
|
3773
|
-
this.shorthand = shorthand;
|
|
3774
3706
|
}
|
|
3775
|
-
resolve(
|
|
3707
|
+
resolve(node) {
|
|
3776
3708
|
const rv = [];
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
rv.push(new JSONPathNode(this.key, node.location.concat(`${KEY_MARK}${this.key}`), node.root));
|
|
3781
|
-
}
|
|
3709
|
+
if (node.value instanceof String || isArray(node.value)) return rv;
|
|
3710
|
+
if (isObject(node.value) && hasStringKey(node.value, this.key)) {
|
|
3711
|
+
rv.push(new JSONPathNode(this.key, node.location.concat(`${KEY_MARK}${this.key}`), node.root));
|
|
3782
3712
|
}
|
|
3783
3713
|
return rv;
|
|
3784
3714
|
}
|
|
3785
|
-
*lazyResolve(
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
if (isObject(node.value) && hasStringKey(node.value, this.key)) {
|
|
3789
|
-
yield new JSONPathNode(this.key, node.location.concat(`${KEY_MARK}${this.key}`), node.root);
|
|
3790
|
-
}
|
|
3715
|
+
*lazyResolve(node) {
|
|
3716
|
+
if (!isString(node.value) && isObject(node.value) && hasStringKey(node.value, this.key)) {
|
|
3717
|
+
yield new JSONPathNode(this.key, node.location.concat(`${KEY_MARK}${this.key}`), node.root);
|
|
3791
3718
|
}
|
|
3792
3719
|
}
|
|
3793
3720
|
toString() {
|
|
3794
|
-
return
|
|
3721
|
+
return `~'${this.key.replaceAll("'", "\\'")}'`;
|
|
3795
3722
|
}
|
|
3796
3723
|
}
|
|
3797
3724
|
|
|
@@ -3800,36 +3727,29 @@ class KeySelector extends JSONPathSelector {
|
|
|
3800
3727
|
*/
|
|
3801
3728
|
class KeysSelector extends JSONPathSelector {
|
|
3802
3729
|
constructor(environment, token) {
|
|
3803
|
-
let shorthand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
3804
3730
|
super(environment, token);
|
|
3805
3731
|
this.environment = environment;
|
|
3806
3732
|
this.token = token;
|
|
3807
|
-
this.shorthand = shorthand;
|
|
3808
3733
|
}
|
|
3809
|
-
resolve(
|
|
3734
|
+
resolve(node) {
|
|
3810
3735
|
const rv = [];
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
rv.push(new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root));
|
|
3816
|
-
}
|
|
3736
|
+
if (node.value instanceof String || isArray(node.value)) return rv;
|
|
3737
|
+
if (isObject(node.value)) {
|
|
3738
|
+
for (const [key, _] of this.environment.entries(node.value)) {
|
|
3739
|
+
rv.push(new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root));
|
|
3817
3740
|
}
|
|
3818
3741
|
}
|
|
3819
3742
|
return rv;
|
|
3820
3743
|
}
|
|
3821
|
-
*lazyResolve(
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
for (const [key, _] of this.environment.entries(node.value)) {
|
|
3826
|
-
yield new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root);
|
|
3827
|
-
}
|
|
3744
|
+
*lazyResolve(node) {
|
|
3745
|
+
if (isObject(node.value) && !isString(node.value) && !isArray(node.value)) {
|
|
3746
|
+
for (const [key, _] of this.environment.entries(node.value)) {
|
|
3747
|
+
yield new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root);
|
|
3828
3748
|
}
|
|
3829
3749
|
}
|
|
3830
3750
|
}
|
|
3831
3751
|
toString() {
|
|
3832
|
-
return
|
|
3752
|
+
return "~";
|
|
3833
3753
|
}
|
|
3834
3754
|
}
|
|
3835
3755
|
class KeysFilterSelector extends JSONPathSelector {
|
|
@@ -3839,41 +3759,37 @@ class KeysFilterSelector extends JSONPathSelector {
|
|
|
3839
3759
|
this.token = token;
|
|
3840
3760
|
this.expression = expression;
|
|
3841
3761
|
}
|
|
3842
|
-
resolve(
|
|
3762
|
+
resolve(node) {
|
|
3843
3763
|
const rv = [];
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
rv.push(new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root));
|
|
3856
|
-
}
|
|
3764
|
+
if (node.value instanceof String || isArray(node.value)) return rv;
|
|
3765
|
+
if (isObject(node.value)) {
|
|
3766
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3767
|
+
const filterContext = {
|
|
3768
|
+
environment: this.environment,
|
|
3769
|
+
currentValue: value,
|
|
3770
|
+
rootValue: node.root,
|
|
3771
|
+
currentKey: key
|
|
3772
|
+
};
|
|
3773
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3774
|
+
rv.push(new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root));
|
|
3857
3775
|
}
|
|
3858
3776
|
}
|
|
3859
3777
|
}
|
|
3860
3778
|
return rv;
|
|
3861
3779
|
}
|
|
3862
|
-
*lazyResolve(
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
yield new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root);
|
|
3876
|
-
}
|
|
3780
|
+
*lazyResolve(node) {
|
|
3781
|
+
if (node.value instanceof String || isArray(node.value)) return;
|
|
3782
|
+
if (isObject(node.value)) {
|
|
3783
|
+
for (const [key, value] of this.environment.entries(node.value)) {
|
|
3784
|
+
const filterContext = {
|
|
3785
|
+
environment: this.environment,
|
|
3786
|
+
currentValue: value,
|
|
3787
|
+
rootValue: node.root,
|
|
3788
|
+
lazy: true,
|
|
3789
|
+
currentKey: key
|
|
3790
|
+
};
|
|
3791
|
+
if (this.expression.evaluate(filterContext)) {
|
|
3792
|
+
yield new JSONPathNode(key, node.location.concat(`${KEY_MARK}${key}`), node.root);
|
|
3877
3793
|
}
|
|
3878
3794
|
}
|
|
3879
3795
|
}
|
|
@@ -3902,52 +3818,59 @@ class Parser {
|
|
|
3902
3818
|
}
|
|
3903
3819
|
parse(stream) {
|
|
3904
3820
|
if (stream.current.kind === TokenKind.ROOT) stream.next();
|
|
3905
|
-
const
|
|
3821
|
+
const segments = this.parseQuery(stream);
|
|
3906
3822
|
if (stream.current.kind !== TokenKind.EOF) {
|
|
3907
3823
|
throw new JSONPathSyntaxError(`unexpected token '${stream.current.kind}'`, stream.current);
|
|
3908
3824
|
}
|
|
3909
|
-
return
|
|
3825
|
+
return segments;
|
|
3910
3826
|
}
|
|
3911
|
-
|
|
3827
|
+
parseQuery(stream) {
|
|
3912
3828
|
let inFilter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
3913
|
-
const
|
|
3914
|
-
for (;;) {
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3829
|
+
const segments = [];
|
|
3830
|
+
loop: for (;;) {
|
|
3831
|
+
switch (stream.current.kind) {
|
|
3832
|
+
case TokenKind.DDOT:
|
|
3833
|
+
{
|
|
3834
|
+
const token = stream.next();
|
|
3835
|
+
const selectors = this.parseSelectors(stream);
|
|
3836
|
+
segments.push(new DescendantSegment(this.environment, token, selectors));
|
|
3837
|
+
break;
|
|
3838
|
+
}
|
|
3839
|
+
case TokenKind.LBRACKET:
|
|
3840
|
+
case TokenKind.KEY:
|
|
3841
|
+
case TokenKind.KEYS:
|
|
3842
|
+
case TokenKind.NAME:
|
|
3843
|
+
case TokenKind.WILD:
|
|
3844
|
+
{
|
|
3845
|
+
const token = stream.current;
|
|
3846
|
+
const selectors = this.parseSelectors(stream);
|
|
3847
|
+
segments.push(new ChildSegment(this.environment, token, selectors));
|
|
3848
|
+
break;
|
|
3849
|
+
}
|
|
3850
|
+
default:
|
|
3851
|
+
{
|
|
3852
|
+
if (inFilter) stream.backup();
|
|
3853
|
+
break loop;
|
|
3854
|
+
}
|
|
3921
3855
|
}
|
|
3922
|
-
selectors.push(selector);
|
|
3923
3856
|
stream.next();
|
|
3924
3857
|
}
|
|
3925
|
-
return
|
|
3858
|
+
return segments;
|
|
3926
3859
|
}
|
|
3927
|
-
|
|
3860
|
+
parseSelectors(stream) {
|
|
3928
3861
|
switch (stream.current.kind) {
|
|
3929
3862
|
case TokenKind.NAME:
|
|
3930
|
-
return new NameSelector(this.environment, stream.current, stream.current.value
|
|
3863
|
+
return [new NameSelector(this.environment, stream.current, stream.current.value)];
|
|
3931
3864
|
case TokenKind.WILD:
|
|
3932
|
-
return new WildcardSelector(this.environment, stream.current
|
|
3865
|
+
return [new WildcardSelector(this.environment, stream.current)];
|
|
3933
3866
|
case TokenKind.KEY:
|
|
3934
|
-
return new KeySelector(this.environment, stream.current, stream.current.value
|
|
3867
|
+
return [new KeySelector(this.environment, stream.current, stream.current.value)];
|
|
3935
3868
|
case TokenKind.KEYS:
|
|
3936
|
-
return new KeysSelector(this.environment, stream.current
|
|
3937
|
-
case TokenKind.DDOT:
|
|
3938
|
-
{
|
|
3939
|
-
const segmentToken = stream.current;
|
|
3940
|
-
stream.next();
|
|
3941
|
-
const selector = this.parseSegment(stream);
|
|
3942
|
-
if (!selector) {
|
|
3943
|
-
throw new JSONPathSyntaxError("bald descendant segment", stream.current);
|
|
3944
|
-
}
|
|
3945
|
-
return new RecursiveDescentSegment(this.environment, segmentToken, selector);
|
|
3946
|
-
}
|
|
3869
|
+
return [new KeysSelector(this.environment, stream.current)];
|
|
3947
3870
|
case TokenKind.LBRACKET:
|
|
3948
3871
|
return this.parseBracketedSelection(stream);
|
|
3949
3872
|
default:
|
|
3950
|
-
return
|
|
3873
|
+
return [];
|
|
3951
3874
|
}
|
|
3952
3875
|
}
|
|
3953
3876
|
parseIndex(stream) {
|
|
@@ -4004,38 +3927,38 @@ class Parser {
|
|
|
4004
3927
|
}
|
|
4005
3928
|
parseBracketedSelection(stream) {
|
|
4006
3929
|
const token = stream.next();
|
|
4007
|
-
const
|
|
3930
|
+
const selectors = [];
|
|
4008
3931
|
while (stream.current.kind !== TokenKind.RBRACKET) {
|
|
4009
3932
|
switch (stream.current.kind) {
|
|
4010
3933
|
case TokenKind.SINGLE_QUOTE_STRING:
|
|
4011
3934
|
case TokenKind.DOUBLE_QUOTE_STRING:
|
|
4012
|
-
|
|
3935
|
+
selectors.push(new NameSelector(this.environment, stream.current, this.decodeString(stream.current)));
|
|
4013
3936
|
break;
|
|
4014
3937
|
case TokenKind.FILTER:
|
|
4015
|
-
|
|
3938
|
+
selectors.push(this.parseFilter(stream));
|
|
4016
3939
|
break;
|
|
4017
3940
|
case TokenKind.INDEX:
|
|
4018
3941
|
if (stream.peek.kind === TokenKind.COLON) {
|
|
4019
|
-
|
|
3942
|
+
selectors.push(this.parseSlice(stream));
|
|
4020
3943
|
} else {
|
|
4021
|
-
|
|
3944
|
+
selectors.push(this.parseIndex(stream));
|
|
4022
3945
|
}
|
|
4023
3946
|
break;
|
|
4024
3947
|
case TokenKind.COLON:
|
|
4025
|
-
|
|
3948
|
+
selectors.push(this.parseSlice(stream));
|
|
4026
3949
|
break;
|
|
4027
3950
|
case TokenKind.WILD:
|
|
4028
|
-
|
|
3951
|
+
selectors.push(new WildcardSelector(this.environment, stream.current));
|
|
4029
3952
|
break;
|
|
4030
3953
|
case TokenKind.KEY_SINGLE_QUOTE_STRING:
|
|
4031
3954
|
case TokenKind.KEY_DOUBLE_QUOTE_STRING:
|
|
4032
|
-
|
|
3955
|
+
selectors.push(new KeySelector(this.environment, stream.current, this.decodeString(stream.current)));
|
|
4033
3956
|
break;
|
|
4034
3957
|
case TokenKind.KEYS_FILTER:
|
|
4035
|
-
|
|
3958
|
+
selectors.push(this.parseFilter(stream, true));
|
|
4036
3959
|
break;
|
|
4037
3960
|
case TokenKind.KEYS:
|
|
4038
|
-
|
|
3961
|
+
selectors.push(new KeysSelector(this.environment, stream.current));
|
|
4039
3962
|
break;
|
|
4040
3963
|
case TokenKind.EOF:
|
|
4041
3964
|
throw new JSONPathSyntaxError("unexpected end of query", stream.current);
|
|
@@ -4049,10 +3972,10 @@ class Parser {
|
|
|
4049
3972
|
}
|
|
4050
3973
|
stream.next();
|
|
4051
3974
|
}
|
|
4052
|
-
if (!
|
|
3975
|
+
if (!selectors.length) {
|
|
4053
3976
|
throw new JSONPathSyntaxError("empty bracketed segment", token);
|
|
4054
3977
|
}
|
|
4055
|
-
return
|
|
3978
|
+
return selectors;
|
|
4056
3979
|
}
|
|
4057
3980
|
parseFilter(stream) {
|
|
4058
3981
|
let keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
@@ -4132,11 +4055,11 @@ class Parser {
|
|
|
4132
4055
|
}
|
|
4133
4056
|
parseRootQuery(stream) {
|
|
4134
4057
|
const tok = stream.next();
|
|
4135
|
-
return new RootQuery(tok, new
|
|
4058
|
+
return new RootQuery(tok, new JSONPathQuery(this.environment, this.parseQuery(stream, true)));
|
|
4136
4059
|
}
|
|
4137
4060
|
parseRelativeQuery(stream) {
|
|
4138
4061
|
const tok = stream.next();
|
|
4139
|
-
return new RelativeQuery(tok, new
|
|
4062
|
+
return new RelativeQuery(tok, new JSONPathQuery(this.environment, this.parseQuery(stream, true)));
|
|
4140
4063
|
}
|
|
4141
4064
|
parseCurrentKey(stream) {
|
|
4142
4065
|
return new CurrentKey(stream.current);
|
|
@@ -4438,10 +4361,10 @@ class JSONPathEnvironment {
|
|
|
4438
4361
|
|
|
4439
4362
|
/**
|
|
4440
4363
|
* @param path - A JSONPath query to parse.
|
|
4441
|
-
* @returns A new {@link
|
|
4364
|
+
* @returns A new {@link JSONPathQuery} object, bound to this environment.
|
|
4442
4365
|
*/
|
|
4443
4366
|
compile(path) {
|
|
4444
|
-
return new
|
|
4367
|
+
return new JSONPathQuery(this, this.parser.parse(new TokenStream(tokenize(this, path))));
|
|
4445
4368
|
}
|
|
4446
4369
|
|
|
4447
4370
|
/**
|
|
@@ -4496,7 +4419,7 @@ class JSONPathEnvironment {
|
|
|
4496
4419
|
/**
|
|
4497
4420
|
* Check the well-typedness of a function's arguments at compile-time.
|
|
4498
4421
|
*
|
|
4499
|
-
* This method is called by the
|
|
4422
|
+
* This method is called by the parser when parsing function calls.
|
|
4500
4423
|
* It is expected to throw a {@link JSONPathTypeError} if the function's
|
|
4501
4424
|
* parameters are not well-typed.
|
|
4502
4425
|
*
|
|
@@ -4523,17 +4446,17 @@ class JSONPathEnvironment {
|
|
|
4523
4446
|
for (const [typ, arg, idx] of func.argTypes.map((t, i) => [t, args[i], i])) {
|
|
4524
4447
|
switch (typ) {
|
|
4525
4448
|
case FunctionExpressionType.ValueType:
|
|
4526
|
-
if (!(arg instanceof FilterExpressionLiteral || arg instanceof CurrentKey || arg instanceof
|
|
4449
|
+
if (!(arg instanceof FilterExpressionLiteral || arg instanceof CurrentKey || arg instanceof FilterQuery && arg.path.singularQuery() || arg instanceof FunctionExtension && this.functionRegister.get(arg.name)?.returnType === FunctionExpressionType.ValueType)) {
|
|
4527
4450
|
throw new JSONPathTypeError(`${token.value}() argument ${idx} must be of ValueType`, arg.token);
|
|
4528
4451
|
}
|
|
4529
4452
|
break;
|
|
4530
4453
|
case FunctionExpressionType.LogicalType:
|
|
4531
|
-
if (!(arg instanceof
|
|
4454
|
+
if (!(arg instanceof FilterQuery || arg instanceof InfixExpression)) {
|
|
4532
4455
|
throw new JSONPathTypeError(`${token.value}() argument ${idx} must be of LogicalType`, arg.token);
|
|
4533
4456
|
}
|
|
4534
4457
|
break;
|
|
4535
4458
|
case FunctionExpressionType.NodesType:
|
|
4536
|
-
if (!(arg instanceof
|
|
4459
|
+
if (!(arg instanceof FilterQuery || arg instanceof FunctionExtension && this.functionRegister.get(arg.name)?.returnType === FunctionExpressionType.NodesType)) {
|
|
4537
4460
|
throw new JSONPathTypeError(`${token.value}() argument ${idx} must be of NodesType`, arg.token);
|
|
4538
4461
|
}
|
|
4539
4462
|
}
|
|
@@ -4649,14 +4572,16 @@ var index$1 = /*#__PURE__*/Object.freeze({
|
|
|
4649
4572
|
__proto__: null,
|
|
4650
4573
|
DEFAULT_ENVIRONMENT: DEFAULT_ENVIRONMENT,
|
|
4651
4574
|
FunctionExpressionType: FunctionExpressionType,
|
|
4652
|
-
JSONPath: JSONPath,
|
|
4653
4575
|
JSONPathEnvironment: JSONPathEnvironment,
|
|
4654
4576
|
JSONPathError: JSONPathError,
|
|
4655
4577
|
JSONPathIndexError: JSONPathIndexError,
|
|
4656
4578
|
JSONPathLexerError: JSONPathLexerError,
|
|
4657
4579
|
JSONPathNode: JSONPathNode,
|
|
4658
4580
|
JSONPathNodeList: JSONPathNodeList,
|
|
4581
|
+
JSONPathQuery: JSONPathQuery,
|
|
4659
4582
|
JSONPathRecursionLimitError: JSONPathRecursionLimitError,
|
|
4583
|
+
JSONPathSegment: JSONPathSegment,
|
|
4584
|
+
JSONPathSelector: JSONPathSelector,
|
|
4660
4585
|
JSONPathSyntaxError: JSONPathSyntaxError,
|
|
4661
4586
|
JSONPathTypeError: JSONPathTypeError,
|
|
4662
4587
|
KEY_MARK: KEY_MARK,
|
|
@@ -5160,6 +5085,6 @@ var index = /*#__PURE__*/Object.freeze({
|
|
|
5160
5085
|
apply: apply
|
|
5161
5086
|
});
|
|
5162
5087
|
|
|
5163
|
-
const version = "
|
|
5088
|
+
const version = "2.0.0";
|
|
5164
5089
|
|
|
5165
|
-
export { DEFAULT_ENVIRONMENT, FunctionExpressionType, JSONPatch, JSONPatchError, JSONPatchTestFailure,
|
|
5090
|
+
export { DEFAULT_ENVIRONMENT, FunctionExpressionType, JSONPatch, JSONPatchError, JSONPatchTestFailure, JSONPathEnvironment, JSONPathError, JSONPathIndexError, JSONPathLexerError, JSONPathNode, JSONPathNodeList, JSONPathQuery, JSONPathRecursionLimitError, JSONPathSyntaxError, JSONPathTypeError, JSONPointer, Nothing, RelativeJSONPointer, Token, TokenKind, UNDEFINED, apply, compile, index as jsonpatch, index$1 as jsonpath, index$3 as jsonpointer, lazyQuery, query, resolve, version };
|