UNPKG

declarative-js

Version:

_declarative-js_ is modern JavaScript library, that helps to: - tackle array transformation with built in JavaScript array api (e.g. `array.filter(toBe.unique())`), - provide a type-level solution for representing optional values instead of null referen

89 lines (88 loc) 4.71 kB
/** * Functions to be used in {@link Array.prototype.sort} as a callback. * @see https://pavel-surinin.github.io/declarativejs/#/?id=sorters */ export declare namespace Sort { interface IfAHigherB { true: number; false: number; } /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Sorts array in ascending order by values provided from callbacks. * First callback has highest priority in sorting and so on. * It accepts as many callbacks as You need. * @param {...Function} getters functions to get values to be compared * @returns a closure that can be used in array.sort() function * @see https://pavel-surinin.github.io/declarativejs/#/?id=ascendingby */ function ascendingBy<T>(...getters: ((val: T) => string | number)[]): (a: T, b: T) => number; /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Sorts array in ascending order by values resolved from object keys passed * as parameter. * First key has highest priority in sorting and so on. * It accepts as many keys as You need. * @param {...string} keys functions to get values to be compared * @returns a closure that can be used in array.sort() function * @see https://pavel-surinin.github.io/declarativejs/#/?id=ascendingby */ function ascendingBy<T, K extends keyof T>(...keys: K[]): (a: T, b: T) => number; /** * Functions to be used in {@link Array.prototype.sort} as a callback. * * Sorts array in descending order by values provided from callbacks. * First callback has highest priority in sorting and so on. * It accepts as many callbacks as You need. * @param {...Function} getters functions to get values to be compared * @returns a closure that can be used in array.sort() function * @see https://pavel-surinin.github.io/declarativejs/#/?id=descendingby */ function descendingBy<T>(...getters: ((val: T) => string | number)[]): (a: T, b: T) => number; /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Sorts array in descending order by values resolved from object keys passed * as parameter. * First key has highest priority in sorting and so on. * It accepts as many keys as You need. * @param {...string} keys functions to get values to be compared * @returns a closure that can be used in array.sort() function * @see https://pavel-surinin.github.io/declarativejs/#/?id=descendingby */ function descendingBy<T, K extends keyof T>(...keys: K[]): (a: T, b: T) => number; interface SortingCondition<T, R> { toValue: (val: T) => R; order: R[]; } /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Function that will sort items in array with custom values, by provided order. * It accepts as a parameter object with valueToOrderElement mapper and array of custom order rule * @type T type of array item * @type R type of item that will be mapped from callback and will be compared * @param {toValue: function(T): R, R[]} ...conditions * @returns comparator for Array.prototype.sort function. * @see https://pavel-surinin.github.io/declarativejs/#/?id=by */ function by<T>(...conditions: SortingCondition<T, any>[]): (a: T, b: T) => number; /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Function that will sort items in array with custom values, by provided order. * @param {string} key object key to extract value. This value will * be compared to another * @param {string | number} values values that will be define order of * extracted value by key * @returns comparator for Array.prototype.sort function. * @see https://pavel-surinin.github.io/declarativejs/#/?id=by */ function by<T, K extends keyof T>(key: K, values: T[K][]): (a: T, b: T) => number; /** * Functions to be used in {@link Array.prototype.sort} as a callback. * Function that will sort items in array, by provided order. * It accepts as a parameter array of custom order rule. * Element, that are not present in order array will be at he the end of the sorted list. * @param order array of custom order of items that are being sorted. * @returns comparator for Array.prototype.sort function. * @see https://pavel-surinin.github.io/declarativejs/#/?id=orderedby */ function orderedBy<T>(order: T[]): (a: T, b: T) => 0 | 1 | -1; }