JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 386071
  • Score
    100M100P100Q170968F

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typeid-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    TypeID TS

    Official JavaScript implementation of TypeIDs using TypeScript.

    This is the official TypeScript package for TypeIDs written by jetpack.io (authors of the original TypeID spec).

    TypeIDs are a modern, type-safe, globally unique identifier based on the upcoming UUIDv7 standard. They provide a ton of nice properties that make them a great choice as the primary identifiers for your data in a database, APIs, and distributed systems. Read more about TypeIDs in their spec.

    This particular implementation provides an npm package that can be used by any JavaScript or TypeScript project.

    Library Usage

    To create a random TypeID of a given type, use the typeid() function:

    import { typeid } from 'typeid-ts';
    const tid = typeid("prefix");

    The prefix is optional, so if you need to create an id with a type prefix, you can do that too:

    import { typeid } from 'typeid-ts';
    const tid = typeid();

    In addition to the typeid() function, there's also a TypeID class that can be used to encode/decode from other formats.

    For example, to parse an existing typeid from a string:

    import { TypeID } from 'typeid-ts';
    
    const tid = TypeID.fromString("prefix_00041061050r3gg28a1c60t3gf);

    To encode an existing UUID as a TypeID:

    import { TypeID } from 'typeid-ts';
    
    const tid = TypeID.fromUUID("prefix", "00000000-0000-0000-0000-000000000000");

    The full list of methods includes:

    • getType(): Returns the type of the type prefix
    • getSuffix(): Returns uuid suffix in its base32 representation
    • toString(): Encodes the object as a string, using the canonical format
    • asUUID(): Decodes the TypeID into a UUID string in hex format. The type prefix is ignored
    • asUUIDBytes(): Decodes the TypeID into a UUID byte array. The type prefix is ignored
    • fromString(str): Parses a TypeID from a string
    • fromUUID(prefix, uuid): Creates a TypeID from a prefix and a UUID in hex format
    • fromUUIDBytes(prefix, bytes): Creates a TypeID from a prefix and a UUID in byte array format