json-p3 2.3.0 → 2.3.2

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.3.0
2
+ * json-p3 version 2.3.2
3
3
  * https://github.com/jg-rp/json-p3
4
4
  *
5
5
  * MIT License
@@ -3398,87 +3398,51 @@ class SliceSelector extends JSONPathSelector {
3398
3398
  }
3399
3399
  }
3400
3400
  }
3401
-
3402
- // eslint-disable-next-line sonarjs/cognitive-complexity
3403
3401
  slice(arr, start, stop, step) {
3404
- if (!arr.length) return [];
3405
-
3406
- // Handle negative start and stop values
3407
- if (start === undefined || start === null) {
3408
- start = step && step < 0 ? arr.length - 1 : 0;
3409
- } else if (start < 0) {
3410
- start = Math.max(arr.length + start, 0);
3411
- } else {
3412
- start = Math.min(start, arr.length - 1);
3413
- }
3414
- if (stop === undefined || stop === null) {
3415
- stop = step && step < 0 ? -1 : arr.length;
3416
- } else if (stop < 0) {
3417
- stop = Math.max(arr.length + stop, -1);
3418
- } else {
3419
- stop = Math.min(stop, arr.length);
3420
- }
3421
-
3422
- // Handle step value
3423
- if (step === 0) {
3424
- return [];
3425
- }
3426
- if (!step) {
3427
- step = 1;
3428
- }
3429
-
3430
- // Perform the slice
3431
- const slicedArray = [];
3402
+ const len = arr.length;
3403
+ step = step ?? 1;
3404
+ if (step === 0 || !len) return [];
3405
+ const result = [];
3406
+ let i;
3432
3407
  if (step > 0) {
3433
- for (let i = start; i < stop; i += step) {
3434
- slicedArray.push([i, arr[i]]);
3408
+ i = this.normalizeIndex(start, 0, len, step);
3409
+ const stop_ = this.normalizeIndex(stop, len, len, step);
3410
+ for (; i < stop_; i += step) {
3411
+ result.push([i, arr[i]]);
3435
3412
  }
3436
3413
  } else {
3437
- for (let i = start; i > stop; i += step) {
3438
- slicedArray.push([i, arr[i]]);
3414
+ i = this.normalizeIndex(start, len - 1, len, step);
3415
+ const stop_ = this.normalizeIndex(stop, -1, len, step);
3416
+ for (; i > stop_; i += step) {
3417
+ result.push([i, arr[i]]);
3439
3418
  }
3440
3419
  }
3441
- return slicedArray;
3420
+ return result;
3442
3421
  }
3443
-
3444
- // eslint-disable-next-line sonarjs/cognitive-complexity
3445
3422
  *lazySlice(arr, start, stop, step) {
3446
- if (!arr.length) return;
3447
-
3448
- // Handle negative and undefined start values
3449
- if (start === undefined || start === null) {
3450
- start = step && step < 0 ? arr.length - 1 : 0;
3451
- } else if (start < 0) {
3452
- start = Math.max(arr.length + start, 0);
3453
- } else {
3454
- start = Math.min(start, arr.length - 1);
3455
- }
3456
-
3457
- // Handle negative and undefined stop values
3458
- if (stop === undefined || stop === null) {
3459
- stop = step && step < 0 ? -1 : arr.length;
3460
- } else if (stop < 0) {
3461
- stop = Math.max(arr.length + stop, -1);
3462
- } else {
3463
- stop = Math.min(stop, arr.length);
3464
- }
3465
-
3466
- // Perform the slice
3467
- if (step === undefined) {
3468
- // Default to a step of 1
3469
- for (let i = start; i < stop; i += 1) {
3470
- yield [i, arr[i]];
3471
- }
3472
- } else if (step > 0) {
3473
- for (let i = start; i < stop; i += step) {
3423
+ const len = arr.length;
3424
+ step = step ?? 1;
3425
+ if (step === 0 || !len) return [];
3426
+ let i;
3427
+ if (step > 0) {
3428
+ i = this.normalizeIndex(start, 0, len, step);
3429
+ const stop_ = this.normalizeIndex(stop, len, len, step);
3430
+ for (; i < stop_; i += step) {
3474
3431
  yield [i, arr[i]];
3475
3432
  }
3476
- } else if (step < 0) {
3477
- for (let i = start; i > stop; i += step) {
3433
+ } else {
3434
+ i = this.normalizeIndex(start, len - 1, len, step);
3435
+ const stop_ = this.normalizeIndex(stop, -1, len, step);
3436
+ for (; i > stop_; i += step) {
3478
3437
  yield [i, arr[i]];
3479
3438
  }
3480
3439
  }
3481
3440
  }
3441
+ normalizeIndex(n, defaultValue, len, step) {
3442
+ if (n === undefined) return defaultValue;
3443
+ if (n < 0) return step < 0 ? Math.max(n + len, -1) : Math.max(n + len, 0);
3444
+ return step < 0 ? Math.min(n, len - 1) : Math.min(n, len);
3445
+ }
3482
3446
  }
3483
3447
  class WildcardSelector extends JSONPathSelector {
3484
3448
  constructor(environment, token) {
@@ -3625,7 +3589,7 @@ class ChildSegment extends JSONPathSegment {
3625
3589
  const rv = [];
3626
3590
  for (const node of nodes) {
3627
3591
  for (const selector of this.selectors) {
3628
- rv.push(...selector.resolve(node));
3592
+ for (const match of selector.resolve(node)) rv.push(match);
3629
3593
  }
3630
3594
  }
3631
3595
  return rv;
@@ -3660,7 +3624,7 @@ class DescendantSegment extends JSONPathSegment {
3660
3624
  for (const node of nodes) {
3661
3625
  for (const _node of visitor(node)) {
3662
3626
  for (const selector of this.selectors) {
3663
- rv.push(...selector.resolve(_node));
3627
+ for (const match of selector.resolve(_node)) rv.push(match);
3664
3628
  }
3665
3629
  }
3666
3630
  }
@@ -4844,31 +4808,7 @@ class OpAdd {
4844
4808
  this.value = value;
4845
4809
  }
4846
4810
  apply(value, index) {
4847
- const [parent, obj] = this.path.resolveWithParent(value);
4848
- if (parent === UNDEFINED) {
4849
- // Replace the root object.
4850
- return this.value;
4851
- }
4852
- const target = this.path.tokens.at(-1);
4853
- if (target === undefined) {
4854
- // this should not be possible
4855
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4856
- } else if (isArray(parent)) {
4857
- if (obj === UNDEFINED) {
4858
- if (target === "-") {
4859
- parent.push(this.value);
4860
- } else {
4861
- throw new JSONPatchError(`index out of range (${this.name}:${index})`);
4862
- }
4863
- } else {
4864
- parent.splice(Number(target), 0, this.value);
4865
- }
4866
- } else if (isObject(parent)) {
4867
- parent[target] = this.value;
4868
- } else {
4869
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4870
- }
4871
- return value;
4811
+ return add(value, this.path, this.value, this.name, index);
4872
4812
  }
4873
4813
  toObject() {
4874
4814
  return {
@@ -4888,27 +4828,7 @@ class OpRemove {
4888
4828
  this.path = path;
4889
4829
  }
4890
4830
  apply(value, index) {
4891
- const [parent, obj] = this.path.resolveWithParent(value);
4892
- if (parent === UNDEFINED) {
4893
- throw new JSONPatchError(`can't remove root (${this.name}:${index})`);
4894
- }
4895
- const target = this.path.tokens.at(-1);
4896
- if (target === undefined) {
4897
- // this should not be possible
4898
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4899
- } else if (isArray(parent)) {
4900
- if (obj === UNDEFINED) {
4901
- throw new JSONPatchError(`can't remove nonexistent item (${this.name}:${index})`);
4902
- }
4903
- parent.splice(Number(target), 1);
4904
- } else if (isObject(parent)) {
4905
- if (obj === UNDEFINED) {
4906
- throw new JSONPatchError(`can't remove nonexistent property (${this.name}:${index})`);
4907
- }
4908
- delete parent[target];
4909
- } else {
4910
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4911
- }
4831
+ remove(value, this.path, this.name, index);
4912
4832
  return value;
4913
4833
  }
4914
4834
  toObject() {
@@ -4944,15 +4864,16 @@ class OpReplace {
4944
4864
  throw new JSONPatchError(`can't replace nonexistent item (${this.name}:${index})`);
4945
4865
  }
4946
4866
  parent.splice(Number(target), 1, this.value);
4947
- } else if (isObject(parent)) {
4867
+ return value;
4868
+ }
4869
+ if (isObject(parent)) {
4948
4870
  if (obj === UNDEFINED) {
4949
4871
  throw new JSONPatchError(`can't replace nonexistent property (${this.name}:${index})`);
4950
4872
  }
4951
4873
  parent[target] = this.value;
4952
- } else {
4953
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4874
+ return value;
4954
4875
  }
4955
- return value;
4876
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4956
4877
  }
4957
4878
  toObject() {
4958
4879
  return {
@@ -4976,42 +4897,8 @@ class OpMove {
4976
4897
  if (this.path.isRelativeTo(this.from)) {
4977
4898
  throw new JSONPatchError(`can't move object to one of its own children (${this.name}:${index})`);
4978
4899
  }
4979
- const [sourceParent, sourceObj] = this.from.resolveWithParent(value);
4980
- if (sourceObj === UNDEFINED) {
4981
- throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
4982
- }
4983
- const sourceTarget = this.from.tokens.at(-1);
4984
- if (sourceTarget === undefined) {
4985
- // this should not be possible
4986
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4987
- }
4988
- if (isArray(sourceParent)) {
4989
- sourceParent.splice(Number(sourceTarget), 1);
4990
- } else if (isObject(sourceParent)) {
4991
- delete sourceParent[sourceTarget];
4992
- }
4993
- const [destParent, _] = this.path.resolveWithParent(value);
4994
- if (destParent === UNDEFINED) {
4995
- // move source to root
4996
- return sourceObj;
4997
- }
4998
- const destTarget = this.path.tokens.at(-1);
4999
- if (destTarget === undefined) {
5000
- // this should not be possible
5001
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5002
- }
5003
- if (isArray(destParent)) {
5004
- if (destTarget === "-") {
5005
- destParent.push(sourceObj);
5006
- } else {
5007
- destParent.splice(Number(destTarget), 0, sourceObj);
5008
- }
5009
- } else if (isObject(destParent)) {
5010
- destParent[destTarget] = sourceObj;
5011
- } else {
5012
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
5013
- }
5014
- return value;
4900
+ const obj = remove(value, this.from, this.name, index);
4901
+ return add(value, this.path, obj, this.name, index);
5015
4902
  }
5016
4903
  toObject() {
5017
4904
  return {
@@ -5036,28 +4923,7 @@ class OpCopy {
5036
4923
  if (sourceObj === UNDEFINED) {
5037
4924
  throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
5038
4925
  }
5039
- const [destParent] = this.path.resolveWithParent(value);
5040
- if (destParent === UNDEFINED) {
5041
- // copy source to root
5042
- return this.deepCopy(sourceObj);
5043
- }
5044
- const destTarget = this.path.tokens.at(-1);
5045
- if (destTarget === undefined) {
5046
- // this should not be possible
5047
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5048
- }
5049
- if (isArray(destParent)) {
5050
- if (destTarget === "-") {
5051
- destParent.push(this.deepCopy(sourceObj));
5052
- } else {
5053
- destParent.splice(Number(destTarget), 0, this.deepCopy(sourceObj));
5054
- }
5055
- } else if (isObject(destParent)) {
5056
- destParent[destTarget] = this.deepCopy(sourceObj);
5057
- } else {
5058
- throw new JSONPatchError(`unexpected operation on '${typeof destParent}' (${this.name}:${index})`);
5059
- }
5060
- return value;
4926
+ return add(value, this.path, sourceObj, this.name, index);
5061
4927
  }
5062
4928
 
5063
4929
  // eslint-disable-next-line sonarjs/no-identical-functions
@@ -5098,6 +4964,72 @@ class OpTest {
5098
4964
  }
5099
4965
  }
5100
4966
 
4967
+ /**
4968
+ * Add _value_ to _data_ at _path_.
4969
+ *
4970
+ * This is semantically the `add` operation, used by `OpAdd`, `OpMove` and
4971
+ * `OpCopy`.
4972
+ */
4973
+ function add(data, path, value, opLabel, opIndex) {
4974
+ const [parent, obj] = path.resolveWithParent(data);
4975
+ if (parent === UNDEFINED) {
4976
+ // Replace the root object.
4977
+ return value;
4978
+ }
4979
+ const target = path.tokens.at(-1);
4980
+ if (target === undefined) {
4981
+ throw new JSONPatchError(`unexpected operation on 'undefined' (${opLabel}:${opIndex})`);
4982
+ }
4983
+ if (isArray(parent)) {
4984
+ if (obj === UNDEFINED) {
4985
+ if (!(target === "-" || Number(target) === parent.length)) {
4986
+ throw new JSONPatchError(`index out of range (${opLabel}:${opIndex})`);
4987
+ }
4988
+ parent.push(value);
4989
+ return data;
4990
+ }
4991
+ parent.splice(Number(target), 0, value);
4992
+ return data;
4993
+ }
4994
+ if (isObject(parent)) {
4995
+ parent[target] = value;
4996
+ return data;
4997
+ }
4998
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${opLabel}:${opIndex})`);
4999
+ }
5000
+
5001
+ /**
5002
+ * Remove the element at _path_ from _data_ and return the removed value.
5003
+ *
5004
+ * This is semantically the `remove` operation used by `OpRemove` and
5005
+ * `OpMove`.
5006
+ */
5007
+ function remove(data, path, opLabel, opIndex) {
5008
+ const [parent, obj] = path.resolveWithParent(data);
5009
+ if (parent === UNDEFINED) {
5010
+ throw new JSONPatchError(`can't remove root (${opLabel}:${opIndex})`);
5011
+ }
5012
+ const target = path.tokens.at(-1);
5013
+ if (target === undefined) {
5014
+ throw new JSONPatchError(`unexpected operation on 'undefined' (${opLabel}:${opIndex})`);
5015
+ }
5016
+ if (isArray(parent)) {
5017
+ if (obj === UNDEFINED) {
5018
+ throw new JSONPatchError(`can't ${opLabel} nonexistent item (${opLabel}:${opIndex})`);
5019
+ }
5020
+ parent.splice(Number(target), 1);
5021
+ return obj;
5022
+ }
5023
+ if (isObject(parent)) {
5024
+ if (obj === UNDEFINED) {
5025
+ throw new JSONPatchError(`can't ${opLabel} nonexistent property (${opLabel}:${opIndex})`);
5026
+ }
5027
+ delete parent[target];
5028
+ return obj;
5029
+ }
5030
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${opLabel}:${opIndex})`);
5031
+ }
5032
+
5101
5033
  /**
5102
5034
  *
5103
5035
  */
@@ -5298,6 +5230,6 @@ var index = /*#__PURE__*/Object.freeze({
5298
5230
  apply: apply
5299
5231
  });
5300
5232
 
5301
- const version = "2.3.0";
5233
+ const version = "2.3.2";
5302
5234
 
5303
5235
  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 };