Skip to content

Commit

Permalink
feat(utils): deep merge for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
glebcha committed Nov 28, 2023
1 parent 2844881 commit 63abca1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/deepMerge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { is } from './getType';

type Entity = Record<string, unknown>

export function deepMerge(target: Entity, source: Entity) {
Object.keys(source).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(target, key) || !is.Object(source[key])) {
const areBothArrays = [target[key], source[key]].every(Array.isArray);

target[key] = areBothArrays ? (target[key] as unknown[]).concat(source[key]) : source[key];
} else {
deepMerge(target[key] as Entity, source[key] as Entity);
}
});

return target;
}

0 comments on commit 63abca1

Please sign in to comment.