All files intersection.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 508x 8x                                         8x 2x       2x 2x     8x 8x 2x 6x 6x 4x           2x              
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
 
/**
 * Returns a list of elements that exist in both array.
 * @param array the source array
 * @param other the second array
 * @signature
 *    P.intersection(array, other)
 * @signature
 *    P.intersection(other)(array)
 * @example
 *    P.intersection([1, 2, 3], [2, 3, 5]) // => [2, 3]
 *    P.intersection([2, 3, 5])([1, 2, 3]) // => [2, 3]
 * @category Array, Pipe
 */
export function intersection<T>(source: readonly T[], other: readonly T[]): T[];
 
export function intersection<T, K>(
  other: readonly T[]
): (source: readonly K[]) => T[];
 
export function intersection() {
  return purry(_intersection, arguments, intersection.lazy);
}
 
function _intersection<T>(array: T[], other: T[]) {
  const lazy = intersection.lazy(other);
  return _reduceLazy(array, lazy);
}
 
export namespace intersection {
  export function lazy<T>(other: T[]) {
    return (value: T): LazyResult<T> => {
      const set = new Set(other);
      if (set.has(value)) {
        return {
          done: false,
          hasNext: true,
          next: value,
        };
      }
      return {
        done: false,
        hasNext: false,
      };
    };
  }
}