@speclynx/apidom-json-pointer-relative 4.0.2 → 4.0.4
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 +12 -0
- package/package.json +6 -6
- package/src/compile.cjs +37 -0
- package/src/compile.mjs +32 -0
- package/src/errors/CompilationRelativeJsonPointerError.cjs +31 -0
- package/src/errors/CompilationRelativeJsonPointerError.mjs +25 -0
- package/src/errors/EvaluationRelativeJsonPointerError.cjs +29 -0
- package/src/errors/EvaluationRelativeJsonPointerError.mjs +23 -0
- package/src/errors/InvalidRelativeJsonPointerError.cjs +23 -0
- package/src/errors/InvalidRelativeJsonPointerError.mjs +16 -0
- package/src/errors/RelativeJsonPointerError.cjs +10 -0
- package/src/errors/RelativeJsonPointerError.mjs +7 -0
- package/src/evaluate.cjs +127 -0
- package/src/evaluate.mjs +122 -0
- package/src/index.cjs +21 -0
- package/src/index.mjs +7 -0
- package/src/parse.cjs +54 -0
- package/src/parse.mjs +48 -0
- package/src/types.cjs +3 -0
- package/src/types.mjs +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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.4](https://github.com/speclynx/apidom/compare/v4.0.3...v4.0.4) (2026-03-12)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **release:** override minimatch 10.2.3 to fix glob pattern regression in lerna publish ([#157](https://github.com/speclynx/apidom/issues/157)) ([c2d65a0](https://github.com/speclynx/apidom/commit/c2d65a06a2187e8563a9dc9db74ba27255450e0b)), closes [lerna/lerna#4305](https://github.com/lerna/lerna/issues/4305) [isaacs/minimatch#284](https://github.com/isaacs/minimatch/issues/284)
|
|
11
|
+
|
|
12
|
+
## [4.0.3](https://github.com/speclynx/apidom/compare/v4.0.2...v4.0.3) (2026-03-11)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
- **release:** fix v4.0.2 failed release ([b4dc1c4](https://github.com/speclynx/apidom/commit/b4dc1c48e8d9b2986a70e49b5554eb0a166d7528))
|
|
17
|
+
|
|
6
18
|
## [4.0.2](https://github.com/speclynx/apidom/compare/v4.0.1...v4.0.2) (2026-03-11)
|
|
7
19
|
|
|
8
20
|
**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.
|
|
3
|
+
"version": "4.0.4",
|
|
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": "4.0.
|
|
58
|
-
"@speclynx/apidom-error": "4.0.
|
|
59
|
-
"@speclynx/apidom-json-pointer": "4.0.
|
|
60
|
-
"@speclynx/apidom-traverse": "4.0.
|
|
57
|
+
"@speclynx/apidom-datamodel": "4.0.4",
|
|
58
|
+
"@speclynx/apidom-error": "4.0.4",
|
|
59
|
+
"@speclynx/apidom-json-pointer": "4.0.4",
|
|
60
|
+
"@speclynx/apidom-traverse": "4.0.4",
|
|
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": "
|
|
74
|
+
"gitHead": "a06f6ef3d37ad5bf860cbbf4b442bfbe3a522cc2"
|
|
75
75
|
}
|
package/src/compile.cjs
ADDED
|
@@ -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;
|
package/src/compile.mjs
ADDED
|
@@ -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;
|
|
@@ -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,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,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,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;
|
package/src/evaluate.cjs
ADDED
|
@@ -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;
|
package/src/evaluate.mjs
ADDED
|
@@ -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;
|
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/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/types.cjs
ADDED
package/src/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|