@segment/analytics-core
Version:
This package represents core 'shared' functionality that is shared by analytics packages. This is not designed to be used directly, but internal to analytics-node and analytics-browser.
31 lines (24 loc) • 684 B
text/typescript
type Grouper<T> = (obj: T) => string | number
export function groupBy<T>(
collection: T[],
grouper: keyof T | Grouper<T>
): Record<string, T[]> {
const results: Record<string, T[]> = {}
collection.forEach((item) => {
let key: string | number | undefined = undefined
if (typeof grouper === 'string') {
const suggestedKey = item[grouper]
key =
typeof suggestedKey !== 'string'
? JSON.stringify(suggestedKey)
: suggestedKey
} else if (grouper instanceof Function) {
key = grouper(item)
}
if (key === undefined) {
return
}
results[key] = [...(results[key] ?? []), item]
})
return results
}