All files / src/lib id.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 2/2
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19              168x     5806x           79x    
import { monotonicFactory } from "ulid";
 
// One process-wide monotonic factory: base ulid() randomizes within a millisecond,
// and items/collections carry JS-ms createdAt, so same-ms rows tied and
// compareByCreatedAtThenId ordered them by chance (purses-w7ss.16). Monotonic
// within this process only — rows minted concurrently on two instances stay
// unordered, which is honest: they were concurrent.
const nextUlid = monotonicFactory();
 
export function generateId(prefix: string): string {
  return `${prefix}_${nextUlid().toLowerCase()}`;
}
 
/** A value shaped like one of our ids (`prefix_` + lowercase base32). URL params that
 *  name an id (`?removed=`, `?restored=`) pass this before they are used or copied. */
export function isIdShaped(value: unknown): value is string {
  return typeof value === "string" && /^[a-z]+_[0-9a-z]{1,40}$/.test(value);
}