UNPKG

sussy-util

Version:
193 lines (192 loc) 7.95 kB
import Optional from './Optional'; declare class ArrayUtils { private static readonly instance; private constructor(); /** * If the item is an array, then push the flattened array into the result array, otherwise push the * item into the result array. * * @template T - The type of the elements in the array. * @param {any[]} arr - any[] - The array to flatten. */ flat<T>(arr: Array<T | T[]>): T[]; /** * It takes two arrays of the same type and returns an array of that type containing the elements * that are common to both arrays * * @template T - The type of the elements in the array. * @param {T[]} a - T[] - The first array to compare * @param {T[]} b - T[] * @returns An array of the elements that are in both a and b. */ intersection<T>(a: T[], b: T[]): T[]; /** * It takes two arrays, concatenates them, removes duplicates, and returns the result * * @template T - The type of the elements in the array. * @param {T[]} a - T[] - The first array to union * @param {T[]} b - T[] * @returns An array of unique values from both arrays. */ union<T>(a: T[], b: T[]): T[]; /** * It takes two arrays, and returns an array of all the elements in the first array that are not in * the second array * * @template T - The type of the elements in the array. * @param {T[]} a - T[] - The first array * @param {T[]} b - T[] - The array to compare against * @returns The difference between two arrays. */ difference<T>(a: T[], b: T[]): T[]; /** * This function takes an array of any type and shuffles it in place. * * @template T - The type of array to shuffle * @template X - The type of the elements in the array. * @param {T | X[]} array - T | X[] */ shuffle<X, T extends Array<X>>(array: T | X[]): void; /** * It takes an array and a value, and returns the number of times the value occurs in the array. * * @template T - The type of the elements in the array. * @param {T[]} array - T[] - The array to search in * @param {T} value - T - The value to count occurrences of. * @returns The number of occurrences of the value in the array. */ countOccurrences<T>(array: T[], value: T): number; /** * It clears an array by setting its length to zero * * @template T - The type of the elements in the array. * @param {any[]} array - any[] */ clear(array: unknown[]): void; /** * It takes an array of any type, and returns a new array of the same type * * @template T - The type of the elements in the array. * @param {T[]} array - T[] - The array to clone. * @returns A new array with the same elements as the original array. */ clone<T>(array: T[]): Array<T>; /** * It takes an array of any type, and returns a new array of the same type * * @template T - The type of the elements in the array. * @param {T[]} array - T[] - The array to clone. * @returns A new array with the same elements as the original array. */ deepClone<T>(array: T[]): Array<T>; /** * It takes an array of any type, and returns an array of the same type, with all duplicates * removed * * @template T - The type of the elements in the array. * @param {T[]} arr - T[] - The array to remove duplicates from. * @returns A new array with the duplicates removed. */ removeDuplicates<T>(arr: T[]): T[]; /** * Sort an array of objects by a key of your choice. * * @template T - The type of the elements in the array. * @param {T[]} arr - T[] - The array to sort * @param key - keyof T * @returns An array of objects. */ sortByKey<T extends object>(arr: T[], key: keyof T): T[]; /** * It takes an array and an item, and returns a new array with the item removed * * @template T - The type of the elements in the array. * @param {T[]} arr - T[] - The array to remove the item from * @param {T} item - The item to remove from the array. * @returns A new array with all the items that are not equal to the item passed in. */ remove<T>(arr: T[], item: T): T[]; /** * It takes two arrays of different types and returns an array of tuples * * @template T - The type of the elements in the first array. * @template U - The type of the elements in the second array. * @param {T[]} arr1 - T[] * @param {U[]} arr2 - U[] * @returns An array of tuples. */ zip<T, U>(arr1: T[], arr2: U[]): [T, U][]; /** * It takes an array, an index, and any number of items, and returns a new array with the items * inserted at the index. * * @template T - The type of the elements in the array. * @param {T[]} arr - T[] - The array to insert into * @param {number} index - The index where you want to insert the items. * @param {T[]} items - T[] - The items to insert into the array. * @returns a new array with the items inserted at the specified index. */ insert<T>(arr: T[], index: number, ...items: T[]): T[]; /** * It takes an array and returns a new array with all the unique sub-arrays. * @param {any[][]} arr - any[][] - The array to get the unique sub-arrays from. * @returns A new array with all the unique sub-arrays. */ uniqueSubArrays(arr: unknown[][]): unknown[][]; /** * Finds the k-th smallest element in an array using the QuickSelect algorithm. * * @param {number[]} arr - The input array. * @param {number} k - The index of the desired smallest element (1-based). * @returns {number} The k-th smallest element in the array. */ quickSelect(arr: number[], k: number): number; /** * Sorts an array in ascending order using the Quick Sort algorithm. * * @template T - The type of the elements in the array. * @param {T[]} arr - The input array to be sorted. * @param {Function} compareFn - A function to compare two elements. * @returns {T[]} The sorted array. */ quickSort<T>(arr: T[], compareFn: (a: T, b: T) => number): T[]; /** * Splits an array into smaller arrays of a specified size. * * @template T - The type of the elements in the array. * @param {T[]} array - The array to be chunked. * @param {number} chunkSize - The size of each chunk. * @returns {T[][]} - An array of smaller arrays (chunks). */ chunks<T>(array: T[], chunkSize: number): T[][]; /** * Returns the found element or undefined in an Optional * * @template T - The type of the elements in the array. * @param array the array to be searched * @param property the property to be searched in * @param value the value to be searched for * @returns Optional object of the value */ findByPropertyValue<T>(array: T[], property: keyof T, value: unknown): Optional<T>; /** * Returns the index of the first occurrence of a given value in an array. * * @template T - The type of the elements in the array. * @param array the array to search in * @param property the property to search on * @param value the value to search for * @returns the index of the value in the array, or -1 if not found */ findIndexByPropertyValue<T>(array: T[], property: keyof T, value: unknown): number; /** * @template T - The type of the elements in the array. * @param array the array to search * @param condition the condition to search for * @returns the indicies found in the array */ findAllIndices<T>(array: T[], condition: (element: T) => boolean): number[]; static getInstance(): ArrayUtils; } declare const _default: ArrayUtils; export default _default;