Skip to content

Commit

Permalink
Merge pull request #77 from c-w/master
Browse files Browse the repository at this point in the history
Improve support for Internet Explorer
  • Loading branch information
pvorb authored Mar 9, 2017
2 parents e7631a4 + 4330011 commit e3f252d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
40 changes: 17 additions & 23 deletions clone.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var clone = (function() {
'use strict';

function _instanceof(obj, type) {
return type != null && obj instanceof type;
}

var nativeMap;
try {
nativeMap = Map;
Expand Down Expand Up @@ -80,11 +84,11 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
return parent;
}

if (parent instanceof nativeMap) {
if (_instanceof(parent, nativeMap)) {
child = new nativeMap();
} else if (parent instanceof nativeSet) {
} else if (_instanceof(parent, nativeSet)) {
child = new nativeSet();
} else if (parent instanceof nativePromise) {
} else if (_instanceof(parent, nativePromise)) {
child = new nativePromise(function (resolve, reject) {
parent.then(function(value) {
resolve(_clone(value, depth - 1));
Expand All @@ -103,7 +107,7 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
child = new Buffer(parent.length);
parent.copy(child);
return child;
} else if (parent instanceof Error) {
} else if (_instanceof(parent, Error)) {
child = Object.create(parent);
} else {
if (typeof prototype == 'undefined') {
Expand All @@ -126,28 +130,18 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
allChildren.push(child);
}

if (parent instanceof nativeMap) {
var keyIterator = parent.keys();
while(true) {
var next = keyIterator.next();
if (next.done) {
break;
}
var keyChild = _clone(next.value, depth - 1);
var valueChild = _clone(parent.get(next.value), depth - 1);
if (_instanceof(parent, nativeMap)) {
parent.forEach(function(value, key) {
var keyChild = _clone(key, depth - 1);
var valueChild = _clone(value, depth - 1);
child.set(keyChild, valueChild);
}
});
}
if (parent instanceof nativeSet) {
var iterator = parent.keys();
while(true) {
var next = iterator.next();
if (next.done) {
break;
}
var entryChild = _clone(next.value, depth - 1);
if (_instanceof(parent, nativeSet)) {
parent.forEach(function(value) {
var entryChild = _clone(value, depth - 1);
child.add(entryChild);
}
});
}

for (var i in parent) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"fscherwi (https://fscherwi.github.io)",
"rictic (https://github.com/rictic)",
"Martin Jurča (https://github.com/jurca)",
"Misery Lee <[email protected]> (https://github.com/miserylee)"
"Misery Lee <[email protected]> (https://github.com/miserylee)",
"Clemens Wolff (https://github.com/c-w)"
],
"license": "MIT",
"engines": {
Expand Down

0 comments on commit e3f252d

Please sign in to comment.