tamda
Version:
Practical functional programming library for TypeScript
19 lines • 821 B
JavaScript
import { infer } from '../function/infer';
import { compareString } from '../helpers/compareString';
import { compareValue } from '../helpers/compareValue';
import { opposite } from '../logic/opposite';
import { sort } from './sort';
export function sortBy() {
return inferred.apply(undefined, arguments);
}
const inferred = infer((array, mapFn, options = { reverse: false }) => sort(array, !options.reverse ? compareFn(mapFn) : opposite(compareFn(mapFn))), args => args[0] instanceof Array);
function compareFn(mapFn) {
return (itemA, itemB) => {
const valueA = mapFn(itemA);
const valueB = mapFn(itemB);
return typeof valueA === 'string'
? compareString(valueA, valueB)
: compareValue(valueA, valueB);
};
}
//# sourceMappingURL=sortBy.js.map