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