npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeid-unboxed

v0.5.0-1

Published

Alternative unboxed implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs

Downloads

33

Readme

typeid-unboxed

An alternative TypeScript implementation of TypeIDs, using strings for runtime representation.

TypeIDs are modern, type-safe, globally unique identifiers based on the upcoming UUIDv7 standard. They offer numerous advantages, making them an excellent choice for primary identifiers in databases, APIs, and distributed systems. Learn more about TypeIDs in their spec.

This package, typeid-unboxed, is an alternative implementation of the original TypeID concept by jetpack.io, which uses class instances at runtime. In contrast, typeid-unboxed utilizes strings in runtime while achieving type safety through branded types, offering a lightweight and flexible approach that does not require serialization and de-serialization steps.

Forked from the official TypeID-JS package, this version is tailored for TypeScript projects that prefer a string-based representation.

Installation

Using npm:

Installation

Using npm:

npm install typeid-unboxed

Using yarn:

yarn add typeid-unboxed

Using pnpm:

pnpm add typeid-unboxed

``

Usage

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

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

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

import { typeid } from "typeid-unboxed";
const tid = typeid();

The return type of typeid("prefix") is TypeID<"prefix">, which lets you use TypeScript's type checking to ensure you are passing the correct type prefix to functions that expect it.

For example, you can create a function that only accepts TypeIDs of type user:

import { typeid, TypeID } from "typeid-unboxed";

function doSomethingWithUserID(id: TypeID<"user">) {
  // ...
}

In addition to the typeid() function, the TypeID class has additional methods to encode/decode from other formats.

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

import { fromString } from "typeid-unboxed";

// The second argument is optional, but it converts to type TypeID<"prefix"> instead
// of TypeID<string>
const tid = fromString("prefix_00041061050r3gg28a1c60t3gf", "prefix");

To encode an existing UUID as a TypeID:

import { fromUUID } from "typeid-unboxed";

// In this case TypeID<"prefix"> is inferred from the second argument
const tid = fromUUID("00000000-0000-0000-0000-000000000000", "prefix");

The full list of exported functions includes:

  • typeid(prefix?, suffix?): Creates a TypeID with an optional prefix and suffix.
  • fromString(typeId, prefix?): Parses a TypeID from a string, optionally validating against a provided prefix.
  • parseTypeID(typeId): Parses a TypeID string into its prefix and suffix components.
  • getType(typeId): Retrieves the prefix from a TypeID.
  • getSuffix(typeId): Retrieves the suffix from a TypeID.
  • isTypeID(value): Checks if a given string is a valid TypeID format.
  • toUUID(typeId): Decodes the TypeID into a UUID string in hex format. The type prefix is ignored.
  • toUUIDBytes(typeId): Decodes the TypeID into a UUID byte array. The type prefix is ignored.
  • fromUUID(uuid, prefix?): Creates a TypeID from a UUID in hex format, with an optional prefix.
  • fromUUIDBytes(prefix, bytes): Creates a TypeID from a prefix and a UUID in byte array format.