All files uniq.ts

100% Statements 13/13
75% Branches 3/4
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5210x 10x                                             10x 27x       25x     10x 10x 27x 27x 237x 38x         199x 199x                
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
 
/**
 * Returns a new array containing only one copy of each element in the original list.
 * Elements are compared by reference using Set.
 * Note: In `pipe`, use `uniq()` form instead of `uniq`. Otherwise, the inferred type is lost.
 * @param array - List of items
 * @signature
 *    P.uniq(array)
 * @signature
 *    P.pipe(array, P.uniq())
 * @example
 *    P.uniq([1, 2, 2, 5, 1, 6, 7]) // => [1, 2, 5, 6, 7]
 *    P.pipe(
 *      [1, 2, 2, 5, 1, 6, 7], // only 4 iterations
 *      P.uniq(),
 *      P.take(3)
 *    ) // => [1, 2, 5]
 * @category Array, Pipe
 */
export function uniq<T>(array: readonly T[]): T[];
export function uniq<T>(): (array: readonly T[]) => T[];
 
export function uniq() {
  return purry(_uniq, arguments, uniq.lazy);
}
 
function _uniq<T>(array: T[]) {
  return _reduceLazy(array, uniq.lazy());
}
 
export namespace uniq {
  export function lazy() {
    const set = new Set<any>();
    return (value: any): LazyResult<any> => {
      if (set.has(value)) {
        return {
          done: false,
          hasNext: false,
        };
      }
      set.add(value);
      return {
        done: false,
        hasNext: true,
        next: value,
      };
    };
  }
}