* Groups items from an iterable by the result of `keyFn`, returning a new * multimap. Mirrors the shape of * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy | `Map.groupBy`, * but returns a code MultiMap so further mutation is supp
(
items: Iterable<T>,
keyFn: (item: T, index: number) => K,
)
| 773 | * ``` |
| 774 | */ |
| 775 | static groupBy<K, T>( |
| 776 | items: Iterable<T>, |
| 777 | keyFn: (item: T, index: number) => K, |
| 778 | ): MultiMap<K, T> { |
| 779 | if (typeof keyFn !== "function") { |
| 780 | throw new TypeError( |
| 781 | `Cannot call MultiMap.groupBy: "keyFn" is not a function: received ${typeof keyFn}`, |
| 782 | ); |
| 783 | } |
| 784 | const map = new MultiMap<K, T>(); |
| 785 | let index = 0; |
| 786 | for (const item of items) { |
| 787 | map.add(keyFn(item, index++), item); |
| 788 | } |
| 789 | return map; |
| 790 | } |
| 791 | } |
no test coverage detected