-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
join-arrays.js
44 lines (35 loc) · 969 Bytes
/
join-arrays.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
import {
cloneDeep, isFunction, isPlainObject, mergeWith
} from 'lodash';
const isArray = Array.isArray;
export default function joinArrays({
customizeArray,
customizeObject,
key
} = {}) {
return function _joinArrays(a, b, k) {
const newKey = key ? `${key}.${k}` : k;
if (isFunction(a) && isFunction(b)) {
return (...args) => _joinArrays(a(...args), b(...args), k);
}
if (isArray(a) && isArray(b)) {
const customResult = customizeArray && customizeArray(a, b, newKey);
return customResult || [...a, ...b];
}
if (isPlainObject(a) && isPlainObject(b)) {
const customResult = customizeObject && customizeObject(a, b, newKey);
return customResult || mergeWith({}, a, b, joinArrays({
customizeArray,
customizeObject,
key: newKey
}));
}
if (isPlainObject(b)) {
return cloneDeep(b);
}
if (isArray(b)) {
return [...b];
}
return b;
};
}