UNPKG

dtsgenerator

Version:

TypeScript d.ts file generator for JSON Schema file

76 lines (75 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const url = tslib_1.__importStar(require("url")); const validateIdentifier_1 = require("./validateIdentifier"); class SchemaId { constructor(inputId, parentIds) { this.inputId = inputId; let absoluteId = url.resolve('', inputId); if (parentIds) { parentIds.forEach((parent) => { if (parent) { absoluteId = url.resolve(parent, absoluteId); } }); } if (!absoluteId.includes('#')) { absoluteId += '#'; } if (!absoluteId.includes('://') && !absoluteId.startsWith('/') && !absoluteId.startsWith('#')) { absoluteId = '/' + absoluteId; } this.id = url.parse(absoluteId); this.absoluteId = this.id.href; } getAbsoluteId() { return this.absoluteId; } isEmpty() { return !!this.absoluteId; } isFetchable() { return /https?:\/\//.test(this.absoluteId); } getFileId() { return this.absoluteId.replace(/#.*$/, '#'); } existsJsonPointerHash() { return this.absoluteId === '#' || this.absoluteId.includes('#/'); } getJsonPointerHash() { const m = /(#\/.*)$/.exec(this.absoluteId); if ((m === null || m === void 0 ? void 0 : m[1]) == null) { return '#'; } return decodeURIComponent(m[1]); } toNames() { const uri = this.id; const ids = []; if (uri.host) { ids.push(decodeURIComponent(uri.host)); } const addAllParts = (path) => { const paths = path.split('/'); if (paths.length > 1 && paths[0] === '') { paths.shift(); } paths.forEach((item) => { ids.push(decodeURIComponent(item)); }); }; if (uri.pathname) { addAllParts(uri.pathname); } if (uri.hash && uri.hash.length > 1) { addAllParts(uri.hash.substr(1)); } return ids.map(validateIdentifier_1.toTypeName); } } SchemaId.empty = new SchemaId(''); exports.default = SchemaId;