export type ItemMapper = (item: TIn, index: number) => TOut; export function pushMany(destArray: T[], items: T[]): void { Array.prototype.push.apply(destArray, items); } export function first(array: T[]): T { if (!array.length) return undefined; return array[0]; } export function last(array: T[]): T { if (!array.length) return undefined; return array[array.length - 1]; } export function toDictionary(array: TIn[], keySelector: ItemMapper, valueSelector?: ItemMapper): Record { if (!array.length) return {}; const res: Record = {}; array.forEach((item, index) => { const key = keySelector(item, index); const value = (valueSelector ? valueSelector(item, index) : item); if (res[key]) throw new Error(`Key '${key}' already exists in the dictionary.`); res[key] = value; }); return res; }