-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
36 lines (32 loc) · 984 Bytes
/
utils.js
File metadata and controls
36 lines (32 loc) · 984 Bytes
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
const _ = require('lodash');
module.exports.deepMerge = deepMerge;
/**
* Merge to objects recursively
* @param {object} target
* @param {object[]} sources
* @return {object}
*/
function deepMerge(target, ...sources) {
const isObject = (item) => item && typeOf(item) === 'object';
return _.mergeWith({}, target, ...sources, function (_subject, _target) {
if (_.isArray(_subject) && _.isArray(_target)) {
const biggerArray = _subject.length > _target.length ? _subject : _target;
const lesser = _subject.length > _target.length ? _target : _subject;
return biggerArray.map((el, i) => {
if (isObject(el) && isObject(lesser[i])) {
return _.mergeWith({}, el, lesser[i]);
} else {
return el;
}
});
}
});
}
/**
* Returns real type of passed variable
* @param obj
* @return {string}
*/
function typeOf(obj) {
return Object.prototype.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}