UNPKG

sussy-util

Version:
98 lines (97 loc) 4.15 kB
import Optional from './Optional'; export default class ImprovedArray<T> extends Array<T> { constructor(..._elements: T[]); /** * It returns a random number between 0 and the length of the array. * @returns The random index of the array. */ getRandomIndex(): number; /** * It returns a random element from the array * @returns The random element from the array. */ getRandomElement(): T; /** * It removes the element at the specified index and returns it. * @param {number} index - number - The index of the item to remove. * @returns The splice method returns an array containing the deleted elements. If only one element * is removed, an array of one element is returned. */ remove(index: number): Optional<T>; /** * It takes an index and an array of items, and inserts the items into the array at the given * index. * @param {number} index - The index to insert the items at. * @param {T[]} items - T[] */ insertAt(index: number, ...items: T[]): boolean; /** * The clear() function removes all elements from an array */ clear(): void; /** * It creates a new instance of the ImprovedArray class, and passes the current array as the * argument * @returns A new instance of ImprovedArray with the same elements as the original. */ clone(): ImprovedArray<T>; /** * If the length of the array is equal to zero, return true, otherwise return false. * @returns The length of the array. */ isEmpty(): boolean; /** * If the predicate returns true for any of the elements in the array, return false, otherwise * return true. * @param predicate - (value: T) => boolean * @returns boolean indicting if none of the elements in the array fit the predicate. */ none(predicate: (value: T) => boolean): boolean; /** * It returns a new array with all the elements that do not satisfy the predicate. * @param predicate - (val: T, ind: number, arr: T[]) => boolean * @param thisArg — An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. * @returns A new instance of ImprovedArray with the filtered values. */ rejected(predicate: (val: T, ind: number, arr: T[]) => boolean, thisArg?: unknown): ImprovedArray<T>; /** * removes all duplicates from the array */ removeDuplicates(): void; /** * The reduce function takes a function as an argument, and that function takes two arguments, the * first being the accumulator, and the second being the current value. The accumulator is * initialized to 0, and the function returns the accumulator plus 1 if the current value is equal * to the value we're looking for, or the accumulator plus 0 if it's not. * @param {T} value - T - The value to count occurrences of. * @returns The number of times the value is found in the array. */ countOccurrences(value: T): number; /** * It takes an array of arrays and flattens it into a single array. */ flatten(): void; /** * This function takes an object and returns a string that represents the object in JSON format. * @returns The JSON string representation of the object. */ toJSONString(): string; /** * For each element in the array, swap it with a random element in the array. */ shuffle(): void; each(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: this): void; /** * It takes an array of any type and returns an array of the same type. * @param {X | any[]} arr - X | any[] * @returns An array of common elements. */ findCommonElements<X extends Array<unknown>>(arr: X | unknown[]): ImprovedArray<T>; /** * Returns a copy of the array. * Each element will be copied into each and every property. * * Does NOT make a new instance a class, just takes the properties of the old object and recreates them. */ deepClone(): ImprovedArray<T>; }