@@ -174,19 +174,22 @@ function stringify(obj, sep, eq, options) {
174174 for ( var i = 0 ; i < len ; ++ i ) {
175175 var k = keys [ i ] ;
176176 var v = obj [ k ] ;
177- var ks = encode ( stringifyPrimitive ( k ) ) + eq ;
177+ var ks = encode ( stringifyPrimitive ( k ) ) ;
178+ ks += eq ;
178179
179180 if ( Array . isArray ( v ) ) {
180181 var vlen = v . length ;
181182 if ( vlen === 0 ) continue ;
182183 var vlast = vlen - 1 ;
183184 for ( var j = 0 ; j < vlen ; ++ j ) {
184- fields += ks + encode ( stringifyPrimitive ( v [ j ] ) ) ;
185+ fields += ks ;
186+ fields += encode ( stringifyPrimitive ( v [ j ] ) ) ;
185187 if ( j < vlast )
186188 fields += sep ;
187189 }
188190 } else {
189- fields += ks + encode ( stringifyPrimitive ( v ) ) ;
191+ fields += ks ;
192+ fields += encode ( stringifyPrimitive ( v ) ) ;
190193 }
191194
192195 if ( i < flast )
@@ -200,14 +203,34 @@ function stringify(obj, sep, eq, options) {
200203function charCodes ( str ) {
201204 if ( str . length === 0 ) return [ ] ;
202205 if ( str . length === 1 ) return [ str . charCodeAt ( 0 ) ] ;
203- const ret = [ ] ;
206+ const ret = new Array ( str . length ) ;
204207 for ( var i = 0 ; i < str . length ; ++ i )
205- ret [ ret . length ] = str . charCodeAt ( i ) ;
208+ ret [ i ] = str . charCodeAt ( i ) ;
206209 return ret ;
207210}
208211const defSepCodes = [ 38 ] ; // &
209212const defEqCodes = [ 61 ] ; // =
210213
214+ function addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) {
215+ if ( key . length > 0 && keyEncoded )
216+ key = decodeStr ( key , decode ) ;
217+ if ( value . length > 0 && valEncoded )
218+ value = decodeStr ( value , decode ) ;
219+
220+ if ( obj [ key ] === undefined ) {
221+ obj [ key ] = value ;
222+ } else {
223+ const curValue = obj [ key ] ;
224+ // A simple Array-specific property check is enough here to
225+ // distinguish from a string value and is faster and still safe
226+ // since we are generating all of the values being assigned.
227+ if ( curValue . pop )
228+ curValue [ curValue . length ] = value ;
229+ else
230+ obj [ key ] = [ curValue , value ] ;
231+ }
232+ }
233+
211234// Parse a key/val string.
212235function parse ( qs , sep , eq , options ) {
213236 const obj = Object . create ( null ) ;
@@ -272,23 +295,8 @@ function parse(qs, sep, eq, options) {
272295 value += qs . slice ( lastPos , end ) ;
273296 }
274297
275- if ( key . length > 0 && keyEncoded )
276- key = decodeStr ( key , decode ) ;
277- if ( value . length > 0 && valEncoded )
278- value = decodeStr ( value , decode ) ;
298+ addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) ;
279299
280- if ( obj [ key ] === undefined ) {
281- obj [ key ] = value ;
282- } else {
283- const curValue = obj [ key ] ;
284- // A simple Array-specific property check is enough here to
285- // distinguish from a string value and is faster and still safe
286- // since we are generating all of the values being assigned.
287- if ( curValue . pop )
288- curValue [ curValue . length ] = value ;
289- else
290- obj [ key ] = [ curValue , value ] ;
291- }
292300 if ( -- pairs === 0 )
293301 return obj ;
294302 keyEncoded = valEncoded = customDecode ;
@@ -370,22 +378,8 @@ function parse(qs, sep, eq, options) {
370378 // We ended on an empty substring
371379 return obj ;
372380 }
373- if ( key . length > 0 && keyEncoded )
374- key = decodeStr ( key , decode ) ;
375- if ( value . length > 0 && valEncoded )
376- value = decodeStr ( value , decode ) ;
377- if ( obj [ key ] === undefined ) {
378- obj [ key ] = value ;
379- } else {
380- const curValue = obj [ key ] ;
381- // A simple Array-specific property check is enough here to
382- // distinguish from a string value and is faster and still safe since
383- // we are generating all of the values being assigned.
384- if ( curValue . pop )
385- curValue [ curValue . length ] = value ;
386- else
387- obj [ key ] = [ curValue , value ] ;
388- }
381+
382+ addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) ;
389383
390384 return obj ;
391385}
0 commit comments