Skip to content

Commit

Permalink
Improve array handling in mergician: correctly deduplicate arrays of …
Browse files Browse the repository at this point in the history
…objects in index.js
  • Loading branch information
Josue Monteiro committed Aug 20, 2024
1 parent 5424b48 commit 95af728
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,19 @@ export function mergician(optionsOrObject, ...objects) {
const propDescriptor = Object.getOwnPropertyDescriptor(obj, key);
const { configurable, enumerable, writable } = propDescriptor;

let value = [...new Set(obj[key])];

// Handle arrays of objects
if (Array.isArray(obj[key]) && typeof obj[key][0] === 'object') {
value = [...new Set(obj[key].map(item => JSON.stringify(item)))];
value = value.map(item => JSON.parse(item));
}

// Set static value to handle arrays received from srcObj getter
Object.defineProperty(obj, key, {
configurable,
enumerable,
value: [...new Set(obj[key])],
value: value,
writable: writable !== undefined ? writable : true
});
}
Expand Down

0 comments on commit 95af728

Please sign in to comment.