From ddcb5a62edf1a7a9a8677faf7b48066c8364abbf Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 1 Dec 2023 16:13:28 +1100 Subject: [PATCH 1/2] chore(ulid): complete documentation --- ulid/_util.ts | 5 ++--- ulid/mod.ts | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/ulid/_util.ts b/ulid/_util.ts index 4d0fe31e88e0..896bf718b176 100644 --- a/ulid/_util.ts +++ b/ulid/_util.ts @@ -1,8 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -interface ULID { - (seedTime?: number): string; -} +/** Type for a ULID generator function. */ +export type ULID = (seedTime?: number) => string; // These values should NEVER change. If // they do, we're no longer making ulids! diff --git a/ulid/mod.ts b/ulid/mod.ts index 398cf219c97c..893578a22142 100644 --- a/ulid/mod.ts +++ b/ulid/mod.ts @@ -4,12 +4,10 @@ // This module is browser compatible. /** + * Utilities for generating and working with + * [Universally Unique Lexicographically Sortable Identifiers (ULIDs)]{@link https://github.com/ulid/spec}. + * * @module - * @example - * ```ts - * import { ulid } from "https://deno.land/std@$STD_VERSION/ulid/mod.ts"; - * ulid(); // 01ARZ3NDEKTSV4RRFFQ69G5FAV - * ``` */ import { @@ -21,10 +19,21 @@ import { RANDOM_LEN, TIME_LEN, TIME_MAX, + ULID, } from "./_util.ts"; +export type { ULID } from "./_util.ts"; + /** - * Extracts the timestamp given a ULID + * Extracts the timestamp given a ULID. + * + * @example + * ```ts + * import { ulid, decodeTime } from "https://deno.land/std@$STD_VERSION/ulid/mod.ts"; + * + * const x = ulid(150000); + * decodeTime(x); // 150000 + * ``` */ export function decodeTime(id: string): number { if (id.length !== TIME_LEN + RANDOM_LEN) { @@ -48,6 +57,9 @@ export function decodeTime(id: string): number { } /** + * Generate a monotonically increasing ULID, optionally based on a given + * timestamp. + * * @example * ```ts * import { monotonicUlid } from "https://deno.land/std@$STD_VERSION/ulid/mod.ts"; @@ -63,9 +75,11 @@ export function decodeTime(id: string): number { * monotonicUlid(100000); // 000XAL6S41ACTAV9WEVGEMMVRD * ``` */ -export const monotonicUlid = monotonicFactory(); +export const monotonicUlid: ULID = monotonicFactory(); /** + * Generate a ULID, optionally based on a given timestamp. + * * @example * ```ts * import { ulid } from "https://deno.land/std@$STD_VERSION/ulid/mod.ts"; @@ -75,6 +89,6 @@ export const monotonicUlid = monotonicFactory(); * ulid(1469918176385); // 01ARYZ6S41TSV4RRFFQ69G5FAV * ``` */ -export function ulid(seedTime: number = Date.now()): string { +export function ulid(seedTime = Date.now()): string { return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN); } From c04ceac51a91e58e0531f882cfcbc6e3bb93a060 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 1 Dec 2023 20:01:32 +1100 Subject: [PATCH 2/2] fix --- ulid/mod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ulid/mod.ts b/ulid/mod.ts index 893578a22142..a46a875d2b14 100644 --- a/ulid/mod.ts +++ b/ulid/mod.ts @@ -89,6 +89,6 @@ export const monotonicUlid: ULID = monotonicFactory(); * ulid(1469918176385); // 01ARYZ6S41TSV4RRFFQ69G5FAV * ``` */ -export function ulid(seedTime = Date.now()): string { +export function ulid(seedTime: number = Date.now()): string { return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN); }