UNPKG

arr-helper-functions

Version:

Misc. functions for finding elements in arrays, sorting, and more

196 lines (188 loc) 6.4 kB
type ElementToLookFor = string | number | boolean | undefined | null; /** * Returns whether an element is found in an array or not * * @export * @param {ElementToLookFor} element Item to look for * @param {any[]} array Array to look in * @return {boolean} Whether an element is found in an array or not */ declare function isInArray(element: ElementToLookFor, array: any[]): boolean; /** * Returns whether an element is not found in an array or not * * @export * @param {ElementToLookFor} element Item to look for * @param {any[]} array Array to look in * @return {boolean} Whether an element is not found in an array or not */ declare function isNotInArray(element: ElementToLookFor, array: any[]): boolean; /** * Checks if all the items is found in the same array * * @export * @param {ElementToLookFor[]} elements Items to look for * @param {any[]} array Array to look in * @return {boolean} True if all elements are found. As soon as an item isn't found, returns false */ declare function areInArray(elements: ElementToLookFor[], array: any[]): boolean; /** * Checks if an item is found in any of the provided arrays * * @export * @param {ElementToLookFor} element Item to look for * @param {any[][]} arrays Arrays to look in * @return {boolean} Returns true if the item is found in any of the arrays */ declare function isInAnyArray(element: ElementToLookFor, arrays: any[][]): boolean; /** * Checks if items are found in any of the provided arrays * * @export * @param {ElementToLookFor[]} items Items to look for * @param {any[][]} arrays Arrays to look in * @return {boolean} Returns false as soon as an item isn't found in any array */ declare function areInAnyArray(items: ElementToLookFor[], arrays: any[][]): boolean; /** * Checks if all items are found in same array * * @export * @param {ElementToLookFor[]} items Items to look for * @param {any[][]} arrays Arrays to look in * @return {boolean} Returns true only if all items are found in the same array */ declare function areInSameArray(items: ElementToLookFor[], arrays: any[][]): boolean; type ObjectLike = { [prop: string]: any; }; type AccessorFunction = (d: any) => any; type IndexerFunction = (d: any) => string; type ArrayOfObjects = ObjectLike[]; interface FastSearcherOptions { indexGetter: string | IndexerFunction; valueGetter?: string | AccessorFunction; keepOriginal?: boolean; } declare class FastArraySearcher { originalArray: any[]; private readonly searchObject; /** * Creates an instance of FastArraySearcher. * @param {ArrayOfObjects} arrayToSearch Array to search * @param {(string | IndexerFunction)} options.indexGetter Property or item to use as index. Must resolve to a string * @param {(string | AccessorFunction)} [options.valueGetter] Property or item to use as value (will be returned if found) * @param {boolean} [options.keepOriginal=false] Whether to keeo original array reference inside * @memberof FastArraySearcher */ constructor(arrayToSearch: ArrayOfObjects, options: FastSearcherOptions); /** * Finds element in array * * @param {string} element The element to search for * @return {(any|undefined)} The found item, if found * @memberof FastArraySearcher */ find(element: string): any; } /** * Gets the distinct values of an array * * @export * @param {any[]} array Array to find unique values of * @param {(AccessorFunction | string)} [property] Property to map or accesor function to use to obtain the desired value * @return {any[]} The unique array of values */ declare function distinctValues(array: any[], property?: AccessorFunction | string): any[]; /** * Returns a new array without falsy values * false, null, and undefined will be removed from an array * * @export * @param {any[]} array Array to filter * @param {boolean} [includeZeros=false] Whether to consider 0s as falsy * @return {any[]} Filtered array */ declare function filterFalsyValues(array: any[], includeZeros?: boolean): any[]; type CompareFn<T> = (a: T, b: T) => number; /** * SortingFunction class * * @export * @class SortingFunction */ declare class SortingFunction { sortType: 'number' | 'string'; sortFunc: CompareFn<string> | CompareFn<number>; /** * Creates an instance of SortingFunction. * @param {('number' | 'string')} sortType * @param {(CompareFn<string> | CompareFn<number>)} sortFunc * @memberof SortingFunction */ constructor(sortType: 'number' | 'string', sortFunc: CompareFn<string> | CompareFn<number>); } /** * Gets the most appropriate sort depending on array content * * @export * @param {any[]} array Array to analyse * @return {SortingFunction} The function to pass the sort() method */ declare function getBestSortingFunction(array: any[]): SortingFunction; /** * ArraySorter class * * @export * @class ArraySorter */ declare class ArraySorter { private readonly arrayToSort; /** * Creates an instance of ArraySorter. * @param {any[]} array * @memberof ArraySorter */ constructor(array: any[]); /** * Sorts array * * @return {any[])} * @memberof ArraySorter */ sort(): any[]; } /** * Clones array and sorts it with most appropriate sorting function * * @export * @param {any[]} array Array to sort * @return {any[]} Cloned and sorted array */ declare function sortArray(array: any[]): any[]; /** * Gets the first and last elements of an array * * @export * @template T * @param {T[]} array The array to get elements of * @return {{[T, T]}} The first and last elements */ declare function getFirstAndLastElement<T>(array: T[]): [T, T]; /** * Checks if array is empty * * @export * @param {any[]} array Array to check length of * @return {boolean} True if array.length === 0 */ declare function isEmpty(array: any[]): boolean; /** * Checks if array is not empty * * @export * @param {any[]} array Array to check if empty * @return {boolean} True if array.length > 0 */ declare function isNotEmpty(array: any[]): boolean; export { ArraySorter, FastArraySearcher, SortingFunction, areInAnyArray, areInArray, areInSameArray, distinctValues, filterFalsyValues, getBestSortingFunction, getFirstAndLastElement, isEmpty, isInAnyArray, isInArray, isNotEmpty, isNotInArray, sortArray };