@speclynx/apidom-json-pointer-relative 4.0.2 → 4.0.3

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,12 @@
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.3](https://github.com/speclynx/apidom/compare/v4.0.2...v4.0.3) (2026-03-11)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **release:** fix v4.0.2 failed release ([b4dc1c4](https://github.com/speclynx/apidom/commit/b4dc1c48e8d9b2986a70e49b5554eb0a166d7528))
11
+
6
12
  ## [4.0.2](https://github.com/speclynx/apidom/compare/v4.0.1...v4.0.2) (2026-03-11)
7
13
 
8
14
  **Note:** Version bump only for package @speclynx/apidom-json-pointer-relative
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speclynx/apidom-json-pointer-relative",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "Evaluate Relative JSON Pointer expressions against ApiDOM.",
5
5
  "keywords": [
6
6
  "apidom",
@@ -54,16 +54,15 @@
54
54
  "license": "Apache-2.0",
55
55
  "dependencies": {
56
56
  "@babel/runtime-corejs3": "^7.28.4",
57
- "@speclynx/apidom-datamodel": "4.0.2",
58
- "@speclynx/apidom-error": "4.0.2",
59
- "@speclynx/apidom-json-pointer": "4.0.2",
60
- "@speclynx/apidom-traverse": "4.0.2",
57
+ "@speclynx/apidom-datamodel": "4.0.3",
58
+ "@speclynx/apidom-error": "4.0.3",
59
+ "@speclynx/apidom-json-pointer": "4.0.3",
60
+ "@speclynx/apidom-traverse": "4.0.3",
61
61
  "ramda": "~0.32.0",
62
62
  "ramda-adjunct": "^6.0.0"
63
63
  },
64
64
  "files": [
65
- "src/**/*.mjs",
66
- "src/**/*.cjs",
65
+ "src/",
67
66
  "dist/",
68
67
  "types/apidom-json-pointer-relative.d.ts",
69
68
  "LICENSES",
@@ -71,5 +70,5 @@
71
70
  "README.md",
72
71
  "CHANGELOG.md"
73
72
  ],
74
- "gitHead": "af1b05d4d5e48a11a3a03cd5699324e0f1b62765"
73
+ "gitHead": "6ccfa09c02232516215e7de3ead276641957e626"
75
74
  }
@@ -0,0 +1,37 @@
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;
@@ -0,0 +1,32 @@
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;
package/src/compile.ts ADDED
@@ -0,0 +1,41 @@
1
+ import { compile as compileJsonPointer } from '@speclynx/apidom-json-pointer';
2
+
3
+ import { RelativeJsonPointer } from './types.ts';
4
+ import CompilationRelativeJsonPointerError from './errors/CompilationRelativeJsonPointerError.ts';
5
+
6
+ /**
7
+ * @public
8
+ */
9
+ const compile = (relativeJsonPointer: RelativeJsonPointer): string => {
10
+ try {
11
+ let relativePointer = '';
12
+
13
+ // non-negative-integer
14
+ relativePointer += String(relativeJsonPointer.nonNegativeIntegerPrefix);
15
+
16
+ // index-manipulation
17
+ if (typeof relativeJsonPointer.indexManipulation === 'number') {
18
+ relativePointer += String(relativeJsonPointer.indexManipulation);
19
+ }
20
+
21
+ if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
22
+ // <json-pointer>
23
+ relativePointer += compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
24
+ } else if (relativeJsonPointer.hashCharacter) {
25
+ // "#"
26
+ relativePointer += '#';
27
+ }
28
+
29
+ return relativePointer;
30
+ } catch (error: unknown) {
31
+ throw new CompilationRelativeJsonPointerError(
32
+ 'Relative JSON Pointer compilation encountered an error.',
33
+ {
34
+ relativePointer: relativeJsonPointer,
35
+ cause: error,
36
+ },
37
+ );
38
+ }
39
+ };
40
+
41
+ export default compile;
@@ -0,0 +1,31 @@
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;
@@ -0,0 +1,25 @@
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;
@@ -0,0 +1,39 @@
1
+ import { ApiDOMErrorOptions } from '@speclynx/apidom-error';
2
+
3
+ import { RelativeJsonPointer } from '../types.ts';
4
+ import RelativeJsonPointerError from './RelativeJsonPointerError.ts';
5
+
6
+ /**
7
+ * @public
8
+ */
9
+ export interface CompilationRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
10
+ readonly relativePointer: RelativeJsonPointer;
11
+ }
12
+
13
+ /**
14
+ * @public
15
+ */
16
+ class CompilationRelativeJsonPointerError extends RelativeJsonPointerError {
17
+ public readonly nonNegativeIntegerPrefix!: number;
18
+
19
+ public readonly indexManipulation?: number;
20
+
21
+ public readonly jsonPointerTokens?: string[];
22
+
23
+ public readonly hashCharacter?: boolean;
24
+
25
+ constructor(message?: string, structuredOptions?: CompilationRelativeJsonPointerErrorOptions) {
26
+ super(message, structuredOptions);
27
+
28
+ if (typeof structuredOptions !== 'undefined') {
29
+ this.nonNegativeIntegerPrefix = structuredOptions.relativePointer.nonNegativeIntegerPrefix;
30
+ this.indexManipulation = structuredOptions.relativePointer.indexManipulation;
31
+ this.hashCharacter = structuredOptions.relativePointer.hashCharacter;
32
+ if (Array.isArray(structuredOptions.relativePointer.jsonPointerTokens)) {
33
+ this.jsonPointerTokens = [...structuredOptions.relativePointer.jsonPointerTokens];
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ export default CompilationRelativeJsonPointerError;
@@ -0,0 +1,29 @@
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;
@@ -0,0 +1,23 @@
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;
@@ -0,0 +1,51 @@
1
+ import { Element } from '@speclynx/apidom-datamodel';
2
+ import { ApiDOMErrorOptions } from '@speclynx/apidom-error';
3
+
4
+ import RelativeJsonPointerError from './RelativeJsonPointerError.ts';
5
+
6
+ /**
7
+ * @public
8
+ */
9
+ export interface EvaluationRelativeJsonPointerErrorOptions<
10
+ T extends Element,
11
+ U extends Element,
12
+ V extends Element,
13
+ > extends ApiDOMErrorOptions {
14
+ readonly relativePointer: string;
15
+ readonly currentElement: T;
16
+ readonly rootElement: U;
17
+ readonly cursorElement?: V;
18
+ }
19
+
20
+ /**
21
+ * @public
22
+ */
23
+ class EvaluationRelativeJsonPointerError<
24
+ T extends Element,
25
+ U extends Element,
26
+ V extends Element,
27
+ > extends RelativeJsonPointerError {
28
+ public readonly relativePointer!: string;
29
+
30
+ public readonly currentElement!: T;
31
+
32
+ public readonly rootElement!: U;
33
+
34
+ public readonly cursorElement?: V;
35
+
36
+ constructor(
37
+ message?: string,
38
+ structuredOptions?: EvaluationRelativeJsonPointerErrorOptions<T, U, V>,
39
+ ) {
40
+ super(message, structuredOptions);
41
+
42
+ if (typeof structuredOptions !== 'undefined') {
43
+ this.relativePointer = structuredOptions.relativePointer;
44
+ this.currentElement = structuredOptions.currentElement;
45
+ this.rootElement = structuredOptions.rootElement;
46
+ this.cursorElement = structuredOptions.cursorElement;
47
+ }
48
+ }
49
+ }
50
+
51
+ export default EvaluationRelativeJsonPointerError;
@@ -0,0 +1,23 @@
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;
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,25 @@
1
+ import { ApiDOMErrorOptions } from '@speclynx/apidom-error';
2
+
3
+ import RelativeJsonPointerError from './RelativeJsonPointerError.ts';
4
+
5
+ /**
6
+ * @public
7
+ */
8
+ export interface InvalidRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
9
+ readonly relativePointer: string;
10
+ }
11
+
12
+ /**
13
+ * @public
14
+ */
15
+ export default class InvalidRelativeJsonPointerError extends RelativeJsonPointerError {
16
+ public readonly relativePointer!: string;
17
+
18
+ constructor(message?: string, structuredOptions?: InvalidRelativeJsonPointerErrorOptions) {
19
+ super(message, structuredOptions);
20
+
21
+ if (typeof structuredOptions !== 'undefined') {
22
+ this.relativePointer = structuredOptions.relativePointer;
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,7 @@
1
+ import { ApiDOMStructuredError } from '@speclynx/apidom-error';
2
+
3
+ /**
4
+ * @public
5
+ */
6
+ class RelativeJsonPointerError extends ApiDOMStructuredError {}
7
+ export default RelativeJsonPointerError;
@@ -0,0 +1,8 @@
1
+ import { ApiDOMStructuredError } from '@speclynx/apidom-error';
2
+
3
+ /**
4
+ * @public
5
+ */
6
+ class RelativeJsonPointerError extends ApiDOMStructuredError {}
7
+
8
+ export default RelativeJsonPointerError;
@@ -0,0 +1,127 @@
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;
@@ -0,0 +1,122 @@
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;
@@ -0,0 +1,178 @@
1
+ import {
2
+ Element,
3
+ isElement,
4
+ isMemberElement,
5
+ isArrayElement,
6
+ MemberElement,
7
+ ArrayElement,
8
+ NumberElement,
9
+ cloneDeep,
10
+ } from '@speclynx/apidom-datamodel';
11
+ import { traverse, Path } from '@speclynx/apidom-traverse';
12
+ import {
13
+ compile as compileJsonPointer,
14
+ evaluate as evaluateJsonPointer,
15
+ } from '@speclynx/apidom-json-pointer';
16
+ import { last } from 'ramda';
17
+
18
+ import EvaluationRelativeJsonPointerError from './errors/EvaluationRelativeJsonPointerError.ts';
19
+ import parse from './parse.ts';
20
+ import { RelativeJsonPointer } from './types.ts';
21
+
22
+ /**
23
+ * Evaluates Relative JSON Pointer against ApiDOM fragment.
24
+ * @public
25
+ */
26
+ const evaluate = <T extends Element, U extends Element>(
27
+ relativePointer: string,
28
+ currentElement: T,
29
+ rootElement: U,
30
+ ): Element | undefined => {
31
+ let ancestorLineage: Element[] = [];
32
+ let cursor: Element | undefined = currentElement;
33
+
34
+ traverse(rootElement, {
35
+ enter(path: Path<Element>) {
36
+ if (path.node === currentElement) {
37
+ ancestorLineage = path.getAncestorNodes().reverse().filter(isElement);
38
+ path.stop();
39
+ }
40
+ },
41
+ });
42
+
43
+ if (ancestorLineage.length === 0) {
44
+ throw new EvaluationRelativeJsonPointerError(
45
+ 'Relative JSON Pointer evaluation failed. Current element not found inside the root element',
46
+ {
47
+ relativePointer,
48
+ currentElement: cloneDeep(currentElement),
49
+ rootElement: cloneDeep(rootElement),
50
+ cursorElement: cloneDeep.safe(cursor),
51
+ },
52
+ );
53
+ }
54
+
55
+ if (last(ancestorLineage) === rootElement) {
56
+ throw new EvaluationRelativeJsonPointerError(
57
+ 'Relative JSON Pointer evaluation failed. Current element cannot be the root element',
58
+ {
59
+ relativePointer,
60
+ currentElement,
61
+ rootElement,
62
+ cursorElement: cursor,
63
+ },
64
+ );
65
+ }
66
+
67
+ let relativeJsonPointer: RelativeJsonPointer;
68
+ try {
69
+ relativeJsonPointer = parse(relativePointer);
70
+ } catch (error: unknown) {
71
+ throw new EvaluationRelativeJsonPointerError(
72
+ 'Relative JSON Pointer evaluation failed while parsing the pointer.',
73
+ {
74
+ relativePointer,
75
+ currentElement: cloneDeep(currentElement),
76
+ rootElement: cloneDeep(currentElement),
77
+ cursorElement: cloneDeep.safe(cursor),
78
+ cause: error,
79
+ },
80
+ );
81
+ }
82
+
83
+ // non-negative-integer
84
+ if (relativeJsonPointer.nonNegativeIntegerPrefix > 0) {
85
+ const ancestorLineageCopy = [...ancestorLineage];
86
+
87
+ for (
88
+ let { nonNegativeIntegerPrefix } = relativeJsonPointer;
89
+ nonNegativeIntegerPrefix > 0;
90
+ nonNegativeIntegerPrefix -= 1
91
+ ) {
92
+ cursor = ancestorLineageCopy.pop();
93
+ if (isMemberElement(cursor)) {
94
+ cursor = ancestorLineageCopy.pop();
95
+ }
96
+ }
97
+
98
+ if (typeof cursor === 'undefined') {
99
+ throw new EvaluationRelativeJsonPointerError(
100
+ `Relative JSON Pointer evaluation failed on non-negative-integer prefix of "${relativeJsonPointer.nonNegativeIntegerPrefix}"`,
101
+ {
102
+ relativePointer,
103
+ currentElement: cloneDeep(currentElement),
104
+ rootElement: cloneDeep(rootElement),
105
+ cursorElement: cloneDeep.safe(cursor),
106
+ },
107
+ );
108
+ }
109
+
110
+ ancestorLineage = ancestorLineageCopy;
111
+ }
112
+
113
+ // index-manipulation
114
+ if (typeof relativeJsonPointer.indexManipulation === 'number') {
115
+ const containedArray = last(ancestorLineage);
116
+
117
+ if (typeof containedArray === 'undefined' || !isArrayElement(containedArray)) {
118
+ throw new EvaluationRelativeJsonPointerError(
119
+ `Relative JSON Pointer evaluation failed failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`,
120
+ {
121
+ relativePointer,
122
+ currentElement: cloneDeep(currentElement),
123
+ rootElement: cloneDeep(rootElement),
124
+ cursorElement: cloneDeep.safe(cursor),
125
+ },
126
+ );
127
+ }
128
+
129
+ const arrayContent = containedArray.content as Element[];
130
+ const currentCursorIndex = arrayContent.indexOf(cursor!);
131
+ const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
132
+ cursor = arrayContent[newCursorIndex] as Element | undefined;
133
+
134
+ if (typeof cursor === 'undefined') {
135
+ throw new EvaluationRelativeJsonPointerError(
136
+ `Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`,
137
+ {
138
+ relativePointer,
139
+ currentElement: cloneDeep(currentElement),
140
+ rootElement: cloneDeep(rootElement),
141
+ cursorElement: cloneDeep.safe(cursor),
142
+ },
143
+ );
144
+ }
145
+ }
146
+
147
+ if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
148
+ // <json-pointer>
149
+ const jsonPointer = compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
150
+ cursor = evaluateJsonPointer(cursor, jsonPointer);
151
+ } else if (relativeJsonPointer.hashCharacter) {
152
+ // "#"
153
+ if (cursor === rootElement) {
154
+ throw new EvaluationRelativeJsonPointerError(
155
+ 'Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"',
156
+ {
157
+ relativePointer,
158
+ currentElement: cloneDeep(currentElement),
159
+ rootElement: cloneDeep(rootElement),
160
+ cursorElement: cloneDeep.safe(cursor),
161
+ },
162
+ );
163
+ }
164
+
165
+ const parentElement = last(ancestorLineage) as ArrayElement | MemberElement | undefined;
166
+ if (typeof parentElement !== 'undefined') {
167
+ if (isMemberElement(parentElement)) {
168
+ cursor = (parentElement as MemberElement).key as Element;
169
+ } else if (isArrayElement(parentElement)) {
170
+ cursor = new NumberElement((parentElement.content as Element[]).indexOf(cursor!));
171
+ }
172
+ }
173
+ }
174
+
175
+ return cursor;
176
+ };
177
+
178
+ export default evaluate;
package/src/index.cjs ADDED
@@ -0,0 +1,21 @@
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 ADDED
@@ -0,0 +1,7 @@
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/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ export { default as RelativeJsonPointerError } from './errors/RelativeJsonPointerError.ts';
2
+ export { default as InvalidRelativeJsonPointerError } from './errors/InvalidRelativeJsonPointerError.ts';
3
+ export type { InvalidRelativeJsonPointerErrorOptions } from './errors/InvalidRelativeJsonPointerError.ts';
4
+ export { default as EvaluationRelativeJsonPointerError } from './errors/EvaluationRelativeJsonPointerError.ts';
5
+ export type { EvaluationRelativeJsonPointerErrorOptions } from './errors/EvaluationRelativeJsonPointerError.ts';
6
+ export { default as CompilationRelativeJsonPointerError } from './errors/CompilationRelativeJsonPointerError.ts';
7
+ export type { CompilationRelativeJsonPointerErrorOptions } from './errors/CompilationRelativeJsonPointerError.ts';
8
+ export { default as parse, isRelativeJsonPointer } from './parse.ts';
9
+ export { default as compile } from './compile.ts';
10
+ export { default as evaluate } from './evaluate.ts';
11
+ export type { RelativeJsonPointer } from './types.ts';
package/src/parse.cjs ADDED
@@ -0,0 +1,54 @@
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 ADDED
@@ -0,0 +1,48 @@
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/parse.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { parse as parseJsonPointer } from '@speclynx/apidom-json-pointer';
2
+
3
+ import InvalidRelativeJsonPointerError from './errors/InvalidRelativeJsonPointerError.ts';
4
+ import { RelativeJsonPointer } from './types.ts';
5
+
6
+ const nonNegativeIntegerPrefixRegExp = '(?<nonNegativeIntegerPrefix>[1-9]\\d*|0)';
7
+ const indexManipulationRegExp = '(?<indexManipulation>[+-][1-9]\\d*|0)';
8
+ const hashCharacterRegExp = '(?<hashCharacter>#)';
9
+ const jsonPointerRegExp = '(?<jsonPointer>\\/.*)';
10
+ const relativeJsonPointerRegExp = new RegExp(
11
+ `^${nonNegativeIntegerPrefixRegExp}${indexManipulationRegExp}?(${hashCharacterRegExp}|${jsonPointerRegExp})?$`,
12
+ );
13
+
14
+ /**
15
+ * @public
16
+ */
17
+ export const isRelativeJsonPointer = (value: any) => {
18
+ return typeof value === 'string' && relativeJsonPointerRegExp.test(value);
19
+ };
20
+
21
+ /**
22
+ * @public
23
+ */
24
+ const parse = (relativePointer: string): RelativeJsonPointer => {
25
+ const match = relativePointer.match(relativeJsonPointerRegExp);
26
+ if (match === null || typeof match.groups === 'undefined') {
27
+ throw new InvalidRelativeJsonPointerError(
28
+ `Invalid Relative JSON Pointer "${relativePointer}".`,
29
+ { relativePointer },
30
+ );
31
+ }
32
+
33
+ try {
34
+ // non-negative-integer
35
+ const nonNegativeIntegerPrefix = parseInt(match.groups.nonNegativeIntegerPrefix, 10);
36
+ // index-manipulation
37
+ const indexManipulation =
38
+ typeof match.groups.indexManipulation === 'string'
39
+ ? parseInt(match.groups.indexManipulation, 10)
40
+ : undefined;
41
+ // <json-pointer>
42
+ const jsonPointerTokens =
43
+ typeof match.groups.jsonPointer === 'string'
44
+ ? parseJsonPointer(match.groups.jsonPointer).tree
45
+ : undefined;
46
+ // "#"
47
+ const hashCharacter = typeof match.groups.hashCharacter === 'string';
48
+
49
+ return {
50
+ nonNegativeIntegerPrefix,
51
+ indexManipulation,
52
+ jsonPointerTokens,
53
+ hashCharacter,
54
+ };
55
+ } catch (error: unknown) {
56
+ throw new InvalidRelativeJsonPointerError(
57
+ `Relative JSON Pointer parsing of "${relativePointer}" encountered an error.`,
58
+ {
59
+ relativePointer,
60
+ cause: error,
61
+ },
62
+ );
63
+ }
64
+ };
65
+
66
+ export default parse;
package/src/types.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
package/src/types.mjs ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/types.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export type RelativeJsonPointer = {
5
+ readonly nonNegativeIntegerPrefix: number;
6
+ readonly indexManipulation?: number;
7
+ readonly jsonPointerTokens?: string[];
8
+ readonly hashCharacter?: boolean;
9
+ };