/** * Identity function */ export const identity = (arg: T): T => arg; type SortingFunction = (a: any, b: any) => number; /** * Turn a less-than-or-equal-to operation into a function which returns 1, 0, or -1 for sorting. * @param {(Object,Object) => Bool} leq - function implementing less-than-equal-to for the desired ordering. */ export function leqToNumericOrdering(leq: (a: any, b: any) => boolean): SortingFunction { return (a, b) => leq(a, b) ? (leq(b, a) ? 0 : -1) : 1; } /** * Map a function over the arguments of a function before calling the function */ export function mapArguments( fn: (...args: Array) => any, argMap: (arg: any) => any, ): (...args: Array) => any { return function(...args: Array) { return fn.apply(this, args.map(argMap)); }; } /** * Compose orderings lexically to create a single combined ordering. */ export function lexicalCompose(...args: Array): SortingFunction { return (a, b) => args.reduce((acc, curr) => acc === 0 ? curr(a, b) : acc, 0); } export const defaultOrdering: SortingFunction = leqToNumericOrdering((a, b) => a <= b);