UNPKG

@neuledge/id

Version:

Globally unique identifier based on the upcoming UUIDv7 standard.

65 lines 1.28 kB
// src/id.ts import { TypeID, typeid } from "typeid-js"; var Id = class { constructor(prefix) { this.prefix = prefix; this.type = `${this.prefix}_`; } type; generate() { return typeid(this.prefix).toString(); } is(id) { if (typeof id !== "string") { return false; } try { const typeId = 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 = 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 typeid(this.prefix, suffix).toString(); } // UUID getUUID(id) { return this.getTypeId(id).toUUID(); } fromUUID(uuid) { return TypeID.fromUUID(this.prefix, uuid).toString(); } // bytes getBytes(id) { return this.getTypeId(id).toUUIDBytes(); } fromBytes(bytes) { return TypeID.fromUUIDBytes( this.prefix, bytes ).toString(); } }; export { Id }; //# sourceMappingURL=index.mjs.map