Created
September 1, 2015 11:43
-
-
Save MNBuyskih/3dd19a017eaf90c3b525 to your computer and use it in GitHub Desktop.
Coffeescript deep object clone
This file contains hidden or 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
clone = (obj) -> | |
return obj if not obj? or typeof obj isnt 'object' | |
return new Date(obj.getTime()) if obj instanceof Date | |
if obj instanceof RegExp | |
flags = '' | |
flags += 'g' if obj.global? | |
flags += 'i' if obj.ignoreCase? | |
flags += 'm' if obj.multiline? | |
flags += 'y' if obj.sticky? | |
return new RegExp(obj.source, flags) | |
newInstance = new obj.constructor() | |
newInstance[key] = clone(obj[key]) for key of obj | |
return newInstance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment