UNPKG

@maddonkeysoftware/orid-node

Version:

A utility page to help produce and consume Open Resource IDs

105 lines (104 loc) 3.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = exports.isValid = exports.generate = void 0; /* eslint-disable prefer-destructuring */ const string_template_1 = __importDefault(require("string-template")); /** * Generate a properly formatted V1 ORID. * * @param data The metadata object for all ORID parts. */ function generate(data) { const mainFormat = 'orid:1:{provider}:{custom1}:{custom2}:{custom3}:{service}:{suffix}'; const suffixFormat = '{resourceId}{separator}{resourceRider}'; let suffix; if (data.resourceRider) { const separator = data.useSlashSeparator ? '/' : ':'; suffix = (0, string_template_1.default)(suffixFormat, { resourceId: data.resourceId, separator, resourceRider: data.resourceRider, }); } else { suffix = (0, string_template_1.default)(suffixFormat, { resourceId: data.resourceId, }); } return (0, string_template_1.default)(mainFormat, { provider: data.provider, custom1: data.custom1, custom2: data.custom2, custom3: data.custom3, service: data.service, suffix, }); } exports.generate = generate; ; /** * * @param value The item to validate if it is a V1 ORID or not. * @returns True if a valid orid string was supplied, false otherwise. */ function isValid(value) { // Leave the runtime type check in for those not using typescript. const quickCheck = typeof value === 'string' && value.startsWith('orid:1:'); if (!quickCheck) return false; const parts = value.split(':'); return parts.length === 8 || parts.length === 9; } exports.isValid = isValid; ; /** * Parses a properly formatted ORID into an object for easier consumption. * @param orid The ORID to parse. * @returns A ORID object. */ function parse(orid) { // Leave the runtime type check in for those not using typescript. if (typeof orid !== 'string') throw TypeError('orid must be of type string'); if (!orid.startsWith('orid:1:')) throw new Error('Provided string does not appear to be a orid'); const oridParts = orid.split(':'); if (oridParts.length !== 8 && oridParts.length !== 9) throw new Error('ORID appears to be invalid format'); if (oridParts.length === 9) { return { provider: oridParts[2], custom1: oridParts[3], custom2: oridParts[4], custom3: oridParts[5], service: oridParts[6], resourceId: oridParts[7], resourceRider: oridParts[8], }; } let resourceRider; let resourceId; if (oridParts[7].indexOf('/') > -1) { const resourceParts = oridParts[7].split('/'); resourceId = resourceParts[0]; resourceRider = resourceParts.slice(1).join('/'); } else { resourceId = oridParts[7]; resourceRider = undefined; } return { provider: oridParts[2], custom1: oridParts[3], custom2: oridParts[4], custom3: oridParts[5], service: oridParts[6], resourceId, resourceRider, }; } exports.parse = parse; ;