Skip to content

Commit fe2ddea

Browse files
✨ Refactor IdHelper to encapsulate options and simplify regex generation; remove unused getIdRegex utility
1 parent 8343207 commit fe2ddea

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

src/id-helper.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,31 @@ export class IdHelper<
1212

1313
constructor(public prefix: P, public options: Partial<Options<S>> = {}) {}
1414

15-
public generate(): GeneratedId<P, SeparatorOrDefault<S>> {
15+
private get optionsOrDefaults() {
1616
const {
1717
separator = IdHelper.DEFAULT_SEPARATOR,
1818
length = IdHelper.DEFAULT_LENGTH,
1919
customAlphabets = IdHelper.DEFAULT_ALPHABETS,
2020
} = this.options;
2121

22+
return {
23+
separator,
24+
length,
25+
customAlphabets,
26+
} as Options<S>;
27+
}
28+
29+
public get regex(): RegExp {
30+
const { separator, length, customAlphabets } = this.optionsOrDefaults;
31+
32+
return new RegExp(
33+
`^${this.prefix}${separator}[${customAlphabets}]{${length}}$`
34+
);
35+
}
36+
37+
public generate(): GeneratedId<P, SeparatorOrDefault<S>> {
38+
const { separator, length, customAlphabets } = this.optionsOrDefaults;
39+
2240
const nanoid = customAlphabet(customAlphabets, length);
2341

2442
const id = nanoid();

src/utils.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/validators/validbot.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
import { IdHelper } from "../id-helper";
22
import { custom as vCustom } from "valibot";
3-
import { getIdRegex } from "../utils";
43
import type { GeneratedId, SeparatorOrDefault } from "../types";
54

65
export function createValibotIdSchema<
76
P extends string,
87
S extends string | undefined = undefined
98
>(idHelper: IdHelper<P, S>) {
10-
const {
11-
separator = IdHelper.DEFAULT_SEPARATOR,
12-
length = IdHelper.DEFAULT_LENGTH,
13-
customAlphabets = IdHelper.DEFAULT_ALPHABETS,
14-
} = idHelper.options;
15-
16-
const idRegex = getIdRegex(
17-
idHelper.prefix,
18-
separator,
19-
length,
20-
customAlphabets
21-
);
9+
const { regex } = idHelper;
2210

2311
return vCustom<GeneratedId<P, SeparatorOrDefault<S>>>(val => {
24-
return typeof val === "string" && idRegex.test(val);
12+
return typeof val === "string" && regex.test(val);
2513
}, "Invalid ID Format");
2614
}

src/validators/zod.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ export function createZodIdSchema<
66
P extends string,
77
S extends string | undefined = undefined
88
>(idHelper: IdHelper<P, S>) {
9-
const {
10-
separator = IdHelper.DEFAULT_SEPARATOR,
11-
length = IdHelper.DEFAULT_LENGTH,
12-
customAlphabets = IdHelper.DEFAULT_ALPHABETS,
13-
} = idHelper.options;
14-
15-
const regex = new RegExp(
16-
`^${idHelper.prefix}${separator}[${customAlphabets}]{${length}}$`
17-
);
9+
const { regex } = idHelper;
1810

1911
return zodCustom<GeneratedId<P, SeparatorOrDefault<S>>>(val => {
2012
return typeof val === "string" && regex.test(val);

0 commit comments

Comments
 (0)