@@ -5,37 +5,59 @@ const decodeComponent = require('decode-uri-component');
55function encoderForArrayFormat ( options ) {
66 switch ( options . arrayFormat ) {
77 case 'index' :
8- return ( key , value , index ) => {
9- return value === null ? [
10- encode ( key , options ) ,
11- '[' ,
12- index ,
13- ']'
14- ] . join ( '' ) : [
15- encode ( key , options ) ,
16- '[' ,
17- encode ( index , options ) ,
18- ']=' ,
19- encode ( value , options )
20- ] . join ( '' ) ;
8+ return key => ( result , value ) => {
9+ const index = result . length ;
10+ if ( value === undefined ) {
11+ return result ;
12+ }
13+
14+ if ( value === null ) {
15+ return [ ...result , [ encode ( key , options ) , '[' , index , ']' ] . join ( '' ) ] ;
16+ }
17+
18+ return [
19+ ...result ,
20+ [ encode ( key , options ) , '[' , encode ( index , options ) , ']=' , encode ( value , options ) ] . join ( '' )
21+ ] ;
2122 } ;
2223
2324 case 'bracket' :
24- return ( key , value ) => {
25- return value === null ? [ encode ( key , options ) , '[]' ] . join ( '' ) : [
26- encode ( key , options ) ,
27- '[]=' ,
28- encode ( value , options )
29- ] . join ( '' ) ;
25+ return key => ( result , value ) => {
26+ if ( value === undefined ) {
27+ return result ;
28+ }
29+
30+ if ( value === null ) {
31+ return [ ...result , [ encode ( key , options ) , '[]' ] . join ( '' ) ] ;
32+ }
33+
34+ return [ ...result , [ encode ( key , options ) , '[]=' , encode ( value , options ) ] . join ( '' ) ] ;
35+ } ;
36+
37+ case 'comma' :
38+ return key => ( result , value , index ) => {
39+ if ( ! value ) {
40+ return result ;
41+ }
42+
43+ if ( index === 0 ) {
44+ return [ [ encode ( key , options ) , '=' , encode ( value , options ) ] . join ( '' ) ] ;
45+ }
46+
47+ return [ [ result , encode ( value , options ) ] . join ( ',' ) ] ;
3048 } ;
3149
3250 default :
33- return ( key , value ) => {
34- return value === null ? encode ( key , options ) : [
35- encode ( key , options ) ,
36- '=' ,
37- encode ( value , options )
38- ] . join ( '' ) ;
51+ return key => ( result , value ) => {
52+ if ( value === undefined ) {
53+ return result ;
54+ }
55+
56+ if ( value === null ) {
57+ return [ ...result , encode ( key , options ) ] ;
58+ }
59+
60+ return [ ...result , [ encode ( key , options ) , '=' , encode ( value , options ) ] . join ( '' ) ] ;
3961 } ;
4062 }
4163}
@@ -80,6 +102,13 @@ function parserForArrayFormat(options) {
80102 accumulator [ key ] = [ ] . concat ( accumulator [ key ] , value ) ;
81103 } ;
82104
105+ case 'comma' :
106+ return ( key , value , accumulator ) => {
107+ const isArray = typeof value === 'string' && value . split ( '' ) . indexOf ( ',' ) > - 1 ;
108+ const newValue = isArray ? value . split ( ',' ) : value ;
109+ accumulator [ key ] = newValue ;
110+ } ;
111+
83112 default :
84113 return ( key , value , accumulator ) => {
85114 if ( accumulator [ key ] === undefined ) {
@@ -205,17 +234,10 @@ exports.stringify = (obj, options) => {
205234 }
206235
207236 if ( Array . isArray ( value ) ) {
208- const result = [ ] ;
209-
210- for ( const value2 of value . slice ( ) ) {
211- if ( value2 === undefined ) {
212- continue ;
213- }
214-
215- result . push ( formatter ( key , value2 , result . length ) ) ;
216- }
217-
218- return result . join ( '&' ) ;
237+ return value
238+ . slice ( )
239+ . reduce ( formatter ( key ) , [ ] )
240+ . join ( '&' ) ;
219241 }
220242
221243 return encode ( key , options ) + '=' + encode ( value , options ) ;
0 commit comments