Skip to content

Instantly share code, notes, and snippets.

@bezenson
Last active January 31, 2019 08:29
Show Gist options
  • Save bezenson/3b308d22c8e962437c31c81efaca5be5 to your computer and use it in GitHub Desktop.
Save bezenson/3b308d22c8e962437c31c81efaca5be5 to your computer and use it in GitHub Desktop.
// 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