Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/util/src/object/spread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@
/**
* @name objectSpread
* @summary Concats all sources into the destination
* @description Spreads object properties while maintaining object integrity
*/
export function objectSpread <T extends object> (dest: object, ...sources: (object | undefined | null)[]): T {
const filterProps = new Set(['__proto__', 'constructor', 'prototype']);

for (let i = 0, count = sources.length; i < count; i++) {
const src = sources[i];

if (src) {
if (typeof (src as Map<string, unknown>).entries === 'function') {
for (const [key, value] of (src as Map<string, unknown>).entries()) {
(dest as Record<string, unknown>)[key] = value;
if (!filterProps.has(key)) {
(dest as Record<string, unknown>)[key] = value;
}
}
} else {
Object.assign(dest, src);
// Create a clean copy of the source object
const sanitizedSrc = Object.create(null) as Record<string, unknown>;

for (const [key, value] of Object.entries(src)) {
if (!filterProps.has(key)) {
sanitizedSrc[key] = value;
}
}

Object.assign(dest, sanitizedSrc);
}
}
}
Expand Down