@@ -82,7 +82,7 @@ export interface ParseOptions {
8282 *
8383 * @default decodeURIComponent
8484 */
85- decode ?: ( str : string ) => string ;
85+ decode ?: ( str : string ) => string | undefined ;
8686}
8787
8888/**
@@ -133,8 +133,8 @@ export function parse(
133133 valEndIdx -- ;
134134 }
135135
136- const val = str . slice ( valStartIdx , valEndIdx ) ;
137- obj [ key ] = tryDecode ( val , dec ) ;
136+ const value = dec ( str . slice ( valStartIdx , valEndIdx ) ) ;
137+ if ( value !== undefined ) obj [ key ] = value ;
138138 }
139139
140140 index = endIdx + 1 ;
@@ -366,8 +366,14 @@ export function serialize(
366366/**
367367 * URL-decode string value. Optimized to skip native call when no %.
368368 */
369- function decode ( str : string ) : string {
370- return str . indexOf ( "%" ) !== - 1 ? decodeURIComponent ( str ) : str ;
369+ function decode ( str : string ) : string | undefined {
370+ if ( str . indexOf ( "%" ) === - 1 ) return str ;
371+
372+ try {
373+ return decodeURIComponent ( str ) ;
374+ } catch ( e ) {
375+ return str ;
376+ }
371377}
372378
373379/**
@@ -376,14 +382,3 @@ function decode(str: string): string {
376382function isDate ( val : any ) : val is Date {
377383 return __toString . call ( val ) === "[object Date]" ;
378384}
379-
380- /**
381- * Try decoding a string using a decoding function.
382- */
383- function tryDecode ( str : string , decode : ( str : string ) => string ) : string {
384- try {
385- return decode ( str ) ;
386- } catch ( e ) {
387- return str ;
388- }
389- }
0 commit comments