Created
May 19, 2021 18:27
-
-
Save jdanyow/255dd11b6d0efcec6e9885f8b5333d21 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
/** | |
* Convert an iterable to an object. | |
*/ | |
export function toObject< | |
TKey extends string | number | symbol, | |
TValue | |
>(iterable: { | |
[Symbol.iterator](): IterableIterator<[TKey, TValue]>; | |
}): Record<TKey, TValue> { | |
const obj = Object.create(null) as Record<TKey, TValue>; | |
for (const [key, value] of iterable) { | |
obj[key] = value; | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment