-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflattenObject.js
More file actions
61 lines (47 loc) · 1.52 KB
/
flattenObject.js
File metadata and controls
61 lines (47 loc) · 1.52 KB
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
51
52
53
54
55
56
57
58
59
60
61
const { mergeDeep } = require("./mergeDeep");
// Flatten object with recursive call
// object => object to flat
// { a: 1, b: { c: 2 } } => { a: 1, "b.c": 2 }
const flattenObject = (object) => Object.entries(object)
.map(([k, v]) => {
if (
typeof v === "object"
&& !Array.isArray(v)
) {
const tmpO = Object.entries(v)
.map(([k2, v2]) => {
return [ `${k}.${k2}`, v2 ];
})
.reduce((acc, [k2, v2]) => ({ ...acc, [k2]: v2 }), {});
return flattenObject(tmpO);
}
return { [k]: v };
})
.reduce((acc, elem) => ({ ...acc, ...elem }), {});
// Unflatten object as string (ex: url with query string parameters)
// a.b=1 => { a: { b: [ 1 ] } }
const unflattenObjectPart = string => {
const [ keys, values ] = string.split("=");
const newValues = Array.isArray(values) ? values : values.split(",");
const attributes = keys.split(".");
if (attributes.length === 1) {
// only one key
return { [attributes[0]]: newValues }
}
if (attributes.length > 1) {
const newAttributes = attributes.slice(1) // remove first attribute
const newKeys = newAttributes.join(".");
return {
[attributes[0]]: unflattenObjectPart(`${newKeys}=${newValues}`)
}
}
return {};
}
// Unflatten object with recursive call
// string => string to construct object on (ex: url with query sstring parameters)
// "a.b=1&a.c.d=2,3" => { a: { b: [ 1 ], c: { d: [ 2, 3 ] } } }
const unflattenObject = (string) => {
const params = string.split("&");
const res = params.map(p => unflattenObjectPart(p));
return mergeDeep(...res);
}