-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.js
More file actions
24 lines (22 loc) · 728 Bytes
/
decoder.js
File metadata and controls
24 lines (22 loc) · 728 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
const set = require("lodash.set");
const has = require("lodash.has");
const get = require("lodash.get");
module.exports = function decode(queryString) {
const queryStringPieces = queryString.split("&");
const decodedQueryString = {};
for (const piece of queryStringPieces) {
let [key, value] = piece.split("=");
value = value || "";
if (has(decodedQueryString, key)) {
const currentValueForKey = get(decodedQueryString, key);
if (!Array.isArray(currentValueForKey)) {
set(decodedQueryString, key, [currentValueForKey, value]);
} else {
currentValueForKey.push(value);
}
} else {
set(decodedQueryString, key, value);
}
}
return decodedQueryString;
};