-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
125 lines (93 loc) · 3.12 KB
/
helper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore?tab=readme-ov-file#_flatten
* https://github.com/you-dont-need-x/you-dont-need-lodash
*/
// ----------------------------------------------------------------------
export function flattenArray(list, key = 'children') {
let children = [];
const flatten = list?.map((item) => {
if (item[key] && item[key].length) {
children = [...children, ...item[key]];
}
return item;
});
return flatten?.concat(children.length ? flattenArray(children, key) : children);
}
// ----------------------------------------------------------------------
export function flattenDeep(array) {
const isArray = array && Array.isArray(array);
if (isArray) {
return array.flat(Infinity);
}
return [];
}
// ----------------------------------------------------------------------
export function orderBy(array, properties, orders) {
return array.slice().sort((a, b) => {
for (let i = 0; i < properties.length; i += 1) {
const property = properties[i];
const order = orders && orders[i] === 'desc' ? -1 : 1;
const aValue = a[property];
const bValue = b[property];
if (aValue < bValue) return -1 * order;
if (aValue > bValue) return 1 * order;
}
return 0;
});
}
// ----------------------------------------------------------------------
export function keyBy(array, key) {
return (array || []).reduce((result, item) => {
const keyValue = key ? item[key] : item;
return { ...result, [String(keyValue)]: item };
}, {});
}
// ----------------------------------------------------------------------
export function sumBy(array, iteratee) {
return array.reduce((sum, item) => sum + iteratee(item), 0);
}
// ----------------------------------------------------------------------
export function isEqual(a, b) {
if (a === null || a === undefined || b === null || b === undefined) {
return a === b;
}
if (typeof a !== typeof b) {
return false;
}
if (typeof a === 'string' || typeof a === 'number' || typeof a === 'boolean') {
return a === b;
}
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
return a.every((item, index) => isEqual(item, b[index]));
}
if (typeof a === 'object' && typeof b === 'object') {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) {
return false;
}
return keysA.every((key) => isEqual(a[key], b[key]));
}
return false;
}
// ----------------------------------------------------------------------
function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
export const merge = (target, ...sources) => {
if (!sources.length) return target;
const source = sources.shift();
// eslint-disable-next-line no-restricted-syntax
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
merge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
return merge(target, ...sources);
};