@@ -17,18 +17,25 @@ function parse(input) {
1717 let separatorIndex = 0 ;
1818 let equalityIndex = 0 ;
1919 let shouldEncode = false ;
20+ let hasPlus = false ;
2021
2122 // Have a boundary of input.length + 1 to access last pair inside the loop.
2223 for ( let i = 0 ; i < input . length + 1 ; i ++ ) {
2324 let c = input . charCodeAt ( i ) ;
2425
25- // Handle '&', '#' and end of line to pass the current values to result
26+ // Handle '&' and end of line to pass the current values to result
2627 if ( c === 38 || c === 35 || isNaN ( c ) ) {
2728 // Disallow empty key values.
2829 if ( equalityIndex - separatorIndex > 0 && i - equalityIndex + 1 > 0 ) {
2930 key = input . slice ( separatorIndex , equalityIndex ) ;
3031 value = input . slice ( equalityIndex + 1 , i ) ;
3132
33+ // Optimization: Replace '+' with space
34+ if ( hasPlus ) {
35+ key = key . replace ( / \+ / g, " " ) ;
36+ value = value . replace ( / \+ / g, " " ) ;
37+ }
38+
3239 // Optimization: Do not decode if it's not necessary.
3340 if ( shouldEncode ) {
3441 key = decodeURIComponent ( key ) ;
@@ -48,11 +55,6 @@ function parse(input) {
4855 }
4956 }
5057
51- // Terminate loop when parser encounters '#'
52- if ( c === 35 ) {
53- break ;
54- }
55-
5658 // Reset reading key value pairs
5759 separatorIndex = i + 1 ;
5860 equalityIndex = i + 1 ;
@@ -64,12 +66,7 @@ function parse(input) {
6466 }
6567 // Check '+', and replace it with empty space.
6668 else if ( c === 43 ) {
67- input [ i ] = " " ;
68- }
69- // Check '?' in the beginning, and omit if exists
70- else if ( c === 63 && i === 0 ) {
71- separatorIndex = 1 ;
72- equalityIndex = 1 ;
69+ hasPlus = true ;
7370 } else {
7471 // Check '%' character for encoding
7572 if ( c === 37 ) {
0 commit comments