import { CastArray, IndexOf, IndexOfLongestString, Slice, SortLongestStrings, Splice, TupleOf, TupleOfAtLeast, TupleOfUpTo, TupleOfUpToButNotIncluding } from '../types/Array'; import { Enumerate } from '../types/Number'; import { FindResult } from '../types/_internal'; import { Flatten } from 'ts-toolbelt/out/List/Flatten'; import { MaybePromise, NonNil } from 'tsdef'; /** * checks whether the given array's length is larger than **or equal to** the given number, and narrows the type of the * array to that length. useful when using the `noUncheckedIndexedAccess` compiler option * @example * declare const foo: string[] * const bar: string = foo[0] //Type 'string | undefined' is not assignable to type 'string' * if (lengthGreaterOrEqual(foo, 3)) { * const a: string = foo[0] //no error * const b: string = foo[2] //no error * const c: string = foo[3] //Type 'string | undefined' is not assignable to type 'string' * } */ export declare const lengthGreaterOrEqual: (arr: readonly T[], length: L) => arr is TupleOfAtLeast; /** * checks whether the given array's length is larger than the given number, and narrows the type of the array to that * length. useful when using the `noUncheckedIndexedAccess` compiler option * @example * declare const foo: string[] * const bar: string = foo[0] //Type 'string | undefined' is not assignable to type 'string' * if (lengthGreaterThan(foo, 3)) { * const a: string = foo[0] //no error * const b: string = foo[3] //no error * const c: string = foo[4] //Type 'string | undefined' is not assignable to type 'string' * } */ export declare const lengthGreaterThan: (arr: readonly T[], length: L) => arr is [...TupleOf, T] & T[]; /** * checks whether the given array's length is less than **or equal to** the given number, and narrows the type of the * array to that length. useful when using the `noUncheckedIndexedAccess` compiler option * @example * declare const foo: string[] * const bar: string = foo[0] //Type 'string | undefined' is not assignable to type 'string' * if (lengthLessOrEqual(foo, 3)) { * const a: string = foo[0] //string | undefined' * const b: string = foo[2] //string | undefined' * const c: string = foo[3] //error: tuple of length '3' has no element at index '3' * } */ export declare const lengthLessOrEqual: (arr: readonly T[], length: L) => arr is TupleOfUpTo; export declare const lengthLessThan: (arr: readonly T[], length: L) => arr is TupleOfUpToButNotIncluding; /** * checks whether the given array's length is equal to the given number, and narrows the type of the array to that * length. useful when using the `noUncheckedIndexedAccess` compiler option * @example * declare const foo: string[] * const bar: string = foo[0] //Type 'string | undefined' is not assignable to type 'string' * if (lengthIs(foo, 3)) { * const a: string = foo[0] //no error * const c: string = foo[2] //Tuple type '[string, string, string]' of length '3' has no element at index '3'. * } */ export declare const lengthIs: (arr: readonly T[], length: L) => arr is TupleOf; /** * creates a function that checks that the given array contains all elements in the union `T` * @example * type Colors = 'red' | 'blue' | 'pink'; * const arrayOfAllColors = arrayOfAll(); * * const missingColors = arrayOfAllColors(['red', 'blue']); // error * const goodColors = arrayOfAllColors(['red', 'blue', 'pink']); // compiles * const extraColors = arrayOfAllColors(['red', 'blue', 'pink', 'bad']); // error * @see https://stackoverflow.com/a/60132060 */ export declare const arrayOfAll: () => (array: U & ([T] extends [U[number]] ? unknown : never)) => U; /** * like {@link Array.prototype.map} but for promises where you want to execute the callback one at a time. * preserves the length of the array at compiletime */ export declare const mapAsync: (arr: T, callbackfn: (value: T[number], index: number, array: T) => Promise) => Promise>; /** * the return type of {@link findNotUndefined} and {@link findNotUndefinedAsync} when a callback is not provided * * ie. guaranteed to not be `undefined` or `null` if a result was found */ type FindNotUndefinedResult = Exclude>>, undefined> | (undefined extends Awaited ? undefined : null extends Awaited ? undefined : never); /** * finds the first item in an array where the given callback doesn't return `null` or `undefined` * @param arr the array of `T`s to check * @param runAtTheSameTime whether to execute the `callback` on each element of `arr` at the same time * @param callback a function to run on `arr` that may return `null` or `undefined`. */ export declare const findNotUndefinedAsync: { (arr: T, runAtTheSameTime: boolean, callback: (it: T[number]) => Promise): Promise>; (arr: T, runAtTheSameTime: boolean): Promise>; }; /** * finds the first item in an array where the given callback doesn't return `null` or `undefined` * @param arr the array of `T`s to check * @param callback a function to run on `arr` that may return `null` or `undefined`. */ export declare const findNotUndefined: { (arr: T, callback: (it: T[number]) => unknown): FindResult; (arr: T): FindNotUndefinedResult; }; /** * checks whether an array contains any duplicates */ export declare const containsDuplicates: (arr: unknown[]) => boolean; /** removes any duplicated items from an array */ export declare const removeDuplicates: (arr: T[]) => TupleOfUpTo; /** * @returns an array of any items that there were duplicates of in the given array (unique) * @example * findDuplicates([1,1,2,3,3,3]) // [1,3] */ export declare const findDuplicates: (arr: TupleOf) => TupleOfUpTo; /** * concatenates two arrays while keeping track of their length */ export declare const concat: (array1: A1, array2: A2) => [...A1, ...A2]; /** * {@link Array.prototype.indexOf} but it uses {@link IndexOf} such that the result can be known at compiletime */ export declare const indexOf: (array: Array_1, value: Value) => IndexOf; /** * {@link Array.prototype.flat} but it uses {@link Flatten} such that the result can be known at compiletime */ export declare const flat: (array: Array_1, depth?: Depth | undefined) => Flatten; /** removes `deleteCount` values from `array` starting at `startIndex` */ export declare const splice: (array: Array_1, startIndex: StartIndex, deleteCount: DeleteCount, ...insertItems: InsertItems) => [...import("ts-toolbelt/out/List/Take").Take">, ...InsertItems, ...import("ts-toolbelt/out/List/Take").Take, "<-">]; /** * runs the given `predicate` on each value in the given `array`, and returns the index of the first value in the `array` * that returned the highest number * @param array the values to execute `predicate` on * @param predicate the callback to execute on each value in the `array` * @example * const foo = findIndexWithHighestNumber(['foo', 'barbaz', 'qux'], value => value.length) //1 */ export declare const findIndexWithHighestNumber: (array: T, predicate: (value: T[number]) => number) => T extends readonly [] ? undefined : number; export declare const indexOfLongestString: (strings: Strings) => IndexOfLongestString; /** sorts an array of strings by longest to shortest */ export declare const sortByLongestStrings: (strings: Strings) => SortLongestStrings; /** * removes any `undefined` values from `array` before mapping over them and returning the mapped array with no * undefined or null values */ export declare const mapNotUndefined: (array: (T | undefined)[], callback: (value: T) => R) => R; /** * {@link Array.slice} using {@link Slice} so the result can be known at compiletime */ export declare const slice: (array: Array_1, start: Start, end?: End | undefined) => Slice; /** * {@link Array.prototype.forEach} on steroids™. * * preserves the length if known at compiletime, allowing you to use the index to access other items in the array when * the `noUncheckedIndexedAccess` compiler flag is enabled without it being possibly `undefined` * * `previous` and `next` functions are available in the callback to allow for easy access of the previous and next item * @example * const numbers = [1,2,3,4] * forEach(numbers, (num, index, prev, next) => { * if (index !== 0) * const foo = prev() // 1 | 2 | 3 * } */ export declare const forEach: (items: T, callback: (value: T[number], index: Enumerate, previous: () => T[number], next: () => T[number]) => void) => void; /** * {@link Array.prototype.map} on steroids™. * * preserves the length if known at compiletime, allowing you to use the index to access other items in the array * when and use the values in teh resulting array when the `noUncheckedIndexedAccess` compiler flag is enabled without * it being possibly `undefined` * * `previous` and `next` functions are available in the callback to allow for easy access of the previous and next item * @example * const numbers = [1,2,3,4] * forEach(numbers, (num, index, prev, next) => { * if (index !== 0) * const foo = prev() // 1 | 2 | 3 * } */ export declare const map: (items: T, callback: (value: T[number], index: Enumerate, previous: () => T[number], next: () => T[number]) => R) => TupleOf; /** {@link lodashCastArray} but the type is known at compiletime */ export declare const castArray: (value: T) => CastArray; /** * like {@link Array.prototype.find} but works properly with promises. it still executes the `predicate`s at the same * time though and returns on the first promise that resolves with `true`. * * if you want them to run one at a time, use {@link findAsync} */ export declare const find: (arr: T, predicate: (value: T[number]) => MaybePromise) => Promise>; export {}; //# sourceMappingURL=Array.d.ts.map