1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var url = tslib_1.__importStar(require("url"));
|
5 | var validateIdentifier_1 = require("./validateIdentifier");
|
6 | var 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 &&
|
21 | absoluteId[0] !== '/' &&
|
22 | absoluteId[0] !== '#') {
|
23 | absoluteId = '/' + absoluteId;
|
24 | }
|
25 | this.id = url.parse(absoluteId);
|
26 | this.absoluteId = this.id.href;
|
27 | }
|
28 | SchemaId.prototype.getAbsoluteId = function () {
|
29 | return this.absoluteId;
|
30 | };
|
31 | SchemaId.prototype.isEmpty = function () {
|
32 | return !!this.absoluteId;
|
33 | };
|
34 | SchemaId.prototype.isFetchable = function () {
|
35 | return /https?:\/\//.test(this.absoluteId);
|
36 | };
|
37 | SchemaId.prototype.getFileId = function () {
|
38 | return this.absoluteId.replace(/#.*$/, '#');
|
39 | };
|
40 | SchemaId.prototype.existsJsonPointerHash = function () {
|
41 | return this.absoluteId === '#' || /#\//.test(this.absoluteId);
|
42 | };
|
43 | SchemaId.prototype.getJsonPointerHash = function () {
|
44 | var m = /(#\/.*)$/.exec(this.absoluteId);
|
45 | if (m == null) {
|
46 | return '#';
|
47 | }
|
48 | return decodeURIComponent(m[1]);
|
49 | };
|
50 | SchemaId.prototype.toNames = function () {
|
51 | var uri = this.id;
|
52 | var ids = [];
|
53 | if (uri.host) {
|
54 | ids.push(decodeURIComponent(uri.host));
|
55 | }
|
56 | var addAllParts = function (path) {
|
57 | var paths = path.split('/');
|
58 | if (paths.length > 1 && paths[0] === '') {
|
59 | paths.shift();
|
60 | }
|
61 | paths.forEach(function (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 | SchemaId.empty = new SchemaId('');
|
74 | return SchemaId;
|
75 | }());
|
76 | exports.default = SchemaId;
|
77 |
|
\ | No newline at end of file |