UNPKG

@neuledge/id

Version:

Globally unique identifier based on the upcoming UUIDv7 standard.

92 lines (90 loc) 2.39 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Id: () => Id }); module.exports = __toCommonJS(src_exports); // src/id.ts var import_typeid_js = require("typeid-js"); var Id = class { constructor(prefix) { this.prefix = prefix; this.type = `${this.prefix}_`; } type; generate() { return (0, import_typeid_js.typeid)(this.prefix).toString(); } is(id) { if (typeof id !== "string") { return false; } try { const typeId = import_typeid_js.TypeID.fromString(id); return typeId.getType() === this.prefix; } catch { return false; } } /** * @deprecated Use `is` instead */ isValid(id) { return this.is(id); } // TypeID getTypeId(id) { const typeId = import_typeid_js.TypeID.fromString(id); if (typeId.getType() !== this.prefix) { throw new Error(`Invalid id type: ${id}`); } return typeId; } // Suffix getSuffix(id) { return this.getTypeId(id).getSuffix(); } fromSuffix(suffix) { return (0, import_typeid_js.typeid)(this.prefix, suffix).toString(); } // UUID getUUID(id) { return this.getTypeId(id).toUUID(); } fromUUID(uuid) { return import_typeid_js.TypeID.fromUUID(this.prefix, uuid).toString(); } // bytes getBytes(id) { return this.getTypeId(id).toUUIDBytes(); } fromBytes(bytes) { return import_typeid_js.TypeID.fromUUIDBytes( this.prefix, bytes ).toString(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Id }); //# sourceMappingURL=index.js.map