Files
twenty/packages/twenty-shared/src/utils/from-array-to-unique-key-record.util.ts
T
Félix MalfaitandGitHub 8b4b9ef8da Change type import rule (#13751)
Forcing "type" to be explicit, works best will rollup on the frontend to
exclude depdendencies
2025-08-08 01:27:05 +02:00

26 lines
714 B
TypeScript

import { type StringPropertyKeys } from '@/utils/trim-and-remove-duplicated-whitespaces-from-object-string-properties';
import { isDefined } from '@/utils/validation';
export const fromArrayToUniqueKeyRecord = <T extends object>({
array,
uniqueKey,
}: {
array: T[];
uniqueKey: StringPropertyKeys<T>;
}) => {
return array.reduce<Record<string, T>>((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,
};
}, {});
};