@speclynx/apidom-json-pointer-relative 3.2.1 → 4.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/CHANGELOG.md CHANGED
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [4.0.0](https://github.com/speclynx/apidom/compare/v3.2.1...v4.0.0) (2026-03-11)
7
+
8
+ **Note:** Version bump only for package @speclynx/apidom-json-pointer-relative
9
+
6
10
  ## [3.2.1](https://github.com/speclynx/apidom/compare/v3.2.0...v3.2.1) (2026-03-09)
7
11
 
8
12
  **Note:** Version bump only for package @speclynx/apidom-json-pointer-relative
@@ -15693,7 +15693,9 @@ class Path {
15693
15693
  *
15694
15694
  * @example
15695
15695
  * // For a path to $.paths['/pets'].get in an OpenAPI document:
15696
+ * ```
15696
15697
  * path.getPathKeys(); // => ['paths', '/pets', 'get']
15698
+ * ```
15697
15699
  */
15698
15700
  getPathKeys() {
15699
15701
  const keys = [];
@@ -15728,18 +15730,22 @@ class Path {
15728
15730
  * or Normalized JSONPath like "$['paths']['/pets']['get']['responses']['200']"
15729
15731
  *
15730
15732
  * @example
15733
+ * ```
15731
15734
  * // JSON Pointer examples:
15732
15735
  * path.formatPath(); // "" (root)
15733
15736
  * path.formatPath(); // "/info"
15734
15737
  * path.formatPath(); // "/paths/~1pets/get"
15735
15738
  * path.formatPath(); // "/paths/~1users~1{id}/parameters/0"
15739
+ * ```
15736
15740
  *
15737
15741
  * @example
15742
+ * ```
15738
15743
  * // JSONPath examples:
15739
15744
  * path.formatPath('jsonpath'); // "$" (root)
15740
15745
  * path.formatPath('jsonpath'); // "$['info']"
15741
15746
  * path.formatPath('jsonpath'); // "$['paths']['/pets']['get']"
15742
15747
  * path.formatPath('jsonpath'); // "$['paths']['/users/{id}']['parameters'][0]"
15748
+ * ```
15743
15749
  */
15744
15750
  formatPath(pathFormat = 'jsonpointer') {
15745
15751
  const parts = this.getPathKeys();
@@ -15947,15 +15953,15 @@ __webpack_require__.r(__webpack_exports__);
15947
15953
  /* harmony import */ var _traversal_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2078);
15948
15954
 
15949
15955
  /**
15950
- * Finds all elements matching the predicate.
15956
+ * Finds all paths whose elements match the predicate.
15951
15957
  * @public
15952
15958
  */
15953
15959
  const filter = (element, predicate) => {
15954
15960
  const result = [];
15955
15961
  (0,_traversal_mjs__WEBPACK_IMPORTED_MODULE_0__.traverse)(element, {
15956
15962
  enter(path) {
15957
- if (predicate(path.node)) {
15958
- result.push(path.node);
15963
+ if (predicate(path)) {
15964
+ result.push(path);
15959
15965
  }
15960
15966
  }
15961
15967
  });
@@ -15980,7 +15986,7 @@ __webpack_require__.r(__webpack_exports__);
15980
15986
  * @public
15981
15987
  */
15982
15988
  /**
15983
- * Finds the most inner node at the given offset.
15989
+ * Finds the path of the most inner node at the given offset.
15984
15990
  * If includeRightBound is set, also finds nodes that end at the given offset.
15985
15991
  * @public
15986
15992
  */
@@ -16005,7 +16011,7 @@ const findAtOffset = (element, options) => {
16005
16011
  const endOffset = node.endOffset;
16006
16012
  const isWithinOffsetRange = offset >= startOffset && (offset < endOffset || includeRightBound && offset <= endOffset);
16007
16013
  if (isWithinOffsetRange) {
16008
- result.push(node);
16014
+ result.push(path);
16009
16015
  return; // push to stack and dive in
16010
16016
  }
16011
16017
  path.skip(); // skip entire sub-tree
@@ -16027,15 +16033,15 @@ __webpack_require__.r(__webpack_exports__);
16027
16033
  /* harmony import */ var _traversal_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2078);
16028
16034
 
16029
16035
  /**
16030
- * Find first element that satisfies the provided predicate.
16036
+ * Finds first path whose element satisfies the provided predicate.
16031
16037
  * @public
16032
16038
  */
16033
16039
  const find = (element, predicate) => {
16034
16040
  let result;
16035
16041
  (0,_traversal_mjs__WEBPACK_IMPORTED_MODULE_0__.traverse)(element, {
16036
16042
  enter(path) {
16037
- if (predicate(path.node)) {
16038
- result = path.node;
16043
+ if (predicate(path)) {
16044
+ result = path;
16039
16045
  path.stop();
16040
16046
  }
16041
16047
  }
@@ -16053,9 +16059,7 @@ __webpack_require__.r(__webpack_exports__);
16053
16059
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
16054
16060
  /* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
16055
16061
  /* harmony export */ });
16056
- /* harmony import */ var _speclynx_apidom_datamodel__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5162);
16057
- /* harmony import */ var _traversal_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2078);
16058
-
16062
+ /* harmony import */ var _traversal_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2078);
16059
16063
 
16060
16064
  /**
16061
16065
  * @public
@@ -16064,7 +16068,7 @@ __webpack_require__.r(__webpack_exports__);
16064
16068
  * @public
16065
16069
  */
16066
16070
  /**
16067
- * Executes the callback on this element and all descendants.
16071
+ * Executes the callback on this element's path and all descendant paths.
16068
16072
  * @public
16069
16073
  */
16070
16074
  const forEach = (element, options) => {
@@ -16072,15 +16076,15 @@ const forEach = (element, options) => {
16072
16076
  let predicate;
16073
16077
  if (typeof options === 'function') {
16074
16078
  callback = options;
16075
- predicate = _speclynx_apidom_datamodel__WEBPACK_IMPORTED_MODULE_0__.isElement;
16079
+ predicate = () => true;
16076
16080
  } else {
16077
16081
  callback = options.callback ?? (() => {});
16078
- predicate = options.predicate ?? _speclynx_apidom_datamodel__WEBPACK_IMPORTED_MODULE_0__.isElement;
16082
+ predicate = options.predicate ?? (() => true);
16079
16083
  }
16080
- (0,_traversal_mjs__WEBPACK_IMPORTED_MODULE_1__.traverse)(element, {
16084
+ (0,_traversal_mjs__WEBPACK_IMPORTED_MODULE_0__.traverse)(element, {
16081
16085
  enter(path) {
16082
- if (predicate(path.node)) {
16083
- callback(path.node);
16086
+ if (predicate(path)) {
16087
+ callback(path);
16084
16088
  }
16085
16089
  }
16086
16090
  });
@@ -16127,11 +16131,11 @@ __webpack_require__.r(__webpack_exports__);
16127
16131
  /* harmony import */ var _filter_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5247);
16128
16132
 
16129
16133
  /**
16130
- * Complement of filter. Finds all elements NOT matching the predicate.
16134
+ * Complement of filter. Finds all paths whose elements do NOT match the predicate.
16131
16135
  * @public
16132
16136
  */
16133
16137
  const reject = (element, predicate) => {
16134
- return (0,_filter_mjs__WEBPACK_IMPORTED_MODULE_0__["default"])(element, el => !predicate(el));
16138
+ return (0,_filter_mjs__WEBPACK_IMPORTED_MODULE_0__["default"])(element, path => !predicate(path));
16135
16139
  };
16136
16140
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (reject);
16137
16141
 
@@ -16147,7 +16151,7 @@ __webpack_require__.r(__webpack_exports__);
16147
16151
  /* harmony import */ var _find_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4860);
16148
16152
 
16149
16153
  /**
16150
- * Tests whether at least one element passes the predicate.
16154
+ * Tests whether at least one path's element passes the predicate.
16151
16155
  * @public
16152
16156
  */
16153
16157
  const some = (element, predicate) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speclynx/apidom-json-pointer-relative",
3
- "version": "3.2.1",
3
+ "version": "4.0.0",
4
4
  "description": "Evaluate Relative JSON Pointer expressions against ApiDOM.",
5
5
  "keywords": [
6
6
  "apidom",
@@ -54,10 +54,10 @@
54
54
  "license": "Apache-2.0",
55
55
  "dependencies": {
56
56
  "@babel/runtime-corejs3": "^7.28.4",
57
- "@speclynx/apidom-datamodel": "3.2.1",
58
- "@speclynx/apidom-error": "3.2.1",
59
- "@speclynx/apidom-json-pointer": "3.2.1",
60
- "@speclynx/apidom-traverse": "3.2.1",
57
+ "@speclynx/apidom-datamodel": "4.0.0",
58
+ "@speclynx/apidom-error": "4.0.0",
59
+ "@speclynx/apidom-json-pointer": "4.0.0",
60
+ "@speclynx/apidom-traverse": "4.0.0",
61
61
  "ramda": "~0.32.0",
62
62
  "ramda-adjunct": "^6.0.0"
63
63
  },
@@ -71,5 +71,5 @@
71
71
  "README.md",
72
72
  "CHANGELOG.md"
73
73
  ],
74
- "gitHead": "83a1e97089ef29134e41ff7cacd351973114ea5b"
74
+ "gitHead": "384ff82a1bb68d015fd6801de641898b4b582876"
75
75
  }
package/src/compile.cjs DELETED
@@ -1,37 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.default = void 0;
6
- var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
7
- var _CompilationRelativeJsonPointerError = _interopRequireDefault(require("./errors/CompilationRelativeJsonPointerError.cjs"));
8
- /**
9
- * @public
10
- */
11
- const compile = relativeJsonPointer => {
12
- try {
13
- let relativePointer = '';
14
-
15
- // non-negative-integer
16
- relativePointer += String(relativeJsonPointer.nonNegativeIntegerPrefix);
17
-
18
- // index-manipulation
19
- if (typeof relativeJsonPointer.indexManipulation === 'number') {
20
- relativePointer += String(relativeJsonPointer.indexManipulation);
21
- }
22
- if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
23
- // <json-pointer>
24
- relativePointer += (0, _apidomJsonPointer.compile)(relativeJsonPointer.jsonPointerTokens);
25
- } else if (relativeJsonPointer.hashCharacter) {
26
- // "#"
27
- relativePointer += '#';
28
- }
29
- return relativePointer;
30
- } catch (error) {
31
- throw new _CompilationRelativeJsonPointerError.default('Relative JSON Pointer compilation encountered an error.', {
32
- relativePointer: relativeJsonPointer,
33
- cause: error
34
- });
35
- }
36
- };
37
- var _default = exports.default = compile;
package/src/compile.mjs DELETED
@@ -1,32 +0,0 @@
1
- import { compile as compileJsonPointer } from '@speclynx/apidom-json-pointer';
2
- import CompilationRelativeJsonPointerError from "./errors/CompilationRelativeJsonPointerError.mjs";
3
- /**
4
- * @public
5
- */
6
- const compile = relativeJsonPointer => {
7
- try {
8
- let relativePointer = '';
9
-
10
- // non-negative-integer
11
- relativePointer += String(relativeJsonPointer.nonNegativeIntegerPrefix);
12
-
13
- // index-manipulation
14
- if (typeof relativeJsonPointer.indexManipulation === 'number') {
15
- relativePointer += String(relativeJsonPointer.indexManipulation);
16
- }
17
- if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
18
- // <json-pointer>
19
- relativePointer += compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
20
- } else if (relativeJsonPointer.hashCharacter) {
21
- // "#"
22
- relativePointer += '#';
23
- }
24
- return relativePointer;
25
- } catch (error) {
26
- throw new CompilationRelativeJsonPointerError('Relative JSON Pointer compilation encountered an error.', {
27
- relativePointer: relativeJsonPointer,
28
- cause: error
29
- });
30
- }
31
- };
32
- export default compile;
@@ -1,31 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.default = void 0;
6
- var _RelativeJsonPointerError = _interopRequireDefault(require("./RelativeJsonPointerError.cjs"));
7
- /**
8
- * @public
9
- */
10
-
11
- /**
12
- * @public
13
- */
14
- class CompilationRelativeJsonPointerError extends _RelativeJsonPointerError.default {
15
- nonNegativeIntegerPrefix;
16
- indexManipulation;
17
- jsonPointerTokens;
18
- hashCharacter;
19
- constructor(message, structuredOptions) {
20
- super(message, structuredOptions);
21
- if (typeof structuredOptions !== 'undefined') {
22
- this.nonNegativeIntegerPrefix = structuredOptions.relativePointer.nonNegativeIntegerPrefix;
23
- this.indexManipulation = structuredOptions.relativePointer.indexManipulation;
24
- this.hashCharacter = structuredOptions.relativePointer.hashCharacter;
25
- if (Array.isArray(structuredOptions.relativePointer.jsonPointerTokens)) {
26
- this.jsonPointerTokens = [...structuredOptions.relativePointer.jsonPointerTokens];
27
- }
28
- }
29
- }
30
- }
31
- var _default = exports.default = CompilationRelativeJsonPointerError;
@@ -1,25 +0,0 @@
1
- import RelativeJsonPointerError from "./RelativeJsonPointerError.mjs";
2
- /**
3
- * @public
4
- */
5
- /**
6
- * @public
7
- */
8
- class CompilationRelativeJsonPointerError extends RelativeJsonPointerError {
9
- nonNegativeIntegerPrefix;
10
- indexManipulation;
11
- jsonPointerTokens;
12
- hashCharacter;
13
- constructor(message, structuredOptions) {
14
- super(message, structuredOptions);
15
- if (typeof structuredOptions !== 'undefined') {
16
- this.nonNegativeIntegerPrefix = structuredOptions.relativePointer.nonNegativeIntegerPrefix;
17
- this.indexManipulation = structuredOptions.relativePointer.indexManipulation;
18
- this.hashCharacter = structuredOptions.relativePointer.hashCharacter;
19
- if (Array.isArray(structuredOptions.relativePointer.jsonPointerTokens)) {
20
- this.jsonPointerTokens = [...structuredOptions.relativePointer.jsonPointerTokens];
21
- }
22
- }
23
- }
24
- }
25
- export default CompilationRelativeJsonPointerError;
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.default = void 0;
6
- var _RelativeJsonPointerError = _interopRequireDefault(require("./RelativeJsonPointerError.cjs"));
7
- /**
8
- * @public
9
- */
10
-
11
- /**
12
- * @public
13
- */
14
- class EvaluationRelativeJsonPointerError extends _RelativeJsonPointerError.default {
15
- relativePointer;
16
- currentElement;
17
- rootElement;
18
- cursorElement;
19
- constructor(message, structuredOptions) {
20
- super(message, structuredOptions);
21
- if (typeof structuredOptions !== 'undefined') {
22
- this.relativePointer = structuredOptions.relativePointer;
23
- this.currentElement = structuredOptions.currentElement;
24
- this.rootElement = structuredOptions.rootElement;
25
- this.cursorElement = structuredOptions.cursorElement;
26
- }
27
- }
28
- }
29
- var _default = exports.default = EvaluationRelativeJsonPointerError;
@@ -1,23 +0,0 @@
1
- import RelativeJsonPointerError from "./RelativeJsonPointerError.mjs";
2
- /**
3
- * @public
4
- */
5
- /**
6
- * @public
7
- */
8
- class EvaluationRelativeJsonPointerError extends RelativeJsonPointerError {
9
- relativePointer;
10
- currentElement;
11
- rootElement;
12
- cursorElement;
13
- constructor(message, structuredOptions) {
14
- super(message, structuredOptions);
15
- if (typeof structuredOptions !== 'undefined') {
16
- this.relativePointer = structuredOptions.relativePointer;
17
- this.currentElement = structuredOptions.currentElement;
18
- this.rootElement = structuredOptions.rootElement;
19
- this.cursorElement = structuredOptions.cursorElement;
20
- }
21
- }
22
- }
23
- export default EvaluationRelativeJsonPointerError;
@@ -1,23 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.default = void 0;
6
- var _RelativeJsonPointerError = _interopRequireDefault(require("./RelativeJsonPointerError.cjs"));
7
- /**
8
- * @public
9
- */
10
-
11
- /**
12
- * @public
13
- */
14
- class InvalidRelativeJsonPointerError extends _RelativeJsonPointerError.default {
15
- relativePointer;
16
- constructor(message, structuredOptions) {
17
- super(message, structuredOptions);
18
- if (typeof structuredOptions !== 'undefined') {
19
- this.relativePointer = structuredOptions.relativePointer;
20
- }
21
- }
22
- }
23
- exports.default = InvalidRelativeJsonPointerError;
@@ -1,16 +0,0 @@
1
- import RelativeJsonPointerError from "./RelativeJsonPointerError.mjs";
2
- /**
3
- * @public
4
- */
5
- /**
6
- * @public
7
- */
8
- export default class InvalidRelativeJsonPointerError extends RelativeJsonPointerError {
9
- relativePointer;
10
- constructor(message, structuredOptions) {
11
- super(message, structuredOptions);
12
- if (typeof structuredOptions !== 'undefined') {
13
- this.relativePointer = structuredOptions.relativePointer;
14
- }
15
- }
16
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.default = void 0;
5
- var _apidomError = require("@speclynx/apidom-error");
6
- /**
7
- * @public
8
- */
9
- class RelativeJsonPointerError extends _apidomError.ApiDOMStructuredError {}
10
- var _default = exports.default = RelativeJsonPointerError;
@@ -1,7 +0,0 @@
1
- import { ApiDOMStructuredError } from '@speclynx/apidom-error';
2
-
3
- /**
4
- * @public
5
- */
6
- class RelativeJsonPointerError extends ApiDOMStructuredError {}
7
- export default RelativeJsonPointerError;
package/src/evaluate.cjs DELETED
@@ -1,127 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.default = void 0;
6
- var _apidomDatamodel = require("@speclynx/apidom-datamodel");
7
- var _apidomTraverse = require("@speclynx/apidom-traverse");
8
- var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
9
- var _ramda = require("ramda");
10
- var _EvaluationRelativeJsonPointerError = _interopRequireDefault(require("./errors/EvaluationRelativeJsonPointerError.cjs"));
11
- var _parse = _interopRequireDefault(require("./parse.cjs"));
12
- /**
13
- * Evaluates Relative JSON Pointer against ApiDOM fragment.
14
- * @public
15
- */
16
- const evaluate = (relativePointer, currentElement, rootElement) => {
17
- let ancestorLineage = [];
18
- let cursor = currentElement;
19
- (0, _apidomTraverse.traverse)(rootElement, {
20
- enter(path) {
21
- if (path.node === currentElement) {
22
- ancestorLineage = path.getAncestorNodes().reverse().filter(_apidomDatamodel.isElement);
23
- path.stop();
24
- }
25
- }
26
- });
27
- if (ancestorLineage.length === 0) {
28
- throw new _EvaluationRelativeJsonPointerError.default('Relative JSON Pointer evaluation failed. Current element not found inside the root element', {
29
- relativePointer,
30
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
31
- rootElement: (0, _apidomDatamodel.cloneDeep)(rootElement),
32
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor)
33
- });
34
- }
35
- if ((0, _ramda.last)(ancestorLineage) === rootElement) {
36
- throw new _EvaluationRelativeJsonPointerError.default('Relative JSON Pointer evaluation failed. Current element cannot be the root element', {
37
- relativePointer,
38
- currentElement,
39
- rootElement,
40
- cursorElement: cursor
41
- });
42
- }
43
- let relativeJsonPointer;
44
- try {
45
- relativeJsonPointer = (0, _parse.default)(relativePointer);
46
- } catch (error) {
47
- throw new _EvaluationRelativeJsonPointerError.default('Relative JSON Pointer evaluation failed while parsing the pointer.', {
48
- relativePointer,
49
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
50
- rootElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
51
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor),
52
- cause: error
53
- });
54
- }
55
-
56
- // non-negative-integer
57
- if (relativeJsonPointer.nonNegativeIntegerPrefix > 0) {
58
- const ancestorLineageCopy = [...ancestorLineage];
59
- for (let {
60
- nonNegativeIntegerPrefix
61
- } = relativeJsonPointer; nonNegativeIntegerPrefix > 0; nonNegativeIntegerPrefix -= 1) {
62
- cursor = ancestorLineageCopy.pop();
63
- if ((0, _apidomDatamodel.isMemberElement)(cursor)) {
64
- cursor = ancestorLineageCopy.pop();
65
- }
66
- }
67
- if (typeof cursor === 'undefined') {
68
- throw new _EvaluationRelativeJsonPointerError.default(`Relative JSON Pointer evaluation failed on non-negative-integer prefix of "${relativeJsonPointer.nonNegativeIntegerPrefix}"`, {
69
- relativePointer,
70
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
71
- rootElement: (0, _apidomDatamodel.cloneDeep)(rootElement),
72
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor)
73
- });
74
- }
75
- ancestorLineage = ancestorLineageCopy;
76
- }
77
-
78
- // index-manipulation
79
- if (typeof relativeJsonPointer.indexManipulation === 'number') {
80
- const containedArray = (0, _ramda.last)(ancestorLineage);
81
- if (typeof containedArray === 'undefined' || !(0, _apidomDatamodel.isArrayElement)(containedArray)) {
82
- throw new _EvaluationRelativeJsonPointerError.default(`Relative JSON Pointer evaluation failed failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
83
- relativePointer,
84
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
85
- rootElement: (0, _apidomDatamodel.cloneDeep)(rootElement),
86
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor)
87
- });
88
- }
89
- const arrayContent = containedArray.content;
90
- const currentCursorIndex = arrayContent.indexOf(cursor);
91
- const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
92
- cursor = arrayContent[newCursorIndex];
93
- if (typeof cursor === 'undefined') {
94
- throw new _EvaluationRelativeJsonPointerError.default(`Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
95
- relativePointer,
96
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
97
- rootElement: (0, _apidomDatamodel.cloneDeep)(rootElement),
98
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor)
99
- });
100
- }
101
- }
102
- if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
103
- // <json-pointer>
104
- const jsonPointer = (0, _apidomJsonPointer.compile)(relativeJsonPointer.jsonPointerTokens);
105
- cursor = (0, _apidomJsonPointer.evaluate)(cursor, jsonPointer);
106
- } else if (relativeJsonPointer.hashCharacter) {
107
- // "#"
108
- if (cursor === rootElement) {
109
- throw new _EvaluationRelativeJsonPointerError.default('Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"', {
110
- relativePointer,
111
- currentElement: (0, _apidomDatamodel.cloneDeep)(currentElement),
112
- rootElement: (0, _apidomDatamodel.cloneDeep)(rootElement),
113
- cursorElement: _apidomDatamodel.cloneDeep.safe(cursor)
114
- });
115
- }
116
- const parentElement = (0, _ramda.last)(ancestorLineage);
117
- if (typeof parentElement !== 'undefined') {
118
- if ((0, _apidomDatamodel.isMemberElement)(parentElement)) {
119
- cursor = parentElement.key;
120
- } else if ((0, _apidomDatamodel.isArrayElement)(parentElement)) {
121
- cursor = new _apidomDatamodel.NumberElement(parentElement.content.indexOf(cursor));
122
- }
123
- }
124
- }
125
- return cursor;
126
- };
127
- var _default = exports.default = evaluate;
package/src/evaluate.mjs DELETED
@@ -1,122 +0,0 @@
1
- import { isElement, isMemberElement, isArrayElement, NumberElement, cloneDeep } from '@speclynx/apidom-datamodel';
2
- import { traverse } from '@speclynx/apidom-traverse';
3
- import { compile as compileJsonPointer, evaluate as evaluateJsonPointer } from '@speclynx/apidom-json-pointer';
4
- import { last } from 'ramda';
5
- import EvaluationRelativeJsonPointerError from "./errors/EvaluationRelativeJsonPointerError.mjs";
6
- import parse from "./parse.mjs";
7
- /**
8
- * Evaluates Relative JSON Pointer against ApiDOM fragment.
9
- * @public
10
- */
11
- const evaluate = (relativePointer, currentElement, rootElement) => {
12
- let ancestorLineage = [];
13
- let cursor = currentElement;
14
- traverse(rootElement, {
15
- enter(path) {
16
- if (path.node === currentElement) {
17
- ancestorLineage = path.getAncestorNodes().reverse().filter(isElement);
18
- path.stop();
19
- }
20
- }
21
- });
22
- if (ancestorLineage.length === 0) {
23
- throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element not found inside the root element', {
24
- relativePointer,
25
- currentElement: cloneDeep(currentElement),
26
- rootElement: cloneDeep(rootElement),
27
- cursorElement: cloneDeep.safe(cursor)
28
- });
29
- }
30
- if (last(ancestorLineage) === rootElement) {
31
- throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element cannot be the root element', {
32
- relativePointer,
33
- currentElement,
34
- rootElement,
35
- cursorElement: cursor
36
- });
37
- }
38
- let relativeJsonPointer;
39
- try {
40
- relativeJsonPointer = parse(relativePointer);
41
- } catch (error) {
42
- throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed while parsing the pointer.', {
43
- relativePointer,
44
- currentElement: cloneDeep(currentElement),
45
- rootElement: cloneDeep(currentElement),
46
- cursorElement: cloneDeep.safe(cursor),
47
- cause: error
48
- });
49
- }
50
-
51
- // non-negative-integer
52
- if (relativeJsonPointer.nonNegativeIntegerPrefix > 0) {
53
- const ancestorLineageCopy = [...ancestorLineage];
54
- for (let {
55
- nonNegativeIntegerPrefix
56
- } = relativeJsonPointer; nonNegativeIntegerPrefix > 0; nonNegativeIntegerPrefix -= 1) {
57
- cursor = ancestorLineageCopy.pop();
58
- if (isMemberElement(cursor)) {
59
- cursor = ancestorLineageCopy.pop();
60
- }
61
- }
62
- if (typeof cursor === 'undefined') {
63
- throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed on non-negative-integer prefix of "${relativeJsonPointer.nonNegativeIntegerPrefix}"`, {
64
- relativePointer,
65
- currentElement: cloneDeep(currentElement),
66
- rootElement: cloneDeep(rootElement),
67
- cursorElement: cloneDeep.safe(cursor)
68
- });
69
- }
70
- ancestorLineage = ancestorLineageCopy;
71
- }
72
-
73
- // index-manipulation
74
- if (typeof relativeJsonPointer.indexManipulation === 'number') {
75
- const containedArray = last(ancestorLineage);
76
- if (typeof containedArray === 'undefined' || !isArrayElement(containedArray)) {
77
- throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
78
- relativePointer,
79
- currentElement: cloneDeep(currentElement),
80
- rootElement: cloneDeep(rootElement),
81
- cursorElement: cloneDeep.safe(cursor)
82
- });
83
- }
84
- const arrayContent = containedArray.content;
85
- const currentCursorIndex = arrayContent.indexOf(cursor);
86
- const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
87
- cursor = arrayContent[newCursorIndex];
88
- if (typeof cursor === 'undefined') {
89
- throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
90
- relativePointer,
91
- currentElement: cloneDeep(currentElement),
92
- rootElement: cloneDeep(rootElement),
93
- cursorElement: cloneDeep.safe(cursor)
94
- });
95
- }
96
- }
97
- if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
98
- // <json-pointer>
99
- const jsonPointer = compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
100
- cursor = evaluateJsonPointer(cursor, jsonPointer);
101
- } else if (relativeJsonPointer.hashCharacter) {
102
- // "#"
103
- if (cursor === rootElement) {
104
- throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"', {
105
- relativePointer,
106
- currentElement: cloneDeep(currentElement),
107
- rootElement: cloneDeep(rootElement),
108
- cursorElement: cloneDeep.safe(cursor)
109
- });
110
- }
111
- const parentElement = last(ancestorLineage);
112
- if (typeof parentElement !== 'undefined') {
113
- if (isMemberElement(parentElement)) {
114
- cursor = parentElement.key;
115
- } else if (isArrayElement(parentElement)) {
116
- cursor = new NumberElement(parentElement.content.indexOf(cursor));
117
- }
118
- }
119
- }
120
- return cursor;
121
- };
122
- export default evaluate;
package/src/index.cjs DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
4
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
5
- exports.__esModule = true;
6
- exports.parse = exports.isRelativeJsonPointer = exports.evaluate = exports.compile = exports.RelativeJsonPointerError = exports.InvalidRelativeJsonPointerError = exports.EvaluationRelativeJsonPointerError = exports.CompilationRelativeJsonPointerError = void 0;
7
- var _RelativeJsonPointerError = _interopRequireDefault(require("./errors/RelativeJsonPointerError.cjs"));
8
- exports.RelativeJsonPointerError = _RelativeJsonPointerError.default;
9
- var _InvalidRelativeJsonPointerError = _interopRequireDefault(require("./errors/InvalidRelativeJsonPointerError.cjs"));
10
- exports.InvalidRelativeJsonPointerError = _InvalidRelativeJsonPointerError.default;
11
- var _EvaluationRelativeJsonPointerError = _interopRequireDefault(require("./errors/EvaluationRelativeJsonPointerError.cjs"));
12
- exports.EvaluationRelativeJsonPointerError = _EvaluationRelativeJsonPointerError.default;
13
- var _CompilationRelativeJsonPointerError = _interopRequireDefault(require("./errors/CompilationRelativeJsonPointerError.cjs"));
14
- exports.CompilationRelativeJsonPointerError = _CompilationRelativeJsonPointerError.default;
15
- var _parse = _interopRequireWildcard(require("./parse.cjs"));
16
- exports.parse = _parse.default;
17
- exports.isRelativeJsonPointer = _parse.isRelativeJsonPointer;
18
- var _compile = _interopRequireDefault(require("./compile.cjs"));
19
- exports.compile = _compile.default;
20
- var _evaluate = _interopRequireDefault(require("./evaluate.cjs"));
21
- exports.evaluate = _evaluate.default;
package/src/index.mjs DELETED
@@ -1,7 +0,0 @@
1
- export { default as RelativeJsonPointerError } from "./errors/RelativeJsonPointerError.mjs";
2
- export { default as InvalidRelativeJsonPointerError } from "./errors/InvalidRelativeJsonPointerError.mjs";
3
- export { default as EvaluationRelativeJsonPointerError } from "./errors/EvaluationRelativeJsonPointerError.mjs";
4
- export { default as CompilationRelativeJsonPointerError } from "./errors/CompilationRelativeJsonPointerError.mjs";
5
- export { default as parse, isRelativeJsonPointer } from "./parse.mjs";
6
- export { default as compile } from "./compile.mjs";
7
- export { default as evaluate } from "./evaluate.mjs";
package/src/parse.cjs DELETED
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.isRelativeJsonPointer = exports.default = void 0;
6
- var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
7
- var _InvalidRelativeJsonPointerError = _interopRequireDefault(require("./errors/InvalidRelativeJsonPointerError.cjs"));
8
- const nonNegativeIntegerPrefixRegExp = '(?<nonNegativeIntegerPrefix>[1-9]\\d*|0)';
9
- const indexManipulationRegExp = '(?<indexManipulation>[+-][1-9]\\d*|0)';
10
- const hashCharacterRegExp = '(?<hashCharacter>#)';
11
- const jsonPointerRegExp = '(?<jsonPointer>\\/.*)';
12
- const relativeJsonPointerRegExp = new RegExp(`^${nonNegativeIntegerPrefixRegExp}${indexManipulationRegExp}?(${hashCharacterRegExp}|${jsonPointerRegExp})?$`);
13
-
14
- /**
15
- * @public
16
- */
17
- const isRelativeJsonPointer = value => {
18
- return typeof value === 'string' && relativeJsonPointerRegExp.test(value);
19
- };
20
-
21
- /**
22
- * @public
23
- */
24
- exports.isRelativeJsonPointer = isRelativeJsonPointer;
25
- const parse = relativePointer => {
26
- const match = relativePointer.match(relativeJsonPointerRegExp);
27
- if (match === null || typeof match.groups === 'undefined') {
28
- throw new _InvalidRelativeJsonPointerError.default(`Invalid Relative JSON Pointer "${relativePointer}".`, {
29
- relativePointer
30
- });
31
- }
32
- try {
33
- // non-negative-integer
34
- const nonNegativeIntegerPrefix = parseInt(match.groups.nonNegativeIntegerPrefix, 10);
35
- // index-manipulation
36
- const indexManipulation = typeof match.groups.indexManipulation === 'string' ? parseInt(match.groups.indexManipulation, 10) : undefined;
37
- // <json-pointer>
38
- const jsonPointerTokens = typeof match.groups.jsonPointer === 'string' ? (0, _apidomJsonPointer.parse)(match.groups.jsonPointer).tree : undefined;
39
- // "#"
40
- const hashCharacter = typeof match.groups.hashCharacter === 'string';
41
- return {
42
- nonNegativeIntegerPrefix,
43
- indexManipulation,
44
- jsonPointerTokens,
45
- hashCharacter
46
- };
47
- } catch (error) {
48
- throw new _InvalidRelativeJsonPointerError.default(`Relative JSON Pointer parsing of "${relativePointer}" encountered an error.`, {
49
- relativePointer,
50
- cause: error
51
- });
52
- }
53
- };
54
- var _default = exports.default = parse;
package/src/parse.mjs DELETED
@@ -1,48 +0,0 @@
1
- import { parse as parseJsonPointer } from '@speclynx/apidom-json-pointer';
2
- import InvalidRelativeJsonPointerError from "./errors/InvalidRelativeJsonPointerError.mjs";
3
- const nonNegativeIntegerPrefixRegExp = '(?<nonNegativeIntegerPrefix>[1-9]\\d*|0)';
4
- const indexManipulationRegExp = '(?<indexManipulation>[+-][1-9]\\d*|0)';
5
- const hashCharacterRegExp = '(?<hashCharacter>#)';
6
- const jsonPointerRegExp = '(?<jsonPointer>\\/.*)';
7
- const relativeJsonPointerRegExp = new RegExp(`^${nonNegativeIntegerPrefixRegExp}${indexManipulationRegExp}?(${hashCharacterRegExp}|${jsonPointerRegExp})?$`);
8
-
9
- /**
10
- * @public
11
- */
12
- export const isRelativeJsonPointer = value => {
13
- return typeof value === 'string' && relativeJsonPointerRegExp.test(value);
14
- };
15
-
16
- /**
17
- * @public
18
- */
19
- const parse = relativePointer => {
20
- const match = relativePointer.match(relativeJsonPointerRegExp);
21
- if (match === null || typeof match.groups === 'undefined') {
22
- throw new InvalidRelativeJsonPointerError(`Invalid Relative JSON Pointer "${relativePointer}".`, {
23
- relativePointer
24
- });
25
- }
26
- try {
27
- // non-negative-integer
28
- const nonNegativeIntegerPrefix = parseInt(match.groups.nonNegativeIntegerPrefix, 10);
29
- // index-manipulation
30
- const indexManipulation = typeof match.groups.indexManipulation === 'string' ? parseInt(match.groups.indexManipulation, 10) : undefined;
31
- // <json-pointer>
32
- const jsonPointerTokens = typeof match.groups.jsonPointer === 'string' ? parseJsonPointer(match.groups.jsonPointer).tree : undefined;
33
- // "#"
34
- const hashCharacter = typeof match.groups.hashCharacter === 'string';
35
- return {
36
- nonNegativeIntegerPrefix,
37
- indexManipulation,
38
- jsonPointerTokens,
39
- hashCharacter
40
- };
41
- } catch (error) {
42
- throw new InvalidRelativeJsonPointerError(`Relative JSON Pointer parsing of "${relativePointer}" encountered an error.`, {
43
- relativePointer,
44
- cause: error
45
- });
46
- }
47
- };
48
- export default parse;
package/src/types.cjs DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
package/src/types.mjs DELETED
@@ -1 +0,0 @@
1
- export {};