Group name spoofing warning
This commit is contained in:
parent
51b45ab275
commit
36c15fead4
20 changed files with 1312 additions and 215 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue