forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathref-utils.ts
More file actions
91 lines (74 loc) · 2.63 KB
/
Copy pathref-utils.ts
File metadata and controls
91 lines (74 loc) · 2.63 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { isPlainObject, isTruthy } from './utils';
import type { Source } from './resolve';
import type { OasRef } from './typings/openapi';
export function joinPointer(base: string, key: string | number) {
if (base === '') base = '#/';
return base[base.length - 1] === '/' ? base + key : base + '/' + key;
}
export function isRef(node: unknown): node is OasRef {
return isPlainObject(node) && typeof node.$ref === 'string';
}
export function isExternalValue(node: unknown) {
return isPlainObject(node) && typeof node.externalValue === 'string';
}
export class Location {
constructor(public source: Source, public pointer: string) {}
child(components: (string | number)[] | string | number) {
return new Location(
this.source,
joinPointer(
this.pointer,
(Array.isArray(components) ? components : [components]).map(escapePointer).join('/')
)
);
}
key() {
return { ...this, reportOnKey: true };
}
get absolutePointer() {
return this.source.absoluteRef + (this.pointer === '#/' ? '' : this.pointer);
}
}
export function unescapePointer(fragment: string): string {
return decodeURIComponent(fragment.replace(/~1/g, '/').replace(/~0/g, '~'));
}
export function escapePointer<T extends string | number>(fragment: T): T {
if (typeof fragment === 'number') return fragment;
return (fragment as string).replace(/~/g, '~0').replace(/\//g, '~1') as T;
}
export function parseRef(ref: string): { uri: string | null; pointer: string[] } {
const [uri, pointer = ''] = ref.split('#/');
return {
uri: (uri.endsWith('#') ? uri.slice(0, -1) : uri) || null,
pointer: parsePointer(pointer),
};
}
export function parsePointer(pointer: string) {
return pointer.split('/').map(unescapePointer).filter(isTruthy);
}
export function pointerBaseName(pointer: string) {
const parts = pointer.split('/');
return parts[parts.length - 1];
}
export function refBaseName(ref: string) {
// eslint-disable-next-line no-useless-escape
const parts = ref.split(/[\/\\]/); // split by '\' and '/'
return parts[parts.length - 1].replace(/\.[^.]+$/, ''); // replace extension with empty string
}
export function isAbsoluteUrl(ref: string) {
return ref.startsWith('http://') || ref.startsWith('https://');
}
export function isMappingRef(mapping: string) {
// TODO: proper detection of mapping refs
return (
mapping.startsWith('#') ||
mapping.startsWith('https://') ||
mapping.startsWith('http://') ||
mapping.startsWith('./') ||
mapping.startsWith('../') ||
mapping.indexOf('/') > -1
);
}
export function isAnchor(ref: string) {
return /^#[A-Za-z][A-Za-z0-9\-_:.]*$/.test(ref);
}