Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created April 3, 2019 08:00
Show Gist options
  • Save KrofDrakula/95a1e7fb5ebf789e919960b2137d570d to your computer and use it in GitHub Desktop.
Save KrofDrakula/95a1e7fb5ebf789e919960b2137d570d to your computer and use it in GitHub Desktop.
Just stashing a proof-of-concept
const isPrimitive = (obj) => {
if (typeof obj === 'number' || typeof obj === 'boolean' || typeof obj === 'string') return true;
if (Number.isNaN(obj) || obj === null || obj === undefined) return true;
return false;
}
const isIterable = (obj) => !isPrimitive(obj);
const clone = (obj) => {
if (isIterable(obj)) {
if (obj instanceof Map) {
return new Map(Array.from(obj.entries()).map(entry => entry.map(clone)));
} else if (obj instanceof Set) {
return new Set(Array.from(obj.values()).map(clone));
} else {
const copy = {};
for (const [key, value] of Object.entries(obj))
copy[key] = clone(value);
return copy;
}
} else {
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment