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
38 lines (31 loc) · 964 Bytes
/
querystring.js
File metadata and controls
38 lines (31 loc) · 964 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.length === 0) {
return queryParams;
}
if (queryString.includes("&")) {
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
if (!pair.includes("=")) {
throw new Error(
"error invalid format string, no = to separate key value pairs"
);
} else {
const keyValuePair = pair.split("=");
console.log(keyValuePair + "the single pair");
queryParams[keyValuePair[0]] = keyValuePair[1];
}
}
} else 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;