Last active
January 31, 2019 08:29
-
-
Save bezenson/3b308d22c8e962437c31c81efaca5be5 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
// Like `anyPass`, but return predicate result, not boolean. | |
import { curry, curryN, isNil, max, pluck, reduce } from 'ramda'; | |
export const anyPassValue = curry(preds => | |
curryN(reduce(max, 0, pluck('length', preds)), (...args) => { | |
let idx = 0; | |
const len = preds.length; | |
while (idx < len) { | |
const callResult = preds[idx].apply(this, args); | |
if (!isNil(callResult)) { | |
return callResult; | |
} | |
idx += 1; | |
} | |
return undefined; | |
}), | |
); | |
// Example. Find valid path. | |
const sizes = ['large', 'medium', 'small']; | |
const possiblePaths = map(size => path(['media_details', 'sizes', size, 'source_url']), sizes); | |
anyPassValue(possiblePaths); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment