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; /** * 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 * * @semanticType * @semanticTopic numeric */ 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. * * @dbxUtil * @dbxUtilCategory sort * @dbxUtilTags sort, reverse, compare, descending, ascending, order * @dbxUtilRelated compare-fn-order, compare-with-mapped-values-function * * @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. * * @dbxUtil * @dbxUtilCategory sort * @dbxUtilTags sort, order, ascending, descending, compare, direction * @dbxUtilRelated reverse-compare-fn, compare-with-mapped-values-function */ 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. * * @dbxUtil * @dbxUtilCategory sort * @dbxUtilKind factory * @dbxUtilTags sort, compare, map, derive, factory, by, key * @dbxUtilRelated reverse-compare-fn, compare-fn-order * * @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' }] * ``` * @__NO_SIDE_EFFECTS__ */ 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 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 = Maybe>; /** * 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 Returns `{ min, max }` or `null` for empty iterables. * * @dbxUtil * @dbxUtilCategory sort * @dbxUtilKind factory * @dbxUtilTags sort, min, max, factory, iterable, compare * @dbxUtilRelated min-and-max-index-items-function, sort-by-number-function * * @example * ```ts * const fn = minAndMaxFunction((a, b) => a - b); * fn([3, 1, 4, 1, 5]); // { min: 1, max: 5 } * fn([]); // null * ``` * @__NO_SIDE_EFFECTS__ */ export declare function minAndMaxFunction(compareFn: SortCompareFunction): MinAndMaxFunction;