Skip to content

Commit

Permalink
fix: using lodash for deep merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Feb 15, 2024
1 parent 6d0c971 commit 0996df4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 81 deletions.
34 changes: 3 additions & 31 deletions src/cdk/v2/destinations/bluecore/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const lodash = require('lodash');

const {
InstrumentationError,
isDefinedAndNotNullAndNotEmpty,
Expand Down Expand Up @@ -146,35 +148,6 @@ const addProductArray = (products) => {
return finalProductArray;
};

function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}

/**
* Recursively merges multiple objects into a single object.
*
* @param {Object} target - The target object to merge into.
* @param {...Object} sources - The source objects to merge from.
* @returns {Object} - The merged object.
*/
function deepMerge(target, ...sources) {
if (sources.length === 0) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
deepMerge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
});
}

return deepMerge(target, ...sources);
}

/**
* Constructs properties based on the given message.
*
Expand All @@ -186,7 +159,7 @@ const constructProperties = (message) => {
const commonPayload = constructPayload(message, MAPPING_CONFIG[commonCategory.name]);
const category = CONFIG_CATEGORIES[message.type.toUpperCase()];
const typeSpecificPayload = constructPayload(message, MAPPING_CONFIG[category.name]);
const finalPayload = deepMerge({}, commonPayload, typeSpecificPayload);
const finalPayload = lodash.merge(commonPayload, typeSpecificPayload);
return finalPayload;
};

Expand Down Expand Up @@ -252,5 +225,4 @@ module.exports = {
constructProperties,
createProductForStandardEcommEvent,
populateAccurateDistinctId,
deepMerge,
};
50 changes: 0 additions & 50 deletions src/cdk/v2/destinations/bluecore/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
isStandardBluecoreEvent,
deduceTrackEventName,
populateAccurateDistinctId,
deepMerge,
} = require('./utils');
const { InstrumentationError } = require('@rudderstack/integrations-lib');

Expand Down Expand Up @@ -329,53 +328,4 @@ describe('populateAccurateDistinctId', () => {
const distinctId = populateAccurateDistinctId(payload, message);
expect(distinctId).toBe('54321');
});

describe('deepMerge', () => {
// Should merge two objects with non-overlapping properties
it('should merge two objects with non-overlapping properties', () => {
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const result = deepMerge(obj1, obj2);
expect(result).toEqual({ a: 1, b: 2, c: 3, d: 4 });
});

// Should merge two objects with overlapping properties
it('should merge two objects with overlapping properties', () => {
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const result = deepMerge(obj1, obj2);
expect(result).toEqual({ a: 1, b: 3, c: 4 });
});

// Should merge multiple objects with non-overlapping properties
it('should merge multiple objects with non-overlapping properties', () => {
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const obj3 = { c: 3 };
const result = deepMerge(obj1, obj2, obj3);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

// Should return an empty object when no arguments are passed
it('should return an empty object when no arguments are passed', () => {
const result = deepMerge();
expect(result).toEqual(undefined);
});

// Should return the target object when no sources are passed
it('should return the target object when no sources are passed', () => {
const obj = { a: 1, b: 2 };
const result = deepMerge(obj);
expect(result).toBe(obj);
});

// Should handle non-object values as the target object
it('target must be an object', () => {
const target = 'hello';
const obj = { a: 1, b: 2 };
const result = deepMerge(target, obj);
expect(result).not.toBe({ a: 1, b: 2 });
expect(result).toBe('hello');
});
});
});

0 comments on commit 0996df4

Please sign in to comment.