UNPKG

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