-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
22 lines (19 loc) · 790 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
module.exports = (body) => {
const mapping = body.split('&').reduce((prev, curr) => { //convert to key value with dot notation keys
const pair = curr.split('=');
const replaceChars = { "[": ".", "]": "" };
const key = decodeURIComponent(pair[0]).replace(/\[|\]/g, match => replaceChars[match]);
const value = decodeURIComponent(pair[1]).replace("+", " ");
prev[key] = value;
return prev;
}, {});
//converts dot notation keys to nested object
return Object.entries(mapping).reduce((o, [k, v]) => setValue(o, k, v), {});
}
function setValue(object, path, value) {
let keys = path.split('.');
let last = keys.pop();
keys.reduce((o, k) => o[k] = o[k] || {}, object)[last] = value;
return object;
}