Code cleanup. Removed not need clone2().

This commit is contained in:
Martin Edenhofer 2016-03-04 09:28:16 +01:00
parent d9cfcbbd34
commit 2825da2ca0

View file

@ -65,14 +65,14 @@ Date.prototype.getWeek = function() {
function difference(object1, object2) { function difference(object1, object2) {
var changes = {}; var changes = {};
for ( var name in object1 ) { for (var name in object1) {
if ( name in object2 ) { if (name in object2) {
if ( _.isObject( object2[name] ) && !_.isArray( object2[name] ) ) { if (_.isObject(object2[name]) && !_.isArray(object2[name])) {
var diff = difference( object1[name], object2[name] ); var diff = difference(object1[name], object2[name]);
if ( !_.isEmpty( diff ) ) { if (!_.isEmpty(diff)) {
changes[name] = diff; changes[name] = diff;
} }
} else if ( !_.isEqual( object1[name], object2[name] ) ) { } else if (!_.isEqual(object1[name], object2[name])) {
changes[name] = object2[name]; changes[name] = object2[name];
} }
} }
@ -113,7 +113,7 @@ function clone(item, full) {
// ignore certain objects // ignore certain objects
var acceptedInstances = [ 'Object', 'Number', 'String', 'Boolean', 'Array' ] var acceptedInstances = [ 'Object', 'Number', 'String', 'Boolean', 'Array' ]
if (full) { if (full) {
acceptedInstances.push( 'Function' ) acceptedInstances.push('Function')
} }
// check if item is accepted to get cloned // check if item is accepted to get cloned
@ -141,7 +141,7 @@ function clone(item, full) {
result = {} result = {}
for(var key in item) { for(var key in item) {
if (item.hasOwnProperty(key)) { if (item.hasOwnProperty(key)) {
result[key] = clone( item[key], full ) result[key] = clone(item[key], full)
} }
} }
} }
@ -152,58 +152,6 @@ function clone(item, full) {
return result return result
} }
// taken from http://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript
function clone2(item) {
if (!item) { return item; } // null, undefined values check
var types = [ Number, String, Boolean ],
result;
// normalizing primitives if someone did new String('aaa'), or new Number('444');
types.forEach(function(type) {
if (item instanceof type) {
result = type( item );
}
});
if (typeof result == "undefined") {
if (Object.prototype.toString.call( item ) === "[object Array]") {
result = [];
item.forEach(function(child, index, array) {
result[index] = 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] = 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;
}
// taken from https://github.com/epeli/underscore.string/blob/master/underscored.js // taken from https://github.com/epeli/underscore.string/blob/master/underscored.js
function underscored (str) { function underscored (str) {
return str.trim().replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase(); return str.trim().replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();