-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(helpers/mergedeep): fix potential cases when source or target obj…
…ects can be mutated
- Loading branch information
Showing
3 changed files
with
91 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,59 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { mergeDeep } from './mergeDeep'; | ||
|
||
describe.concurrent('Helper / mergeDeep (Deeply merge two objects)', () => { | ||
it('should use the overriding value given an identical key in both inputs', () => { | ||
const defaultTheme = { | ||
base: 'base', | ||
content: { | ||
base: 'content', | ||
}, | ||
flush: { | ||
off: 'no-flush', | ||
on: 'flush', | ||
}, | ||
}; | ||
const overrides = { | ||
content: { | ||
base: 'new-content', | ||
}, | ||
flush: { | ||
off: 'new-no-flush', | ||
on: 'new-flush', | ||
}, | ||
}; | ||
|
||
expect(mergeDeep(defaultTheme, overrides)).toEqual({ | ||
base: 'base', | ||
content: { | ||
base: 'new-content', | ||
}, | ||
flush: { | ||
off: 'new-no-flush', | ||
on: 'new-flush', | ||
}, | ||
}); | ||
describe('Helper / mergeDeep (Deeply merge two objects)', () => { | ||
it('should merge keys that do not exist in target', () => { | ||
const target = {}; | ||
const source = { foo: 'bar' }; | ||
|
||
const result = mergeDeep(target, source); | ||
|
||
expect(result).to.deep.equal({ foo: 'bar' }); | ||
}); | ||
|
||
it('should merge keys that do not exist in source', () => { | ||
const target = { foo: 'bar' }; | ||
const source = {}; | ||
|
||
const result = mergeDeep(target, source); | ||
|
||
expect(result).to.deep.equal({ foo: 'bar' }); | ||
}); | ||
|
||
it('should override target key if source key is identical', () => { | ||
const target = { foo: { bar: 'baz' } }; | ||
const source = { foo: { bar: 'foobar' } }; | ||
|
||
const result = mergeDeep(target, source); | ||
|
||
expect(result).to.deep.equal({ foo: { bar: 'foobar' } }); | ||
}); | ||
|
||
it('should not mutate target', () => { | ||
const target = { foo: { bar: 'test' } }; | ||
const source = {}; | ||
|
||
const result = mergeDeep(target, source); | ||
|
||
expect(result).to.deep.equal({ foo: { bar: 'test' } }); | ||
|
||
result.foo.bar = 'new test'; | ||
|
||
expect(result).to.deep.equal({ foo: { bar: 'new test' } }); | ||
expect(target).to.deep.equal({ foo: { bar: 'test' } }); | ||
}); | ||
|
||
it('should not mutate source', () => { | ||
const target = {}; | ||
const source = { foo: { bar: 'test' } }; | ||
|
||
const result = mergeDeep(target, source); | ||
|
||
expect(result).to.deep.equal({ foo: { bar: 'test' } }); | ||
|
||
result.foo.bar = 'new test'; | ||
|
||
expect(result).to.deep.equal({ foo: { bar: 'new test' } }); | ||
expect(source).to.deep.equal({ foo: { bar: 'test' } }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
// source: https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge | ||
|
||
import { DeepPartial } from '../components'; | ||
|
||
/** | ||
* Simple object check. | ||
* Check if provided parameter is plain object | ||
* @param item | ||
* @returns {boolean} | ||
* @returns true or false | ||
*/ | ||
export function isObject(item: unknown) { | ||
return item && typeof item === 'object' && !Array.isArray(item); | ||
function isObject(item: unknown): item is Record<string, unknown> { | ||
return item !== null && typeof item === 'object' && item.constructor === Object; | ||
} | ||
|
||
export function cloneDeep<T>(source: T) { | ||
if (!isObject(source)) { | ||
return source; | ||
} | ||
|
||
const output = { ...source }; | ||
|
||
Object.keys(source).forEach((key) => { | ||
(output as Record<string, unknown>)[key] = cloneDeep(source[key]); | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
/** | ||
* Deep merge two objects with deep copy of the target object. | ||
* @param target | ||
* @param ...sources | ||
* Deep copy the values of all of the enumerable own properties of target object from source object to a new object | ||
* @param target The target object to get properties from. | ||
* @param source The source object from which to copy properties. | ||
* @return A new merged and deep copied object. | ||
*/ | ||
export function mergeDeep<T extends Record<string, unknown>>(target: T, ...sources: DeepPartial<T>[]): T { | ||
if (!sources.length) return target; | ||
const source = sources.shift(); | ||
const output = { ...target }; | ||
|
||
if (isObject(target) && isObject(source)) { | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!target[key]) Object.assign(output, { [key]: {} }); | ||
(output[key] as Record<string, unknown>) = mergeDeep( | ||
target[key] as Record<string, unknown>, | ||
source[key] as Record<string, unknown>, | ||
); | ||
export function mergeDeep<T extends object, S extends object>(target: T, source: S): T & S { | ||
if (isObject(source) && Object.keys(source).length === 0) { | ||
return cloneDeep({ ...target, ...source }); | ||
} | ||
|
||
let output = { ...target, ...source }; | ||
|
||
if (isObject(source) && isObject(target)) { | ||
Object.keys(source).forEach((key) => { | ||
if (isObject(source[key]) && key in target && isObject(target[key])) { | ||
(output as Record<string, unknown>)[key] = mergeDeep(target[key] as object, source[key] as object); | ||
} else { | ||
Object.assign(output, { [key]: source[key] }); | ||
(output as Record<string, unknown>)[key] = isObject(source[key]) ? cloneDeep(source[key]) : source[key]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return mergeDeep(output, ...sources); | ||
return output; | ||
} |