main
Last change
on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import { map, pipe, split, startsWith, tail } from 'ramda';
|
---|
| 2 | import { isEmptyString, trimCharsStart } from 'ramda-adjunct';
|
---|
| 3 | import unescape from "./unescape.mjs";
|
---|
| 4 | import InvalidJsonPointerError from "./errors/InvalidJsonPointerError.mjs"; // parse :: String -> String[]
|
---|
| 5 | const parse = pointer => {
|
---|
| 6 | if (isEmptyString(pointer)) {
|
---|
| 7 | return [];
|
---|
| 8 | }
|
---|
| 9 | if (!startsWith('/', pointer)) {
|
---|
| 10 | throw new InvalidJsonPointerError(`Invalid JSON Pointer "${pointer}". JSON Pointers must begin with "/"`, {
|
---|
| 11 | pointer
|
---|
| 12 | });
|
---|
| 13 | }
|
---|
| 14 | try {
|
---|
| 15 | const tokens = pipe(split('/'), map(unescape))(pointer);
|
---|
| 16 | return tail(tokens);
|
---|
| 17 | } catch (error) {
|
---|
| 18 | throw new InvalidJsonPointerError(`JSON Pointer parsing of "${pointer}" encountered an error.`, {
|
---|
| 19 | pointer,
|
---|
| 20 | cause: error
|
---|
| 21 | });
|
---|
| 22 | }
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Returns the hash (URL fragment), of the given path.
|
---|
| 27 | * If there is no hash, then the root hash ("#") is returned.
|
---|
| 28 | */
|
---|
| 29 | const getHash = uri => {
|
---|
| 30 | const hashIndex = uri.indexOf('#');
|
---|
| 31 | if (hashIndex !== -1) {
|
---|
| 32 | return uri.substring(hashIndex);
|
---|
| 33 | }
|
---|
| 34 | return '#';
|
---|
| 35 | };
|
---|
| 36 |
|
---|
| 37 | // uriToPointer :: String -> String
|
---|
| 38 | export const uriToPointer = uri => {
|
---|
| 39 | const hash = getHash(uri);
|
---|
| 40 | return trimCharsStart('#', hash);
|
---|
| 41 | };
|
---|
| 42 | export default parse; |
---|
Note:
See
TracBrowser
for help on using the repository browser.