-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathquerystring.js
More file actions
30 lines (20 loc) · 694 Bytes
/
querystring.js
File metadata and controls
30 lines (20 loc) · 694 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
function parseQueryString(queryString) {
const queryParams = {};
if (typeof queryString!=="string"||queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
if (!pair.includes("=")) continue;
const equalIndex = pair.indexOf("=");
const key = pair.slice(0, equalIndex);
const value = pair.slice(equalIndex + 1);
if (key === "") continue;
queryParams[key] = value;
}
return queryParams;
}
console.log(parseQueryString("color=blue&quality=good"))
console.log(parseQueryString("equation=x=y+1"))
console.log(parseQueryString(""))
module.exports = parseQueryString;