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
29 lines (23 loc) · 697 Bytes
/
querystring.js
File metadata and controls
29 lines (23 loc) · 697 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
function parseQueryString(queryString) {
const queryParams = {};
if (!queryString) {
return queryParams;
}
function decodePart(text) {
try {
return decodeURIComponent(text);
} catch (error) {
return text;
}
}
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
const index = pair.indexOf("="); // find the first =
if (index === -1) continue; // skip if no =
const key = decodePart(pair.substring(0, index)); // before the =
const value = decodePart(pair.substring(index + 1)); // after the =
queryParams[key] = value;
}
return queryParams;
}
module.exports = parseQueryString;