blob: 294b91d10ae8bb14bf916bcc49d2c0266978da78 [file]
import isBinaryObject from './isBinaryObject';
import cloneBinaryObject from './cloneBinaryObject';
import isPlainObject from './isPlainObject';
function clone(object) {
var newObject;
var i;
var len;
if (!object || typeof object !== 'object') {
return object;
}
if (Array.isArray(object)) {
newObject = [];
for (i = 0, len = object.length; i < len; i++) {
newObject[i] = clone(object[i]);
}
return newObject;
}
// special case: to avoid inconsistencies between IndexedDB
// and other backends, we automatically stringify Dates
if (object instanceof Date && isFinite(object)) {
return object.toISOString();
}
if (isBinaryObject(object)) {
return cloneBinaryObject(object);
}
if (!isPlainObject(object)) {
return object; // don't clone objects like Workers
}
newObject = {};
for (i in object) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(object, i) && i !== '__proto__') {
var value = clone(object[i]);
if (typeof value !== 'undefined') {
newObject[i] = value;
Object.defineProperty(newObject, i, {
value, writable: true, enumerable: true, configurable: true
});
}
}
}
return newObject;
}
export default clone;