Skip to content

A minimal utility for converting between UUID (Universally Unique IDentifier) and ULID (Universally Unique Lexicographically Sortable Identifier) in their string and binary (Uint8Array) forms.

License

Notifications You must be signed in to change notification settings

jsr-core/uxidutil

Repository files navigation

uxidutil

jsr test codecov

A minimal utility for converting between UUID (Universally Unique IDentifier) and ULID (Universally Unique Lexicographically Sortable Identifier) in their string and binary (Uint8Array) forms.

Features

  • parseUuid(string): Convert UUID string to Uint8Array
  • formatUuid(Uint8Array): Convert Uint8Array to UUID string
  • parseUlid(string): Convert ULID string to Uint8Array
  • formatUlid(Uint8Array): Convert Uint8Array to ULID string

Usage

Note

Use @std/uuid and @std/ulid to generate new UUIDs and ULIDs.

import {
  formatUlid,
  formatUuid,
  parseUlid,
  parseUuid,
} from "jsr:@core/uxidutil";

const uuid = "123e4567-e89b-12d3-a456-426614174000";
const ulid = "01HZYHQ6X4V9E9ZEM8Y6MVPMX3";

// UUID to bytes and back
const uuidBytes = parseUuid(uuid);
const uuidAgain = formatUuid(uuidBytes);

// ULID to bytes and back
const ulidBytes = parseUlid(ulid);
const ulidAgain = formatUlid(ulidBytes);

// Interchangeable via Uint8Array
const fromUuidToUlid = formatUlid(parseUuid(uuid));
const fromUlidToUuid = formatUuid(parseUlid(ulid));

API

parseUuid(uuid: string): Uint8Array

Parses a standard UUID string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) into a 16-byte array.

formatUuid(bytes: Uint8Array): string

Formats a 16-byte array into a UUID string.

parseUlid(ulid: string): Uint8Array

Parses a 26-character ULID string into a 16-byte array. Strictly validates Crockford Base32 (excluding I, L, O, U).

formatUlid(bytes: Uint8Array): string

Formats a 16-byte array into a 26-character ULID string.

License

The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.

About

A minimal utility for converting between UUID (Universally Unique IDentifier) and ULID (Universally Unique Lexicographically Sortable Identifier) in their string and binary (Uint8Array) forms.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published