From 12e4c738c9c5461b7586bd375e5f9e659d3ee7d0 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Thu, 30 Jul 2015 15:20:08 +0200 Subject: [PATCH] fix(collection): MapIterator.next() is not supported (Safari) Fixes #3015 Closes #3389 --- modules/angular2/src/facade/collection.ts | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/modules/angular2/src/facade/collection.ts b/modules/angular2/src/facade/collection.ts index 448bab07b8..feea9a14a1 100644 --- a/modules/angular2/src/facade/collection.ts +++ b/modules/angular2/src/facade/collection.ts @@ -52,6 +52,26 @@ var _clearValues: {(m: Map)} = (function() { }; } })(); +// Safari doesn't implement MapIterator.next(), which is used is Traceur's polyfill of Array.from +// TODO(mlaval): remove the work around once we have a working polyfill of Array.from +var _arrayFromMap: {(m: Map, getValues: boolean): List} = (function() { + try { + if (((new Map()).values()).next) { + return function createArrayFromMap(m: Map, getValues: boolean): List { + return getValues ? (Array).from(m.values()) : (Array).from(m.keys()); + }; + } + } catch (e) { + } + return function createArrayFromMapWithForeach(m: Map, getValues: boolean): List { + var res = ListWrapper.createFixedSize(m.size), i = 0; + m.forEach((v, k) => { + ListWrapper.set(res, i, getValues ? v : k); + i++; + }); + return res; + }; +})(); export class MapWrapper { static clone(m: Map): Map { return createMapFromMap(m); } @@ -74,8 +94,8 @@ export class MapWrapper { static delete(m: Map, k: K) { m.delete(k); } static clearValues(m: Map) { _clearValues(m); } static iterable(m: T): T { return m; } - static keys(m: Map): List { return (Array).from(m.keys()); } - static values(m: Map): List { return (Array).from(m.values()); } + static keys(m: Map): List { return _arrayFromMap(m, false); } + static values(m: Map): List { return _arrayFromMap(m, true); } } /**