import { PlainObject, Primitive } from "../models.mjs"; //#region src/array/sort.d.ts /** * Sorting information for arrays _(using a comparison callback)_ */ type ArrayComparisonSorter = { /** * Callback to use when comparing items and values */ comparison: ComparisonSorter; /** * Direction to sort by */ direction?: SortDirection; }; /** * Sorting information for arrays _(using a key)_ */ type ArrayKeySorter = { /** * Comparator to use when comparing items and values */ compare?: CompareCallback; /** * Direction to sort by */ direction?: SortDirection; /** * Key to sort by */ key: ItemKey; }; /** * Sorters based on keys in an object */ type ArrayKeySorters = { [ItemKey in keyof Item]: ArrayKeySorter }[keyof Item]; /** * Sorter to use for sorting */ type ArraySorter = Item extends PlainObject ? keyof Item | ArrayComparisonSorter | ArrayKeySorters | ArrayValueSorter | ComparisonSorter : ArrayComparisonSorter | ArrayValueSorter | ComparisonSorter; /** * Sorting information for arrays _(using a value callback and built-in comparison)_ */ type ArrayValueSorter = { /** * Direction to sort by */ direction?: SortDirection; /** * Value to sort by */ value: (item: Item) => unknown; }; /** * Comparator to use when comparing items and values */ type CompareCallback> = (first: Item, firstValue: Value, second: Item, secondValue: Value) => number; type CompareCallbackValue = Item extends Primitive ? Item : unknown; /** * Callback to use when comparing items and values */ type ComparisonSorter = (first: Item, second: Item) => number; /** * Direction to sort by */ type SortDirection = 'ascending' | 'descending'; /** * Sorter for an array with predefined sorters * * Can be used to sort an array, get the predicted index for an item, and check if an array is sorted */ type Sorter = { /** * Sort an array of items * * @param array Array to sort * @returns Sorted array */ (array: Item[]): Item[]; /** * Get the index for an item _(to be inserted into an array of items)_ * * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_ * * @param array Array to get the index from * @param item Item to get the index for * @returns Index for item */ index(array: Item[], item: Item): number; /** * Is the array sorted? * * @param array Array to check * @returns `true` if sorted, otherwise `false` */ is(array: Item[]): boolean; }; /** * Get the index for an item _(to be inserted into an array of items)_ based on sorters _(and an optional default direction)_ * * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_ * * _Available as `getSortedIndex` and `sort.getIndex`_ * * @param array Array to get the index from * @param item Item to get the index for * @param sorters Sorters to use to determine sorting * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Index for item */ declare function getSortedIndex(array: Item[], item: Item, sorters: Array>, descending?: boolean): number; /** * Get the index for an item _(to be inserted into an array of items)_ based on a sorter _(and an optional default direction)_ * * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_ * * _Available as `getSortedIndex` and `sort.getIndex`_ * * @param array Array to get the index from * @param item Item to get the index for * @param sorter Sorter to use to determine sorting * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Index for item */ declare function getSortedIndex(array: Item[], item: Item, sorter: ArraySorter, descending?: boolean): number; /** * Get the index for an item _(to be inserted into an array of items)_ based on an optional default direction_ * * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_ * * _Available as `getSortedIndex` and `sort.getIndex`_ * * @param array Array to get the index from * @param item Item to get the index for * @param descending Sorted in descending order? _(defaults to `false`)_ * @returns Index for item */ declare function getSortedIndex(array: Item[], item: Item, descending?: boolean): number; /** * Initialize a sort handler with sorters _(and an optional default direction)_ * * _Available as `initializeSorter` and `sort.initialize`_ * * @param sorters Sorters to use for sorting * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Sort handler */ declare function initializeSorter(sorters: Array>, descending?: boolean): Sorter; /** * Initialize a sort handler with a sorter _(and an optional default direction)_ * * _Available as `initializeSorter` and `sort.initialize`_ * * @param sorter Sorter to use for sorting * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Sort handler */ declare function initializeSorter(sorter: ArraySorter, descending?: boolean): Sorter; /** * Initialize a sort handler _(with an optional default direction)_ * * _Available as `initializeSorter` and `sort.initialize`_ * * @param descending Sort in descending order? _(defaults to `false`)_ * @returns Sort handler */ declare function initializeSorter(descending?: boolean): Sorter; /** * Is the array sorted according to the sorters _(and the optional default direction)_? * * @param array Array to check * @param sorters Sorters to determine sorting * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns `true` if sorted, otherwise `false` */ declare function isSorted(array: Item[], sorters: Array>, descending?: boolean): boolean; /** * Is the array sorted according to the sorter _(and the optional default direction)_? * * @param array Array to check * @param sorter Sorter to determine sorting * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns `true` if sorted, otherwise `false` */ declare function isSorted(array: Item[], sorter: ArraySorter, descending?: boolean): boolean; /** * Is the array sorted? * * _Available as `isSorted` and `sort.is`_ * * @param array Array to check * @param descending Sorted in descending order? _(defaults to `false`)_ * @returns `true` if sorted, otherwise `false` */ declare function isSorted(array: Item[], descending?: boolean): boolean; /** * Sort an array of items using a comparison callback * * @param array Array to sort * @param comparator Comparator to use for sorting * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Sorted array * * @example * ```typescript * sort( * [{id: 3}, {id: 1}, {id: 2}], * (first, second) => first.id - second.id, * ); // => [{id: 1}, {id: 2}, {id: 3}] * ``` */ declare function sort(array: Item[], comparator: (first: Item, second: Item) => number, descending?: boolean): Item[]; /** * Sort an array of items, using multiple sorters to sort by specific values * * @param array Array to sort * @param sorters Sorters to use for sorting * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Sorted array */ declare function sort(array: Item[], sorters: Array>, descending?: boolean): Item[]; /** * Sort an array of items, using a single sorter to sort by a specific value * * @param array Array to sort * @param sorter Sorter to use for sorting * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_ * @returns Sorted array */ declare function sort(array: Item[], sorter: ArraySorter, descending?: boolean): Item[]; /** * Sort an array of items * * @param array Array to sort * @param descending Sort in descending order? _(defaults to `false`)_ * @returns Sorted array */ declare function sort(array: Item[], descending?: boolean): Item[]; declare namespace sort { var getIndex: typeof getSortedIndex; var initialize: typeof initializeSorter; var is: typeof isSorted; } declare const SORT_DIRECTION_ASCENDING: SortDirection; declare const SORT_DIRECTION_DESCENDING: SortDirection; //#endregion export { ArrayComparisonSorter, ArrayKeySorter, ArrayValueSorter, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SortDirection, Sorter, getSortedIndex, initializeSorter, isSorted, sort };