clone() is now IE comparable.

This commit is contained in:
Martin Edenhofer 2015-06-03 07:53:28 +02:00
parent 51da02c96b
commit 9ac998edea

View file

@ -80,22 +80,49 @@ function difference(object1, object2) {
// clone, just data, no instances of objects // clone, just data, no instances of objects
function clone(item, full) { function clone(item, full) {
// just return/clone false conditions
if (!item) { return item } if (!item) { return item }
var itemType = item.constructor.name
// IE behavior // doesn't know item.constructor.name, detect it by underscore
if (itemType === undefined) {
if (_.isArray(item)) {
itemType = 'Array'
}
else if (_.isNumber(item)) {
itemType = 'Number'
}
else if (_.isString(item)) {
itemType = 'String'
}
else if (_.isBoolean(item)) {
itemType = 'Boolean'
}
else if (_.isFunction(item)) {
itemType = 'Function'
}
else if (_.isObject(item)) {
itemType = 'Object'
}
}
// 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' )
} }
if (item && item.constructor) {
if (!_.contains(acceptedInstances, item.constructor.name)) { // check if item is accepted to get cloned
return if (itemType && !_.contains(acceptedInstances, itemType)) {
} console.log('no acceptedInstances', itemType)
return
} }
// copy array // copy array
var result; var result;
if ( _.isArray(item) ) { if (itemType == 'Array') {
result = [] result = []
item.forEach(function(child, index, array) { item.forEach(function(child, index, array) {
result[index] = clone( child, full ) result[index] = clone( child, full )
@ -103,12 +130,12 @@ function clone(item, full) {
} }
// copy function // copy function
else if ( _.isFunction(item) ) { else if (itemType == 'Function') {
result = item.bind({}) result = item.bind({})
} }
// copy object // copy object
else if ( _.isObject(item) ) { else if (itemType == 'Object') {
result = {} result = {}
for(var key in item) { for(var key in item) {
if (item.hasOwnProperty(key)) { if (item.hasOwnProperty(key)) {
@ -116,7 +143,6 @@ function clone(item, full) {
} }
} }
} }
// copy others // copy others
else { else {
result = item result = item