All files difference.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 52 53 54 55 56 57 58 59 60 61 62 63 64 658x 8x                                                                       8x 3x       2x 2x     8x 8x 3x 3x 12x 6x           6x              
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
 
/**
 * Excludes the values from `other` array.
 * @param array - the source array
 * @param other - the values to exclude
 * @signature
 *    P.difference(array, other)
 * @example
 *    P.difference([1, 2, 3, 4], [2, 5, 3]) // => [1, 4]
 * @data_first
 * @category Array
 * @pipeable
 */
export function difference<T>(array: readonly T[], other: readonly T[]): T[];
 
/**
 * Excludes the values from `other` array.
 * @param other the values to exclude
 * @signature
 *    P.difference(other)(array)
 * @example
 *    P.difference([2, 5, 3])([1, 2, 3, 4]) // => [1, 4]
 *    P.pipe(
 *      [1, 2, 3, 4, 5, 6], // only 4 iterations
 *      P.difference([2, 3]),
 *      P.take(2)
 *    ) // => [1, 4]
 * @data_last
 * @category Array
 * @pipeable
 */
export function difference<T, K>(
  other: readonly T[]
): (array: readonly K[]) => readonly T[];
 
export function difference() {
  return purry(_difference, arguments, difference.lazy);
}
 
function _difference<T>(array: T[], other: T[]) {
  const lazy = difference.lazy(other);
  return _reduceLazy(array, lazy);
}
 
export namespace difference {
  export function lazy<T>(other: readonly T[]) {
    const set = new Set(other);
    return (value: T): LazyResult<T> => {
      if (!set.has(value)) {
        return {
          done: false,
          hasNext: true,
          next: value,
        };
      }
      return {
        done: false,
        hasNext: false,
      };
    };
  }
}