mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 15:02:42 +01:00
18 lines
517 B
TypeScript
18 lines
517 B
TypeScript
import { ById } from '@app/models/state'
|
|
import merge from 'lodash.merge'
|
|
|
|
export function reduceById<T extends { id: string }>(collection: T[]): ById<T> {
|
|
return collection.reduce((acc, value) => {
|
|
acc[value.id] = value
|
|
return acc
|
|
}, {} as ById<T>)
|
|
}
|
|
|
|
export function mergeById<T extends { [id: string]: unknown }>(object: T, source: T): void {
|
|
merge(object, source)
|
|
}
|
|
|
|
export function mapById<T>(object: ById<T>, ids: string[]): T[] {
|
|
return ids.map(id => object[id]).filter(a => a !== undefined)
|
|
}
|