@@ -9,9 +9,10 @@ const commaRE = /%2C/g
99// fixed encodeURIComponent which is more conformant to RFC3986:
1010// - escapes [!'()*]
1111// - preserve commas
12- const encode = str => encodeURIComponent ( str )
13- . replace ( encodeReserveRE , encodeReserveReplacer )
14- . replace ( commaRE , ',' )
12+ const encode = str =>
13+ encodeURIComponent ( str )
14+ . replace ( encodeReserveRE , encodeReserveReplacer )
15+ . replace ( commaRE , ',' )
1516
1617const decode = decodeURIComponent
1718
@@ -30,11 +31,15 @@ export function resolveQuery (
3031 }
3132 for ( const key in extraQuery ) {
3233 const value = extraQuery [ key ]
33- parsedQuery [ key ] = Array . isArray ( value ) ? value . map ( v => '' + v ) : '' + value
34+ parsedQuery [ key ] = Array . isArray ( value )
35+ ? value . map ( castQueryParamValue )
36+ : castQueryParamValue ( value )
3437 }
3538 return parsedQuery
3639}
3740
41+ const castQueryParamValue = value => ( value == null ? value : '' + value )
42+
3843function parseQuery ( query : string ) : Dictionary < string > {
3944 const res = { }
4045
@@ -47,9 +52,7 @@ function parseQuery (query: string): Dictionary<string> {
4752 query . split ( '&' ) . forEach ( param => {
4853 const parts = param . replace ( / \+ / g, ' ' ) . split ( '=' )
4954 const key = decode ( parts . shift ( ) )
50- const val = parts . length > 0
51- ? decode ( parts . join ( '=' ) )
52- : null
55+ const val = parts . length > 0 ? decode ( parts . join ( '=' ) ) : null
5356
5457 if ( res [ key ] === undefined ) {
5558 res [ key ] = val
@@ -64,33 +67,38 @@ function parseQuery (query: string): Dictionary<string> {
6467}
6568
6669export function stringifyQuery ( obj : Dictionary < string > ) : string {
67- const res = obj ? Object . keys ( obj ) . map ( key => {
68- const val = obj [ key ]
69-
70- if ( val === undefined ) {
71- return ''
72- }
70+ const res = obj
71+ ? Object . keys ( obj )
72+ . map ( key => {
73+ const val = obj [ key ]
7374
74- if ( val === null ) {
75- return encode ( key )
76- }
75+ if ( val === undefined ) {
76+ return ''
77+ }
7778
78- if ( Array . isArray ( val ) ) {
79- const result = [ ]
80- val . forEach ( val2 => {
81- if ( val2 === undefined ) {
82- return
79+ if ( val === null ) {
80+ return encode ( key )
8381 }
84- if ( val2 === null ) {
85- result . push ( encode ( key ) )
86- } else {
87- result . push ( encode ( key ) + '=' + encode ( val2 ) )
82+
83+ if ( Array . isArray ( val ) ) {
84+ const result = [ ]
85+ val . forEach ( val2 => {
86+ if ( val2 === undefined ) {
87+ return
88+ }
89+ if ( val2 === null ) {
90+ result . push ( encode ( key ) )
91+ } else {
92+ result . push ( encode ( key ) + '=' + encode ( val2 ) )
93+ }
94+ } )
95+ return result . join ( '&' )
8896 }
89- } )
90- return result . join ( '&' )
91- }
9297
93- return encode ( key ) + '=' + encode ( val )
94- } ) . filter ( x => x . length > 0 ) . join ( '&' ) : null
98+ return encode ( key ) + '=' + encode ( val )
99+ } )
100+ . filter ( x => x . length > 0 )
101+ . join ( '&' )
102+ : null
95103 return res ? `?${ res } ` : ''
96104}
0 commit comments