1 | import lodashMerge from "lodash.merge";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export function isNil(item) {
|
12 | return item === null || item === undefined;
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function isPlainObject(item) {
|
24 | return (
|
25 | typeof item === "object" &&
|
26 | !isNil(item) &&
|
27 | item.constructor === Object &&
|
28 | Object.prototype.toString.call(item) === "[object Object]"
|
29 | );
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export const merge = lodashMerge;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export function flatten(data, result = {}, path = "") {
|
57 | for (const key of Object.keys(data)) {
|
58 | let resultPath = `${path}.${key}`;
|
59 | if (path === "") {
|
60 | resultPath = key;
|
61 | }
|
62 | const value = data[key];
|
63 |
|
64 | if (!isPlainObject(value)) {
|
65 | result[resultPath] = value;
|
66 | } else {
|
67 | flatten(value, result, resultPath);
|
68 | }
|
69 | }
|
70 |
|
71 | return result;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export function unFlatten(data) {
|
83 | const result = {};
|
84 | for (const key of Object.keys(data)) {
|
85 | const value = data[key];
|
86 | const keyParts = key.split(".");
|
87 |
|
88 | let tmpObject = result;
|
89 | for (const part of keyParts.slice(0, keyParts.length - 1)) {
|
90 | if (isNil(tmpObject[part]) || !isPlainObject(tmpObject[part])) {
|
91 | tmpObject[part] = {};
|
92 | }
|
93 | tmpObject = tmpObject[part];
|
94 | }
|
95 | tmpObject[keyParts[keyParts.length - 1]] = value;
|
96 | }
|
97 |
|
98 | return result;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | export function camelToSnakeCase(input) {
|
109 | return input
|
110 | .replace(/(.)([A-Z][a-z]+)/g, "$1_$2")
|
111 | .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
112 | .toLowerCase()
|
113 | .trim();
|
114 | }
|