@@ -9,9 +9,10 @@ const commaRE = /%2C/g
9
9
// fixed encodeURIComponent which is more conformant to RFC3986:
10
10
// - escapes [!'()*]
11
11
// - 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 , ',' )
15
16
16
17
const decode = decodeURIComponent
17
18
@@ -30,11 +31,15 @@ export function resolveQuery (
30
31
}
31
32
for ( const key in extraQuery ) {
32
33
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 )
34
37
}
35
38
return parsedQuery
36
39
}
37
40
41
+ const castQueryParamValue = value => ( value == null ? value : '' + value )
42
+
38
43
function parseQuery ( query : string ) : Dictionary < string > {
39
44
const res = { }
40
45
@@ -47,9 +52,7 @@ function parseQuery (query: string): Dictionary<string> {
47
52
query . split ( '&' ) . forEach ( param => {
48
53
const parts = param . replace ( / \+ / g, ' ' ) . split ( '=' )
49
54
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
53
56
54
57
if ( res [ key ] === undefined ) {
55
58
res [ key ] = val
@@ -64,33 +67,38 @@ function parseQuery (query: string): Dictionary<string> {
64
67
}
65
68
66
69
export 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 ]
73
74
74
- if ( val === null ) {
75
- return encode ( key )
76
- }
75
+ if ( val === undefined ) {
76
+ return ''
77
+ }
77
78
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 )
83
81
}
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 ( '&' )
88
96
}
89
- } )
90
- return result . join ( '&' )
91
- }
92
97
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
95
103
return res ? `?${ res } ` : ''
96
104
}
0 commit comments