2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.isObjectNotArray = exports.deepMerge = void 0;
5 * Check if the variable contains an object strictly rejecting arrays
7 * @returns `true` if obj is an object
9 function isObjectNotArray(obj) {
10 return typeof obj === 'object' && !Array.isArray(obj);
12 exports.isObjectNotArray = isObjectNotArray;
14 * Pure function - doesn't mutate either parameter!
15 * Merges two objects together deeply, overwriting the properties in first with the properties in second
16 * @param first The first object
17 * @param second The second object
18 * @returns a new object
20 function deepMerge(first = {}, second = {}) {
21 // get the unique set of keys across both objects
22 const keys = new Set(Object.keys(first).concat(Object.keys(second)));
23 return Array.from(keys).reduce((acc, key) => {
24 const firstHasKey = key in first;
25 const secondHasKey = key in second;
26 const firstValue = first[key];
27 const secondValue = second[key];
28 if (firstHasKey && secondHasKey) {
29 if (isObjectNotArray(firstValue) && isObjectNotArray(secondValue)) {
31 acc[key] = deepMerge(firstValue, secondValue);
35 acc[key] = secondValue;
38 else if (firstHasKey) {
39 acc[key] = firstValue;
42 acc[key] = secondValue;
47 exports.deepMerge = deepMerge;
48 //# sourceMappingURL=deepMerge.js.map