Repository navigation
Expand file tree
/
Copy pathquerystring.js
More file actions
53 lines (43 loc) · 1.42 KB
/
Copy pathquerystring.js
File metadata and controls
53 lines (43 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function parseQueryString(queryString) {
const queryParams = {};
if (!queryString || queryString.length === 0) {
return queryParams;
}
// Helper to decode '+' as spaces and percent-encoded characters
function decodeParam(str) {
return decodeURIComponent(str.replace(/\+/g, " "));
}
// Split by '&' to get raw key-value pairs
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
// Ignore empty pairs caused by trailing or duplicate '&' (e.g. "a=1&&b=2&")
if (pair.length === 0) {
continue;
}
let rawKey, rawValue;
const equalIndex = pair.indexOf("=");
if (equalIndex === -1) {
// Key with no '=' (e.g., "key") -> value is empty string
rawKey = pair;
rawValue = "";
} else {
// Split on the FIRST '=' only (handles values containing '=', e.g. "a=b-2")
rawKey = pair.slice(0, equalIndex);
rawValue = pair.slice(equalIndex + 1);
}
const key = decodeParam(rawKey);
const value = decodeParam(rawValue);
// Stretch Goal: Handle duplicate keys by converting to an array
if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
if (Array.isArray(queryParams[key])) {
queryParams[key].push(value);
} else {
queryParams[key] = [queryParams[key], value];
}
} else {
queryParams[key] = value;
}
}
return queryParams;
}
module.exports = parseQueryString;