import type { Dict, Primitive } from '@/type'; export class Collection { static unique(array: T[]): T[]; static unique(array: T[], primaryKey: string): T[]; /** * unique values * @param arr * @param primaryKey */ static unique(arr: T[], primaryKey?: string) { return arr.filter((item, index, self) => { return ( self.findIndex((t) => { if (primaryKey) { return t[primaryKey] === item[primaryKey]; } return t === item; }) === index ); }); } static merge(arr1: T[], arr2: T[]): T[]; static merge(arr1: T[], arr2: T[], primaryKey: string): T[]; /** * Merges the passed iterable into the current collection. * * @returns The merged array. * @param arr1 * @param arr2 * @param primaryKey */ static merge(arr1: T[], arr2: T[], primaryKey?: string): T[] { const concat = arr1.concat(arr2); if (primaryKey) { return Collection.unique(concat, primaryKey); } return [...concat]; } }