Skip to content

Instantly share code, notes, and snippets.

@kaw2k
Created April 7, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaw2k/d5e4b79bc66d43649adb78920d86a91a to your computer and use it in GitHub Desktop.
Save kaw2k/d5e4b79bc66d43649adb78920d86a91a to your computer and use it in GitHub Desktop.
const { find, identity, toPairs, compose, map, curry } = require('ramda');
/* tslint:disable: no-any */
type AnyType = any;
/* tslint:enable: no-any */
type TComparator = (a: AnyType, b: AnyType) => number;
type TObjectComparator = {
[key: string]: TComparator;
}
const findOr = curry((fallback, fn, list) => find(fn, list) || fallback);
export function multiArrayComparator(comparators: TComparator[]): TComparator {
return (x, y) => compose(
findOr(0, identity),
map(fn => fn(x, y))
)(comparators);
}
export function multiObjectComparator(comparators: TObjectComparator): TComparator {
return (x, y) => compose(
findOr(0, identity),
map(([key, fn]) => fn(x[key], y[key])),
toPairs
)(comparators);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment