forked from StayDistributed/amazon-api-gateway-querystring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (32 loc) · 1.09 KB
/
index.js
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
43
44
45
46
47
48
49
50
function recursivelyCheckIfArray (parentObj) {
if (Object.prototype.toString.call(parentObj) != '[object Object]') return parentObj;
Object.keys(parentObj).map((parentKey) => {
var childObj = parentObj[parentKey];
if (Object.prototype.toString.call(childObj) != '[object Object]') return;
var keys = Object.keys(childObj);
if (keys.every((childKey) => { return /^(\d+)$/g.test(childKey); }))
parentObj[parentKey] = keys.map((key) => { return childObj[key] });
recursivelyCheckIfArray(childObj);
});
return parentObj;
}
function parseParam (json) {
Object.keys(json).map((paramName) => {
var segments = paramName.match(/([^\[\]]+)/g),
step = json;
// No nested params found
if (segments.length <= 1) return;
segments.map(function (segment, k) {
if (k >= segments.length-1) {
step[segment] = json[paramName];
return;
}
if (!step[segment]) step[segment] = {};
step = step[segment];
});
});
return recursivelyCheckIfArray(json);
}
module.exports = function (json) {
return parseParam(json);
};