1
1
'use strict' ;
2
- var strictUriEncode = require ( 'strict-uri-encode' ) ;
3
- var objectAssign = require ( 'object-assign' ) ;
4
- var decodeComponent = require ( 'decode-uri-component' ) ;
2
+ const strictUriEncode = require ( 'strict-uri-encode' ) ;
3
+ const decodeComponent = require ( 'decode-uri-component' ) ;
5
4
6
- function encoderForArrayFormat ( opts ) {
7
- switch ( opts . arrayFormat ) {
5
+ function encoderForArrayFormat ( options ) {
6
+ switch ( options . arrayFormat ) {
8
7
case 'index' :
9
- return function ( key , value , index ) {
8
+ return ( key , value , index ) => {
10
9
return value === null ? [
11
- encode ( key , opts ) ,
10
+ encode ( key , options ) ,
12
11
'[' ,
13
12
index ,
14
13
']'
15
14
] . join ( '' ) : [
16
- encode ( key , opts ) ,
15
+ encode ( key , options ) ,
17
16
'[' ,
18
- encode ( index , opts ) ,
17
+ encode ( index , options ) ,
19
18
']=' ,
20
- encode ( value , opts )
19
+ encode ( value , options )
21
20
] . join ( '' ) ;
22
21
} ;
23
-
24
22
case 'bracket' :
25
- return function ( key , value ) {
26
- return value === null ? encode ( key , opts ) : [
27
- encode ( key , opts ) ,
23
+ return ( key , value ) => {
24
+ return value === null ? encode ( key , options ) : [
25
+ encode ( key , options ) ,
28
26
'[]=' ,
29
- encode ( value , opts )
27
+ encode ( value , options )
30
28
] . join ( '' ) ;
31
29
} ;
32
-
33
30
default :
34
- return function ( key , value ) {
35
- return value === null ? encode ( key , opts ) : [
36
- encode ( key , opts ) ,
31
+ return ( key , value ) => {
32
+ return value === null ? encode ( key , options ) : [
33
+ encode ( key , options ) ,
37
34
'=' ,
38
- encode ( value , opts )
35
+ encode ( value , options )
39
36
] . join ( '' ) ;
40
37
} ;
41
38
}
42
39
}
43
40
44
- function parserForArrayFormat ( opts ) {
45
- var result ;
41
+ function parserForArrayFormat ( options ) {
42
+ let result ;
46
43
47
- switch ( opts . arrayFormat ) {
44
+ switch ( options . arrayFormat ) {
48
45
case 'index' :
49
- return function ( key , value , accumulator ) {
46
+ return ( key , value , accumulator ) => {
50
47
result = / \[ ( \d * ) \] $ / . exec ( key ) ;
51
48
52
49
key = key . replace ( / \[ \d * \] $ / , '' ) ;
@@ -62,25 +59,25 @@ function parserForArrayFormat(opts) {
62
59
63
60
accumulator [ key ] [ result [ 1 ] ] = value ;
64
61
} ;
65
-
66
62
case 'bracket' :
67
- return function ( key , value , accumulator ) {
63
+ return ( key , value , accumulator ) => {
68
64
result = / ( \[ \] ) $ / . exec ( key ) ;
69
65
key = key . replace ( / \[ \] $ / , '' ) ;
70
66
71
67
if ( ! result ) {
72
68
accumulator [ key ] = value ;
73
69
return ;
74
- } else if ( accumulator [ key ] === undefined ) {
70
+ }
71
+
72
+ if ( accumulator [ key ] === undefined ) {
75
73
accumulator [ key ] = [ value ] ;
76
74
return ;
77
75
}
78
76
79
77
accumulator [ key ] = [ ] . concat ( accumulator [ key ] , value ) ;
80
78
} ;
81
-
82
79
default :
83
- return function ( key , value , accumulator ) {
80
+ return ( key , value , accumulator ) => {
84
81
if ( accumulator [ key ] === undefined ) {
85
82
accumulator [ key ] = value ;
86
83
return ;
@@ -91,9 +88,9 @@ function parserForArrayFormat(opts) {
91
88
}
92
89
}
93
90
94
- function encode ( value , opts ) {
95
- if ( opts . encode ) {
96
- return opts . strict ? strictUriEncode ( value ) : encodeURIComponent ( value ) ;
91
+ function encode ( value , options ) {
92
+ if ( options . encode ) {
93
+ return options . strict ? strictUriEncode ( value ) : encodeURIComponent ( value ) ;
97
94
}
98
95
99
96
return value ;
@@ -102,65 +99,60 @@ function encode(value, opts) {
102
99
function keysSorter ( input ) {
103
100
if ( Array . isArray ( input ) ) {
104
101
return input . sort ( ) ;
105
- } else if ( typeof input === 'object' ) {
106
- return keysSorter ( Object . keys ( input ) ) . sort ( function ( a , b ) {
107
- return Number ( a ) - Number ( b ) ;
108
- } ) . map ( function ( key ) {
109
- return input [ key ] ;
110
- } ) ;
102
+ }
103
+
104
+ if ( typeof input === 'object' ) {
105
+ return keysSorter ( Object . keys ( input ) )
106
+ . sort ( ( a , b ) => Number ( a ) - Number ( b ) )
107
+ . map ( key => input [ key ] ) ;
111
108
}
112
109
113
110
return input ;
114
111
}
115
112
116
- function extract ( str ) {
117
- var queryStart = str . indexOf ( '?' ) ;
113
+ function extract ( input ) {
114
+ const queryStart = input . indexOf ( '?' ) ;
118
115
if ( queryStart === - 1 ) {
119
116
return '' ;
120
117
}
121
- return str . slice ( queryStart + 1 ) ;
118
+ return input . slice ( queryStart + 1 ) ;
122
119
}
123
120
124
- function parse ( str , opts ) {
125
- opts = objectAssign ( { arrayFormat : 'none' } , opts ) ;
121
+ function parse ( input , options ) {
122
+ options = Object . assign ( { arrayFormat : 'none' } , options ) ;
126
123
127
- var formatter = parserForArrayFormat ( opts ) ;
124
+ const formatter = parserForArrayFormat ( options ) ;
128
125
129
126
// Create an object with no prototype
130
- // https://github.com/sindresorhus/query-string/issues/47
131
- var ret = Object . create ( null ) ;
127
+ const ret = Object . create ( null ) ;
132
128
133
- if ( typeof str !== 'string' ) {
129
+ if ( typeof input !== 'string' ) {
134
130
return ret ;
135
131
}
136
132
137
- str = str . trim ( ) . replace ( / ^ [ ? # & ] / , '' ) ;
133
+ input = input . trim ( ) . replace ( / ^ [ ? # & ] / , '' ) ;
138
134
139
- if ( ! str ) {
135
+ if ( ! input ) {
140
136
return ret ;
141
137
}
142
138
143
- str . split ( '&' ) . forEach ( function ( param ) {
144
- var parts = param . replace ( / \+ / g, ' ' ) . split ( '=' ) ;
145
- // Firefox (pre 40) decodes `%3D` to `=`
146
- // https://github.com/sindresorhus/query-string/pull/37
147
- var key = parts . shift ( ) ;
148
- var val = parts . length > 0 ? parts . join ( '=' ) : undefined ;
139
+ for ( const param of input . split ( '&' ) ) {
140
+ let [ key , value ] = param . replace ( / \+ / g, ' ' ) . split ( '=' ) ;
149
141
150
- // missing `=` should be `null`:
142
+ // Missing `=` should be `null`:
151
143
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
152
- val = val === undefined ? null : decodeComponent ( val ) ;
144
+ value = value === undefined ? null : decodeComponent ( value ) ;
153
145
154
- formatter ( decodeComponent ( key ) , val , ret ) ;
155
- } ) ;
146
+ formatter ( decodeComponent ( key ) , value , ret ) ;
147
+ }
156
148
157
- return Object . keys ( ret ) . sort ( ) . reduce ( function ( result , key ) {
158
- var val = ret [ key ] ;
159
- if ( Boolean ( val ) && typeof val === 'object' && ! Array . isArray ( val ) ) {
149
+ return Object . keys ( ret ) . sort ( ) . reduce ( ( result , key ) => {
150
+ const value = ret [ key ] ;
151
+ if ( Boolean ( value ) && typeof value === 'object' && ! Array . isArray ( value ) ) {
160
152
// Sort object keys, not values
161
- result [ key ] = keysSorter ( val ) ;
153
+ result [ key ] = keysSorter ( value ) ;
162
154
} else {
163
- result [ key ] = val ;
155
+ result [ key ] = value ;
164
156
}
165
157
166
158
return result ;
@@ -170,55 +162,53 @@ function parse(str, opts) {
170
162
exports . extract = extract ;
171
163
exports . parse = parse ;
172
164
173
- exports . stringify = function ( obj , opts ) {
174
- var defaults = {
165
+ exports . stringify = ( obj , options ) => {
166
+ const defaults = {
175
167
encode : true ,
176
168
strict : true ,
177
169
arrayFormat : 'none'
178
170
} ;
179
171
180
- opts = objectAssign ( defaults , opts ) ;
172
+ options = Object . assign ( defaults , options ) ;
181
173
182
- if ( opts . sort === false ) {
183
- opts . sort = function ( ) { } ;
174
+ if ( options . sort === false ) {
175
+ options . sort = ( ) => { } ;
184
176
}
185
177
186
- var formatter = encoderForArrayFormat ( opts ) ;
178
+ const formatter = encoderForArrayFormat ( options ) ;
187
179
188
- return obj ? Object . keys ( obj ) . sort ( opts . sort ) . map ( function ( key ) {
189
- var val = obj [ key ] ;
180
+ return obj ? Object . keys ( obj ) . sort ( options . sort ) . map ( key => {
181
+ const value = obj [ key ] ;
190
182
191
- if ( val === undefined ) {
183
+ if ( value === undefined ) {
192
184
return '' ;
193
185
}
194
186
195
- if ( val === null ) {
196
- return encode ( key , opts ) ;
187
+ if ( value === null ) {
188
+ return encode ( key , options ) ;
197
189
}
198
190
199
- if ( Array . isArray ( val ) ) {
200
- var result = [ ] ;
191
+ if ( Array . isArray ( value ) ) {
192
+ const result = [ ] ;
201
193
202
- val . slice ( ) . forEach ( function ( val2 ) {
203
- if ( val2 === undefined ) {
204
- return ;
194
+ for ( const value2 of value . slice ( ) ) {
195
+ if ( value2 === undefined ) {
196
+ continue ;
205
197
}
206
198
207
- result . push ( formatter ( key , val2 , result . length ) ) ;
208
- } ) ;
199
+ result . push ( formatter ( key , value2 , result . length ) ) ;
200
+ }
209
201
210
202
return result . join ( '&' ) ;
211
203
}
212
204
213
- return encode ( key , opts ) + '=' + encode ( val , opts ) ;
214
- } ) . filter ( function ( x ) {
215
- return x . length > 0 ;
216
- } ) . join ( '&' ) : '' ;
205
+ return encode ( key , options ) + '=' + encode ( value , options ) ;
206
+ } ) . filter ( x => x . length > 0 ) . join ( '&' ) : '' ;
217
207
} ;
218
208
219
- exports . parseUrl = function ( str , opts ) {
209
+ exports . parseUrl = ( input , options ) => {
220
210
return {
221
- url : str . split ( '?' ) [ 0 ] || '' ,
222
- query : parse ( extract ( str ) , opts )
211
+ url : input . split ( '?' ) [ 0 ] || '' ,
212
+ query : parse ( extract ( input ) , options )
223
213
} ;
224
214
} ;
0 commit comments