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<
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 ( ) ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11import { IdHelper } from "../id-helper" ;
22import { custom as vCustom } from "valibot" ;
3- import { getIdRegex } from "../utils" ;
43import type { GeneratedId , SeparatorOrDefault } from "../types" ;
54
65export 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}
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments