Created
April 7, 2016 15:15
-
-
Save kaw2k/d5e4b79bc66d43649adb78920d86a91a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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