Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ulid): complete documentation #3889

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ulid/_util.ts
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
30 changes: 22 additions & 8 deletions ulid/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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 {
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN);
}