UNPKG

@nevware21/ts-utils

Version:

Common JavaScript/TypeScript helper functions for better minification

1,012 lines (989 loc) 290 kB
/** * Appends the `elms` to the `target` where the elms may be an array, a single object * or an iterator object * @group Array * @group Iterator * @example * ```ts * let theArray = arrAppend([], 1); * arrAppend(theArray, [ 2, 3, 4 ]); * arrAppend(theArray, [ "a", "b", "c" ]); * // theArray is now [ 1, 2, 3, 4, "a", "b", "c" ] * ``` * @param target - The target array * @param elms - The item, array of items an iterable or iterator object of items to add to the target * @returns The passed in target array * @example * ```ts * // Adding a single value * arrAppend([], undefined); // [] * arrAppend([], 0); // [ 0 ] * arrAppend([1], undefined); // [ 1 ] * arrAppend([1], 2); // [ 1, 2 ] * * // Adding an array * arrAppend([], [] as number[]); // [] * arrAppend([], [0]); // [ 0 ] * arrAppend([1], []); // [ 1 ] * arrAppend([1], [2]); // [ 1, 2 ] * * // Adding with an iterator * arrAppend([], ([] as number[]).values()); // [] * arrAppend([], [0].values()); // [ 0 ] * arrAppend([1], [].keys()); // [ 1 ] * arrAppend([1], [2].values()); // [ 1, 2 ] * arrAppend([1], [2].keys()); // [ 1, 0 ] - 0 is from the index from the first element * ``` */ export declare function arrAppend<T>(target: T[], elms: T | T[] | Iterator<T>): T[]; /** * Deep copy handler to identify and copy arrays. * @since 0.4.4 * @group Object - Deep Copy * @param details - The details object for the current property being copied * @returns `true` if the current value is a function otherwise `false` */ export declare function arrayDeepCopyHandler(details: IObjDeepCopyHandlerDetails): boolean; /** * The arrContains() method determines whether an array contains a certain value among its * entries, returning true or false as appropriate. * @since 0.8.0 * @group Array * @group ArrayLike * @param theArray - The array or array like object of elements to be searched. * @param searchElement - The value to search for * @param fromIndex - The optional Zero-based index at which to start searching, converted to an integer. * - Negative index counts back from the end of the array — if fromIndex \< 0, fromIndex + array.length * is used. However, the array is still searched from front to back in this case. * - If fromIndex \< -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched. * - If fromIndex \>= array.length, the array is not searched and false is returned. * @returns A boolean value which is true if the value searchElement is found within the array (or the part of * the array indicated by the index fromIndex, if specified). * @example * ```ts * arrContains([1, 2, 3], 2); // true * arrContains([1, 2, 3], 4); // false * arrContains([1, 2, 3], 3, 3); // false * arrContains([1, 2, 3], 3, -1); // true * arrContains([1, 2, NaN], NaN); // true * arrContains(["1", "2", "3"], 3 as any); // false * * // Array Like * arrContains({ length: 3, 0: 1, 1: 2, 2: 3 }, 2); // true * arrContains({ length: 3, 0: 1, 1: 2, 2: 3 }, 4); // false * arrContains({ length: 3, 0: 1, 1: 2, 2: 3 }, 3, 3); // false * arrContains({ length: 3, 0: 1, 1: 2, 2: 3 }, 3, -1); // true * arrContains({ length: 3, 0: 1, 1: 2, 2: NaN }, NaN); // true * arrContains({ length: 3, 0: "1", 1: "2", 2: "3" }, 3 as any); // false * ``` */ export declare const arrContains: <T>(theArray: ArrayLike<T>, searchElement: T, fromIndex?: number) => boolean; /** * The arrEvery() method is an iterative method. It calls a provided callbackFn function once for * each element in an array, until the callbackFn returns a falsy value. If such an element is found, * arrEvery() immediately returns false and stops iterating through the array. Otherwise, if callbackFn * returns a truthy value for all elements, every() returns true. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return `true` if the callbackFn returns a `truthy` value for every array elements. Otherwise `false`. * @example * ```ts * function isBigEnough<T>(element: T, index: number, array: T[]) { * return element >= 10; * } * * arrEvery([12, 5, 8, 130, 44], isBigEnough); // false * arrEvery([12, 54, 18, 130, 44], isBigEnough); // true * * const isSubset = <T>(array1: T[], array2: T[]) => arrEvery(array2, (element) => arrIncludes(array1, element)); * * isSubset([1, 2, 3, 4, 5, 6, 7], [5, 7, 6]); // true * isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7]); // false * * arrEvery([1, , 3], (x) => x !== undefined); // true * arrEvery([2, , 2], (x) => x === 2); // true * * // Array Like combinations * isSubset([1, 2, 3, 4, 5, 6, 7], { length: 3, 0: 5, 1: 7, 2: 6}); // true * isSubset([1, 2, 3, 4, 5, 6, 7], { length: 3, 0: 5, 1: 8, 2: 7}); // false * * isSubset({ length: 7, 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7 }, [ 5, 7, 6 ]); // true * isSubset({ length: 7, 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7 }, [5, 8, 7]); // false * * isSubset({ length: 7, 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7 }, { length: 3, 0: 5, 1: 7, 2: 6}); // true * isSubset({ length: 7, 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7 }, { length: 3, 0: 5, 1: 8, 2: 7}); // false * ``` */ export declare const arrEvery: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => theArray is T[]; /** * The arrFilter() method creates a shallow copy of a portion of a given array, filtered down to just the elements * from the given array that pass the test implemented by the provided function. * * The filter() method is an iterative method. It calls a provided callbackFn function once for each element in an * array, and constructs a new array of all the values for which callbackFn returns a truthy value. Array elements * which do not pass the callbackFn test are not included in the new array. * * `callbackFn` is invoked only for array indexes which have assigned values. It is not invoked for empty slots in * sparse arrays. * * The arrFilter() method is a copying method. It does not alter this but instead returns a shallow copy that contains * the same elements as the ones from the original array (with some filtered out). However, the function provided as * callbackFn can mutate the array. Note, however, that the length of the array is saved before the first invocation * of callbackFn. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrFilter() * began. * - Changes to already-visited indexes do not cause callbackFn to be invoked on them again. * - If an existing, yet-unvisited element of the array is changed by callbackFn, its value passed to the `callbackFn` * will be the value at the time that element gets visited. Deleted elements are not visited. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of the elements. * @typeParam E - Identifies a more specific instance of the base array type. * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return A shallow copy of a portion of the given array, filtered down to just the elements from the given * array that pass the test implemented by the provided function. If no elements pass the test, an empty array * will be returned. * @example * ```ts * function isBigEnough<T>(value: T) { * return value >= 10; * } * * const filtered = arrFilter([12, 5, 8, 130, 44], isBigEnough); * // filtered is [12, 130, 44] * * const arrayLikeFiltered = arrFilter({ length: 5, 0: 12, 1: 5, 2: 8, 3: 130, 4: 44}, isBigEnough); * // arrayLikeFiltered is [12, 130, 44] * * const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; * * function isPrime(num) { * for (let i = 2; num > i; i++) { * if (num % i === 0) { * return false; * } * } * return num > 1; * } * * console.log(arrFilter(array, isPrime)); // [2, 3, 5, 7, 11, 13] * ``` */ export declare const arrFilter: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => T[] | E[]; /** * The arrFind() method returns the first element in the provided array that satisfies * the provided testing function. If no values satisfy the testing function, undefined * is returned. * - If you need the index of the found element in the array, use {@link arrFindIndex}. * - If you need to find the index of a value, use arrIndexOf(). (It's similar to {@link arrFindIndex}, but * checks each element for equality with the value instead of using a testing function.) * - If you need to find if a value exists in an array, use {@link arrIncludes}. Again, it checks each element for * equality with the value instead of using a testing function. * - If you need to find if any element satisfies the provided testing function, use {@link arrSome}. * * The arrFind() method is an iterative method. It calls a provided `callbackFn` function once for each element * in an array in ascending-index order, until `callbackFn` returns a truthy value. arrFind() then returns that * element and stops iterating through the array. If callbackFn never returns a truthy value, arrFind() returns * undefined. * * `callbackFn` is invoked for every index of the array, not just those with assigned values. Empty slots in * sparse arrays behave the same as undefined. * * arrFind() does not mutate the array on which it is called, but the function provided as callbackFn can. * Note, however, that the length of the array is saved before the first invocation of `callbackFn`. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrFind() began. * - Changes to already-visited indexes do not cause callbackFn to be invoked on them again. * - If an existing, yet-unvisited element of the array is changed by callbackFn, its value passed to the * `callbackFn` will be the value at the time that element gets visited. Deleted elements are visited as if they * were undefined. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return The first element in the array that satisfies the provided testing function. Otherwise, undefined * is returned. * @example * ```ts * const inventory = [ * { name: "apples", quantity: 2 }, * { name: "bananas", quantity: 0 }, * { name: "cherries", quantity: 5 }, * ]; * * function isCherries(fruit) { * return fruit.name === "cherries"; * } * * console.log(arrFind(inventory, isCherries)); * // { name: 'cherries', quantity: 5 } * * function isPrime(element, index, array) { * let start = 2; * while (start <= Math.sqrt(element)) { * if (element % start++ < 1) { * return false; * } * } * return element > 1; * } * * console.log(arrFind([4, 6, 8, 12], isPrime)); // undefined, not found * console.log(arrFind([4, 5, 8, 12], isPrime)); // 5 * * // Array Like * console.log(arrFind({ length: 4, 0: 4, 1: 6, 2: 8, 3: 12 }, isPrime)); // undefined, not found * console.log(arrFind({ length: 4:, 0: 4, 1: 5, 2: 8, 3: 12 }, isPrime)); // 5 * ``` */ export declare const arrFind: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => T | E; /** * The arrFindIndex() method returns the index of the first element in an array that satisfies the provided testing * function. If no elements satisfy the testing function, -1 is returned. * * The arrFindIndex() is an iterative method. It calls a provided callbackFn function once for each element in an * array in ascending-index order, until callbackFn returns a truthy value. arrFindIndex() then returns the index * of that element and stops iterating through the array. If `callbackFn` never returns a truthy value, arrFindIndex() * returns -1. * * `callbackFn` is invoked for every index of the array, not just those with assigned values. Empty slots in sparse * arrays behave the same as undefined. * * arrFindIndex() does not mutate the array on which it is called, but the function provided as `callbackFn` can. * Note, however, that the length of the array is saved before the first invocation of callbackFn. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrFindIndex() began. * - Changes to already-visited indexes do not cause `callbackFn` to be invoked on them again. * If an existing, yet-unvisited element of the array is changed by `callbackFn`, its value passed to the `callbackFn` * will be the value at the time that element gets visited. Deleted elements are visited as if they were undefined. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return The index of the first element in the array that passes the test. Otherwise, -1. * @example * ```ts * const inventory: Array<{ name: string, quantity: number}> = [ * { name: "apples", quantity: 2 }, * { name: "bananas", quantity: 0 }, * { name: "cherries", quantity: 5 } * ]; * * function isCherries(fruit: { name: string, quantity: number}) { * return fruit.name === "cherries"; * } * * arrFindIndex(inventory, isCherries); // 2 * * function isPrime(element: number) { * if (element % 2 === 0 || element < 2) { * return false; * } * * for (let factor = 3; factor <= Math.sqrt(element); factor += 2) { * if (element % factor === 0) { * return false; * } * } * return true; * } * * arrFindIndex([4, 6, 8, 9, 12], isPrime) // -1 * arrFindIndex([4, 6, 7, 9, 12], isPrime) // 2 * * // Array Like * arrFindIndex({ length: 5, 0: 4, 1: 6, 2: 8, 3: 9, 4: 12 }, isPrime) // -1 * arrFindIndex({ length: 5:, 0: 4, 1: 6, 2: 7, 3: 9, 4: 12 }, isPrime) // 2 * ``` */ export declare const arrFindIndex: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => number; /** * The arrFindLast() method iterates the array in reverse order and returns the value of the first element that * satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned. * - If you need the index of the found element in the array, use arrFindLastIndex(). * - If you need to find the index of a value, use arrLastIndexOf(). (It's similar to arrFindLastIndex(), but checks * each element for equality with the value instead of using a testing function.) * - If you need to find if a value exists in an array, use {@link arrIncludes}. Again, it checks each element for * equality with the value instead of using a testing function. * - If you need to find if any element satisfies the provided testing function, use {@link arrSome}. * * The arrFindLast() method is an iterative method. It calls a provided callbackFn function once for each element * in an array in descending-index order, until callbackFn returns a truthy value. arrFindLast() then returns that * element and stops iterating through the array. If `callbackFn` never returns a truthy value, arrFindLast() returns * undefined. * * `callbackFn` is invoked for every index of the array, not just those with assigned values. Empty slots in sparse * arrays behave the same as undefined. * * arrFindLast() does not mutate the array on which it is called, but the function provided as `callbackFn` can. * Note, however, that the length of the array is saved before the first invocation of `callbackFn`. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrFindLast() began. * - Changes to already-visited indexes do not cause callbackFn to be invoked on them again. * - If an existing, yet-unvisited element of the array is changed by `callbackFn`, its value passed to the `callbackFn` * will be the value at the time that element gets visited. Deleted elements are visited as if they were undefined. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return The last element in the array that satisfies the provided testing function. Otherwise, undefined * is returned. * @example * ```ts * const inventory = [ * { name: "apples", quantity: 2 }, * { name: "bananas", quantity: 0 }, * { name: "cherries", quantity: 5 }, * ]; * * function isCherries(fruit) { * return fruit.name === "cherries"; * } * * console.log(arrFindLast(inventory, isCherries)); * // { name: 'cherries', quantity: 5 } * * function isPrime(element, index, array) { * let start = 2; * while (start <= Math.sqrt(element)) { * if (element % start++ < 1) { * return false; * } * } * return element > 1; * } * * console.log(arrFindLast([4, 6, 8, 12], isPrime)); // undefined, not found * console.log(arrFindLast([4, 5, 7, 12], isPrime)); // 7 * * // Array Like * console.log(arrFindLast({ length: 4, 0: 4, 1: 6, 2: 8, 3: 12 }, isPrime)); // undefined, not found * console.log(arrFindLast({ length: 4, 0: 4, 1: 5, 2: 7, 3: 12 }, isPrime)); // 7 * ``` */ export declare const arrFindLast: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => T | E; /** * The arrFindLastIndex() method iterates the array in reverse order and returns the index of the first element that * satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. * * The arrFindLastIndex() method is an iterative method. It calls a provided `callbackFn` function once for each element * in an array in descending-index order, until callbackFn returns a truthy value. arrFindLastIndex() then returns the * index of that element and stops iterating through the array. If callbackFn never returns a truthy value, returns -1. * * `callbackFn` is invoked for every index of the array, not just those with assigned values. Empty slots in sparse arrays * behave the same as undefined. * * arrFindLastIndex() does not mutate the array on which it is called, but the function provided as callbackFn can. * Note, however, that the length of the array is saved before the first invocation of callbackFn. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrFindLastIndex() began. * - Changes to already-visited indexes do not cause callbackFn to be invoked on them again. * - If an existing, yet-unvisited element of the array is changed by `callbackFn`, its value passed to the callbackFn * will be the value at the time that element gets visited. Deleted elements are visited as if they were undefined. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return The index of the last (highest-index) element in the array that passes the test. Otherwise -1 if * no matching element is found. * @example * ```ts * const inventory: Array<{ name: string, quantity: number}> = [ * { name: "apples", quantity: 2 }, * { name: "bananas", quantity: 0 }, * { name: "cherries", quantity: 5 } * ]; * * let called = 0; * function isCherries(fruit: { name: string, quantity: number}) { * called++; * return fruit.name === "cherries"; * } * * arrFindLastIndex(inventory, isCherries)' // 2 * // called === 1 * * called = 0; * function isPrime(element: number) { * called++; * if (element % 2 === 0 || element < 2) { * return false; * } * for (let factor = 3; factor <= Math.sqrt(element); factor += 2) { * if (element % factor === 0) { * return false; * } * } * return true; * } * * arrFindLastIndex([4, 6, 8, 9, 12], isPrime); // -1 * // called === 5 * * called = 0; * arrFindLastIndex([4, 6, 7, 9, 12], isPrime); // 2 * // called === 3 * * // Array Like * called = 0; * arrFindLastIndex({ length: 5: 0: 4, 1: 6, 2: 8, 3: 9, 4: 12 }, isPrime); // -1 * // called === 5 * * called = 0; * arrFindLastIndex({ length: 5: 0: 4, 1: 6, 2: 7, 3: 9, 4: 12 }, isPrime); // 2 * // called === 3 * ``` */ export declare const arrFindLastIndex: <T, E extends T>(theArray: ArrayLike<T>, callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>, thisArg?: any) => number; /** * Calls the provided `callbackFn` function once for each element in an array in ascending index order. It is not invoked for index properties * that have been deleted or are uninitialized. And unlike the ES6 forEach() you CAN stop or break the iteration by returning -1 from the * `callbackFn` function. * * The range (number of elements) processed by arrForEach() is set before the first call to the `callbackFn`. Any elements added beyond the range * or elements which as assigned to indexes already processed will not be visited by the `callbackFn`. * @group Array * @group ArrayLike * @typeParam T - Identifies the element type of the array * @param theArray - The array or array like object of elements to be searched. * @param callbackfn - A `synchronous` function that accepts up to three arguments. arrForEach calls the callbackfn function one time for each element in the array. * @param thisArg - An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, null or undefined * the array will be used as the this value. * @remarks * arrForEach expects a `synchronous` function. * arrForEach does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback. * @example * ```ts * const items = ['item1', 'item2', 'item3']; * const copyItems = []; * * // before using for loop * for (let i = 0; i < items.length; i++) { * copyItems.push(items[i]); * } * * // before using forEach() * items.forEach((item) => { * copyItems.push(item); * }); * * // after * arrForEach(items, (item) => { * copyItems.push(item); * // May return -1 to abort the iteration * }); * * // Also supports input as an array like object * const items = { length: 3, 0: 'item1', 1: 'item2', 2: 'item3' }; * ``` */ export declare function arrForEach<T = any>(theArray: ArrayLike<T>, callbackfn: (value: T, index: number, array: T[]) => void | number, thisArg?: any): void; /** * Creates an new shallow-copied array from an array-like object or an iterable. * @since 0.9.7 * @group ArrayLike * @group Array * @group Iterator * @typeParam T - Identifies the element type of the array-like or iterable. * @typeParam U - Identifies returned type of the array * @param theValue - An array-like object or iterable to convert to an array. * @param mapFn - A {@link ArrFromMapFn | mapping function} to call on every element of the array. If provided, every * value to be added to the array is first passed through this map function, and the return * value is added to the array instead. The function is called with the following arguments: * @param thisArg - Value of 'this' used to invoke the mapfn. * @example * ```ts * arrFrom("Hello"); * // [ "H", "e", "l", "l", "o" ] * * arrFrom(new Set(["Hello", "Darkness", "my", "old", "friend"])); * // ["Hello", "Darkness", "my", "old", "friend"] * * let map = new Map([ * [ 1, "Hello" ], * [ 2, "Darkness" ], * [ 3, "my" ], * [ 4, "old" ], * [ 5, "friend"] * ]); * * arrFrom(map.values()); * // ["Hello", "Darkness", "my", "old", "friend"] * * arrFrom(map.keys()); * // [ 1, 2, 3, 4, 5 ] * * arrFrom(map.entries()); * // [ [ 1, "Hello" ], [ 2, "Darkness" ], [ 3, "my" ], [ 4, "old" ], [ 5, "friend"] ] * * // With a Mapping function * const map = new Map([ * [ 1, "Hello" ], * [ 2, "Darkness" ], * [ 3, "my" ], * [ 4, "old" ], * [ 5, "friend"] * ]); * * arrFrom(map, ([ key, value ]) => ({ [key]: value })); * // [ {"1": "Hello"}, {"2": "Darkness"}, {"3": "my"}, {"4": "old"}, {"5": "friend"} ] * ``` */ export declare const arrFrom: <T, U = T>(theValue: ArrayLike<T> | Iterable<T>, mapFn?: ArrFromMapFn<T, U>, thisArg?: any) => U[]; /** * Callback signature for {@link arrFrom} mapFn that is called for every element of array. Each time mapFn * executes, the returned value is added to newArray. * * @since 0.9.7 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of the array elements * @typeParam R - Identifies the type of the elements returned by the map function, defaults to T. * @param value - The current element being processed in the array. * @param index - The index of the current element being processed in the array. */ export declare type ArrFromMapFn<T, R = T> = (value: T, index?: number) => R; /** * The arrIncludes() method determines whether an array includes a certain value among its * entries, returning true or false as appropriate. * @since 0.8.0 * @group Array * @group ArrayLike * @param theArray - The array or array like object of elements to be searched. * @param searchElement - The value to search for * @param fromIndex - The optional Zero-based index at which to start searching, converted to an integer. * - Negative index counts back from the end of the array — if fromIndex \< 0, fromIndex + array.length * is used. However, the array is still searched from front to back in this case. * - If fromIndex \< -array.length or fromIndex is omitted, 0 is used, causing the entire array to be searched. * - If fromIndex \>= array.length, the array is not searched and false is returned. * @returns A boolean value which is true if the value searchElement is found within the array (or the part of * the array indicated by the index fromIndex, if specified). * @example * ```ts * arrIncludes([1, 2, 3], 2); // true * arrIncludes([1, 2, 3], 4); // false * arrIncludes([1, 2, 3], 3, 3); // false * arrIncludes([1, 2, 3], 3, -1); // true * arrIncludes([1, 2, NaN], NaN); // true * arrIncludes(["1", "2", "3"], 3 as any); // false * * // Array Like * arrIncludes({ length: 3, 0: 1, 1: 2, 2: 3 }, 2); // true * arrIncludes({ length: 3, 0: 1, 1: 2, 2: 3 }, 4); // false * arrIncludes({ length: 3, 0: 1, 1: 2, 2: 3 }, 3, 3); // false * arrIncludes({ length: 3, 0: 1, 1: 2, 2: 3 }, 3, -1); // true * arrIncludes({ length: 3, 0: 1, 1: 2, 2: NaN }, NaN); // true * arrIncludes({ length: 3, 0: "1", 1: "2", 2: "3" }, 3 as any); // false * ``` */ export declare const arrIncludes: <T>(theArray: ArrayLike<T>, searchElement: T, fromIndex?: number) => boolean; /** * The arrIndexOf() method returns the first index at which a given element can be found in the array, * or -1 if it is not present. * `arrIndexOf()` compares searchElement to elements of the Array using strict equality (the same * method used by the === or triple-equals operator). * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @param theArray - The array or array like object of elements to be searched. * @param searchElement - The element to locate in the array. * @param fromIndex - The index to start the search at. If the index is greater than or equal to * the array's length, -1 is returned, which means the array will not be searched. If the provided * index value is a negative number, it is taken as the offset from the end of the array. * Note: if the provided index is negative, the array is still searched from front to back. If the * provided index is 0, then the whole array will be searched. Default: 0 (entire array is searched). * @return The first index of the element in the array; -1 if not found. * @example * ```ts * const array = [2, 9, 9]; * arrIndexOf(array, 2); // 0 * arrIndexOf(array, 7); // -1 * arrIndexOf(array, 9, 2); // 2 * arrIndexOf(array, 2, -1); // -1 * arrIndexOf(array, 2, -3); // 0 * * let indices: number[] = []; * const array = ['a', 'b', 'a', 'c', 'a', 'd']; * const element = 'a'; * let idx = arrIndexOf(array, element); * while (idx !== -1) { * indices.push(idx); * idx = arrIndexOf(array, element, idx + 1); * } * console.log(indices); * // [0, 2, 4] * * function updateVegetablesCollection (veggies, veggie) { * if (arrIndexOf(veggies, veggie) === -1) { * veggies.push(veggie); * console.log('New veggies collection is : ' + veggies); * } else { * console.log(veggie + ' already exists in the veggies collection.'); * } * } * * let veggies = ['potato', 'tomato', 'chillies', 'green-pepper']; * * updateVegetablesCollection(veggies, 'spinach'); * // New veggies collection is : potato,tomato,chillies,green-pepper,spinach * updateVegetablesCollection(veggies, 'spinach'); * // spinach already exists in the veggies collection. * * // Array Like * let arrayLike = { * length: 3, * 0: "potato", * 1: "tomato", * 2: "chillies", * 3: "green-pepper" // Not checked as index is > length * }; * * arrIndexOf(arrayLike, "potato"); // 0 * arrIndexOf(arrayLike, "tomato"); // 1 * arrIndexOf(arrayLike, "chillies"); 2 * arrIndexOf(arrayLike, "green-pepper"); // -1 * ``` */ export declare const arrIndexOf: <T>(theArray: ArrayLike<T>, searchElement: T, fromIndex?: number) => number; /** * The arrLastIndexOf() method returns the last index at which a given element can be found in the array, * or -1 if it is not present. * `arrLastIndexOf()` compares searchElement to elements of the Array using strict equality (the same * method used by the === or triple-equals operator). [NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) * values are never compared as equal, so arrLastIndexOf() always returns -1 when searchElement is NaN. * * The arrLastIndexOf() method skips empty slots in sparse arrays. * * The arrLastIndexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties. * * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @param theArray - The array or array like object of elements to be searched. * @param searchElement - The element to locate in the array. * @param fromIndex - Zero-based index at which to start searching backwards, converted to an integer. * - Negative index counts back from the end of the array — if fromIndex \< 0, fromIndex + array.length is used. * - If fromIndex \< -array.length, the array is not searched and -1 is returned. You can think of it conceptually * as starting at a nonexistent position before the beginning of the array and going backwards from there. There * are no array elements on the way, so searchElement is never found. * - If fromIndex \>= array.length or fromIndex is omitted, array.length - 1 is used, causing the entire array to * be searched. You can think of it conceptually as starting at a nonexistent position beyond the end of the array and going backwards from there. It eventually reaches the real end position of the array, at which point it starts searching backwards through the actual array elements. * @return The first index of the element in the array; -1 if not found. * @example * ```ts * const numbers = [2, 5, 9, 2]; * arrLastIndexOf(numbers, 2); // 3 * arrLastIndexOf(numbers, 7); // -1 * arrLastIndexOf(numbers, 2, 3); // 3 * arrLastIndexOf(numbers, 2, 2); // 0 * arrLastIndexOf(numbers, 2, -2); // 0 * arrLastIndexOf(numbers, 2, -1); // 3 * * let indices: number[] = []; * const array = ["a", "b", "a", "c", "a", "d"]; * const element = "a"; * let idx = arrLastIndexOf(array, element); * while (idx !== -1) { * indices.push(idx); * idx = arrLastIndexOf(array, element, idx ? idx - 1 : -(array.length + 1)); * } * console.log(indices); * // [4, 2, 0] * * function updateVegetablesCollection (veggies, veggie) { * if (arrLastIndexOf(veggies, veggie) === -1) { * veggies.push(veggie); * console.log('New veggies collection is : ' + veggies); * } else { * console.log(veggie + ' already exists in the veggies collection.'); * } * } * * let veggies = ['potato', 'tomato', 'chillies', 'green-pepper']; * * updateVegetablesCollection(veggies, 'spinach'); * // New veggies collection is : potato,tomato,chillies,green-pepper,spinach * updateVegetablesCollection(veggies, 'spinach'); * // spinach already exists in the veggies collection. * * // Array Like * let arrayLike = { * length: 3, * 0: "potato", * 1: "tomato", * 2: "chillies", * 3: "green-pepper" // Not checked as index is > length * }; * * arrLastIndexOf(arrayLike, "potato"); // 0 * arrLastIndexOf(arrayLike, "tomato"); // 1 * arrLastIndexOf(arrayLike, "chillies"); 2 * arrLastIndexOf(arrayLike, "green-pepper"); // -1 * ``` */ export declare const arrLastIndexOf: <T>(theArray: ArrayLike<T>, searchElement: T, fromIndex?: number) => number; /** * The arrMap() method creates a new array populated with the results of calling a provided function on every * element in the calling array. * * `arrMap` calls a provided callbackFn function once for each element in an array, in order, and constructs * a new array from the results. callbackFn is invoked only for indexes of the array which have assigned * values (including undefined). * * It is not called for missing elements of the array; that is: * - indexes that have never been set; * - indexes which have been deleted. * * @since 0.3.3 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of the array elements * @typeParam R - Identifies the type of the elements returned by the callback function, defaults to T. * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - The function that is called for evetn element of `theArray`. * @param thisArg - The value to use as the `this` when executing the `callbackFn`. * @example * ```ts * const numbers = [1, 4, 9]; * const roots = arrMap(numbers, (num) => Math.sqrt(num)); * * // roots is now [1, 2, 3] * // numbers is still [1, 4, 9] * * const kvArray = [{ key: 1, value: 10 }, * { key: 2, value: 20 }, * { key: 3, value: 30 }]; * * const reformattedArray = arrMap(kvArray, ({ key, value}) => ({ [key]: value })); * * // reformattedArray is now [{1: 10}, {2: 20}, {3: 30}], * * // kvArray is still: * // [{key: 1, value: 10}, * // {key: 2, value: 20}, * // {key: 3, value: 30}] * * // Also supports Array Like objects with same output * const kvArray = { * length: 3, * 0: { key: 1, value: 10 }, * 1: { key: 2, value: 20 }, * 2: { key: 3, value: 30 } * }; * ``` */ export declare const arrMap: <T, R = T>(theArray: ArrayLike<T>, callbackFn: ArrMapCallbackFn<T, R>, thisArg?: any) => R[]; /** * Callback signature for {@link arrMap} that is called for every element of array. Each time callbackFn * executes, the returned value is added to newArray. * * @since 0.3.3 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of the array elements * @typeParam R - Identifies the type of the elements returned by the callback function, defaults to T. * @param value - The current element being processed in the array. * @param index - The index of the current element being processed in the array. * @param array - The array that the `map` function was called on. */ export declare type ArrMapCallbackFn<T, R = T> = (value: T, index?: number, array?: T[]) => R; /** * The `ArrPredicateCallbackFn` function is used for {@link arrEvery} and {@link arrFilter}, * it should return a `truthy` value to indicate a matching element has been found. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @typeParam E - Identifies the type of the return array elements (defaults to T) * @param value - The cuirrent element of the array being processed. * @param index - The index of the current elemety of the array being processed. * @param array - The array being processed. * @returns A boolean value indicating that the value is of the type expected (the test is true) */ export declare type ArrPredicateCallbackFn<T, E extends T> = (value: T, index: number, array: T[]) => value is E; /** * The `ArrPredicateCallbackFn2` function is used for {@link arrEvery} and {@link arrFilter}, * it should return a `truthy` value to indicate a matching element has been found. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @typeParam E - Identifies the type of the return array elements (defaults to T) * @param value - The cuirrent element of the array being processed. * @param index - The index of the current elemety of the array being processed. * @param array - The array being processed. */ export declare type ArrPredicateCallbackFn2<T> = (value: T, index: number, array: T[]) => unknown; /** * The arrReduce() method executes a user-supplied "reducer" callback function on each element of the array, * in order, passing in the return value from the calculation on the preceding element. The final result of * running the reducer across all elements of the array is a single value. * * The first time that the callback is run there is no "return value of the previous calculation". If supplied, * an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial * value and iteration starts from the next element (index 1 instead of index 0). * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @param theArray - The array or array like object of elements to be searched. * @param callbackfn - A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. * @returns The value that results from running the "reducer" callback function to completion over the entire array. * @example * ```ts * const getMax = (a: number, b: number) => Math.max(a, b); * * // callback is invoked for each element in the array starting at index 0 * arrReduce([1, 100], getMax, 50); // 100 * arrReduce([ 50], getMax, 10); // 50 * * // callback is invoked once for element at index 1 * arrReduce([1, 100], getMax); // 100 * * // callback is not invoked * arrReduce([ 50], getMax); // 50 * arrReduce([ ], getMax, 1); // 1 * * arrReduce([ ], getMax); // throws TypeError * * // Also supports Array like objects * arrReduce({ length: 2, 0: 1, 1: 100 }, getMax, 50); // 100 * arrReduce({ length: 1, 0: 50 }, getMax, 10); // 50 * * // callback is invoked once for element at index 1 * arrReduce({ length: 2, 0: 1, 1: 100 }, getMax); // 100 * * // callback is not invoked * arrReduce({ length: 1, 0: 50 }, getMax); // 50 * arrReduce({ length: 0 }, getMax, 1); // 1 * ``` */ export declare const arrReduce: <T, R = T>(theArray: ArrayLike<T>, callbackfn: ArrReduceCallbackFn<T, R>, initialValue?: T | R) => R; /** * The `reducer` function called for {@link arrReduce}. * @group Array * @group ArrayLike * @typeParam T - Identifies the type of array elements * @typeParam R - Identifies the type of the return array elements (defaults to T) * @param previousValue - The value resulting from the previous call to callbackFn. On first call, initialValue if * specified, otherwise the value of array[0]. * @param currentValue - The value of the current element. On first call, the value of array[0] if an initialValue * was specified, otherwise the value of array[1]. * @param currentIndex - The index position of currentValue in the array. On first call, 0 if initialValue was * specified, otherwise 1. * @param array -The array being traversed. */ export declare type ArrReduceCallbackFn<T, R = T> = (previousValue: T | R, currentValue: T, currentIndex: number, array: T[]) => R; /** * The arrSlice() method returns a shallow copy of a portion of an array into a new array object * selected from start to end (end not included) where start and end represent the index of items * in that array. The original array will not be modified. * * The `arrSlice()` method is a copying method. It does not alter this but instead returns a shallow * copy that contains some of the same elements as the ones from the original array. * * The `arrSlice()` method preserves empty slots. If the sliced portion is sparse, the returned arra * is sparse as well. * * The `arrSlice()` method is generic. It only expects the this value to have a length property and * integer-keyed properties. * * For both start and end, a negative index can be used to indicate an offset from the end of the array. * For example, -2 refers to the second to last element of the array. * @since 0.9.3 * @group Array * @group ArrayLike * @param start - Zero-based index at which to start extraction, converted to an integer. * - Negative index counts back from the end of the array — if start \< 0, start + array.length is used. * - If start \< -array.length or start is omitted, 0 is used. * - If start \>= array.length, nothing is extracted. * @param end - Zero-based index at which to end extraction, converted to an integer. slice() extracts * up to but not including end. * - Negative index counts back from the end of the array — if end \< 0, end + array.length is used. * - If end \< -array.length, 0 is used. * - If end \>= array.length or end is omitted, array.length is used, causing all elements until the * end to be extracted. * - If end is positioned before or at start after normalization, nothing is extracted. * @example * ```ts * const lyrics = ["Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ]; * * arrSlice(lyrics); // [ "Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ] * arrSlice(lyrics, 1, 3); // [ "Darkness", "my" ] * arrSlicw(lyrics, 2); // [ "my", "old", "friend.", "I've", "come", "to", "talk" ] * arrSlice(lyrics, 2, 4); // [ "my", "old" ] * arrSlice(lyrics, 1, 5); // [ "Darkness", "my", "old", "friend." ] * arrSlice(lyrics, -2); // [ "to", "talk" ] * arrSlice(lyrics, 2, -1); // [ "my", "old", "friend.", "I've", "come", "to" ] * ``` */ export declare function arrSlice<T>(theArray: ArrayLike<T>, start?: number, end?: number): T[]; /** * The arrSome() method tests whether at least one element in the array passes the test implemented by the * provided function. It returns true if, in the array, it finds an element for which the provided function * returns true; otherwise it returns false. It doesn't modify the array. * * The arrSome() method is an iterative method. It calls a provided `callbackFn` function once for each element * in an array, until the `callbackFn` returns a truthy value. If such an element is found, arrSome() immediately * returns true and stops iterating through the array. Otherwise, if callbackFn returns a falsy value for all * elements, some() returns false. * * arrSome() acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it * returns false for any condition. * * `callbackFn` is invoked only for array indexes which have assigned values. It is not invoked for empty slots * in sparse arrays. * * arrSome() does not mutate the array on which it is called, but the function provided as callbackFn can. * Note, however, that the length of the array is saved before the first invocation of callbackFn. Therefore: * - `callbackFn` will not visit any elements added beyond the array's initial length when the call to arrSome() began. * - Changes to already-visited indexes do not cause callbackFn to be invoked on them again. * - If an existing, yet-unvisited element of the array is changed by `callbackFn`, its value passed to the * `callbackFn` will be the value at the time that element gets visited. Deleted elements are not visited. * - The arrSome() method is generic. It only expects the this value to have a length property and integer-keyed properties. * @since 0.8.0 * @group Array * @group ArrayLike * @typeParam T - Identifies the base type of array elements * @typeParam E - Identifies a more specific instance of the base array type * @param theArray - The array or array like object of elements to be searched. * @param callbackFn - A function that accepts up to three arguments of type {@link ArrPredicateCallbackFn} or * {@link ArrPredicateCallbackFn2}. The predicate function is called for each element in the thArray until * the predicate returns a value which is coercible to the Boolean value false, or until the end of the array. * @param thisArg - A value to use as this when executing callbackFn. Defaults to the array if not provided. * @return `true` if the callback function returns a truthy value for at least one element in the array. * Otherwis