json-p3 2.2.2 → 2.3.1

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.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * json-p3 version 2.2.2
2
+ * json-p3 version 2.3.1
3
3
  * https://github.com/jg-rp/json-p3
4
4
  *
5
5
  * MIT License
@@ -337,10 +337,7 @@ class JSONPointer {
337
337
  }
338
338
  static encode(tokens) {
339
339
  if (!tokens.length) return "";
340
- return (
341
- // eslint-disable-next-line prefer-template
342
- "/" + tokens.map(token => token.replaceAll("~", "~0").replaceAll("/", "~1")).join("/")
343
- );
340
+ return "/" + tokens.map(token => token.replaceAll("~", "~0").replaceAll("/", "~1")).join("/");
344
341
  }
345
342
 
346
343
  /**
@@ -357,8 +354,7 @@ class JSONPointer {
357
354
  * If the value pointed to by _pointer_ does not exist in _value_, and
358
355
  * no fallback value is given.
359
356
  */
360
- resolve(value) {
361
- let fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : UNDEFINED;
357
+ resolve(value, fallback = UNDEFINED) {
362
358
  try {
363
359
  return this.tokens.reduce(this.getItem.bind(this), value);
364
360
  } catch (error) {
@@ -463,10 +459,7 @@ class JSONPointer {
463
459
  * @returns A new JSON Pointer that is the concatenation of all tokens or
464
460
  * "parts".
465
461
  */
466
- join() {
467
- for (var _len = arguments.length, tokens = new Array(_len), _key = 0; _key < _len; _key++) {
468
- tokens[_key] = arguments[_key];
469
- }
462
+ join(...tokens) {
470
463
  if (!tokens.length) {
471
464
  return this;
472
465
  }
@@ -515,7 +508,7 @@ class JSONPointer {
515
508
  }
516
509
  }
517
510
  const RE_RELATIVE_POINTER = /(?<ORIGIN>\d+)(?<INDEX_G>(?<SIGN>[+-])(?<INDEX>\d))?(?<POINTER>.*)/s;
518
- const RE_INT = /(0|[1-9][0-9]*)/;
511
+ const RE_INT = /(0|[1-9]\d*)/;
519
512
 
520
513
  /**
521
514
  * A relative JSON Pointer.
@@ -634,8 +627,7 @@ class RelativeJSONPointer {
634
627
  * @throws {@link JSONPointerSyntaxError}
635
628
  * If _pointer_ is malformed according to RFC 6901.
636
629
  */
637
- function resolve(pointer, value) {
638
- let fallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : UNDEFINED;
630
+ function resolve(pointer, value, fallback = UNDEFINED) {
639
631
  return new JSONPointer(pointer).resolve(value, fallback);
640
632
  }
641
633
 
@@ -739,10 +731,7 @@ class JSONPathNode {
739
731
  ...defaultSerializationOptions,
740
732
  ...options
741
733
  };
742
- return (
743
- // eslint-disable-next-line prefer-template
744
- "$" + this.location.map(s => isString(s) ? this.decodeNameLocation(s, opts) : `[${s}]`).join("")
745
- );
734
+ return "$" + this.location.map(s => isString(s) ? this.decodeNameLocation(s, opts) : `[${s}]`).join("");
746
735
  }
747
736
 
748
737
  /**
@@ -1137,16 +1126,16 @@ var expression = /*#__PURE__*/Object.freeze({
1137
1126
  });
1138
1127
 
1139
1128
  class Count {
1140
- argTypes = (() => [FunctionExpressionType.NodesType])();
1141
- returnType = (() => FunctionExpressionType.ValueType)();
1129
+ argTypes = [FunctionExpressionType.NodesType];
1130
+ returnType = FunctionExpressionType.ValueType;
1142
1131
  call(nodes) {
1143
1132
  return nodes.length;
1144
1133
  }
1145
1134
  }
1146
1135
 
1147
1136
  class Length {
1148
- argTypes = (() => [FunctionExpressionType.ValueType])();
1149
- returnType = (() => FunctionExpressionType.ValueType)();
1137
+ argTypes = [FunctionExpressionType.ValueType];
1138
+ returnType = FunctionExpressionType.ValueType;
1150
1139
  call(value) {
1151
1140
  if (isArray(value) || isString(value)) return value.length;
1152
1141
  if (isObject(value)) return Object.keys(value).length;
@@ -1158,9 +1147,7 @@ class Length {
1158
1147
  * A Least Recently Used cache, implemented as an extended Map.
1159
1148
  */
1160
1149
  class LRUCache extends Map {
1161
- constructor() {
1162
- let maxSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 128;
1163
- let entries = arguments.length > 1 ? arguments[1] : undefined;
1150
+ constructor(maxSize = 128, entries) {
1164
1151
  if (entries !== undefined) {
1165
1152
  super(entries);
1166
1153
  } else {
@@ -2478,11 +2465,10 @@ var check_1 = {
2478
2465
  var check$1 = check_1.check;
2479
2466
 
2480
2467
  class Match {
2481
- argTypes = (() => [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType])();
2482
- returnType = (() => FunctionExpressionType.LogicalType)();
2468
+ argTypes = [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType];
2469
+ returnType = FunctionExpressionType.LogicalType;
2483
2470
  #cache;
2484
- constructor() {
2485
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2471
+ constructor(options = {}) {
2486
2472
  this.options = options;
2487
2473
  this.cacheSize = options.cacheSize ?? 10;
2488
2474
  this.throwErrors = options.throwErrors ?? false;
@@ -2527,11 +2513,10 @@ class Match {
2527
2513
  }
2528
2514
 
2529
2515
  class Search {
2530
- argTypes = (() => [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType])();
2531
- returnType = (() => FunctionExpressionType.LogicalType)();
2516
+ argTypes = [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType];
2517
+ returnType = FunctionExpressionType.LogicalType;
2532
2518
  #cache;
2533
- constructor() {
2534
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2519
+ constructor(options = {}) {
2535
2520
  this.options = options;
2536
2521
  this.cacheSize = options.cacheSize ?? 10;
2537
2522
  this.throwErrors = options.throwErrors ?? false;
@@ -2576,8 +2561,8 @@ class Search {
2576
2561
  }
2577
2562
 
2578
2563
  class Value {
2579
- argTypes = (() => [FunctionExpressionType.NodesType])();
2580
- returnType = (() => FunctionExpressionType.ValueType)();
2564
+ argTypes = [FunctionExpressionType.NodesType];
2565
+ returnType = FunctionExpressionType.ValueType;
2581
2566
  call(nodes) {
2582
2567
  if (nodes.length === 1) return nodes.nodes[0].value;
2583
2568
  return Nothing;
@@ -2593,6 +2578,7 @@ let TokenKind = /*#__PURE__*/function (TokenKind) {
2593
2578
  TokenKind["COMMA"] = "TOKEN_COMMA";
2594
2579
  TokenKind["CURRENT"] = "TOKEN_CURRENT_VALUE";
2595
2580
  TokenKind["CURRENT_KEY"] = "TOKEN_CURRENT_KEY";
2581
+ // non-standard, default `#`
2596
2582
  TokenKind["DDOT"] = "TOKEN_DDOT";
2597
2583
  TokenKind["DOT"] = "TOKEN_DOT";
2598
2584
  TokenKind["DOUBLE_QUOTE_STRING"] = "TOKEN_DOUBLE_QUOTE_STRING";
@@ -2606,10 +2592,15 @@ let TokenKind = /*#__PURE__*/function (TokenKind) {
2606
2592
  TokenKind["GT"] = "TOKEN_GT";
2607
2593
  TokenKind["INDEX"] = "TOKEN_INDEX";
2608
2594
  TokenKind["KEY"] = "TOKEN_KEY";
2595
+ // non-standard, default `~<name>`
2609
2596
  TokenKind["KEY_DOUBLE_QUOTE_STRING"] = "TOKEN_KEY_DOUBLE_QUOTE_STRING";
2597
+ // non-standard, `~"<name>"`
2610
2598
  TokenKind["KEY_SINGLE_QUOTE_STRING"] = "TOKEN_KEY_SINGLE_QUOTE_STRING";
2599
+ // non-standard, `~'<name>'`
2611
2600
  TokenKind["KEYS"] = "TOKEN_KEYS";
2601
+ // non-standard, default `~`
2612
2602
  TokenKind["KEYS_FILTER"] = "TOKEN_KEYS_FILTER";
2603
+ // non-standard, `~?<expression>`
2613
2604
  TokenKind["LBRACKET"] = "TOKEN_LBRACKET";
2614
2605
  TokenKind["LE"] = "TOKEN_LE";
2615
2606
  TokenKind["LG"] = "TOKEN_LG";
@@ -2693,7 +2684,7 @@ class TokenStream {
2693
2684
  const exponentPattern = /[eE][+-]?\d+/y;
2694
2685
  const functionNamePattern = /[a-z][a-z_0-9]*/y;
2695
2686
  const indexPattern = /-?\d+/y;
2696
- const intPattern = /-?[0-9]+/y;
2687
+ const intPattern = /-?\d+/y;
2697
2688
  const namePattern = /[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*/y;
2698
2689
  const whitespace = new Set([" ", "\n", "\t", "\r"]);
2699
2690
  const nameFirstPattern = /[\u0080-\uFFFFa-zA-Z_]/; // don't set sticky bit
@@ -3011,7 +3002,6 @@ function lexInsideBracketedSelection(l) {
3011
3002
  continue;
3012
3003
  }
3013
3004
  if (!l.environment.strict && l.acceptMatchRun(l.environment.keysPattern)) {
3014
- // FIXME: fall back to legacy behavior if keysPattern is not the default
3015
3005
  switch (l.peek()) {
3016
3006
  case "'":
3017
3007
  l.ignore(); // ~
@@ -3403,97 +3393,58 @@ class SliceSelector extends JSONPathSelector {
3403
3393
  const step = this.step ? this.step : "1";
3404
3394
  return `${start}:${stop}:${step}`;
3405
3395
  }
3406
- checkRange() {
3407
- for (var _len = arguments.length, indices = new Array(_len), _key = 0; _key < _len; _key++) {
3408
- indices[_key] = arguments[_key];
3409
- }
3396
+ checkRange(...indices) {
3410
3397
  for (const index of indices) {
3411
3398
  if (index !== undefined && (index < this.environment.minIntIndex || index > this.environment.maxIntIndex)) {
3412
3399
  throw new JSONPathIndexError("index out of range", this.token);
3413
3400
  }
3414
3401
  }
3415
3402
  }
3416
-
3417
- // eslint-disable-next-line sonarjs/cognitive-complexity
3418
3403
  slice(arr, start, stop, step) {
3419
- if (!arr.length) return [];
3420
-
3421
- // Handle negative start and stop values
3422
- if (start === undefined || start === null) {
3423
- start = step && step < 0 ? arr.length - 1 : 0;
3424
- } else if (start < 0) {
3425
- start = Math.max(arr.length + start, 0);
3426
- } else {
3427
- start = Math.min(start, arr.length - 1);
3428
- }
3429
- if (stop === undefined || stop === null) {
3430
- stop = step && step < 0 ? -1 : arr.length;
3431
- } else if (stop < 0) {
3432
- stop = Math.max(arr.length + stop, -1);
3433
- } else {
3434
- stop = Math.min(stop, arr.length);
3435
- }
3436
-
3437
- // Handle step value
3438
- if (step === 0) {
3439
- return [];
3440
- }
3441
- if (!step) {
3442
- step = 1;
3443
- }
3444
-
3445
- // Perform the slice
3446
- const slicedArray = [];
3404
+ const len = arr.length;
3405
+ step = step ?? 1;
3406
+ if (step === 0 || !len) return [];
3407
+ const result = [];
3408
+ let i;
3447
3409
  if (step > 0) {
3448
- for (let i = start; i < stop; i += step) {
3449
- slicedArray.push([i, arr[i]]);
3410
+ i = this.normalizeIndex(start, 0, len, step);
3411
+ const stop_ = this.normalizeIndex(stop, len, len, step);
3412
+ for (; i < stop_; i += step) {
3413
+ result.push([i, arr[i]]);
3450
3414
  }
3451
3415
  } else {
3452
- for (let i = start; i > stop; i += step) {
3453
- slicedArray.push([i, arr[i]]);
3416
+ i = this.normalizeIndex(start, len - 1, len, step);
3417
+ const stop_ = this.normalizeIndex(stop, -1, len, step);
3418
+ for (; i > stop_; i += step) {
3419
+ result.push([i, arr[i]]);
3454
3420
  }
3455
3421
  }
3456
- return slicedArray;
3422
+ return result;
3457
3423
  }
3458
-
3459
- // eslint-disable-next-line sonarjs/cognitive-complexity
3460
3424
  *lazySlice(arr, start, stop, step) {
3461
- if (!arr.length) return;
3462
-
3463
- // Handle negative and undefined start values
3464
- if (start === undefined || start === null) {
3465
- start = step && step < 0 ? arr.length - 1 : 0;
3466
- } else if (start < 0) {
3467
- start = Math.max(arr.length + start, 0);
3468
- } else {
3469
- start = Math.min(start, arr.length - 1);
3470
- }
3471
-
3472
- // Handle negative and undefined stop values
3473
- if (stop === undefined || stop === null) {
3474
- stop = step && step < 0 ? -1 : arr.length;
3475
- } else if (stop < 0) {
3476
- stop = Math.max(arr.length + stop, -1);
3477
- } else {
3478
- stop = Math.min(stop, arr.length);
3479
- }
3480
-
3481
- // Perform the slice
3482
- if (step === undefined) {
3483
- // Default to a step of 1
3484
- for (let i = start; i < stop; i += 1) {
3485
- yield [i, arr[i]];
3486
- }
3487
- } else if (step > 0) {
3488
- for (let i = start; i < stop; i += step) {
3425
+ const len = arr.length;
3426
+ step = step ?? 1;
3427
+ if (step === 0 || !len) return [];
3428
+ let i;
3429
+ if (step > 0) {
3430
+ i = this.normalizeIndex(start, 0, len, step);
3431
+ const stop_ = this.normalizeIndex(stop, len, len, step);
3432
+ for (; i < stop_; i += step) {
3489
3433
  yield [i, arr[i]];
3490
3434
  }
3491
- } else if (step < 0) {
3492
- for (let i = start; i > stop; i += step) {
3435
+ } else {
3436
+ i = this.normalizeIndex(start, len - 1, len, step);
3437
+ const stop_ = this.normalizeIndex(stop, -1, len, step);
3438
+ for (; i > stop_; i += step) {
3493
3439
  yield [i, arr[i]];
3494
3440
  }
3495
3441
  }
3496
3442
  }
3443
+ normalizeIndex(n, defaultValue, len, step) {
3444
+ if (n === undefined) return defaultValue;
3445
+ if (n < 0) return step < 0 ? Math.max(n + len, -1) : Math.max(n + len, 0);
3446
+ return step < 0 ? Math.min(n, len - 1) : Math.min(n, len);
3447
+ }
3497
3448
  }
3498
3449
  class WildcardSelector extends JSONPathSelector {
3499
3450
  constructor(environment, token) {
@@ -3693,53 +3644,45 @@ class DescendantSegment extends JSONPathSegment {
3693
3644
  toString(options) {
3694
3645
  return `..[${this.selectors.map(s => s.toString(options)).join(", ")}]`;
3695
3646
  }
3696
- visit(node) {
3697
- var _this = this;
3698
- let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
3699
- return function* () {
3700
- if (depth >= _this.environment.maxRecursionDepth) {
3701
- throw new JSONPathRecursionLimitError("recursion limit reached", _this.token);
3647
+ *visit(node, depth = 1) {
3648
+ if (depth >= this.environment.maxRecursionDepth) {
3649
+ throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
3650
+ }
3651
+ yield node;
3652
+ if (isArray(node.value)) {
3653
+ for (let i = 0; i < node.value.length; i++) {
3654
+ const _node = new JSONPathNode(node.value[i], node.location.concat(i), node.root);
3655
+ yield* this.visit(_node, depth + 1);
3656
+ }
3657
+ } else if (isObject(node.value)) {
3658
+ for (const [key, value] of this.environment.entries(node.value)) {
3659
+ const _node = new JSONPathNode(value, node.location.concat(key), node.root);
3660
+ yield* this.visit(_node, depth + 1);
3702
3661
  }
3662
+ }
3663
+ }
3664
+ *nondeterministicVisit(root, depth = 1) {
3665
+ let queue = Array.from(this.nondeterministicChildren(root)).map(node => [node, depth]);
3666
+ yield root;
3667
+ while (queue.length) {
3668
+ const [node, _depth] = queue.shift();
3703
3669
  yield node;
3704
- if (isArray(node.value)) {
3705
- for (let i = 0; i < node.value.length; i++) {
3706
- const _node = new JSONPathNode(node.value[i], node.location.concat(i), node.root);
3707
- yield* _this.visit(_node, depth + 1);
3708
- }
3709
- } else if (isObject(node.value)) {
3710
- for (const [key, value] of _this.environment.entries(node.value)) {
3711
- const _node = new JSONPathNode(value, node.location.concat(key), node.root);
3712
- yield* _this.visit(_node, depth + 1);
3713
- }
3670
+ if (_depth >= this.environment.maxRecursionDepth) {
3671
+ throw new JSONPathRecursionLimitError("recursion limit reached", this.token);
3714
3672
  }
3715
- }();
3716
- }
3717
- nondeterministicVisit(root) {
3718
- var _this2 = this;
3719
- let depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
3720
- return function* () {
3721
- let queue = Array.from(_this2.nondeterministicChildren(root)).map(node => [node, depth]);
3722
- yield root;
3723
- while (queue.length) {
3724
- const [node, _depth] = queue.shift();
3725
- yield node;
3726
- if (_depth >= _this2.environment.maxRecursionDepth) {
3727
- throw new JSONPathRecursionLimitError("recursion limit reached", _this2.token);
3728
- }
3729
3673
 
3730
- // Visit child nodes now or queue them for later?
3731
- const visitChildren = Math.random() < 0.5;
3732
- for (const child of _this2.nondeterministicChildren(node)) {
3733
- if (visitChildren) {
3734
- yield child;
3735
- const grandchildren = Array.from(_this2.nondeterministicChildren(child)).map(n => [n, _depth + 2]);
3736
- queue = interleave(queue, grandchildren);
3737
- } else {
3738
- queue.push([child, _depth + 1]);
3739
- }
3674
+ // Visit child nodes now or queue them for later?
3675
+ const visitChildren = Math.random() < 0.5;
3676
+ for (const child of this.nondeterministicChildren(node)) {
3677
+ if (visitChildren) {
3678
+ yield child;
3679
+ const grandchildren = Array.from(this.nondeterministicChildren(child)).map(n => [n, _depth + 2]);
3680
+ queue = interleave(queue, grandchildren);
3681
+ } else {
3682
+ queue.push([child, _depth + 1]);
3740
3683
  }
3741
3684
  }
3742
- }();
3685
+ }
3743
3686
  }
3744
3687
  *nondeterministicChildren(node) {
3745
3688
  if (isString(node.value)) return;
@@ -4005,8 +3948,7 @@ class Parser {
4005
3948
  }
4006
3949
  return segments;
4007
3950
  }
4008
- parseQuery(stream) {
4009
- let inFilter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
3951
+ parseQuery(stream, inFilter = false) {
4010
3952
  const segments = [];
4011
3953
  loop: for (;;) {
4012
3954
  switch (stream.current.kind) {
@@ -4158,8 +4100,7 @@ class Parser {
4158
4100
  }
4159
4101
  return selectors;
4160
4102
  }
4161
- parseFilter(stream) {
4162
- let keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
4103
+ parseFilter(stream, keys = false) {
4163
4104
  const tok = stream.next();
4164
4105
  const expr = this.parseFilterExpression(stream);
4165
4106
  if (expr instanceof FunctionExtension) {
@@ -4273,8 +4214,7 @@ class Parser {
4273
4214
  stream.expect(TokenKind.RPAREN);
4274
4215
  return new FunctionExtension(tok, tok.value, this.environment.checkWellTypedness(tok, args));
4275
4216
  }
4276
- parseFilterExpression(stream) {
4277
- let precedence = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : PRECEDENCE_LOWEST;
4217
+ parseFilterExpression(stream, precedence = PRECEDENCE_LOWEST) {
4278
4218
  const func = this.tokenMap.get(stream.current.kind);
4279
4219
  if (!func) {
4280
4220
  let msg;
@@ -4344,7 +4284,6 @@ class Parser {
4344
4284
  rv.push(this.stringFromCodePoint(codepoint, token));
4345
4285
  break;
4346
4286
  default:
4347
- // TODO: This is unreachable. The lexer will catch unknown escape sequences.
4348
4287
  throw new JSONPathSyntaxError(`unknown escape sequence at index ${token.index + index - 1}`, token);
4349
4288
  }
4350
4289
  } else {
@@ -4524,12 +4463,11 @@ class JSONPathEnvironment {
4524
4463
  * A map of function names to objects implementing the {@link FilterFunction}
4525
4464
  * interface. You are free to set or delete custom filter functions directly.
4526
4465
  */
4527
- functionRegister = (() => new Map())();
4466
+ functionRegister = new Map();
4528
4467
  /**
4529
4468
  * @param options - Environment configuration options.
4530
4469
  */
4531
- constructor() {
4532
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
4470
+ constructor(options = {}) {
4533
4471
  this.strict = options.strict ?? true;
4534
4472
  this.maxIntIndex = options.maxIntIndex ?? Math.pow(2, 53) - 1;
4535
4473
  this.minIntIndex = options.maxIntIndex ?? -Math.pow(2, 53) + 1;
@@ -4674,11 +4612,10 @@ class JSONPathEnvironment {
4674
4612
  * value and it contains a property matching the second argument.
4675
4613
  */
4676
4614
  class Has {
4677
- argTypes = (() => [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType])();
4678
- returnType = (() => FunctionExpressionType.LogicalType)();
4615
+ argTypes = [FunctionExpressionType.ValueType, FunctionExpressionType.ValueType];
4616
+ returnType = FunctionExpressionType.LogicalType;
4679
4617
  #cache;
4680
- constructor() {
4681
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
4618
+ constructor(options = {}) {
4682
4619
  this.options = options;
4683
4620
  this.cacheSize = options.cacheSize ?? 10;
4684
4621
  this.throwErrors = options.throwErrors ?? false;
@@ -4873,31 +4810,7 @@ class OpAdd {
4873
4810
  this.value = value;
4874
4811
  }
4875
4812
  apply(value, index) {
4876
- const [parent, obj] = this.path.resolveWithParent(value);
4877
- if (parent === UNDEFINED) {
4878
- // Replace the root object.
4879
- return this.value;
4880
- }
4881
- const target = this.path.tokens.at(-1);
4882
- if (target === undefined) {
4883
- // this should not be possible
4884
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4885
- } else if (isArray(parent)) {
4886
- if (obj === UNDEFINED) {
4887
- if (target === "-") {
4888
- parent.push(this.value);
4889
- } else {
4890
- throw new JSONPatchError(`index out of range (${this.name}:${index})`);
4891
- }
4892
- } else {
4893
- parent.splice(Number(target), 0, this.value);
4894
- }
4895
- } else if (isObject(parent)) {
4896
- parent[target] = this.value;
4897
- } else {
4898
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4899
- }
4900
- return value;
4813
+ return add(value, this.path, this.value, this.name, index);
4901
4814
  }
4902
4815
  toObject() {
4903
4816
  return {
@@ -4917,27 +4830,7 @@ class OpRemove {
4917
4830
  this.path = path;
4918
4831
  }
4919
4832
  apply(value, index) {
4920
- const [parent, obj] = this.path.resolveWithParent(value);
4921
- if (parent === UNDEFINED) {
4922
- throw new JSONPatchError(`can't remove root (${this.name}:${index})`);
4923
- }
4924
- const target = this.path.tokens.at(-1);
4925
- if (target === undefined) {
4926
- // this should not be possible
4927
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4928
- } else if (isArray(parent)) {
4929
- if (obj === UNDEFINED) {
4930
- throw new JSONPatchError(`can't remove nonexistent item (${this.name}:${index})`);
4931
- }
4932
- parent.splice(Number(target), 1);
4933
- } else if (isObject(parent)) {
4934
- if (obj === UNDEFINED) {
4935
- throw new JSONPatchError(`can't remove nonexistent property (${this.name}:${index})`);
4936
- }
4937
- delete parent[target];
4938
- } else {
4939
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4940
- }
4833
+ remove(value, this.path, this.name, index);
4941
4834
  return value;
4942
4835
  }
4943
4836
  toObject() {
@@ -4973,15 +4866,16 @@ class OpReplace {
4973
4866
  throw new JSONPatchError(`can't replace nonexistent item (${this.name}:${index})`);
4974
4867
  }
4975
4868
  parent.splice(Number(target), 1, this.value);
4976
- } else if (isObject(parent)) {
4869
+ return value;
4870
+ }
4871
+ if (isObject(parent)) {
4977
4872
  if (obj === UNDEFINED) {
4978
4873
  throw new JSONPatchError(`can't replace nonexistent property (${this.name}:${index})`);
4979
4874
  }
4980
4875
  parent[target] = this.value;
4981
- } else {
4982
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4876
+ return value;
4983
4877
  }
4984
- return value;
4878
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4985
4879
  }
4986
4880
  toObject() {
4987
4881
  return {
@@ -5005,42 +4899,8 @@ class OpMove {
5005
4899
  if (this.path.isRelativeTo(this.from)) {
5006
4900
  throw new JSONPatchError(`can't move object to one of its own children (${this.name}:${index})`);
5007
4901
  }
5008
- const [sourceParent, sourceObj] = this.from.resolveWithParent(value);
5009
- if (sourceObj === UNDEFINED) {
5010
- throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
5011
- }
5012
- const sourceTarget = this.from.tokens.at(-1);
5013
- if (sourceTarget === undefined) {
5014
- // this should not be possible
5015
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5016
- }
5017
- if (isArray(sourceParent)) {
5018
- sourceParent.splice(Number(sourceTarget), 1);
5019
- } else if (isObject(sourceParent)) {
5020
- delete sourceParent[sourceTarget];
5021
- }
5022
- const [destParent, _] = this.path.resolveWithParent(value);
5023
- if (destParent === UNDEFINED) {
5024
- // move source to root
5025
- return sourceObj;
5026
- }
5027
- const destTarget = this.path.tokens.at(-1);
5028
- if (destTarget === undefined) {
5029
- // this should not be possible
5030
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5031
- }
5032
- if (isArray(destParent)) {
5033
- if (destTarget === "-") {
5034
- destParent.push(sourceObj);
5035
- } else {
5036
- destParent.splice(Number(destTarget), 0, sourceObj);
5037
- }
5038
- } else if (isObject(destParent)) {
5039
- destParent[destTarget] = sourceObj;
5040
- } else {
5041
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
5042
- }
5043
- return value;
4902
+ const obj = remove(value, this.from, this.name, index);
4903
+ return add(value, this.path, obj, this.name, index);
5044
4904
  }
5045
4905
  toObject() {
5046
4906
  return {
@@ -5065,28 +4925,7 @@ class OpCopy {
5065
4925
  if (sourceObj === UNDEFINED) {
5066
4926
  throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
5067
4927
  }
5068
- const [destParent] = this.path.resolveWithParent(value);
5069
- if (destParent === UNDEFINED) {
5070
- // copy source to root
5071
- return this.deepCopy(sourceObj);
5072
- }
5073
- const destTarget = this.path.tokens.at(-1);
5074
- if (destTarget === undefined) {
5075
- // this should not be possible
5076
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5077
- }
5078
- if (isArray(destParent)) {
5079
- if (destTarget === "-") {
5080
- destParent.push(this.deepCopy(sourceObj));
5081
- } else {
5082
- destParent.splice(Number(destTarget), 0, this.deepCopy(sourceObj));
5083
- }
5084
- } else if (isObject(destParent)) {
5085
- destParent[destTarget] = this.deepCopy(sourceObj);
5086
- } else {
5087
- throw new JSONPatchError(`unexpected operation on '${typeof destParent}' (${this.name}:${index})`);
5088
- }
5089
- return value;
4928
+ return add(value, this.path, sourceObj, this.name, index);
5090
4929
  }
5091
4930
 
5092
4931
  // eslint-disable-next-line sonarjs/no-identical-functions
@@ -5127,6 +4966,72 @@ class OpTest {
5127
4966
  }
5128
4967
  }
5129
4968
 
4969
+ /**
4970
+ * Add _value_ to _data_ at _path_.
4971
+ *
4972
+ * This is semantically the `add` operation, used by `OpAdd`, `OpMove` and
4973
+ * `OpCopy`.
4974
+ */
4975
+ function add(data, path, value, opLabel, opIndex) {
4976
+ const [parent, obj] = path.resolveWithParent(data);
4977
+ if (parent === UNDEFINED) {
4978
+ // Replace the root object.
4979
+ return value;
4980
+ }
4981
+ const target = path.tokens.at(-1);
4982
+ if (target === undefined) {
4983
+ throw new JSONPatchError(`unexpected operation on 'undefined' (${opLabel}:${opIndex})`);
4984
+ }
4985
+ if (isArray(parent)) {
4986
+ if (obj === UNDEFINED) {
4987
+ if (!(target === "-" || Number(target) === parent.length)) {
4988
+ throw new JSONPatchError(`index out of range (${opLabel}:${opIndex})`);
4989
+ }
4990
+ parent.push(value);
4991
+ return data;
4992
+ }
4993
+ parent.splice(Number(target), 0, value);
4994
+ return data;
4995
+ }
4996
+ if (isObject(parent)) {
4997
+ parent[target] = value;
4998
+ return data;
4999
+ }
5000
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${opLabel}:${opIndex})`);
5001
+ }
5002
+
5003
+ /**
5004
+ * Remove the element at _path_ from _data_ and return the removed value.
5005
+ *
5006
+ * This is semantically the `remove` operation used by `OpRemove` and
5007
+ * `OpMove`.
5008
+ */
5009
+ function remove(data, path, opLabel, opIndex) {
5010
+ const [parent, obj] = path.resolveWithParent(data);
5011
+ if (parent === UNDEFINED) {
5012
+ throw new JSONPatchError(`can't remove root (${opLabel}:${opIndex})`);
5013
+ }
5014
+ const target = path.tokens.at(-1);
5015
+ if (target === undefined) {
5016
+ throw new JSONPatchError(`unexpected operation on 'undefined' (${opLabel}:${opIndex})`);
5017
+ }
5018
+ if (isArray(parent)) {
5019
+ if (obj === UNDEFINED) {
5020
+ throw new JSONPatchError(`can't ${opLabel} nonexistent item (${opLabel}:${opIndex})`);
5021
+ }
5022
+ parent.splice(Number(target), 1);
5023
+ return obj;
5024
+ }
5025
+ if (isObject(parent)) {
5026
+ if (obj === UNDEFINED) {
5027
+ throw new JSONPatchError(`can't ${opLabel} nonexistent property (${opLabel}:${opIndex})`);
5028
+ }
5029
+ delete parent[target];
5030
+ return obj;
5031
+ }
5032
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${opLabel}:${opIndex})`);
5033
+ }
5034
+
5130
5035
  /**
5131
5036
  *
5132
5037
  */
@@ -5327,7 +5232,7 @@ var index = /*#__PURE__*/Object.freeze({
5327
5232
  apply: apply
5328
5233
  });
5329
5234
 
5330
- const version = "2.2.2";
5235
+ const version = "2.3.1";
5331
5236
 
5332
5237
  exports.DEFAULT_ENVIRONMENT = DEFAULT_ENVIRONMENT;
5333
5238
  exports.FunctionExpressionType = FunctionExpressionType;