Add new cloneJSON method for cloning an object

This is useful in tests where `deepMerge` would retain references to old
objects.
This commit is contained in:
Robin Ward
2020-10-23 14:01:25 -04:00
parent 6f5d8cad51
commit e246208756
2 changed files with 12 additions and 9 deletions

View File

@ -3,6 +3,8 @@ function isObject(obj) {
}
// a fairly simple deep merge based on: https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
// note: this approach might reference the original object. If you mutate an object once you've deep
// cloned it, say in a test, it might remain modified. Consider `cloneJSON` instead.
export function deepMerge(...objects) {
function deepMergeInner(target, source) {
Object.keys(source).forEach((key) => {
@ -53,3 +55,7 @@ export function deepEqual(obj1, obj2) {
return true;
}
}
export function cloneJSON(obj) {
return JSON.parse(JSON.stringify(obj));
}