{"version":3,"sources":["../../src/functions/unique/unique.ts"],"names":[],"mappings":";AAmBO,SAAS,OACd,UACA,aAA4B,mBACvB;AACL,MAAI,eAAe,mBAAmB;AAEpC,WAAO,CAAC,GAAG,IAAI,IAAI,QAAQ,CAAC;AAAA,EAC9B;AAEA,QAAM,SAAc,CAAC;AAErB,aAAW,SAAS,YAAY,CAAC,GAAG;AAClC,QAAI,CAAC,OAAO,KAAK,CAAC,UAAU,WAAW,OAAO,KAAK,CAAC,GAAG;AACrD,aAAO,KAAK,KAAK;AAAA,IACnB;AAAA,EACF;AAEA,SAAO;AACT;AAOA,IAAM,oBAAyC,OAAO","sourcesContent":["import { Maybe } from '../../types';\n\n/**\n * Returns an array containing only the unique elements of the input iterable.\n * @note The order of the elements in the input matters, the first occurrence of an element (per the `comparator`) is the one that will be kept.\n * @param iterable The input iterable to extract unique elements from.\n * @param comparator An optional function that takes two elements and returns a boolean indicating whether they are equal. Defaults to the `Object.is` function.\n * @returns An array containing only the unique elements of the input iterable.\n * @example\n * unique([1, 2, 3, 2, 1, 4, 5, 4, 3]); // [1, 2, 3, 4, 5]\n * unique(['a', 'b', 'c', 'b', 'a']); // ['a', 'b', 'c']\n * unique([\n *     { id: 1, name: 'Alice' },\n *     { id: 2, name: 'Bob' },\n *     { id: 1, name: 'Alice', age: 42 },\n *   ],\n *   (a, b) => a.id === b.id\n * ) // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]\n */\nexport function unique<T>(\n  iterable: Maybe<Iterable<T>>,\n  comparator: Comparator<T> = defaultComparator\n): T[] {\n  if (comparator === defaultComparator) {\n    // we can get a performance boost for the default case, which is probably the most common.\n    return [...new Set(iterable)];\n  }\n\n  const result: T[] = [];\n\n  for (const value of iterable ?? []) {\n    if (!result.some((other) => comparator(value, other))) {\n      result.push(value);\n    }\n  }\n\n  return result;\n}\n\n/**\n * Checks if two values are equal.\n */\ntype Comparator<T> = (a: T, b: T) => boolean;\n\nconst defaultComparator: Comparator<unknown> = Object.is;\n"]}