Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created May 5, 2017 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/1e728798b3c606d97dfc66b97f33deed to your computer and use it in GitHub Desktop.
Save mattpodwysocki/1e728798b3c606d97dfc66b97f33deed to your computer and use it in GitHub Desktop.
'use strict';
export function toMap<TSource, TKey>(
source: Iterable<TSource>,
keySelector: (item: TSource) => TKey): Map<TKey, TSource>;
export function toMap<TSource, TKey, TElement = TSource>(
source: Iterable<TSource>,
keySelector: (item: TSource) => TKey,
elementSelector?: (item: TSource) => TElement): Map<TKey, TElement> {
let map = new Map<TKey, TElement | TSource>();
for (let item of source) {
let value = elementSelector ? elementSelector(item) : item;
map.set(keySelector(item), value);
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment