import { type MapFunction, type MapSameFunction } from './value/map'; import { type Maybe, type MaybeMap } from './value/maybe.type'; export type SortingOrder = 'asc' | 'desc'; export declare const SORT_VALUE_LESS_THAN: SortComparisonNumber; export declare const SORT_VALUE_GREATER_THAN: SortComparisonNumber; export declare const SORT_VALUE_EQUAL: SortComparisonNumber; /** * A number that is the result of comparison two items. * * In "ascending" order, a value: * - smaller than another will return -1 (or less). * - equal to another will return 0 * - greater than another will return 1 (or more). * - Example: a - b * * In "descending" order, this returns the opposite values as "ascending". * - Example: b - a */ export type SortComparisonNumber = number; /** * A comparison function that returns a SortComparisonNumber. */ export type SortCompareFunction = (a: T, b: T) => SortComparisonNumber; /** * An object that has a reference to a SortCompareFunction function. */ export interface SortCompareFunctionRef { /** * Sort comparison function to sort with. */ readonly sortWith: SortCompareFunction; } /** * Comparison function that sorts in ascending order. */ export type AscendingSortCompareFunction = SortCompareFunction; /** * Comparison function that sorts in descending order. */ export type SortDescendingCompareFunction = SortCompareFunction; /** * Convenience function that reverses the order of the sorted values. * * @param compareFn - the comparison function whose order should be reversed * @returns a new comparison function with the opposite sort direction */ export declare function reverseCompareFn(compareFn: AscendingSortCompareFunction): SortDescendingCompareFunction; export declare function reverseCompareFn(compareFn: SortDescendingCompareFunction): AscendingSortCompareFunction; /** * Convenience function that reverses the order of the sorted values if the order is specified descending. * * The input comparison function must be in ascending order. * * @param ascendingCompareFn - a comparison function that sorts in ascending order * @param order - the desired sort direction; defaults to 'asc' * @returns the original function if ascending, or a reversed version if descending */ export declare function compareFnOrder(ascendingCompareFn: AscendingSortCompareFunction, order?: SortingOrder): SortCompareFunction; /** * Creates a {@link SortCompareFunction} that maps values to a different type before comparing. * Useful for sorting objects by a derived property. * * @param mapValue - Maps each value to the type used for comparison. * @param comparesFunction - Compares the mapped values. * @returns A sort comparison function for the original type. * * @example * ```ts * const byName = compareWithMappedValuesFunction( * (user: { name: string }) => user.name, * (a, b) => a.localeCompare(b) * ); * [{ name: 'Bob' }, { name: 'Alice' }].sort(byName); * // [{ name: 'Alice' }, { name: 'Bob' }] * ``` */ export declare function compareWithMappedValuesFunction(mapValue: MapFunction, comparesFunction: SortCompareFunction): SortCompareFunction; /** * Simple SortValuesFunction that only sorts the input and has no configuration. */ export type SimpleSortValuesFunction = MapSameFunction; /** * Function that sorts the input values array. Can be configured to return a copy. */ export type SortValuesFunction = (values: T[], sortOnCopy?: boolean) => T[]; /** * Input for sortValues(). */ export type SortValuesInput = MaybeMap>> & { /** * Values to sort. */ readonly values: T[]; /** * Whether or not to sort on a copy of the input values. */ readonly sortOnCopy?: Maybe; /** * Whether or not to always return a copy of the input values, even if no sorting occurs. */ readonly alwaysReturnCopy?: Maybe; }; /** * Sorts values using the configuration in {@link SortValuesInput}. Optionally sorts on a copy to avoid mutating the original array. * * @param input - Configuration including values, sort function, and copy behavior. * @returns The sorted array (may be the original or a copy depending on configuration). */ export declare function sortValues(input: SortValuesInput): T[]; /** * Creates a {@link SortValuesFunction} from a {@link SortCompareFunctionRef}. * * @param sortRef - Reference containing the sort comparison function. * @param sortOnCopyDefault - Whether to sort on a copy by default (default: true). * @returns A function that sorts arrays using the configured comparison. */ export declare function sortValuesFunctionWithSortRef(sortRef: Maybe>>, sortOnCopyDefault?: boolean): SortValuesFunction; /** * Creates a SortValuesFunction using the input. If the input is not defined, or it's sort function is not defined, then returns mapIdentityFunction(). * * @param sortRef - optional reference containing the sort comparison function * @param sortOnCopyDefault - whether to sort on a copy by default * @returns a sort function that sorts arrays, or the identity function if no sort comparison is configured */ export declare function sortValuesFunctionOrMapIdentityWithSortRef(sortRef: Maybe>>, sortOnCopyDefault?: boolean): SortValuesFunction; /** * Equivalent to {@link sortValuesFunctionOrMapIdentityWithSortRef}, but returns a {@link SimpleSortValuesFunction} instead. * * @param sortRef - Reference containing the sort comparison function. * @param sortOnCopyDefault - Whether to sort on a copy by default. * @returns A simple sort function or identity function. */ export declare const simpleSortValuesFunctionWithSortRef: (sortRef: Maybe>>, sortOnCopyDefault?: boolean) => SimpleSortValuesFunction; export interface MinAndMax { min: T; max: T; } export type MinAndMaxFunctionResult = MinAndMax | null; /** * Returns the min and maximum value from the input values. * * If the input iterable is empty, then returns undefined. */ export type MinAndMaxFunction = (values: Iterable) => MinAndMaxFunctionResult; /** * Creates a {@link MinAndMaxFunction} that finds the minimum and maximum values from an iterable using the provided comparison function. * * @param compareFn - Ascending sort comparison function used to determine min/max. * @returns A function that returns `{ min, max }` or `null` for empty iterables. * * @example * ```ts * const fn = minAndMaxFunction((a, b) => a - b); * fn([3, 1, 4, 1, 5]); // { min: 1, max: 5 } * fn([]); // null * ``` */ export declare function minAndMaxFunction(compareFn: SortCompareFunction): MinAndMaxFunction;