From 57b88ec2d6abec8797f38a72c3d3e28da21d0b91 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Thu, 21 May 2015 18:18:19 +0200 Subject: [PATCH] fix(collection): new Set(iterable) is not supported (IE11, Safari) Closes #2063 --- modules/angular2/src/facade/collection.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/angular2/src/facade/collection.ts b/modules/angular2/src/facade/collection.ts index 47659c5755..b76358af5e 100644 --- a/modules/angular2/src/facade/collection.ts +++ b/modules/angular2/src/facade/collection.ts @@ -233,7 +233,26 @@ export function iterateListLike(obj, fn: Function) { } } + +// Safari and Internet Explorer do not support the iterable parameter to the +// Set constructor. We work around that by manually adding the items. +var createSetFromList: {(lst: List): Set} = (function() { + var test = new Set([1, 2, 3]); + if (test.size === 3) { + return function createSetFromList(lst: List): Set { return new Set(lst); }; + } else { + return function createSetAndPopulateFromList(lst: List): Set { + var res = new Set(lst); + if (res.size !== lst.length) { + for (var i = 0; i < lst.length; i++) { + res.add(lst[i]); + } + } + return res; + }; + } +})(); export class SetWrapper { - static createFromList(lst: List): Set { return new Set(lst); } + static createFromList(lst: List): Set { return createSetFromList(lst); } static has(s: Set, key: T): boolean { return s.has(key); } }