This repository was archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.ts
More file actions
48 lines (39 loc) · 1.29 KB
/
utils.ts
File metadata and controls
48 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as URI from 'urijs';
import { ExtendedURI } from './uri';
const replace = (str: string, find: string, repl: string): string => {
// modified from http://jsperf.com/javascript-replace-all/10
const orig = str.toString();
let res = '';
let rem = orig;
let beg = 0;
let end = rem.indexOf(find);
while (end > -1) {
res += orig.substring(beg, beg + end) + repl;
rem = rem.substring(end + find.length, rem.length);
beg += end + find.length;
end = rem.indexOf(find);
}
if (rem.length > 0) {
res += orig.substring(orig.length - rem.length, orig.length);
}
return res;
};
const encodeFragmentSegment = (segment: string): string => {
return replace(replace(segment, '~', '~0'), '/', '~1');
};
// TODO: move to @stoplight/json
/** @hidden */
export const addToJSONPointer = (pointer: string, part: string): string => {
return `${pointer}/${encodeFragmentSegment(part)}`;
};
/** @hidden */
export const uriToJSONPointer = (uri: URI | ExtendedURI): string => {
if ('length' in uri && uri.length === 0) {
return '';
}
return uri.fragment() !== '' ? `#${uri.fragment()}` : uri.href() === '' ? '#' : '';
};
/** @hidden */
export const uriIsJSONPointer = (ref: URI | ExtendedURI): boolean => {
return (!('length' in ref) || ref.length > 0) && ref.path() === '';
};