mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 04:26:23 +08:00
24 lines
614 B
TypeScript
24 lines
614 B
TypeScript
import groupBy from 'lodash-es/groupBy';
|
|
import maxBy from 'lodash-es/maxBy';
|
|
|
|
export function getMostCommonValue<T, F extends keyof T>(
|
|
records: T[],
|
|
field: F
|
|
) {
|
|
const grouped = groupBy(records, record => record[field]);
|
|
const values = Object.values(grouped);
|
|
const record = maxBy(values, records => records.length)?.[0];
|
|
return record?.[field];
|
|
}
|
|
|
|
export function getMostCommonResolvedValue<T, F extends keyof T, U>(
|
|
records: T[],
|
|
field: F,
|
|
resolve: (value: T[F]) => U
|
|
) {
|
|
return getMostCommonValue(
|
|
records.map(record => ({ [field]: resolve(record[field]) })),
|
|
String(field)
|
|
);
|
|
}
|