-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (28 loc) · 857 Bytes
/
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
const get = require('@basket/get');
const _set = require('lodash/set');
const mergeWith = require('lodash/mergeWith');
const cloneDeep = require('lodash/cloneDeep');
const isPlainObject = require('lodash/isPlainObject');
const isArray = Array.isArray;
function merge(...args) {
return mergeWith(...args, (objValue, srcValue) => {
if (isArray(srcValue)) {
return cloneDeep(srcValue);
}
});
}
function set(object, path, value) {
if (isPlainObject(path)) {
return merge(object, path);
}
if (!object || !path) {
return object;
}
let oldValue = get(object, path);
let newValue = value;
if (isPlainObject(oldValue) && isPlainObject(newValue)) {
newValue = merge({}, oldValue, newValue);
}
return _set(object, path, cloneDeep(newValue));
}
module.exports = set;