json-p3 2.3.0 → 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.
Files changed (43) hide show
  1. package/dist/deep_equals.d.ts +10 -0
  2. package/dist/index.d.ts +10 -0
  3. package/dist/json-p3.browser.js +109 -177
  4. package/dist/json-p3.browser.min.js +1 -1
  5. package/dist/json-p3.browser.min.js.map +1 -1
  6. package/dist/json-p3.cjs.js +109 -177
  7. package/dist/json-p3.esm.js +109 -177
  8. package/dist/json-p3.iife.min.js +1 -1
  9. package/dist/json-p3.iife.min.js.map +1 -1
  10. package/dist/patch/errors.d.ts +11 -0
  11. package/dist/patch/index.d.ts +11 -0
  12. package/dist/patch/patch.d.ts +161 -0
  13. package/dist/path/environment.d.ts +165 -0
  14. package/dist/path/errors.d.ts +65 -0
  15. package/dist/path/expression.d.ts +101 -0
  16. package/dist/path/extra/expression.d.ts +6 -0
  17. package/dist/path/extra/index.d.ts +0 -0
  18. package/dist/path/extra/selectors.d.ts +35 -0
  19. package/dist/path/functions/count.d.ts +7 -0
  20. package/dist/path/functions/function.d.ts +26 -0
  21. package/dist/path/functions/has.d.ts +44 -0
  22. package/dist/path/functions/index.d.ts +11 -0
  23. package/dist/path/functions/length.d.ts +7 -0
  24. package/dist/path/functions/match.d.ts +33 -0
  25. package/dist/path/functions/pattern.d.ts +2 -0
  26. package/dist/path/functions/search.d.ts +34 -0
  27. package/dist/path/functions/value.d.ts +7 -0
  28. package/dist/path/index.d.ts +76 -0
  29. package/dist/path/lex.d.ts +74 -0
  30. package/dist/path/lru_cache.d.ts +10 -0
  31. package/dist/path/node.d.ts +84 -0
  32. package/dist/path/parse.d.ts +61 -0
  33. package/dist/path/path.d.ts +42 -0
  34. package/dist/path/segments.d.ts +39 -0
  35. package/dist/path/selectors.d.ts +86 -0
  36. package/dist/path/serialize.d.ts +6 -0
  37. package/dist/path/token.d.ts +70 -0
  38. package/dist/path/types.d.ts +46 -0
  39. package/dist/pointer/errors.d.ts +42 -0
  40. package/dist/pointer/index.d.ts +24 -0
  41. package/dist/pointer/pointer.d.ts +106 -0
  42. package/dist/types.d.ts +25 -0
  43. package/package.json +3 -3
@@ -1,5 +1,5 @@
1
1
  /*
2
- * json-p3 version 2.3.0
2
+ * json-p3 version 2.3.1
3
3
  * https://github.com/jg-rp/json-p3
4
4
  *
5
5
  * MIT License
@@ -3400,87 +3400,51 @@ class SliceSelector extends JSONPathSelector {
3400
3400
  }
3401
3401
  }
3402
3402
  }
3403
-
3404
- // eslint-disable-next-line sonarjs/cognitive-complexity
3405
3403
  slice(arr, start, stop, step) {
3406
- if (!arr.length) return [];
3407
-
3408
- // Handle negative start and stop values
3409
- if (start === undefined || start === null) {
3410
- start = step && step < 0 ? arr.length - 1 : 0;
3411
- } else if (start < 0) {
3412
- start = Math.max(arr.length + start, 0);
3413
- } else {
3414
- start = Math.min(start, arr.length - 1);
3415
- }
3416
- if (stop === undefined || stop === null) {
3417
- stop = step && step < 0 ? -1 : arr.length;
3418
- } else if (stop < 0) {
3419
- stop = Math.max(arr.length + stop, -1);
3420
- } else {
3421
- stop = Math.min(stop, arr.length);
3422
- }
3423
-
3424
- // Handle step value
3425
- if (step === 0) {
3426
- return [];
3427
- }
3428
- if (!step) {
3429
- step = 1;
3430
- }
3431
-
3432
- // Perform the slice
3433
- const slicedArray = [];
3404
+ const len = arr.length;
3405
+ step = step ?? 1;
3406
+ if (step === 0 || !len) return [];
3407
+ const result = [];
3408
+ let i;
3434
3409
  if (step > 0) {
3435
- for (let i = start; i < stop; i += step) {
3436
- 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]]);
3437
3414
  }
3438
3415
  } else {
3439
- for (let i = start; i > stop; i += step) {
3440
- 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]]);
3441
3420
  }
3442
3421
  }
3443
- return slicedArray;
3422
+ return result;
3444
3423
  }
3445
-
3446
- // eslint-disable-next-line sonarjs/cognitive-complexity
3447
3424
  *lazySlice(arr, start, stop, step) {
3448
- if (!arr.length) return;
3449
-
3450
- // Handle negative and undefined start values
3451
- if (start === undefined || start === null) {
3452
- start = step && step < 0 ? arr.length - 1 : 0;
3453
- } else if (start < 0) {
3454
- start = Math.max(arr.length + start, 0);
3455
- } else {
3456
- start = Math.min(start, arr.length - 1);
3457
- }
3458
-
3459
- // Handle negative and undefined stop values
3460
- if (stop === undefined || stop === null) {
3461
- stop = step && step < 0 ? -1 : arr.length;
3462
- } else if (stop < 0) {
3463
- stop = Math.max(arr.length + stop, -1);
3464
- } else {
3465
- stop = Math.min(stop, arr.length);
3466
- }
3467
-
3468
- // Perform the slice
3469
- if (step === undefined) {
3470
- // Default to a step of 1
3471
- for (let i = start; i < stop; i += 1) {
3472
- yield [i, arr[i]];
3473
- }
3474
- } else if (step > 0) {
3475
- 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) {
3476
3433
  yield [i, arr[i]];
3477
3434
  }
3478
- } else if (step < 0) {
3479
- 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) {
3480
3439
  yield [i, arr[i]];
3481
3440
  }
3482
3441
  }
3483
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
+ }
3484
3448
  }
3485
3449
  class WildcardSelector extends JSONPathSelector {
3486
3450
  constructor(environment, token) {
@@ -4846,31 +4810,7 @@ class OpAdd {
4846
4810
  this.value = value;
4847
4811
  }
4848
4812
  apply(value, index) {
4849
- const [parent, obj] = this.path.resolveWithParent(value);
4850
- if (parent === UNDEFINED) {
4851
- // Replace the root object.
4852
- return this.value;
4853
- }
4854
- const target = this.path.tokens.at(-1);
4855
- if (target === undefined) {
4856
- // this should not be possible
4857
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4858
- } else if (isArray(parent)) {
4859
- if (obj === UNDEFINED) {
4860
- if (target === "-") {
4861
- parent.push(this.value);
4862
- } else {
4863
- throw new JSONPatchError(`index out of range (${this.name}:${index})`);
4864
- }
4865
- } else {
4866
- parent.splice(Number(target), 0, this.value);
4867
- }
4868
- } else if (isObject(parent)) {
4869
- parent[target] = this.value;
4870
- } else {
4871
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4872
- }
4873
- return value;
4813
+ return add(value, this.path, this.value, this.name, index);
4874
4814
  }
4875
4815
  toObject() {
4876
4816
  return {
@@ -4890,27 +4830,7 @@ class OpRemove {
4890
4830
  this.path = path;
4891
4831
  }
4892
4832
  apply(value, index) {
4893
- const [parent, obj] = this.path.resolveWithParent(value);
4894
- if (parent === UNDEFINED) {
4895
- throw new JSONPatchError(`can't remove root (${this.name}:${index})`);
4896
- }
4897
- const target = this.path.tokens.at(-1);
4898
- if (target === undefined) {
4899
- // this should not be possible
4900
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4901
- } else if (isArray(parent)) {
4902
- if (obj === UNDEFINED) {
4903
- throw new JSONPatchError(`can't remove nonexistent item (${this.name}:${index})`);
4904
- }
4905
- parent.splice(Number(target), 1);
4906
- } else if (isObject(parent)) {
4907
- if (obj === UNDEFINED) {
4908
- throw new JSONPatchError(`can't remove nonexistent property (${this.name}:${index})`);
4909
- }
4910
- delete parent[target];
4911
- } else {
4912
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4913
- }
4833
+ remove(value, this.path, this.name, index);
4914
4834
  return value;
4915
4835
  }
4916
4836
  toObject() {
@@ -4946,15 +4866,16 @@ class OpReplace {
4946
4866
  throw new JSONPatchError(`can't replace nonexistent item (${this.name}:${index})`);
4947
4867
  }
4948
4868
  parent.splice(Number(target), 1, this.value);
4949
- } else if (isObject(parent)) {
4869
+ return value;
4870
+ }
4871
+ if (isObject(parent)) {
4950
4872
  if (obj === UNDEFINED) {
4951
4873
  throw new JSONPatchError(`can't replace nonexistent property (${this.name}:${index})`);
4952
4874
  }
4953
4875
  parent[target] = this.value;
4954
- } else {
4955
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4876
+ return value;
4956
4877
  }
4957
- return value;
4878
+ throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
4958
4879
  }
4959
4880
  toObject() {
4960
4881
  return {
@@ -4978,42 +4899,8 @@ class OpMove {
4978
4899
  if (this.path.isRelativeTo(this.from)) {
4979
4900
  throw new JSONPatchError(`can't move object to one of its own children (${this.name}:${index})`);
4980
4901
  }
4981
- const [sourceParent, sourceObj] = this.from.resolveWithParent(value);
4982
- if (sourceObj === UNDEFINED) {
4983
- throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
4984
- }
4985
- const sourceTarget = this.from.tokens.at(-1);
4986
- if (sourceTarget === undefined) {
4987
- // this should not be possible
4988
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
4989
- }
4990
- if (isArray(sourceParent)) {
4991
- sourceParent.splice(Number(sourceTarget), 1);
4992
- } else if (isObject(sourceParent)) {
4993
- delete sourceParent[sourceTarget];
4994
- }
4995
- const [destParent, _] = this.path.resolveWithParent(value);
4996
- if (destParent === UNDEFINED) {
4997
- // move source to root
4998
- return sourceObj;
4999
- }
5000
- const destTarget = this.path.tokens.at(-1);
5001
- if (destTarget === undefined) {
5002
- // this should not be possible
5003
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5004
- }
5005
- if (isArray(destParent)) {
5006
- if (destTarget === "-") {
5007
- destParent.push(sourceObj);
5008
- } else {
5009
- destParent.splice(Number(destTarget), 0, sourceObj);
5010
- }
5011
- } else if (isObject(destParent)) {
5012
- destParent[destTarget] = sourceObj;
5013
- } else {
5014
- throw new JSONPatchError(`unexpected operation on '${typeof parent}' (${this.name}:${index})`);
5015
- }
5016
- return value;
4902
+ const obj = remove(value, this.from, this.name, index);
4903
+ return add(value, this.path, obj, this.name, index);
5017
4904
  }
5018
4905
  toObject() {
5019
4906
  return {
@@ -5038,28 +4925,7 @@ class OpCopy {
5038
4925
  if (sourceObj === UNDEFINED) {
5039
4926
  throw new JSONPatchError(`source object does not exist (${this.name}:${index})`);
5040
4927
  }
5041
- const [destParent] = this.path.resolveWithParent(value);
5042
- if (destParent === UNDEFINED) {
5043
- // copy source to root
5044
- return this.deepCopy(sourceObj);
5045
- }
5046
- const destTarget = this.path.tokens.at(-1);
5047
- if (destTarget === undefined) {
5048
- // this should not be possible
5049
- throw new JSONPatchError(`unexpected operation on 'undefined' (${this.name}:${index})`);
5050
- }
5051
- if (isArray(destParent)) {
5052
- if (destTarget === "-") {
5053
- destParent.push(this.deepCopy(sourceObj));
5054
- } else {
5055
- destParent.splice(Number(destTarget), 0, this.deepCopy(sourceObj));
5056
- }
5057
- } else if (isObject(destParent)) {
5058
- destParent[destTarget] = this.deepCopy(sourceObj);
5059
- } else {
5060
- throw new JSONPatchError(`unexpected operation on '${typeof destParent}' (${this.name}:${index})`);
5061
- }
5062
- return value;
4928
+ return add(value, this.path, sourceObj, this.name, index);
5063
4929
  }
5064
4930
 
5065
4931
  // eslint-disable-next-line sonarjs/no-identical-functions
@@ -5100,6 +4966,72 @@ class OpTest {
5100
4966
  }
5101
4967
  }
5102
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
+
5103
5035
  /**
5104
5036
  *
5105
5037
  */
@@ -5300,7 +5232,7 @@ var index = /*#__PURE__*/Object.freeze({
5300
5232
  apply: apply
5301
5233
  });
5302
5234
 
5303
- const version = "2.3.0";
5235
+ const version = "2.3.1";
5304
5236
 
5305
5237
  exports.DEFAULT_ENVIRONMENT = DEFAULT_ENVIRONMENT;
5306
5238
  exports.FunctionExpressionType = FunctionExpressionType;