All files deepUniq.ts

94.44% Statements 17/18
50% Branches 2/4
100% Functions 2/2
100% Lines 17/17

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 431x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x   1x                                       1x 1x    
import { purry } from './purry';
import { uniq } from './uniq';
import { deepEqual } from './deepEqual';
 
function _deepUniq<T>(array: T[], mode: 'soft' | 'hard') {
  const uniqArr = uniq(array);
  let uniqeItems: T[] = [];
  let compareList = [...uniqArr];
  while (compareList.length !== 0) {
    const compareValue = compareList.shift();
    Iif (!compareValue) break;
    for (const iIndex in compareList) {
      Eif (deepEqual(compareValue, compareList[iIndex], mode)) {
        compareList.splice(parseInt(iIndex), 1);
        continue;
      }
    }
    uniqeItems.push(compareValue);
  }
  return uniqeItems;
}
 
/**
 * Function will filter non unique value recursivly.
 * @description
 * The function has two modes `soft` and `hard` soft mode ignores array order hard mode preserves array order.
 * @param array - source array
 * @param mode - array comparison mode
 * @example
 *    deepUniq([{ a: [1, 2] }, { a: [1, 2] }] }, { a: [2, 1] }] }], 'soft') // [{ a: [1, 2] }]
 * @example
 *    deepUniq([{ a: [1, 2] }, { a: [1, 2] }] }, { a: [2, 1] }] }], 'hard') // [{ a: [1, 2] }, { a: [2, 1] }]
 * @pipeable
 * @category Array
 */
export function deepUniq<T>(array: readonly T[], mode?: 'soft' | 'hard'): T[];
export function deepUniq<T>(
  mode?: 'soft' | 'hard'
): (array: readonly T[]) => T[];
export function deepUniq() {
  return purry(_deepUniq, arguments);
}