Group name spoofing warning

This commit is contained in:
Evan Hahn 2021-06-01 18:30:25 -05:00 committed by GitHub
parent 51b45ab275
commit 36c15fead4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1312 additions and 215 deletions

View file

@ -4,6 +4,8 @@
/* eslint-disable max-classes-per-file */
/* eslint-disable no-restricted-syntax */
import { getOwn } from './getOwn';
export function isIterable(value: unknown): value is Iterable<unknown> {
return (
(typeof value === 'object' && value !== null && Symbol.iterator in value) ||
@ -88,6 +90,23 @@ class FilterIterator<T> implements Iterator<T> {
}
}
export function groupBy<T>(
iterable: Iterable<T>,
fn: (value: T) => string
): Record<string, Array<T>> {
const result: Record<string, Array<T>> = Object.create(null);
for (const value of iterable) {
const key = fn(value);
const existingGroup = getOwn(result, key);
if (existingGroup) {
existingGroup.push(value);
} else {
result[key] = [value];
}
}
return result;
}
export function map<T, ResultT>(
iterable: Iterable<T>,
fn: (value: T) => ResultT