Created
May 5, 2017 18:23
-
-
Save mattpodwysocki/1e728798b3c606d97dfc66b97f33deed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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