Skip to content

Commit 33bdc37

Browse files
committed
Merge pull request coffeescript-cookbook#41 from jacobbubu/master
Add clone support for Date and RegExp class
2 parents 5e7fe47 + bcdad46 commit 33bdc37

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

chapters/classes_and_objects/cloning.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ clone = (obj) ->
1414
if not obj? or typeof obj isnt 'object'
1515
return obj
1616

17+
if obj instanceof Date
18+
return new Date(obj.getTime())
19+
20+
if obj instanceof RegExp
21+
flags = ''
22+
flags += 'g' if obj.global?
23+
flags += 'i' if obj.ignoreCase?
24+
flags += 'm' if obj.multiline?
25+
flags += 'y' if obj.sticky?
26+
return new RegExp(obj.source, flags)
27+
1728
newInstance = new obj.constructor()
1829

1930
for key of obj

0 commit comments

Comments
 (0)