1
- import { number , union , object , optional , parse , safeParse , string , type InferInput , type InferOutput , pipe , transform } from 'valibot'
2
- import { checkUserA , checkUserB , checkUserC , nbIterations } from './utils.ts'
1
+ import { type InferInput , type InferOutput , number , object , optional , parse , pipe , safeParse , string , transform , union } from 'valibot'
2
+ import { checkUserA , checkUserB , checkUserC , logExecTime , nbIterations } from './utils.ts'
3
3
4
4
const startTime = performance . now ( )
5
5
6
6
for ( let i = 0 ; i < nbIterations ; i ++ ) {
7
-
8
7
const userSchema = object ( {
9
8
name : string ( ) ,
10
9
age : optional ( number ( ) , 42 ) ,
11
10
phone : pipe (
12
- optional ( union ( [ string ( ) , number ( ) ] ) , " 123-456-7890" ) ,
13
- transform ( ( phone ) => typeof phone === 'number' ? phone . toString ( ) : phone )
11
+ optional ( union ( [ string ( ) , number ( ) ] ) , ' 123-456-7890' ) ,
12
+ transform ( phone => ( typeof phone === 'number' ? phone . toString ( ) : phone ) ) ,
14
13
) ,
15
14
} )
16
15
@@ -19,22 +18,21 @@ for (let i = 0; i < nbIterations; i++) {
19
18
20
19
type UserInput = InferInput < typeof userSchema >
21
20
22
- const userA = parse ( userSchema , { name : " Jordan" } )
21
+ const userA = parse ( userSchema , { name : ' Jordan' } )
23
22
checkUserA ( userA )
24
23
25
- function createUser ( input : UserInput ) {
24
+ function createUser ( input : UserInput ) {
26
25
const result = safeParse ( userSchema , input )
27
26
if ( ! result . success ) return userA
28
27
return result . output
29
28
}
30
29
31
- const userB = createUser ( { name : " Romain" , age : 35 , phone : 1234567890 } )
30
+ const userB = createUser ( { name : ' Romain' , age : 35 , phone : 1234567890 } )
32
31
checkUserB ( userB )
33
32
34
33
// @ts -expect-error age should be a number
35
- const userC = createUser ( { name : " Romain" , age : "35" } )
34
+ const userC = createUser ( { name : ' Romain' , age : '35' } )
36
35
checkUserC ( userC )
37
-
38
36
}
39
37
40
- console . log ( ` Valibot exec time for ${ nbIterations } iterations :` , performance . now ( ) - startTime , 'ms' )
38
+ logExecTime ( ' Valibot' , startTime )
0 commit comments