File tree 2 files changed +15
-4
lines changed
2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -70,11 +70,16 @@ describe('serializer', () => {
70
70
const result = serialize ( url , { str : 'foo' } )
71
71
expect ( result ) . toBe ( 'https://example.com/path?bar=egg&str=foo' )
72
72
} )
73
- test ( 'null deletes from base' , ( ) => {
73
+ test ( 'null value deletes from base' , ( ) => {
74
74
const serialize = createSerializer ( parsers )
75
75
const result = serialize ( '?str=bar&int=-1' , { str : 'foo' , int : null } )
76
76
expect ( result ) . toBe ( '?str=foo' )
77
77
} )
78
+ test ( 'null deletes all from base' , ( ) => {
79
+ const serialize = createSerializer ( parsers )
80
+ const result = serialize ( '?str=bar&int=-1' , null )
81
+ expect ( result ) . toBe ( '' )
82
+ } )
78
83
test ( 'clears value when setting the default value when `clearOnDefault` is used' , ( ) => {
79
84
const serialize = createSerializer ( {
80
85
int : parseAsInteger . withOptions ( { clearOnDefault : true } ) . withDefault ( 0 ) ,
Original file line number Diff line number Diff line change @@ -27,15 +27,21 @@ export function createSerializer<
27
27
* - another value is given for an existing key, in which case the
28
28
* search param will be updated
29
29
*/
30
- function serialize ( base : Base , values : Values < Parsers > ) : string
30
+ function serialize ( base : Base , values : Values < Parsers > | null ) : string
31
31
function serialize (
32
- baseOrValues : Base | Values < Parsers > ,
33
- values : Values < Parsers > = { }
32
+ baseOrValues : Base | Values < Parsers > | null ,
33
+ values : Values < Parsers > | null = { }
34
34
) {
35
35
const [ base , search ] = isBase ( baseOrValues )
36
36
? splitBase ( baseOrValues )
37
37
: [ '' , new URLSearchParams ( ) ]
38
38
const vals = isBase ( baseOrValues ) ? values : baseOrValues
39
+ if ( vals === null ) {
40
+ for ( const key in parsers ) {
41
+ search . delete ( key )
42
+ }
43
+ return base + renderQueryString ( search )
44
+ }
39
45
for ( const key in parsers ) {
40
46
const parser = parsers [ key ]
41
47
const value = vals [ key ]
You can’t perform that action at this time.
0 commit comments