-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathquerystring.js
More file actions
38 lines (27 loc) · 717 Bytes
/
querystring.js
File metadata and controls
38 lines (27 loc) · 717 Bytes
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
function parseQueryString(queryString) {
const queryParams = {};
if (!queryString) return queryParams;
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
// skip empty pairs (e.g. &&)
if (pair === "") continue;
const index = pair.indexOf("=");
let key, value;
if (key === -1) {
key = pair;
value = "";
} else {
key = pair.substring(0, index);
value = pair.substring(index + 1);
}
// decoding URL
try {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
} catch (error) {
}
queryParams[key] = value;
}
return queryParams;
}
module.exports = parseQueryString;