source: node_modules/@swagger-api/apidom-json-pointer/cjs/parse.cjs@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.uriToPointer = exports.default = void 0;
6var _ramda = require("ramda");
7var _ramdaAdjunct = require("ramda-adjunct");
8var _unescape = _interopRequireDefault(require("./unescape.cjs"));
9var _InvalidJsonPointerError = _interopRequireDefault(require("./errors/InvalidJsonPointerError.cjs"));
10// parse :: String -> String[]
11const parse = pointer => {
12 if ((0, _ramdaAdjunct.isEmptyString)(pointer)) {
13 return [];
14 }
15 if (!(0, _ramda.startsWith)('/', pointer)) {
16 throw new _InvalidJsonPointerError.default(`Invalid JSON Pointer "${pointer}". JSON Pointers must begin with "/"`, {
17 pointer
18 });
19 }
20 try {
21 const tokens = (0, _ramda.pipe)((0, _ramda.split)('/'), (0, _ramda.map)(_unescape.default))(pointer);
22 return (0, _ramda.tail)(tokens);
23 } catch (error) {
24 throw new _InvalidJsonPointerError.default(`JSON Pointer parsing of "${pointer}" encountered an error.`, {
25 pointer,
26 cause: error
27 });
28 }
29};
30
31/**
32 * Returns the hash (URL fragment), of the given path.
33 * If there is no hash, then the root hash ("#") is returned.
34 */
35const getHash = uri => {
36 const hashIndex = uri.indexOf('#');
37 if (hashIndex !== -1) {
38 return uri.substring(hashIndex);
39 }
40 return '#';
41};
42
43// uriToPointer :: String -> String
44const uriToPointer = uri => {
45 const hash = getHash(uri);
46 return (0, _ramdaAdjunct.trimCharsStart)('#', hash);
47};
48exports.uriToPointer = uriToPointer;
49var _default = exports.default = parse;
Note: See TracBrowser for help on using the repository browser.