All files / src/support/utils array.ts

100% Statements 29/29
100% Branches 19/19
100% Functions 9/9
100% Lines 26/26

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                                                3568x 3248x 7163x 268x     320x 2148x   3568x                 22x 1373x             22x 3423x     3423x             22x 700x 699x   1x   1x 3x     1x             22x 3x             22x 812x 410x 1x   409x      
import { isArrayLike, isString } from 'support/utils/types';
import { PlainObject } from 'typings';
 
type RunEachItem = ((...args: any) => any | any[]) | null | undefined;
 
/**
 * Iterates through a array or object
 * @param arrayLikeOrObject The array or object through which shall be iterated.
 * @param callback The function which is responsible for the iteration.
 * If the function returns true its treated like a "continue" statement.
 * If the function returns false its treated like a "break" statement.
 */
export function each<T>(
  array: Array<T> | ReadonlyArray<T>,
  callback: (value: T, indexOrKey: number, source: Array<T>) => boolean | unknown
): Array<T> | ReadonlyArray<T>;
export function each<T>(
  array: Array<T> | ReadonlyArray<T> | null | undefined,
  callback: (value: T, indexOrKey: number, source: Array<T>) => boolean | unknown
): Array<T> | ReadonlyArray<T> | null | undefined;
export function each<T>(
  arrayLikeObject: ArrayLike<T>,
  callback: (value: T, indexOrKey: number, source: ArrayLike<T>) => boolean | unknown
): ArrayLike<T>;
export function each<T>(
  arrayLikeObject: ArrayLike<T> | null | undefined,
  callback: (value: T, indexOrKey: number, source: ArrayLike<T>) => boolean | unknown
): ArrayLike<T> | null | undefined;
export function each(
  obj: PlainObject,
  callback: (value: any, indexOrKey: string, source: PlainObject) => boolean | unknown
): PlainObject;
export function each(
  obj: PlainObject | null | undefined,
  callback: (value: any, indexOrKey: string, source: PlainObject) => boolean | unknown
): PlainObject | null | undefined;
export function each<T>(
  source: ArrayLike<T> | PlainObject | null | undefined,
  callback: (value: T, indexOrKey: any, source: any) => boolean | unknown
): Array<T> | ReadonlyArray<T> | ArrayLike<T> | PlainObject | null | undefined {
  if (isArrayLike(source)) {
    for (let i = 0; i < source.length; i++) {
      if (callback(source[i], i, source) === false) {
        break;
      }
    }
  } else if (source) {
    each(Object.keys(source), (key) => callback(source[key], key, source));
  }
  return source;
}
 
/**
 * Returns the index of the given inside the given array or -1 if the given item isn't part of the given array.
 * @param arr The array.
 * @param item The item.
 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
 */
export const indexOf = <T = any>(arr: T[], item: T, fromIndex?: number): number =>
  arr.indexOf(item, fromIndex);
 
/**
 * Pushesh all given items into the given array and returns it.
 * @param array The array the items shall be pushed into.
 * @param items The items which shall be pushed into the array.
 */
export const push = <T>(array: T[], items: T | ArrayLike<T>, arrayIsSingleItem?: boolean): T[] => {
  !arrayIsSingleItem && !isString(items) && isArrayLike(items)
    ? Array.prototype.push.apply(array, items as T[])
    : array.push(items as T);
  return array;
};
 
/**
 * Creates a shallow-copied Array instance from an array-like or iterable object.
 * @param arr The object from which the array instance shall be created.
 */
export const from = <T = any>(arr: ArrayLike<T> | Set<T>) => {
  if (Array.from) {
    return Array.from(arr);
  }
  const result: T[] = [];
 
  each(arr, (elm) => {
    push(result, elm);
  });
 
  return result;
};
 
/**
 * Check whether the passed array is empty.
 * @param array The array which shall be checked.
 */
export const isEmptyArray = (array: any[] | null | undefined): boolean =>
  !!array && array.length === 0;
 
/**
 * Calls all functions in the passed array/set of functions.
 * @param arr The array filled with function which shall be called.
 * @param p1 The first param.
 */
export const runEach = (arr: ArrayLike<RunEachItem> | Set<RunEachItem>, p1?: unknown): void => {
  const runFn = (fn: RunEachItem) => fn && fn(p1);
  if (arr instanceof Set) {
    arr.forEach(runFn);
  } else {
    each(arr, runFn);
  }
};