@swagger-api/apidom-json-pointer 0.68.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 +214 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +57 -0
- package/README.md +78 -0
- package/cjs/errors/EvaluationJsonPointerError.cjs +17 -0
- package/cjs/errors/InvalidJsonPointerError.cjs +17 -0
- package/cjs/errors/index.cjs +9 -0
- package/cjs/index.cjs +96 -0
- package/dist/apidom-json-pointer.browser.js +24375 -0
- package/dist/apidom-json-pointer.browser.min.js +1 -0
- package/es/errors/EvaluationJsonPointerError.js +12 -0
- package/es/errors/InvalidJsonPointerError.js +12 -0
- package/es/errors/index.js +2 -0
- package/es/index.js +86 -0
- package/package.json +57 -0
- package/types/dist.d.ts +17 -0
package/cjs/index.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.uriToPointer = exports.unescape = exports.parse = exports.evaluate = exports.escape = exports.compile = void 0;
|
|
5
|
+
var _ramda = require("ramda");
|
|
6
|
+
var _ramdaAdjunct = require("ramda-adjunct");
|
|
7
|
+
var _apidomCore = require("@swagger-api/apidom-core");
|
|
8
|
+
var _index = require("./errors/index.cjs");
|
|
9
|
+
exports.InvalidJsonPointerError = _index.InvalidJsonPointerError;
|
|
10
|
+
exports.EvaluationJsonPointerError = _index.EvaluationJsonPointerError;
|
|
11
|
+
/**
|
|
12
|
+
* decodeURIComponent can throw URIError in certain cases like 'c%d'.
|
|
13
|
+
* safeDecodeURIComponent is a safe variant of decodeURIComponent that never trows.
|
|
14
|
+
*
|
|
15
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_URI|More info about URIError}
|
|
16
|
+
*/
|
|
17
|
+
const safeDecodeURIComponent = encodedURIComponent => {
|
|
18
|
+
try {
|
|
19
|
+
return decodeURIComponent(encodedURIComponent);
|
|
20
|
+
} catch {
|
|
21
|
+
return encodedURIComponent;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// escape :: String -> String
|
|
26
|
+
const escape = (0, _ramda.pipe)((0, _ramda.replace)(/~/g, '~0'), (0, _ramda.replace)(/\//g, '~1'), encodeURIComponent);
|
|
27
|
+
|
|
28
|
+
// unescape :: String -> String
|
|
29
|
+
exports.escape = escape;
|
|
30
|
+
const unescape = (0, _ramda.pipe)((0, _ramda.replace)(/~1/g, '/'), (0, _ramda.replace)(/~0/g, '~'), safeDecodeURIComponent);
|
|
31
|
+
|
|
32
|
+
// parse :: String -> String[]
|
|
33
|
+
exports.unescape = unescape;
|
|
34
|
+
const parse = pointer => {
|
|
35
|
+
if ((0, _ramdaAdjunct.isEmptyString)(pointer)) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
if (!(0, _ramda.startsWith)('/', pointer)) {
|
|
39
|
+
throw new _index.InvalidJsonPointerError(pointer);
|
|
40
|
+
}
|
|
41
|
+
const tokens = (0, _ramda.pipe)((0, _ramda.split)('/'), (0, _ramda.map)(unescape))(pointer);
|
|
42
|
+
return (0, _ramda.tail)(tokens);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// compile :: String[] -> String
|
|
46
|
+
exports.parse = parse;
|
|
47
|
+
const compile = tokens => {
|
|
48
|
+
if (tokens.length === 0) {
|
|
49
|
+
return '';
|
|
50
|
+
}
|
|
51
|
+
return `/${tokens.map(escape).join('/')}`;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// evaluates JSON Pointer against ApiDOM fragment
|
|
55
|
+
exports.compile = compile;
|
|
56
|
+
const evaluate = (pointer, element) => {
|
|
57
|
+
const tokens = parse(pointer);
|
|
58
|
+
return tokens.reduce((acc, token) => {
|
|
59
|
+
if ((0, _apidomCore.isObjectElement)(acc)) {
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
if (!acc.hasKey(token)) {
|
|
62
|
+
throw new _index.EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
|
|
63
|
+
}
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
return acc.get(token);
|
|
66
|
+
}
|
|
67
|
+
if ((0, _apidomCore.isArrayElement)(acc)) {
|
|
68
|
+
if (!(token in acc.content) || !(0, _ramdaAdjunct.isInteger)(Number(token))) {
|
|
69
|
+
throw new _index.EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
|
|
70
|
+
}
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return acc.get(Number(token));
|
|
73
|
+
}
|
|
74
|
+
throw new _index.EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
|
|
75
|
+
}, element);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns the hash (URL fragment), of the given path.
|
|
80
|
+
* If there is no hash, then the root hash ("#") is returned.
|
|
81
|
+
*/
|
|
82
|
+
exports.evaluate = evaluate;
|
|
83
|
+
const getHash = uri => {
|
|
84
|
+
const hashIndex = uri.indexOf('#');
|
|
85
|
+
if (hashIndex !== -1) {
|
|
86
|
+
return uri.substring(hashIndex);
|
|
87
|
+
}
|
|
88
|
+
return '#';
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// uriToPointer :: String -> String
|
|
92
|
+
const uriToPointer = uri => {
|
|
93
|
+
const hash = getHash(uri);
|
|
94
|
+
return (0, _ramdaAdjunct.trimCharsStart)('#', hash);
|
|
95
|
+
};
|
|
96
|
+
exports.uriToPointer = uriToPointer;
|