Public
Edited
Dec 23, 2020
Importers
1 star
testOverlapAce Editor InputChai Unit TesterUtilitiesdeepMerge
deepClone allowing cyclic object references & functions
Insert cell
Insert cell
deepClone = function deepClone(value, seen = new WeakMap()) {
// if primitive, return without cloning
if (value !== Object(value)) return value;
// if circular, return object reference
if (seen.has(value)) return seen.get(value);

let result;
try {
result = new value.constructor();
} catch {
result = Object.create(Object.getPrototypeOf(value));
}

if (typeof value === "function")
result = function(...args) {
return value.apply(this, args);
};

seen.set(value, result);

for (const key of Object.keys(value)) {
result[key] = deepClone(value[key], seen);
}

return result;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more