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
42 lines (34 loc) · 985 Bytes
/
querystring.js
File metadata and controls
42 lines (34 loc) · 985 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
39
40
41
42
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
if (queryString.includes("&")) {
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
let countMatch = 0;
if (pair.includes("=")) {
const equalSignIndex = pair.indexOf("=");
queryParams[pair.slice(0, equalSignIndex)] = pair.slice(
equalSignIndex + 1
);
} else {
throw new Error(
"error invalid format string, no = to separate key value pairs"
);
}
}
}
if (queryString.includes("=")) {
const equalSignIndex = queryString.indexOf("=");
queryParams[queryString.slice(0, equalSignIndex)] = queryString.slice(
equalSignIndex + 1
);
} else {
throw new Error(
"error invalid format string, no = to separate key value pairs"
);
}
return queryParams;
}
module.exports = parseQueryString;