Skip to content

Commit e3f252d

Browse files
authored
Merge pull request #77 from c-w/master
Improve support for Internet Explorer
2 parents e7631a4 + 4330011 commit e3f252d

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

clone.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
var clone = (function() {
22
'use strict';
33

4+
function _instanceof(obj, type) {
5+
return type != null && obj instanceof type;
6+
}
7+
48
var nativeMap;
59
try {
610
nativeMap = Map;
@@ -80,11 +84,11 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
8084
return parent;
8185
}
8286

83-
if (parent instanceof nativeMap) {
87+
if (_instanceof(parent, nativeMap)) {
8488
child = new nativeMap();
85-
} else if (parent instanceof nativeSet) {
89+
} else if (_instanceof(parent, nativeSet)) {
8690
child = new nativeSet();
87-
} else if (parent instanceof nativePromise) {
91+
} else if (_instanceof(parent, nativePromise)) {
8892
child = new nativePromise(function (resolve, reject) {
8993
parent.then(function(value) {
9094
resolve(_clone(value, depth - 1));
@@ -103,7 +107,7 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
103107
child = new Buffer(parent.length);
104108
parent.copy(child);
105109
return child;
106-
} else if (parent instanceof Error) {
110+
} else if (_instanceof(parent, Error)) {
107111
child = Object.create(parent);
108112
} else {
109113
if (typeof prototype == 'undefined') {
@@ -126,28 +130,18 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
126130
allChildren.push(child);
127131
}
128132

129-
if (parent instanceof nativeMap) {
130-
var keyIterator = parent.keys();
131-
while(true) {
132-
var next = keyIterator.next();
133-
if (next.done) {
134-
break;
135-
}
136-
var keyChild = _clone(next.value, depth - 1);
137-
var valueChild = _clone(parent.get(next.value), depth - 1);
133+
if (_instanceof(parent, nativeMap)) {
134+
parent.forEach(function(value, key) {
135+
var keyChild = _clone(key, depth - 1);
136+
var valueChild = _clone(value, depth - 1);
138137
child.set(keyChild, valueChild);
139-
}
138+
});
140139
}
141-
if (parent instanceof nativeSet) {
142-
var iterator = parent.keys();
143-
while(true) {
144-
var next = iterator.next();
145-
if (next.done) {
146-
break;
147-
}
148-
var entryChild = _clone(next.value, depth - 1);
140+
if (_instanceof(parent, nativeSet)) {
141+
parent.forEach(function(value) {
142+
var entryChild = _clone(value, depth - 1);
149143
child.add(entryChild);
150-
}
144+
});
151145
}
152146

153147
for (var i in parent) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"fscherwi (https://fscherwi.github.io)",
3939
"rictic (https://github.com/rictic)",
4040
"Martin Jurča (https://github.com/jurca)",
41-
"Misery Lee <[email protected]> (https://github.com/miserylee)"
41+
"Misery Lee <[email protected]> (https://github.com/miserylee)",
42+
"Clemens Wolff (https://github.com/c-w)"
4243
],
4344
"license": "MIT",
4445
"engines": {

0 commit comments

Comments
 (0)