import { isArrayLike } from 'core/utils/types'; import { PlainObject } from 'core/typings'; /** * 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(array: Array | ReadonlyArray, callback: (value: T, indexOrKey: number, source: Array) => boolean | void): Array | ReadonlyArray; export function each(array: Array | ReadonlyArray | null, callback: (value: T, indexOrKey: number, source: Array) => boolean | void): Array | ReadonlyArray | null; export function each(arrayLikeObject: ArrayLike, callback: (value: T, indexOrKey: number, source: ArrayLike) => boolean | void): ArrayLike; export function each(arrayLikeObject: ArrayLike | null, callback: (value: T, indexOrKey: number, source: ArrayLike) => boolean | void): ArrayLike | null; export function each(obj: PlainObject, callback: (value: any, indexOrKey: string, source: PlainObject) => boolean | void): PlainObject; export function each(obj: PlainObject | null, callback: (value: any, indexOrKey: string, source: PlainObject) => boolean | void): PlainObject | null; export function each(source: ArrayLike | PlainObject | null, callback: (value: T | any, indexOrKey: any, source: any) => boolean | void): Array | ReadonlyArray | ArrayLike | PlainObject | null { let i: number | string = 0; if (isArrayLike(source)) { for (; i < source.length; i++) { if (callback(source[i], i, source) === false) break; } } else if (source) { for (i in source) { if (callback(source[i], i, source) === false) break; } } 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: (arr: Array, item: T, fromIndex?: number) => number = (arr, item, fromIndex) => { return arr.indexOf(item, fromIndex); }