/** * ARRAY FUNCTIONS ============================================================= * ============================================================================= */ /** * Add Value to Array if not found, with optional mutation in place. * * @param {any[]} array - list of values to add to * @param {any|any[]} valueToAdd - if given array, will add all elements inside it to `array` * @param {boolean} mutate - whether to mutate the original array * @return {any[]} - new/mutated array with value(s) added, without duplicates */ export function addToList(array: any[], valueToAdd: any | any[], mutate?: boolean): any[]; /** * Find the index of target element in the sorted array with binary search algorithm * @param {*[]} array - haystack to search (elements must be comparable with `<` and `>` operators) * @param {*} target - needle value to find index of in the array * @param {number} [left] - index of array to search from (inclusive) * @param {number} [right] - index of array to search up to (inclusive) * @param {number} [count] - iteration count * @returns {number} index - of target element in the array, or -1 if not found */ export function binarySearch(array: any[], target: any, left?: number | undefined, right?: number | undefined, count?: number | undefined): number; /** * Flatten Array recursively * @example: * flatList([1, ['2', '3'], 4]) * >>> [1, '2', '3', 4] * @param {*[]} array - with nested array elements to flatten * @returns {*[]} list - new flat array with all values */ export function flatList(array: any[]): any[]; /** * Check if the data passed is an array and has values. * * @param {*} data - The variable to check * @return {boolean} */ export function hasListValue(data: any): boolean; /** * Check if given lists have at least one intersecting value in each list * * @param {Array[]} lists - array of arrays to test * @return {boolean} - true if a common value is found among given lists */ export function hasCommonListValue(...lists: any[][]): boolean; /** * Check if given list contains primitive duplicate values * @Note: array.indexOf(NaN) always returns -1, even if the array has `NaN` value * @param {*[]} array - to check for duplicates * @returns {Boolean} true - if duplicate found */ export function hasDuplicateInList(array: any[]): boolean; /** * Get All Indexes of Value in Array * @param {Array<*>} list - to search for index list * @param {*} value - to find indexes for * @returns {Number[]} index - list of value indexes, or empty array if nothing found */ export function indexesOf(list: Array, value: any): number[]; /** * Check if the data passed is an array or plain object. * * @param {*} data - The variable to check * @return {boolean} */ export function isCollection(data: any): boolean; /** * Check if Given Arrays are Equal in Values by Element References * * @param {Array|*} a * @param {Array|*} b * @returns {Boolean} true - if all elements of `a` are equal to all elements of `b` using exact equality match */ export function isEqualList(a: any[] | any, b: any[] | any): boolean; /** * Check if the data passed is an array. * * @param {*} data - The variable to check * @return {boolean} */ export function isList(data: any): boolean; /** * Check if the value passed exists within the array passed. * * @param {Array} array - haystack * @param {*} value - needle * @return {boolean} */ export function isInList(array: any[], value: any): boolean; /** * Check if any of the values passed in via ...args exists within the array passed. * * @param {Array} array - the array to search for the values * @param {*} args - the values to search for */ export function isInListAny(array: any[], ...args: any): boolean; /** * Check if a given Element exists in the Collection, with shallow include match * * @uses lodash * @see {@link https://lodash.com/docs/4.17.2#some} for further information. * @return {Boolean} true - if element found in given collection with shallow include match */ export function isInCollection(collection: any, element: any): boolean; /** * Check if any given Element exists in the Collection, with shallow include match * * @example: * isInCollectionAny([{name: 'god', age: 'eternal'}], {name: 'dog'}, {name: 'god'}) * >>> true * * @param {Array|Object} collection - to search from * @param {*} elements - to search for * @return {boolean} true - if any of the given elements found in the collection */ export function isInCollectionAny(collection: any[] | Object, ...elements: any): boolean; /** * Compare two Arrays and determine if they have the same values * @param array1 * @param array2 */ export function isSameList(array1: any, array2: any): any; /** * Creates an array with all falsey values removed * (values false, null, 0, "", undefined, and NaN are falsey) * * @param {Array} array - the array to clean from falsey values * @return {Array} - returns the new array of filtered values */ export function cleanList(array: any[]): any[]; /** * Converts Any Value to Array (or keep it as is if already Array) * * @param {*} value - the value to convert * @param {*} [clean] - if truthy, remove falsey values: false, null, 0, "", undefined, and NaN * @return {Array} */ export function toList(value: any, clean?: any): any[]; /** * Compute the Average Number from Array Elements * * @param {Array} array - list of numbers to calculate average of * @returns {number} - the average value */ export function toListAvg(array: any[]): number; /** * Compute the Total Number from Array Elements * @Note: ~2.5 times faster than array.reduce() in Node.js * @example: * toListTotal([1, 2]) * >>> 3 * * @param {Array} array - list of numbers to calculate total of * @returns {number} total - value of all elements */ export function toListTotal(array?: any[]): number; /** * Compute the Total Number from Array Element Values * @Note: ~2.5 times faster than array.reduce() in Node.js * @example: * toListValuesTotal([{count: 1}, {count: 2}], 'count') * >>> 3 * * @param {Array} array - list of objects to calculate total values for * @param {String} [key] - object key to extract values from * @param {Number} [fallback] - default value to use when not a number encountered * @returns {Number} total - of all element values */ export function toListValuesTotal(array?: any[], key?: string | undefined, fallback?: number | undefined): number; /** * Ensures the array has unique values, including nested objects. * * @uses lodash * @see https://lodash.com/docs/#uniqWith * @example before [1, 2, 2, {a: 5, b: {c: 6}}, {a: 5, b: {c: 6}}]; after [1, 2, {a: 5, b: {c: 6}}]; * @param {Array} array - the array to enforce unique values for * @return {Array} */ export function toUniqueList(array: any[]): any[]; /** * Ensure array has only unique values using exact match `===` * * @param {Array} array - the array to enforce unique primitive values for * @return {Array} - new array */ export function toUniqueListFast(array: any[]): any[]; /** * A predicate to filter Array and keep only unique values using exact match `===` * @example: * array.filter(filterUnique) * * @param {?} value * @param {number} index * @param {?[]} self * @returns {boolean} */ export function filterUnique(value: unknown, index: number, self: [] | null): boolean; /** * Ensure array has only unique values case-insensitive keeping the first occurrence of duplicates. * * @param {Array<*>} array - the array to enforce unique values for (can be a mix of value types) * @return {Array<*>} list - new array containing only unique values, case-insensitive */ export function toUniqueListCaseInsensitive(array: Array): Array; /** * Merge two arrays into one, with objects containing only unique key, keeping the value from the first array * @Note: this method is faster than `array.filter((val, _, self) => self.find(i => i[key] === val[key]) === val)` * by about x5 times * * @param {Array} newList - list of new objects to keep * @param {Array} oldList - list of old objects to be updated * @param {string} key - objects's key that needs to be unique * @returns {Array} list - of unique objects */ export function toUniqueListByKey(newList: any[], oldList: any[], key: string): any[]; /** * Add value to the beginning of Array, optionally trimming its length to provided limit * * @param {Array} array - to prepend to * @param {*} value - to prepend to `array` * @param {number} [limit] - optionally trim array to this length */ export function prependToList(array: any[], value: any, limit?: number | undefined): any[]; /** * Merge Arrays into a single List with unique values keeping orders given (using SameValueZero comparison) * * @param {Array} arrays - lists to combine (if not array given, it will be ignored without error) * @returns {Array} - merged list with unique values */ export function mergeLists(...arrays: any[]): any[]; /** * Remove Value from Array if found, with optional mutation in place. * * @param {any[]} array - list of values to search from * @param {any|any[]} valueToRemove - if given array, will remove all elements inside it from `array` * @param {boolean} mutate - whether to mutate the original array * @return {any[]} - new/mutated array with matching value(s) removed */ export function removeFromList(array: any[], valueToRemove: any | any[], mutate?: boolean): any[]; /** * Get the first element of an array, or return the value if it's not array * * @param {Array|*} array - The array to query * @returns {*} - The first element of the array */ export function firstListValue(array: any[] | any): any; /** * Gets the first value of array * * @param {Array} array - The array to query * @return {*} - The last element of the given array */ export function first(array: any[]): any; /** * Gets the last value of array * * @param {Array} array - The array to query * @return {*} - The last element of the given array */ export function last(array: any[]): any; /** * Get a random value from provided list */ export function randomFromList(array: any): any; /** * Get a random value from provided list, removing it from the list */ export function randomFromListExtract(array: any): any; export function listAlphabetically(array: any): any; /** * Array.reduce callback to convert list of objects with .id attributes to a key-value hashmap object * @example: * [{id: "unique", name: "test"}].reduce(listToMap, {}) * >>> {"unique": {id: "unique", name: "test"}} * * // Or * ['id1', null].reduce(listToMap, {}) * >>> {"id1": "id1", "null": null} * * @returns {Object} object with optional element.id being keys, and elements of original array being values */ export function listToMap(obj: any, data: any): Object; /** * Sort List in Ascending Order * (Fastest) * * @example: * array.sort(sortAscending) * * @param {*} a - first value in the iteration * @param {*} b - second value in the iteration * @return {number} - whether values should be re-arranged */ export function sortAscending(a: any, b: any): number; /** * Sort List in Descending Order * (Fastest) * * @example: * array.sort(sortDescending) * * @param {*} a - first value in the iteration * @param {*} b - second value in the iteration * @return {number} - whether values should be re-arranged */ export function sortDescending(a: any, b: any): number; /** * Sort List by Object Property * (Fastest) * * @example: * array.sort(sort('name', 'asc')) * * @param {String} [key] - property value used to compare for sorting * @param {String} [order] - enum ['asc', 'desc'] * @return {Function} - to be used as argument for native Array.sort() */ export function sort(key?: string | undefined, order?: string | undefined): Function; /** * Sort List By Collection Properties or custom sort Function (with optional chaining) * (Moderately Fast) * * @example: * // sort objects by descending 'name' length property, then by descending 'name', then by given function, * // useful in situations when 'name' is a string containing numbers * array.sort(by('-name.length', '-name', (a, b) => a.localCompare(b))) * * @param {String|Function} args - compare function, object Key, or Path (prepend string with '-' for descending) */ export function by(...args: string | Function): (a: any, b: any) => number; /** * Randomize List Value Orders by mutation * * @param {Array} list - to shuffle values for * @return {Array} list - mutated with shuffled values */ export function shuffle(list: any[]): any[]; /** * Flatten an Array a single level deep * * @param {Array} array - The array to flatten * @return {Array} - The new flattened array */ export const toFlatList: any; export { min, max, difference, intersection, unionWith };