Skip to content

Commit bf04496

Browse files
Document more methods
1 parent 42505ca commit bf04496

4 files changed

Lines changed: 43 additions & 13 deletions

File tree

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ To quickly generate a ULID, you can simply import the `ulid` function:
6060
```typescript
6161
import { ulid } from "ulid";
6262

63-
ulid(); // 01ARZ3NDEKTSV4RRFFQ69G5FAV
63+
ulid(); // "01ARZ3NDEKTSV4RRFFQ69G5FAV"
6464
```
6565

6666
### Seed Time
6767

6868
You can also input a seed time which will consistently give you the same string for the time component. This is useful for migrating to ulid.
6969

7070
```typescript
71-
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
71+
ulid(1469918176385) // "01ARYZ6S41TSV4RRFFQ69G5FAV"
7272
```
7373

7474
### Monotonic ULIDs
@@ -83,14 +83,14 @@ import { monotonicFactory } from "ulid";
8383
const ulid = monotonicFactory();
8484

8585
// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
86-
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR8
87-
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVR9
88-
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRA
89-
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRB
90-
ulid(150000); // 000XAL6S41ACTAV9WEVGEMMVRC
86+
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVR8"
87+
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVR9"
88+
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRA"
89+
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRB"
90+
ulid(150000); // "000XAL6S41ACTAV9WEVGEMMVRC"
9191

9292
// Even if a lower timestamp is passed (or generated), it will preserve sort order
93-
ulid(100000); // 000XAL6S41ACTAV9WEVGEMMVRD
93+
ulid(100000); // "000XAL6S41ACTAV9WEVGEMMVRD"
9494
```
9595

9696
### Pseudo-Random Number Generators
@@ -104,7 +104,7 @@ By default, `ulid` will not use `Math.random` to generate random values. You can
104104
```typescript
105105
const ulid = monotonicFactory(() => Math.random());
106106

107-
ulid(); // 01BXAVRG61YJ5YSBRM51702F6M
107+
ulid(); // "01BXAVRG61YJ5YSBRM51702F6M"
108108
```
109109

110110
### Validity
@@ -118,6 +118,24 @@ isValid("01ARYZ6S41TSV4RRFFQ69G5FAV"); // true
118118
isValid("01ARYZ6S41TSV4RRFFQ69G5FA"); // false
119119
```
120120

121+
### ULID Time
122+
123+
You can encode and decode ULID timestamps by using `encodeTime` and `decodeTime` respectively:
124+
125+
```typescript
126+
import { decodeTime } from "ulid";
127+
128+
decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV"); // 1469918176385
129+
```
130+
131+
Note that while `decodeTime` works on full ULIDs, `encodeTime` encodes only the _time portion_ of ULIDs:
132+
133+
```typescript
134+
import { encodeTime } from "ulid";
135+
136+
encodeTime(1469918176385); // "01ARYZ6S41"
137+
```
138+
121139
### Tests
122140

123141
Install dependencies using `npm install` first, and then simply run `npm test` to run the test suite.

source/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { isValid, monotonicFactory, ulid } from "./ulid.js";
1+
export { decodeTime, encodeTime, isValid, monotonicFactory, ulid } from "./ulid.js";
22
export { ulidToUUID, uuidToULID } from "./uuid.js";
33
export { fixULIDBase32, incrementBase32 } from "./crockford.js";
44
export { ULIDError, ULIDErrorCode } from "./error.js";
5-
export { MAX_ULID, MIN_ULID } from "./constants.js";
5+
export { MAX_ULID, MIN_ULID, TIME_LEN, TIME_MAX } from "./constants.js";
66
export * from "./types.js";

source/ulid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function encodeRandom(len: number, prng: PRNG): string {
9090
* @param len Length to generate
9191
* @returns The encoded time
9292
*/
93-
export function encodeTime(now: number, len: number): string {
93+
export function encodeTime(now: number, len: number = TIME_LEN): string {
9494
if (isNaN(now)) {
9595
throw new ULIDError(
9696
ULIDErrorCode.EncodeTimeValueMalformed,

test/node/ulid.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { describe, expect, it } from "vitest";
2-
import { monotonicFactory, ulid, ULIDFactory } from "../../";
2+
import { decodeTime, encodeTime, monotonicFactory, ulid, ULIDFactory } from "../../";
33

44
const ULID_REXP = /^[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}$/;
55

6+
describe("decodeTime", function () {
7+
it("extracts a timestamp from a ULID", () => {
8+
expect(decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV")).to.equal(1469918176385);
9+
});
10+
});
11+
12+
describe("encodeTime", function () {
13+
it("encodes a timestamp", () => {
14+
expect(encodeTime(1469918176385)).to.equal("01ARYZ6S41");
15+
});
16+
});
17+
618
describe("monotonicFactory", () => {
719
function stubbedPrng() {
820
return 0.96;

0 commit comments

Comments
 (0)