File tree Expand file tree Collapse file tree 3 files changed +13
-15
lines changed Expand file tree Collapse file tree 3 files changed +13
-15
lines changed Original file line number Diff line number Diff line change @@ -27,10 +27,11 @@ which encodes most non-alphanumeric characters.
2727- Any ` undefined ` values are removed,
2828 e.g., ` { foo: undefined, bar: 1 } ` serializes to ` bar=1 ` .
2929- The primitive type ` string ` is serialized using ` .toString() ` .
30- - Serialization of the empty string
31- is not supported and will throw an ` UnserializableParamError ` .
30+ - Serialization of the empty string is treated as ` undefined ` .
3231 Otherwise, the serialization of ` { foo: null } ` would conflict with ` { foo: '' } ` .
33- This serializer chooses to support the more common and more useful case of ` null ` .
32+ This serializer chooses to support the more common and more useful case of sending ` null `
33+ as a meaningful value while treating the empty string as sending no value.
34+ This aligns with common UX patterns where input fields are initialized to the empty string.
3435- The primitive ` number ` and ` bigint ` types are serialized using ` .toString() ` .
3536- The primitive ` boolean ` type is serialized using ` .toString() ` ,
3637 e.g., ` { foo: true, bar: false } ` serializes to ` foo=true&bar=false ` .
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ const nestedUpdateUrlSearchParams = (
4242 continue
4343 }
4444
45+ if ( typeof value === 'string' && value . length === 0 ) {
46+ continue
47+ }
48+
4549 if ( Array . isArray ( value ) ) {
4650 if ( value . length === 0 ) {
4751 searchParams . set ( name , '' )
@@ -79,12 +83,6 @@ const nestedUpdateUrlSearchParams = (
7983const serialize = ( k : string , v : unknown ) : string => {
8084 if ( v === null ) return ''
8185 if ( typeof v === 'string' ) {
82- if ( v . length === 0 ) {
83- throw new UnserializableParamError (
84- k ,
85- 'is the empty string which is unsupported' ,
86- )
87- }
8886 return v . toString ( )
8987 }
9088 if ( typeof v === 'number' ) {
Original file line number Diff line number Diff line change @@ -17,6 +17,11 @@ test('serializes string', (t) => {
1717 t . is ( serializeUrlSearchParams ( { foo : '0' } ) , 'foo=0' )
1818} )
1919
20+ test ( 'serializes the empty string to undefined' , ( t ) => {
21+ t . is ( serializeUrlSearchParams ( { foo : '' } ) , '' )
22+ t . is ( serializeUrlSearchParams ( { foo : 'd' , bar : '' } ) , 'foo=d' )
23+ } )
24+
2025test ( 'serializes number' , ( t ) => {
2126 t . is ( serializeUrlSearchParams ( { foo : 1 } ) , 'foo=1' )
2227 t . is ( serializeUrlSearchParams ( { foo : 23.8 } ) , 'foo=23.8' )
@@ -149,12 +154,6 @@ test('cannot serialize non-plain objects', (t) => {
149154 } )
150155} )
151156
152- test ( 'cannot serialize the empty string' , ( t ) => {
153- t . throws ( ( ) => serializeUrlSearchParams ( { foo : '' } ) , {
154- instanceOf : UnserializableParamError ,
155- } )
156- } )
157-
158157test ( 'cannot serialize array params with unserializable values' , ( t ) => {
159158 t . throws ( ( ) => serializeUrlSearchParams ( { foo : [ '' ] } ) , {
160159 instanceOf : UnserializableParamError ,
You can’t perform that action at this time.
0 commit comments