@@ -22,42 +22,45 @@ function parse(input) {
2222 let shouldDecodeValue = false ;
2323 let keyHasPlus = false ;
2424 let valueHasPlus = false ;
25+ let hasBothKeyValuePair = false ;
26+ let keyEndingIndex = 0 ;
2527
2628 // Have a boundary of input.length + 1 to access last pair inside the loop.
2729 for ( let i = 0 ; i < inputLength + 1 ; i ++ ) {
2830 let c = i !== inputLength ? input . charCodeAt ( i ) : - 1 ;
2931
3032 // Handle '&' and end of line to pass the current values to result
3133 if ( c === 38 || c === - 1 ) {
34+ hasBothKeyValuePair = equalityIndex > startingIndex ;
35+ keyEndingIndex = hasBothKeyValuePair ? equalityIndex : i ;
36+
37+ key = input . slice ( startingIndex + 1 , keyEndingIndex ) ;
38+
3239 // Check if the current range consist of a single key
33- if ( equalityIndex <= startingIndex ) {
34- key = input . slice ( startingIndex + 1 , i ) ;
35- value = "" ;
36- }
37- // Range consist of both key and value
38- else {
39- key = input . slice ( startingIndex + 1 , equalityIndex ) ;
40+ if ( hasBothKeyValuePair ) {
4041 value = input . slice ( equalityIndex + 1 , i ) ;
4142 }
4243
4344 // Add key/value pair only if the range size is greater than 1; a.k.a. contains at least "="
44- if ( i - startingIndex > 1 ) {
45+ if ( hasBothKeyValuePair || i - startingIndex > 1 ) {
4546 // Optimization: Replace '+' with space
4647 if ( keyHasPlus ) {
4748 key = key . replaceAll ( "+" , " " ) ;
4849 }
4950
50- if ( valueHasPlus ) {
51- value = value . replaceAll ( "+" , " " ) ;
52- }
53-
5451 // Optimization: Do not decode if it's not necessary.
5552 if ( shouldDecodeKey ) {
5653 key = fastDecode ( key ) || key ;
5754 }
5855
59- if ( shouldDecodeValue ) {
60- value = fastDecode ( value ) || value ;
56+ if ( hasBothKeyValuePair ) {
57+ if ( valueHasPlus ) {
58+ value = value . replaceAll ( "+" , " " ) ;
59+ }
60+
61+ if ( shouldDecodeValue ) {
62+ value = fastDecode ( value ) || value ;
63+ }
6164 }
6265
6366 if ( result [ key ] === undefined ) {
@@ -82,6 +85,7 @@ function parse(input) {
8285 shouldDecodeValue = false ;
8386 keyHasPlus = false ;
8487 valueHasPlus = false ;
88+ hasBothKeyValuePair = false ;
8589 }
8690 // Check '='
8791 else if ( c === 61 ) {
0 commit comments