|
7 | 7 | function parse(input) { |
8 | 8 | let result = Object.create(null); |
9 | 9 |
|
10 | | - let currentKey = [""]; |
| 10 | + let currentKey = ""; |
11 | 11 | let currentValue = ""; |
12 | | - let currentPairIsArray = false; |
13 | 12 | let seenEqualOperator = false; |
14 | 13 |
|
15 | 14 | // Have a boundary of input.length + 1 to access last pair inside the loop. |
16 | 15 | for (let i = 0; i < input.length + 1; i++) { |
17 | | - let currentCharacter = input.charCodeAt(i); |
18 | | - let currentKeyLength = currentKey.length; |
19 | | - |
20 | | - if (isNaN(currentCharacter)) { |
21 | | - currentCharacter = 38; |
22 | | - } |
23 | | - |
24 | | - switch (currentCharacter) { |
25 | | - // & |
26 | | - case 38: { |
27 | | - let root = result; |
28 | | - |
29 | | - for (let k = 0; k < currentKeyLength; k++) { |
30 | | - let key = currentKey[k]; |
31 | | - |
32 | | - if (typeof root[key] === "undefined") { |
33 | | - if (k === currentKeyLength - 1) { |
34 | | - if (currentPairIsArray) { |
35 | | - root[key] = [currentValue]; |
36 | | - } else { |
37 | | - root[key] = currentValue; |
38 | | - } |
39 | | - } else { |
40 | | - root[key] = {}; |
41 | | - root = root[key]; |
42 | | - } |
43 | | - } else if (Array.isArray(root[key])) { |
44 | | - root[key].push(currentValue); |
45 | | - } else if (k === currentKeyLength - 1) { |
46 | | - root[key] = [root[key], currentValue]; |
47 | | - } else { |
48 | | - Object.assign(root[key], { [key]: {} }); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - // Reset reading key value pairs |
53 | | - currentKey = [""]; |
54 | | - currentValue = ""; |
55 | | - seenEqualOperator = false; |
56 | | - currentPairIsArray = false; |
57 | | - break; |
| 16 | + let c = input.charCodeAt(i); |
| 17 | + |
| 18 | + // Handle '&' and end of line to pass the current values to result |
| 19 | + if (c === 38 || isNaN(c)) { |
| 20 | + // Disallow empty key values. |
| 21 | + if (currentKey.length === 0) { |
| 22 | + continue |
| 23 | + } else if (typeof result[currentKey] === "undefined") { |
| 24 | + result[currentKey] = currentValue; |
| 25 | + } else if (Array.isArray(result[currentKey])) { |
| 26 | + result[currentKey].push(currentValue); |
| 27 | + } else { |
| 28 | + result[currentKey] = [result[currentKey], currentValue]; |
58 | 29 | } |
59 | | - // ']' |
60 | | - case 93: { |
61 | | - // Ignore ']' character, since it's already handled inside '['. |
62 | | - break; |
63 | | - } |
64 | | - // '[' |
65 | | - case 91: { |
66 | | - let nextCharacter = input.charCodeAt(i + 1); |
67 | | - |
68 | | - // Check if input is an array. Example: hello[]=world equals to { hello: ['world'] } |
69 | | - if (nextCharacter === 93) { |
70 | | - currentPairIsArray = true; |
71 | | - i++; |
72 | | - } |
73 | | - // Check push a new key to keys. Only applicable when key is not an array, but consists of length > 0. |
74 | | - else { |
75 | | - currentKey.push(""); |
76 | | - } |
77 | 30 |
|
78 | | - break; |
79 | | - } |
80 | | - // '=' |
81 | | - case 61: { |
82 | | - seenEqualOperator = true; |
83 | | - break; |
84 | | - } |
85 | | - default: { |
86 | | - if (seenEqualOperator) { |
87 | | - currentValue += input[i]; |
88 | | - } else { |
89 | | - currentKey[currentKeyLength - 1] += input[i]; |
90 | | - } |
| 31 | + // Reset reading key value pairs |
| 32 | + currentKey = ""; |
| 33 | + currentValue = ""; |
| 34 | + seenEqualOperator = false; |
| 35 | + } |
| 36 | + // Handle equal operator |
| 37 | + else if (c === 61) { |
| 38 | + seenEqualOperator = true; |
| 39 | + } else { |
| 40 | + if (seenEqualOperator) { |
| 41 | + currentValue += input[i]; |
| 42 | + } else { |
| 43 | + currentKey += input[i]; |
91 | 44 | } |
92 | 45 | } |
93 | 46 | } |
|
0 commit comments