@speclynx/apidom-json-pointer-relative 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -0
- package/LICENSE +202 -0
- package/LICENSES/AFL-3.0.txt +182 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/BSD-3-Clause.txt +26 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +65 -0
- package/README.md +64 -0
- package/dist/apidom-json-pointer-relative.browser.js +15057 -0
- package/dist/apidom-json-pointer-relative.browser.min.js +1 -0
- package/package.json +60 -0
- 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 +126 -0
- package/src/evaluate.mjs +121 -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/types/apidom-json-pointer-relative.d.ts +96 -0
package/src/evaluate.cjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
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 _apidomCore = require("@speclynx/apidom-core");
|
|
7
|
+
var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
|
|
8
|
+
var _ramda = require("ramda");
|
|
9
|
+
var _EvaluationRelativeJsonPointerError = _interopRequireDefault(require("./errors/EvaluationRelativeJsonPointerError.cjs"));
|
|
10
|
+
var _parse = _interopRequireDefault(require("./parse.cjs"));
|
|
11
|
+
/**
|
|
12
|
+
* Evaluates Relative JSON Pointer against ApiDOM fragment.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const evaluate = (relativePointer, currentElement, rootElement) => {
|
|
16
|
+
let ancestorLineage = [];
|
|
17
|
+
let cursor = currentElement;
|
|
18
|
+
(0, _apidomCore.visit)(rootElement, {
|
|
19
|
+
enter(element, key, parent, path, ancestors) {
|
|
20
|
+
if (element === currentElement) {
|
|
21
|
+
ancestorLineage = [...ancestors, parent].filter(_apidomCore.isElement);
|
|
22
|
+
return _apidomCore.BREAK;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
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, _apidomCore.cloneDeep)(currentElement),
|
|
31
|
+
rootElement: (0, _apidomCore.cloneDeep)(rootElement),
|
|
32
|
+
cursorElement: _apidomCore.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, _apidomCore.cloneDeep)(currentElement),
|
|
50
|
+
rootElement: (0, _apidomCore.cloneDeep)(currentElement),
|
|
51
|
+
cursorElement: _apidomCore.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, _apidomCore.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, _apidomCore.cloneDeep)(currentElement),
|
|
71
|
+
rootElement: (0, _apidomCore.cloneDeep)(rootElement),
|
|
72
|
+
cursorElement: _apidomCore.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, _apidomCore.isArrayElement)(containedArray)) {
|
|
82
|
+
throw new _EvaluationRelativeJsonPointerError.default(`Relative JSON Pointer evaluation failed failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
|
|
83
|
+
relativePointer,
|
|
84
|
+
currentElement: (0, _apidomCore.cloneDeep)(currentElement),
|
|
85
|
+
rootElement: (0, _apidomCore.cloneDeep)(rootElement),
|
|
86
|
+
cursorElement: _apidomCore.cloneDeep.safe(cursor)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const currentCursorIndex = containedArray.content.indexOf(cursor);
|
|
90
|
+
const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
|
|
91
|
+
cursor = containedArray.content[newCursorIndex];
|
|
92
|
+
if (typeof cursor === 'undefined') {
|
|
93
|
+
throw new _EvaluationRelativeJsonPointerError.default(`Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
|
|
94
|
+
relativePointer,
|
|
95
|
+
currentElement: (0, _apidomCore.cloneDeep)(currentElement),
|
|
96
|
+
rootElement: (0, _apidomCore.cloneDeep)(rootElement),
|
|
97
|
+
cursorElement: _apidomCore.cloneDeep.safe(cursor)
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
|
|
102
|
+
// <json-pointer>
|
|
103
|
+
const jsonPointer = (0, _apidomJsonPointer.compile)(relativeJsonPointer.jsonPointerTokens);
|
|
104
|
+
cursor = (0, _apidomJsonPointer.evaluate)(cursor, jsonPointer);
|
|
105
|
+
} else if (relativeJsonPointer.hashCharacter) {
|
|
106
|
+
// "#"
|
|
107
|
+
if (cursor === rootElement) {
|
|
108
|
+
throw new _EvaluationRelativeJsonPointerError.default('Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"', {
|
|
109
|
+
relativePointer,
|
|
110
|
+
currentElement: (0, _apidomCore.cloneDeep)(currentElement),
|
|
111
|
+
rootElement: (0, _apidomCore.cloneDeep)(rootElement),
|
|
112
|
+
cursorElement: _apidomCore.cloneDeep.safe(cursor)
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const parentElement = (0, _ramda.last)(ancestorLineage);
|
|
116
|
+
if (typeof parentElement !== 'undefined') {
|
|
117
|
+
if ((0, _apidomCore.isMemberElement)(parentElement)) {
|
|
118
|
+
cursor = parentElement.key;
|
|
119
|
+
} else if ((0, _apidomCore.isArrayElement)(parentElement)) {
|
|
120
|
+
cursor = new _apidomCore.NumberElement(parentElement.content.indexOf(cursor));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return cursor;
|
|
125
|
+
};
|
|
126
|
+
var _default = exports.default = evaluate;
|
package/src/evaluate.mjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { visit, cloneDeep, BREAK, isElement, isMemberElement, isArrayElement, NumberElement } from '@speclynx/apidom-core';
|
|
2
|
+
import { compile as compileJsonPointer, evaluate as evaluateJsonPointer } from '@speclynx/apidom-json-pointer';
|
|
3
|
+
import { last } from 'ramda';
|
|
4
|
+
import EvaluationRelativeJsonPointerError from "./errors/EvaluationRelativeJsonPointerError.mjs";
|
|
5
|
+
import parse from "./parse.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Evaluates Relative JSON Pointer against ApiDOM fragment.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const evaluate = (relativePointer, currentElement, rootElement) => {
|
|
11
|
+
let ancestorLineage = [];
|
|
12
|
+
let cursor = currentElement;
|
|
13
|
+
visit(rootElement, {
|
|
14
|
+
enter(element, key, parent, path, ancestors) {
|
|
15
|
+
if (element === currentElement) {
|
|
16
|
+
ancestorLineage = [...ancestors, parent].filter(isElement);
|
|
17
|
+
return BREAK;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
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 currentCursorIndex = containedArray.content.indexOf(cursor);
|
|
85
|
+
const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
|
|
86
|
+
cursor = containedArray.content[newCursorIndex];
|
|
87
|
+
if (typeof cursor === 'undefined') {
|
|
88
|
+
throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
|
|
89
|
+
relativePointer,
|
|
90
|
+
currentElement: cloneDeep(currentElement),
|
|
91
|
+
rootElement: cloneDeep(rootElement),
|
|
92
|
+
cursorElement: cloneDeep.safe(cursor)
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
|
|
97
|
+
// <json-pointer>
|
|
98
|
+
const jsonPointer = compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
|
|
99
|
+
cursor = evaluateJsonPointer(cursor, jsonPointer);
|
|
100
|
+
} else if (relativeJsonPointer.hashCharacter) {
|
|
101
|
+
// "#"
|
|
102
|
+
if (cursor === rootElement) {
|
|
103
|
+
throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"', {
|
|
104
|
+
relativePointer,
|
|
105
|
+
currentElement: cloneDeep(currentElement),
|
|
106
|
+
rootElement: cloneDeep(rootElement),
|
|
107
|
+
cursorElement: cloneDeep.safe(cursor)
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const parentElement = last(ancestorLineage);
|
|
111
|
+
if (typeof parentElement !== 'undefined') {
|
|
112
|
+
if (isMemberElement(parentElement)) {
|
|
113
|
+
cursor = parentElement.key;
|
|
114
|
+
} else if (isArrayElement(parentElement)) {
|
|
115
|
+
cursor = new NumberElement(parentElement.content.indexOf(cursor));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return cursor;
|
|
120
|
+
};
|
|
121
|
+
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 {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ApiDOMErrorOptions } from '@speclynx/apidom-error';
|
|
2
|
+
import { ApiDOMStructuredError } from '@speclynx/apidom-error';
|
|
3
|
+
import { Element as Element_2 } from '@speclynx/apidom-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export declare class CompilationRelativeJsonPointerError extends RelativeJsonPointerError {
|
|
9
|
+
readonly nonNegativeIntegerPrefix: number;
|
|
10
|
+
readonly indexManipulation?: number;
|
|
11
|
+
readonly jsonPointerTokens?: string[];
|
|
12
|
+
readonly hashCharacter?: boolean;
|
|
13
|
+
constructor(message?: string, structuredOptions?: CompilationRelativeJsonPointerErrorOptions);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export declare interface CompilationRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
|
|
20
|
+
readonly relativePointer: RelativeJsonPointer;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare const compile: (relativeJsonPointer: RelativeJsonPointer) => string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Evaluates Relative JSON Pointer against ApiDOM fragment.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare const evaluate: <T extends Element_2, U extends Element_2>(relativePointer: string, currentElement: T, rootElement: U) => Element_2 | undefined;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export declare class EvaluationRelativeJsonPointerError<T extends Element_2, U extends Element_2, V extends Element_2> extends RelativeJsonPointerError {
|
|
38
|
+
readonly relativePointer: string;
|
|
39
|
+
readonly currentElement: T;
|
|
40
|
+
readonly rootElement: U;
|
|
41
|
+
readonly cursorElement?: V;
|
|
42
|
+
constructor(message?: string, structuredOptions?: EvaluationRelativeJsonPointerErrorOptions<T, U, V>);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare interface EvaluationRelativeJsonPointerErrorOptions<T extends Element_2, U extends Element_2, V extends Element_2> extends ApiDOMErrorOptions {
|
|
49
|
+
readonly relativePointer: string;
|
|
50
|
+
readonly currentElement: T;
|
|
51
|
+
readonly rootElement: U;
|
|
52
|
+
readonly cursorElement?: V;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export declare class InvalidRelativeJsonPointerError extends RelativeJsonPointerError {
|
|
59
|
+
readonly relativePointer: string;
|
|
60
|
+
constructor(message?: string, structuredOptions?: InvalidRelativeJsonPointerErrorOptions);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
export declare interface InvalidRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
|
|
67
|
+
readonly relativePointer: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
73
|
+
export declare const isRelativeJsonPointer: (value: any) => boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
export declare const parse: (relativePointer: string) => RelativeJsonPointer;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
export declare type RelativeJsonPointer = {
|
|
84
|
+
readonly nonNegativeIntegerPrefix: number;
|
|
85
|
+
readonly indexManipulation?: number;
|
|
86
|
+
readonly jsonPointerTokens?: string[];
|
|
87
|
+
readonly hashCharacter?: boolean;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
export declare class RelativeJsonPointerError extends ApiDOMStructuredError {
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { }
|