upid-ts
v0.1.9
Published
Universally Unique Prefixed Lexicographically Sortable Identifier
Downloads
102
Readme
upid-ts
This is a TypeScript implementation of the UPID spec. A UPID is like a UUID but has a 40-bit timestamp so is sortable, and has a four-character prefix, so it's pretty and useful!
Published at npmjs/upid-ts. Works with Node and browser, and probably Bun/Deno too. 1827 bytes gzip'd.
This is a UPID in TypeScript:
upid("user") // user_2accvpp5guht4dts56je5a
Implementations in other languages (Python, Rust, Postgres) are listed at the carderne/upid repo.
Demo
You can give it a spin at upid.rdrn.me.
Installation
With a build step:
npm install --save upid-ts
Directly in a browser:
import { upid } from "https://unpkg.com/upid-ts";
Usage
Use in a program:
import { upid } from "upid-ts";
upid("user") // user_2accvpp5guht4dts56je5a
Or more explicitly:
import { UPID } from "upid-ts";
UPID.from_prefix("user");
Specifying your own timestamp:
const ms = BigInt(1720366562288);
const u = UPID.fromPrefixAndMilliseconds("user", ms);
From and to a string:
const u = UPID.from_str("user_2accvpp5guht4dts56je5a");
u.to_str() // user_2a...
Get stuff out:
u.prefix // user
Development
npm install
npm run fmt
npm run lint
npm run test
npm run build
Please open a PR if you spot a bug or improvement!