Created
April 3, 2019 08:00
-
-
Save KrofDrakula/95a1e7fb5ebf789e919960b2137d570d to your computer and use it in GitHub Desktop.
Just stashing a proof-of-concept
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
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