Skip to content

Commit 12e4c73

Browse files
committed
fix(collection): MapIterator.next() is not supported (Safari)
Fixes angular#3015 Closes angular#3389
1 parent 8822460 commit 12e4c73

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

modules/angular2/src/facade/collection.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ var _clearValues: {(m: Map<any, any>)} = (function() {
5252
};
5353
}
5454
})();
55+
// Safari doesn't implement MapIterator.next(), which is used is Traceur's polyfill of Array.from
56+
// TODO(mlaval): remove the work around once we have a working polyfill of Array.from
57+
var _arrayFromMap: {(m: Map<any, any>, getValues: boolean): List<any>} = (function() {
58+
try {
59+
if ((<any>(new Map()).values()).next) {
60+
return function createArrayFromMap(m: Map<any, any>, getValues: boolean): List<any> {
61+
return getValues ? (<any>Array).from(m.values()) : (<any>Array).from(m.keys());
62+
};
63+
}
64+
} catch (e) {
65+
}
66+
return function createArrayFromMapWithForeach(m: Map<any, any>, getValues: boolean): List<any> {
67+
var res = ListWrapper.createFixedSize(m.size), i = 0;
68+
m.forEach((v, k) => {
69+
ListWrapper.set(res, i, getValues ? v : k);
70+
i++;
71+
});
72+
return res;
73+
};
74+
})();
5575

5676
export class MapWrapper {
5777
static clone<K, V>(m: Map<K, V>): Map<K, V> { return createMapFromMap(m); }
@@ -74,8 +94,8 @@ export class MapWrapper {
7494
static delete<K>(m: Map<K, any>, k: K) { m.delete(k); }
7595
static clearValues(m: Map<any, any>) { _clearValues(m); }
7696
static iterable<T>(m: T): T { return m; }
77-
static keys<K>(m: Map<K, any>): List<K> { return (<any>Array).from(m.keys()); }
78-
static values<V>(m: Map<any, V>): List<V> { return (<any>Array).from(m.values()); }
97+
static keys<K>(m: Map<K, any>): List<K> { return _arrayFromMap(m, false); }
98+
static values<V>(m: Map<any, V>): List<V> { return _arrayFromMap(m, true); }
7999
}
80100

81101
/**

0 commit comments

Comments
 (0)