File tree Expand file tree Collapse file tree 4 files changed +22
-32
lines changed Expand file tree Collapse file tree 4 files changed +22
-32
lines changed Original file line number Diff line number Diff line change @@ -12,13 +12,31 @@ export class IdHelper<
12
12
13
13
constructor ( public prefix : P , public options : Partial < Options < S > > = { } ) { }
14
14
15
- public generate ( ) : GeneratedId < P , SeparatorOrDefault < S > > {
15
+ private get optionsOrDefaults ( ) {
16
16
const {
17
17
separator = IdHelper . DEFAULT_SEPARATOR ,
18
18
length = IdHelper . DEFAULT_LENGTH ,
19
19
customAlphabets = IdHelper . DEFAULT_ALPHABETS ,
20
20
} = this . options ;
21
21
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
+
22
40
const nanoid = customAlphabet ( customAlphabets , length ) ;
23
41
24
42
const id = nanoid ( ) ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
import { IdHelper } from "../id-helper" ;
2
2
import { custom as vCustom } from "valibot" ;
3
- import { getIdRegex } from "../utils" ;
4
3
import type { GeneratedId , SeparatorOrDefault } from "../types" ;
5
4
6
5
export function createValibotIdSchema <
7
6
P extends string ,
8
7
S extends string | undefined = undefined
9
8
> ( 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 ;
22
10
23
11
return vCustom < GeneratedId < P , SeparatorOrDefault < S > > > ( val => {
24
- return typeof val === "string" && idRegex . test ( val ) ;
12
+ return typeof val === "string" && regex . test ( val ) ;
25
13
} , "Invalid ID Format" ) ;
26
14
}
Original file line number Diff line number Diff line change @@ -6,15 +6,7 @@ export function createZodIdSchema<
6
6
P extends string ,
7
7
S extends string | undefined = undefined
8
8
> ( 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 ;
18
10
19
11
return zodCustom < GeneratedId < P , SeparatorOrDefault < S > > > ( val => {
20
12
return typeof val === "string" && regex . test ( val ) ;
You can’t perform that action at this time.
0 commit comments