Skip to content

Instantly share code, notes, and snippets.

@stevewadsworth
Last active July 18, 2018 10:04
Show Gist options
  • Select an option

  • Save stevewadsworth/e04bdcaf323656d4f9812b66084d1359 to your computer and use it in GitHub Desktop.

Select an option

Save stevewadsworth/e04bdcaf323656d4f9812b66084d1359 to your computer and use it in GitHub Desktop.
withEither
/**
* withEither takes three parameters and returns a function.
* When the returned function is called a decision is made to call either the first or second functions.
*
* @param {function} funcTrue The function to call if choice returns true
* @param {function} funcFalse The function to call if choice returns false
* @param {function} choice The function which returns true or false depending on the input params
* @returns {function}
*/
const withEither = (funcTrue, funcFalse, choice) => {
return (...params) => {
if (choice(...params)) {
return funcTrue(...params);
}
return funcFalse(...params);
};
};
export default withEither;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment