import { ById, CollectionById } from '@app/models/state' import _ from 'lodash' export function reduceById(collection: T[]): ById { return collection.reduce((acc, value) => { acc[value.id] = value return acc }, {} as ById) } export function mergeById(object: T, source: T): void { _.merge(object, source) } export function mapById(object: ById, ids: string[]): T[] { return ids.map(id => object[id]).filter(a => a !== undefined) } export function mapId(entities: { id: string }[]): string[] { return entities.map(e => e.id) } export function mapCollectionById( collection: T[], map: (item: T) => U, ): CollectionById { const mapped = collection.map(map) return { byId: reduceById(mapped), allIds: mapId(mapped), } }