Utils updated.
This commit is contained in:
parent
81679176a4
commit
9f024d3a3d
64
lib/utils.js
64
lib/utils.js
|
|
@ -303,52 +303,38 @@ Cola.Dictionary.prototype = {
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.clone = function (item) {
|
Cola.clone = function (item) {
|
||||||
if (!item) { return item; } // null, undefined values check
|
if (item === undefined || item === null) return item;
|
||||||
|
|
||||||
var types = [ Number, String, Boolean ],
|
var result, types = [ Number, String, Boolean ];
|
||||||
result;
|
|
||||||
|
|
||||||
// normalizing primitives if someone did new String('aaa'), or new Number('444');
|
for (var i in types)
|
||||||
types.forEach(function(type) {
|
if(types.hasOwnProperty(i) && item instanceof types[i])
|
||||||
if (item instanceof type) {
|
return type( item );
|
||||||
result = type( item );
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (typeof result == "undefined") {
|
if (item.__proto__ === Array.prototype) {
|
||||||
if (Object.prototype.toString.call( item ) === "[object Array]") {
|
|
||||||
result = [];
|
result = [];
|
||||||
item.forEach(function(child, index, array) {
|
item.forEach(function(child, index, array) {
|
||||||
result[index] = Cola.clone( child );
|
result[index] = Cola.clone( child );
|
||||||
});
|
});
|
||||||
} else if (typeof item == "object") {
|
|
||||||
// testing that this is DOM
|
|
||||||
if (item.nodeType && typeof item.cloneNode == "function") {
|
|
||||||
var result = item.cloneNode( true );
|
|
||||||
} else if (!item.prototype) { // check that this is a literal
|
|
||||||
if (item instanceof Date) {
|
|
||||||
result = new Date(item);
|
|
||||||
} else {
|
|
||||||
// it is an object literal
|
|
||||||
result = {};
|
|
||||||
for (var i in item) {
|
|
||||||
result[i] = Cola.clone( item[i] );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// depending what you would like here,
|
|
||||||
// just keep the reference, or create new object
|
|
||||||
if (false && item.constructor) {
|
|
||||||
// would not advice to do that, reason? Read below
|
|
||||||
result = new item.constructor();
|
|
||||||
} else {
|
|
||||||
result = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(item instanceof Object)) return item;
|
||||||
|
|
||||||
|
if (item.nodeType && item.cloneNode instanceof Function) return item.cloneNode( true );
|
||||||
|
|
||||||
|
if (!item.prototype) {
|
||||||
|
if (item instanceof Date) return new Date(item);
|
||||||
|
if (item instanceof Function) return item;
|
||||||
|
if (item.clone instanceof Function) return item.clone();
|
||||||
|
|
||||||
|
result = {};
|
||||||
|
for (var i in item) result[i] = Cola.clone( item[i] );
|
||||||
|
result.__proto__ = item.__proto__;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return item;
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user