forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerystring.js
More file actions
46 lines (31 loc) · 1.19 KB
/
Copy pathquerystring.js
File metadata and controls
46 lines (31 loc) · 1.19 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
function parseQueryString(queryString) {
const parsedParams = {}; // Gets the final key-value pairs
if (!queryString) return parsedParams;
// Removes leading '?' if present
if (queryString.startsWith("?")) {
queryString = queryString.slice(1);
}
if (queryString.length === 0) {
return parsedParams;
}
// Split the string into individual key-value pairs
const pairs = queryString.split("&");
for (const pair of pairs) {
if (!pair) continue; // skip empty segments (like from && or trailing &) eg "name=John&&age=30"
const equalSignIndex = pair.indexOf("=");
let paramKey, paramValue;
if (equalSignIndex === -1) {
// If '=' not found we have a key exists but value is empty
paramKey = decodeURIComponent(pair);
paramValue = "";
} else {
paramKey = decodeURIComponent(pair.slice(0, equalSignIndex));
paramValue = decodeURIComponent(pair.slice(equalSignIndex + 1));
}
parsedParams[paramKey] = paramValue; // overwrite previous value if key repeats
}
return parsedParams;
}
module.exports = parseQueryString;
// In querystring.js function implemented.
// Decoding of paramKey = decodeURIComponent(pair); added.