import { type StringPropertyKeys } from '@/utils/trim-and-remove-duplicated-whitespaces-from-object-string-properties'; import { isDefined } from '@/utils/validation'; export const fromArrayToUniqueKeyRecord = ({ array, uniqueKey, }: { array: T[]; uniqueKey: StringPropertyKeys; }) => { return array.reduce>((acc, occurrence) => { const currentUniqueKey = occurrence[uniqueKey] as string; if (isDefined(acc[currentUniqueKey])) { throw new Error( `Should never occur, flat array contains twice the same unique key ${occurrence[uniqueKey]}`, ); } return { ...acc, [currentUniqueKey]: occurrence, }; }, {}); };