UNPKG

2.25 kBPlain TextView Raw
1export interface Specifier {
2 rootName?: string;
3 collection?: string;
4 namespace?: string;
5 name?: string;
6 type?: string;
7}
8
9export function isSpecifierStringAbsolute(specifier: string): boolean {
10 let split = specifier.split(':');
11 let type = split[0];
12 let path = split[1];
13 return !!(type && path && path.indexOf('/') === 0 && path.split('/').length > 3);
14}
15
16export function isSpecifierObjectAbsolute(specifier: Specifier): boolean {
17 return specifier.rootName !== undefined &&
18 specifier.collection !== undefined &&
19 specifier.name !== undefined &&
20 specifier.type !== undefined;
21}
22
23export function serializeSpecifier(specifier: Specifier): string {
24 let type = specifier.type;
25 let path = serializeSpecifierPath(specifier);
26
27 if (path) {
28 return type + ':' + path;
29 } else {
30 return type;
31 }
32}
33
34export function serializeSpecifierPath(specifier: Specifier): string {
35 let path = [];
36 if (specifier.rootName) {
37 path.push(specifier.rootName);
38 }
39 if (specifier.collection) {
40 path.push(specifier.collection);
41 }
42 if (specifier.namespace) {
43 path.push(specifier.namespace);
44 }
45 if (specifier.name) {
46 path.push(specifier.name);
47 }
48
49 if (path.length > 0) {
50 let fullPath = path.join('/');
51 if (isSpecifierObjectAbsolute(specifier)) {
52 fullPath = '/' + fullPath;
53 }
54 return fullPath;
55 }
56}
57
58export function deserializeSpecifier(specifier: string): Specifier {
59 let obj: Specifier = {};
60
61 if (specifier.indexOf(':') > -1) {
62 let split = specifier.split(':');
63 let type = split[0];
64 let path = split[1];
65 obj.type = type;
66
67 let pathSegments;
68
69 if (path.indexOf('/') === 0) {
70 pathSegments = path.substr(1).split('/');
71 if (path.substr(1).startsWith('@')) {
72 obj.rootName = pathSegments.shift() + '/' + pathSegments.shift();
73 } else {
74 obj.rootName = pathSegments.shift();
75 }
76 obj.collection = pathSegments.shift();
77 } else {
78 pathSegments = path.split('/');
79 }
80
81 if (pathSegments.length > 0) {
82 obj.name = pathSegments.pop();
83
84 if (pathSegments.length > 0) {
85 obj.namespace = pathSegments.join('/');
86 }
87 }
88
89 } else {
90 obj.type = specifier;
91 }
92
93 return obj;
94}