On this page
@std/ulid
Overview Jump to heading
Utilities for generating and working with Universally Unique Lexicographically Sortable Identifiers (ULIDs).
To generate a ULID use the ulid function. This will generate a
ULID based on the current time.
import { ulid } from "@std/ulid";
ulid(); // 01HYFKMDF3HVJ4J3JZW8KXPVTY
ulid does not guarantee that the ULIDs will be strictly
increasing for the same current time. If you need to guarantee that the ULIDs
will be strictly increasing, even for the same current time, use the
monotonicUlid function.
import { monotonicUlid } from "@std/ulid";
monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAS
monotonicUlid(); // 01HYFKHG5F8RHM2PM3D7NSTDAT
Because each ULID encodes the time it was generated, you can extract the
timestamp from a ULID using the decodeTime function.
import { decodeTime, ulid } from "@std/ulid";
import { assertEquals } from "@std/assert";
const timestamp = 150_000;
const ulidString = ulid(timestamp);
assertEquals(decodeTime(ulidString), timestamp);
Add to your project Jump to heading
deno add jsr:@std/ulid
See all symbols in @std/ulid on
什么是 ULID? Jump to heading
ULID(通用唯一的按字典序排序的标识符)是一个 26 字符的字符串,用作唯一标识符。它结合了时间戳和随机数据,使其既唯一又可以按创建时间排序。ULID 设计为 URL 安全,可用于数据库和分布式系统等各种应用。
为什么使用 @std/ulid? Jump to heading
当您需要唯一且可按时间排序的 ID,且生成和共享都很方便时使用。非常适合数据库、分布式系统和面向用户的令牌。
使用小贴士 Jump to heading
- 需要在同一毫秒内严格递增值时,请使用
monotonicUlid()。 - 可以用
decodeTime(ulid)提取时间戳,帮助调试。