Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding Date object support to dojo.clone(). Fixes #5284.

Added tests for dojo.clone().
  • Loading branch information...
commit 9fcf1af297f36385dc54236c3721842c5b5ed14d 1 parent c5c15a4
Eugene Lazutkin uhop authored
Showing with 37 additions and 13 deletions.
  1. +16 −12 _base/lang.js
  2. +21 −1 tests/_base/lang.js
28 _base/lang.js
View
@@ -214,20 +214,24 @@ dojo.clone = function(/*anything*/ o){
r.push(dojo.clone(o[i]));
}
return r; // Array
- }else if(dojo.isObject(o)){
- if(o.nodeType && o.cloneNode){ // isNode
- return o.cloneNode(true); // Node
- }else{
- var r = new o.constructor(); // specific to dojo.declare()'d classes!
- for(var i in o){
- if(!(i in r) || r[i] != o[i]){
- r[i] = dojo.clone(o[i]);
- }
- }
- return r; // Object
+ }
+ if(!dojo.isObject(o)){
+ return o; /*anything*/
+ }
+ if(o.nodeType && o.cloneNode){ // isNode
+ return o.cloneNode(true); // Node
+ }
+ if(o instanceof Date){
+ return new Date(o.getTime()); // Date
+ }
+ // Generic objects
+ var r = new o.constructor(); // specific to dojo.declare()'d classes!
+ for(var i in o){
+ if(!(i in r) || r[i] != o[i]){
+ r[i] = dojo.clone(o[i]);
}
}
- return o; /*anything*/
+ return r; // Object
}
dojo.trim = function(/*String*/ str){
22 tests/_base/lang.js
View
@@ -151,6 +151,26 @@ tests.register("tests._base.lang",
}
var obj2 = thinger.apply(this, obj1);
t.assertEqual(obj1[0], obj2[0]);
- }
+ },
+
+ function clone(t) {
+ var obj1 = {foo: 'bar', answer: 42, jan102007: new Date(2007, 0, 10),
+ baz: {
+ a: null,
+ b: [
+ 1, "b", 2.3, true, false
+ //, function(){ return 4; }, /\d+/gm
+ ]
+ }
+ };
+ var obj2 = dojo.clone(obj1);
+ t.assertEqual(obj1.foo, obj2.foo);
+ t.assertEqual(obj1.answer, obj2.answer);
+ t.assertEqual(obj1.jan102007, obj2.jan102007);
+ t.assertEqual(obj1.baz.a, obj2.baz.a);
+ for(var i = 0; i < obj1.baz.b.length; ++i){
+ t.assertEqual(obj1.baz.b[i], obj2.baz.b[i]);
+ }
+ }
]
);
Please sign in to comment.
Something went wrong with that request. Please try again.