UNPKG

844 BJavaScriptView Raw
1'use strict';
2const {URL} = require('url');
3const is = require('@sindresorhus/is');
4
5const merge = (target, ...sources) => {
6 for (const source of sources) {
7 for (const [key, sourceValue] of Object.entries(source)) {
8 if (is.undefined(sourceValue)) {
9 continue;
10 }
11
12 const targetValue = target[key];
13 if (is.urlInstance(targetValue) && (is.urlInstance(sourceValue) || is.string(sourceValue))) {
14 target[key] = new URL(sourceValue, targetValue);
15 } else if (is.plainObject(sourceValue)) {
16 if (is.plainObject(targetValue)) {
17 target[key] = merge({}, targetValue, sourceValue);
18 } else {
19 target[key] = merge({}, sourceValue);
20 }
21 } else if (is.array(sourceValue)) {
22 target[key] = merge([], sourceValue);
23 } else {
24 target[key] = sourceValue;
25 }
26 }
27 }
28
29 return target;
30};
31
32module.exports = merge;