-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathquerystring.js
More file actions
42 lines (34 loc) · 1.15 KB
/
Copy pathquerystring.js
File metadata and controls
42 lines (34 loc) · 1.15 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
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
// Adds percentage-encoded characters
queryString = changePEC(queryString)
// Replaces + with space
queryString = queryString.replaceAll("+", " ")
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
if (pair !== "") {
let index = pair.indexOf("=")
if (!pair.includes("=")) {index = pair.length}
const [key, value] = [pair.slice(0, index), pair.slice(index + 1)]
queryParams[key] = value;
}
else {};
}
return queryParams;
}
// this function below can have the object PEC expanded to include all percentage-encoded characters
// currently without adding this database, an unknown PEC could cause the code to get stuck in an infinite loop
function changePEC(string) {
const PEC = {"%24": "$", "%2F": "/"}
while (string.includes("%")) {
const index = string.indexOf("%");
const code = string.slice(index, index + 3);
if (PEC[code]) {string = string.replace(code, PEC[code]);}
else {break;}
}
return string
}
module.exports = parseQueryString;