Skip to content

getUlidFromId

function getUlidFromId(__namedParameters): string | null;

Defined in: ulid-utils.ts:55

Get the ULID part of a prefixed (usr_01jd…) or plain id, uppercased.

It only strips the prefix and does not validate the ULID itself: "usr_hello" gives "HELLO". Use isValidUlid when you need a real ULID.

Parameter Type
__namedParameters { id: string; throwOnInvalid?: boolean; }
__namedParameters.id string
__namedParameters.throwOnInvalid? boolean

string | null

The ULID part, or null for an empty id or one with more than one _. With throwOnInvalid: true, those cases throw instead ("ID is required" / "Invalid ID format").

import { getUlidFromId } from "@xtandard/lib/ulid";
getUlidFromId({ id: "usr_01jd8x3kq6f2v9w4z7y5b8c1ne" }); // "01JD8X3KQ6F2V9W4Z7Y5B8C1NE"
getUlidFromId({ id: "01JD8X3KQ6F2V9W4Z7Y5B8C1NE" }); // "01JD8X3KQ6F2V9W4Z7Y5B8C1NE"
getUlidFromId({ id: "usr_hello" }); // "HELLO" (not validated)
getUlidFromId({ id: "a_b_c" }); // null
getUlidFromId({ id: "a_b_c", throwOnInvalid: true }); // throws Error("Invalid ID format")