@@ -14,8 +14,10 @@ export type Parser<T> = {
14
14
15
15
/**
16
16
* Render the state value into a query string value.
17
+ *
18
+ * Return `null` to remove the query parameter from the URL.
17
19
*/
18
- serialize ?: ( value : T ) => string
20
+ serialize ?: ( value : T ) => string | null
19
21
20
22
/**
21
23
* Check if two state values are equal.
@@ -94,7 +96,7 @@ export type ParserBuilder<T> = Required<Parser<T>> &
94
96
* you can pass to one of the hooks, making its default value type safe.
95
97
*/
96
98
export function createParser < T > (
97
- parser : Require < Parser < T > , 'parse' | 'serialize' >
99
+ parser : Require < Parser < T > , 'parse' >
98
100
) : ParserBuilder < T > {
99
101
function parseServerSideNullable ( value : string | string [ ] | undefined ) {
100
102
if ( typeof value === 'undefined' ) {
@@ -117,6 +119,7 @@ export function createParser<T>(
117
119
118
120
return {
119
121
eq : ( a , b ) => a === b ,
122
+ serialize : String ,
120
123
...parser ,
121
124
parseServerSide : parseServerSideNullable ,
122
125
withDefault ( defaultValue ) {
@@ -401,12 +404,11 @@ export function parseAsArrayOf<ItemType>(
401
404
} ,
402
405
serialize : values =>
403
406
values
404
- . map < string > ( value => {
405
- const str = itemParser . serialize
406
- ? itemParser . serialize ( value )
407
- : String ( value )
408
- return str . replaceAll ( separator , encodedSeparator )
407
+ . map < string | null > ( value => {
408
+ const str = ( itemParser . serialize ?? String ) ( value )
409
+ return str ?. replaceAll ( separator , encodedSeparator ) ?? null
409
410
} )
411
+ . filter ( value => value !== null )
410
412
. join ( separator ) ,
411
413
eq ( a , b ) {
412
414
if ( a === b ) {
0 commit comments