@@ -4,24 +4,23 @@ import { getKeyString } from './utils/get-key-string';
4
4
5
5
const defaultConfig : CreateURLParamsConfig = {
6
6
keyPrefix : '' ,
7
- toString : false ,
8
7
index : null ,
9
8
booleanMapper : ( val : boolean ) => ( val ? '1' : '0' ) ,
10
9
} ;
11
10
12
- export const createURLParams = < T extends object > (
11
+ export const createURLParams = < T extends Record < string , any > > (
13
12
value : T | T [ ] | null | undefined ,
14
13
config ?: Partial < CreateURLParamsConfig >
15
- ) : object => {
16
- const { keyPrefix, toString , index, booleanMapper } : CreateURLParamsConfig =
14
+ ) : Record < string , string > => {
15
+ const { keyPrefix, index, booleanMapper } : CreateURLParamsConfig =
17
16
Object . assign ( { ...defaultConfig } , config || { } ) ;
18
17
19
18
if ( value === undefined || value === null ) {
20
19
return { } ;
21
20
}
22
21
23
22
if ( Array . isArray ( value ) ) {
24
- return value . reduce ( ( acc : object , curr , i ) => {
23
+ return value . reduce ( ( acc : Record < string , any > , curr , i ) => {
25
24
if ( typeof curr === 'object' && curr ) {
26
25
return {
27
26
...acc ,
@@ -38,16 +37,14 @@ export const createURLParams = <T extends object>(
38
37
const value =
39
38
typeof curr === 'boolean'
40
39
? booleanMapper ( curr )
41
- : toString
42
- ? String ( curr )
43
- : curr ;
40
+ : String ( curr ) ;
44
41
return { ...acc , [ `${ keyPrefix } [${ i } ]` ] : value } ;
45
42
} , { } ) ;
46
43
}
47
44
48
- return Object . keys ( value ) . reduce ( ( acc : object , key : string ) => {
45
+ return Object . keys ( value ) . reduce ( ( acc : Record < string , string > , key : string ) => {
49
46
const keyString = getKeyString ( keyPrefix , key , index ) ;
50
- const item = value [ key as keyof object ] ;
47
+ const item = value [ key as keyof Record < string , string > ] ;
51
48
52
49
if ( Array . isArray ( item ) ) {
53
50
return {
@@ -59,21 +56,20 @@ export const createURLParams = <T extends object>(
59
56
} ;
60
57
}
61
58
59
+ if ( item === null || item === undefined ) {
60
+ return acc ;
61
+ }
62
+
62
63
if ( typeof item === 'boolean' ) {
63
64
return { ...acc , [ `${ keyString } ` ] : booleanMapper ( item ) } ;
64
65
}
65
66
66
- if ( item && typeof item === 'object' ) {
67
+ if ( typeof item === 'object' ) {
67
68
return {
68
69
...acc ,
69
70
...createURLParams ( item , { ...config , keyPrefix : keyString } ) ,
70
71
} ;
71
72
}
72
-
73
- if ( item !== null && item !== undefined ) {
74
- const val = toString ? String ( item ) : item ;
75
- return { ...acc , [ `${ keyString } ` ] : val } ;
76
- }
77
- return acc ;
73
+ return { ...acc , [ `${ keyString } ` ] : String ( item ) } ;
78
74
} , { } ) ;
79
75
} ;
0 commit comments