11'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' ) ;
54
6- function encoderForArrayFormat ( opts ) {
7- switch ( opts . arrayFormat ) {
5+ function encoderForArrayFormat ( options ) {
6+ switch ( options . arrayFormat ) {
87 case 'index' :
9- return function ( key , value , index ) {
8+ return ( key , value , index ) => {
109 return value === null ? [
11- encode ( key , opts ) ,
10+ encode ( key , options ) ,
1211 '[' ,
1312 index ,
1413 ']'
1514 ] . join ( '' ) : [
16- encode ( key , opts ) ,
15+ encode ( key , options ) ,
1716 '[' ,
18- encode ( index , opts ) ,
17+ encode ( index , options ) ,
1918 ']=' ,
20- encode ( value , opts )
19+ encode ( value , options )
2120 ] . join ( '' ) ;
2221 } ;
23-
2422 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 ) ,
2826 '[]=' ,
29- encode ( value , opts )
27+ encode ( value , options )
3028 ] . join ( '' ) ;
3129 } ;
32-
3330 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 ) ,
3734 '=' ,
38- encode ( value , opts )
35+ encode ( value , options )
3936 ] . join ( '' ) ;
4037 } ;
4138 }
4239}
4340
44- function parserForArrayFormat ( opts ) {
45- var result ;
41+ function parserForArrayFormat ( options ) {
42+ let result ;
4643
47- switch ( opts . arrayFormat ) {
44+ switch ( options . arrayFormat ) {
4845 case 'index' :
49- return function ( key , value , accumulator ) {
46+ return ( key , value , accumulator ) => {
5047 result = / \[ ( \d * ) \] $ / . exec ( key ) ;
5148
5249 key = key . replace ( / \[ \d * \] $ / , '' ) ;
@@ -62,25 +59,25 @@ function parserForArrayFormat(opts) {
6259
6360 accumulator [ key ] [ result [ 1 ] ] = value ;
6461 } ;
65-
6662 case 'bracket' :
67- return function ( key , value , accumulator ) {
63+ return ( key , value , accumulator ) => {
6864 result = / ( \[ \] ) $ / . exec ( key ) ;
6965 key = key . replace ( / \[ \] $ / , '' ) ;
7066
7167 if ( ! result ) {
7268 accumulator [ key ] = value ;
7369 return ;
74- } else if ( accumulator [ key ] === undefined ) {
70+ }
71+
72+ if ( accumulator [ key ] === undefined ) {
7573 accumulator [ key ] = [ value ] ;
7674 return ;
7775 }
7876
7977 accumulator [ key ] = [ ] . concat ( accumulator [ key ] , value ) ;
8078 } ;
81-
8279 default :
83- return function ( key , value , accumulator ) {
80+ return ( key , value , accumulator ) => {
8481 if ( accumulator [ key ] === undefined ) {
8582 accumulator [ key ] = value ;
8683 return ;
@@ -91,9 +88,9 @@ function parserForArrayFormat(opts) {
9188 }
9289}
9390
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 ) ;
9794 }
9895
9996 return value ;
@@ -102,65 +99,60 @@ function encode(value, opts) {
10299function keysSorter ( input ) {
103100 if ( Array . isArray ( input ) ) {
104101 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 ] ) ;
111108 }
112109
113110 return input ;
114111}
115112
116- function extract ( str ) {
117- var queryStart = str . indexOf ( '?' ) ;
113+ function extract ( input ) {
114+ const queryStart = input . indexOf ( '?' ) ;
118115 if ( queryStart === - 1 ) {
119116 return '' ;
120117 }
121- return str . slice ( queryStart + 1 ) ;
118+ return input . slice ( queryStart + 1 ) ;
122119}
123120
124- function parse ( str , opts ) {
125- opts = objectAssign ( { arrayFormat : 'none' } , opts ) ;
121+ function parse ( input , options ) {
122+ options = Object . assign ( { arrayFormat : 'none' } , options ) ;
126123
127- var formatter = parserForArrayFormat ( opts ) ;
124+ const formatter = parserForArrayFormat ( options ) ;
128125
129126 // 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 ) ;
132128
133- if ( typeof str !== 'string' ) {
129+ if ( typeof input !== 'string' ) {
134130 return ret ;
135131 }
136132
137- str = str . trim ( ) . replace ( / ^ [ ? # & ] / , '' ) ;
133+ input = input . trim ( ) . replace ( / ^ [ ? # & ] / , '' ) ;
138134
139- if ( ! str ) {
135+ if ( ! input ) {
140136 return ret ;
141137 }
142138
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 ( '=' ) ;
149141
150- // missing `=` should be `null`:
142+ // Missing `=` should be `null`:
151143 // 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 ) ;
153145
154- formatter ( decodeComponent ( key ) , val , ret ) ;
155- } ) ;
146+ formatter ( decodeComponent ( key ) , value , ret ) ;
147+ }
156148
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 ) ) {
160152 // Sort object keys, not values
161- result [ key ] = keysSorter ( val ) ;
153+ result [ key ] = keysSorter ( value ) ;
162154 } else {
163- result [ key ] = val ;
155+ result [ key ] = value ;
164156 }
165157
166158 return result ;
@@ -170,55 +162,53 @@ function parse(str, opts) {
170162exports . extract = extract ;
171163exports . parse = parse ;
172164
173- exports . stringify = function ( obj , opts ) {
174- var defaults = {
165+ exports . stringify = ( obj , options ) => {
166+ const defaults = {
175167 encode : true ,
176168 strict : true ,
177169 arrayFormat : 'none'
178170 } ;
179171
180- opts = objectAssign ( defaults , opts ) ;
172+ options = Object . assign ( defaults , options ) ;
181173
182- if ( opts . sort === false ) {
183- opts . sort = function ( ) { } ;
174+ if ( options . sort === false ) {
175+ options . sort = ( ) => { } ;
184176 }
185177
186- var formatter = encoderForArrayFormat ( opts ) ;
178+ const formatter = encoderForArrayFormat ( options ) ;
187179
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 ] ;
190182
191- if ( val === undefined ) {
183+ if ( value === undefined ) {
192184 return '' ;
193185 }
194186
195- if ( val === null ) {
196- return encode ( key , opts ) ;
187+ if ( value === null ) {
188+ return encode ( key , options ) ;
197189 }
198190
199- if ( Array . isArray ( val ) ) {
200- var result = [ ] ;
191+ if ( Array . isArray ( value ) ) {
192+ const result = [ ] ;
201193
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 ;
205197 }
206198
207- result . push ( formatter ( key , val2 , result . length ) ) ;
208- } ) ;
199+ result . push ( formatter ( key , value2 , result . length ) ) ;
200+ }
209201
210202 return result . join ( '&' ) ;
211203 }
212204
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 ( '&' ) : '' ;
217207} ;
218208
219- exports . parseUrl = function ( str , opts ) {
209+ exports . parseUrl = ( input , options ) => {
220210 return {
221- url : str . split ( '?' ) [ 0 ] || '' ,
222- query : parse ( extract ( str ) , opts )
211+ url : input . split ( '?' ) [ 0 ] || '' ,
212+ query : parse ( extract ( input ) , options )
223213 } ;
224214} ;
0 commit comments