Skip to content

Commit f86c10a

Browse files
authored
refactor(ulid): remove len argument from encodeTime() and encodeRandom() (denoland#5054)
1 parent 635e062 commit f86c10a

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

ulid/_util.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ function replaceCharAt(str: string, index: number, char: string) {
1616
return str.substring(0, index) + char + str.substring(index + 1);
1717
}
1818

19-
export function encodeTime(now: number, len: number = TIME_LEN): string {
20-
if (!Number.isInteger(now) || now < 0 || now > TIME_MAX) {
19+
export function encodeTime(timestamp: number): string {
20+
if (!Number.isInteger(timestamp) || timestamp < 0 || timestamp > TIME_MAX) {
2121
throw new RangeError(
22-
"Time must be a positive integer less than " + TIME_MAX,
22+
`Time must be a positive integer less than ${TIME_MAX}`,
2323
);
2424
}
2525
let str = "";
26-
for (; len > 0; len--) {
27-
const mod = now % ENCODING_LEN;
26+
for (let len = TIME_LEN; len > 0; len--) {
27+
const mod = timestamp % ENCODING_LEN;
2828
str = ENCODING[mod] + str;
29-
now = (now - mod) / ENCODING_LEN;
29+
timestamp = Math.floor(timestamp / ENCODING_LEN);
3030
}
3131
return str;
3232
}
3333

34-
export function encodeRandom(len: number): string {
34+
export function encodeRandom(): string {
3535
let str = "";
36-
const randomBytes = crypto.getRandomValues(new Uint8Array(len));
37-
for (const randomByte of randomBytes) {
38-
str += ENCODING[randomByte % ENCODING_LEN];
36+
const bytes = crypto.getRandomValues(new Uint8Array(RANDOM_LEN));
37+
for (const byte of bytes) {
38+
str += ENCODING[byte % ENCODING_LEN];
3939
}
4040
return str;
4141
}
@@ -67,10 +67,10 @@ export function monotonicFactory(encodeRand = encodeRandom): ULID {
6767
return function ulid(seedTime: number = Date.now()): string {
6868
if (seedTime <= lastTime) {
6969
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
70-
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
70+
return encodeTime(lastTime) + incrementedRandom;
7171
}
7272
lastTime = seedTime;
73-
const newRandom = (lastRandom = encodeRand(RANDOM_LEN));
74-
return encodeTime(seedTime, TIME_LEN) + newRandom;
73+
const newRandom = (lastRandom = encodeRand());
74+
return encodeTime(seedTime) + newRandom;
7575
};
7676
}

ulid/mod.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import {
5050
ENCODING,
5151
ENCODING_LEN,
5252
monotonicFactory,
53-
RANDOM_LEN,
5453
TIME_LEN,
5554
TIME_MAX,
5655
ULID_LEN,
@@ -175,5 +174,5 @@ export function monotonicUlid(seedTime: number = Date.now()): string {
175174
* @returns A ULID.
176175
*/
177176
export function ulid(seedTime: number = Date.now()): string {
178-
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN);
177+
return encodeTime(seedTime) + encodeRandom();
179178
}

ulid/test.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import { decodeTime, monotonicUlid, ulid } from "./mod.ts";
99
import {
1010
encodeRandom,
1111
encodeTime,
12-
ENCODING,
13-
ENCODING_LEN,
1412
incrementBase32,
1513
monotonicFactory,
14+
RANDOM_LEN,
1615
} from "./_util.ts";
1716

1817
Deno.test("incrementBase32()", async (t) => {
@@ -46,28 +45,20 @@ Deno.test("incrementBase32()", async (t) => {
4645

4746
Deno.test("encodeTime()", async (t) => {
4847
await t.step("should return expected encoded result", () => {
49-
assertEquals("01ARYZ6S41", encodeTime(1469918176385, 10));
50-
});
51-
52-
await t.step("should change length properly", () => {
53-
assertEquals("0001AS99AA60", encodeTime(1470264322240, 12));
54-
});
55-
56-
await t.step("should truncate time if not enough length", () => {
57-
assertEquals("AS4Y1E11", encodeTime(1470118279201, 8));
48+
assertEquals("01ARYZ6S41", encodeTime(1469918176385));
5849
});
5950

6051
await t.step("should throw an error", async (t) => {
6152
await t.step("if time greater than (2 ^ 48) - 1", () => {
6253
assertThrows(() => {
63-
encodeTime(Math.pow(2, 48), 8);
54+
encodeTime(Math.pow(2, 48));
6455
}, Error);
6556
});
6657

6758
await t.step("if time is not a number", () => {
6859
assertThrows(() => {
6960
// deno-lint-ignore no-explicit-any
70-
encodeTime("test" as any, 3);
61+
encodeTime("test" as any);
7162
}, Error);
7263
});
7364

@@ -93,7 +84,7 @@ Deno.test("encodeTime()", async (t) => {
9384

9485
Deno.test("encodeRandom()", async (t) => {
9586
await t.step("should return correct length", () => {
96-
assertEquals(12, encodeRandom(12).length);
87+
assertEquals(RANDOM_LEN, encodeRandom().length);
9788
});
9889
});
9990

@@ -146,13 +137,8 @@ Deno.test("ulid()", async (t) => {
146137
});
147138

148139
Deno.test("monotonicUlid() handles monotonicity", async (t) => {
149-
function encodeRandom(len: number): string {
150-
let str = "";
151-
const randomBytes = new Array(len).fill(30);
152-
for (let i = 0; i < len; i++) {
153-
str += ENCODING[randomBytes[i] % ENCODING_LEN];
154-
}
155-
return str;
140+
function encodeRandom(): string {
141+
return "YYYYYYYYYYYYYYYY";
156142
}
157143

158144
await t.step("without seedTime", async (t) => {

0 commit comments

Comments
 (0)