mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
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:
@ -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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user