UNPKG

2.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const url = tslib_1.__importStar(require("url"));
5const validateIdentifier_1 = require("./validateIdentifier");
6class SchemaId {
7 constructor(inputId, parentIds) {
8 this.inputId = inputId;
9 let absoluteId = url.resolve('', inputId);
10 if (parentIds) {
11 parentIds.forEach((parent) => {
12 if (parent) {
13 absoluteId = url.resolve(parent, absoluteId);
14 }
15 });
16 }
17 if (!absoluteId.includes('#')) {
18 absoluteId += '#';
19 }
20 if (!absoluteId.includes('://') &&
21 !absoluteId.startsWith('/') &&
22 !absoluteId.startsWith('#')) {
23 absoluteId = '/' + absoluteId;
24 }
25 this.id = url.parse(absoluteId);
26 this.absoluteId = this.id.href;
27 }
28 getAbsoluteId() {
29 return this.absoluteId;
30 }
31 isEmpty() {
32 return !!this.absoluteId;
33 }
34 isFetchable() {
35 return /https?:\/\//.test(this.absoluteId);
36 }
37 getFileId() {
38 return this.absoluteId.replace(/#.*$/, '#');
39 }
40 existsJsonPointerHash() {
41 return this.absoluteId === '#' || this.absoluteId.includes('#/');
42 }
43 getJsonPointerHash() {
44 const m = /(#\/.*)$/.exec(this.absoluteId);
45 if ((m === null || m === void 0 ? void 0 : m[1]) == null) {
46 return '#';
47 }
48 return decodeURIComponent(m[1]);
49 }
50 toNames() {
51 const uri = this.id;
52 const ids = [];
53 if (uri.host) {
54 ids.push(decodeURIComponent(uri.host));
55 }
56 const addAllParts = (path) => {
57 const paths = path.split('/');
58 if (paths.length > 1 && paths[0] === '') {
59 paths.shift();
60 }
61 paths.forEach((item) => {
62 ids.push(decodeURIComponent(item));
63 });
64 };
65 if (uri.pathname) {
66 addAllParts(uri.pathname);
67 }
68 if (uri.hash && uri.hash.length > 1) {
69 addAllParts(uri.hash.substr(1));
70 }
71 return ids.map(validateIdentifier_1.toTypeName);
72 }
73}
74exports.default = SchemaId;
75SchemaId.empty = new SchemaId('');