Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(facade): MapWrapper.createFromPairs #1644

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion modules/angular2/src/facade/collection.ts
Expand Up @@ -10,6 +10,27 @@ export var Map = global.Map;
export var Set = global.Set;
export var StringMap = global.Object;

// Safari and Internet Explorer do not support the iterable parameter to the
// Map constructor. We work around that by manually adding the items.
var createMapFromPairs: {(pairs: List<any>): Map<any, any>} = (function() {
try {
if (new Map([1, 2]).size === 2) {
return function createMapFromPairs(pairs: List<any>): Map<any, any> {
return new Map(pairs);
};
}
} catch (e) {
}
return function createMapAndPopulateFromPairs(pairs: List<any>): Map<any, any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be moved inside the catch ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it outside the catch. For style check reasons, the empty catch is required to occupy two lines :)

While the browser I tested throw an exception when passed the parameter, it's possible that some polyfill or a different browser just ignores the parameter. That's why I have the size check. Failing the size check also requires using the slower approach just like for the catch clause. In the interest of DRY, I have a single return as a catch all right at the end to accomplish this.

var map = new Map();
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
map.set(pair[0], pair[1]);
}
return map;
}
})();

export class MapWrapper {
static create(): Map<any, any> { return new Map(); }
static clone<K, V>(m: Map<K, V>): Map<K, V> { return new Map(m); }
Expand All @@ -20,7 +41,7 @@ export class MapWrapper {
}
return result;
}
static createFromPairs(pairs: List<any>): Map<any, any> { return new Map(pairs); }
static createFromPairs(pairs: List<any>): Map<any, any> { return createMapFromPairs(pairs); }
static get(m, k) { return m.get(k); }
static set(m, k, v) { m.set(k, v); }
static contains(m, k) { return m.has(k); }
Expand Down