Source

eitherOr.js

  1. import curry from "./curry"
  2. /**
  3. * A function that accepts two functions and a value and will return the first result which is "truthy".
  4. * This function is curried because it doesn't make any sense to even have it unless you have the functions ahead of time but not the value.
  5. *
  6. * @function
  7. * @name eitherOr
  8. * @param {function} fnA The first function to be executed on the value
  9. * @param {function} fnB The second function to be executed on the value
  10. * @param {*} val A value to be passed into the functions
  11. * @returns {*} The result of executing value passed in
  12. */
  13. function eitherOr(fnA, fnB, val) {
  14. if (typeof fnA !== "function") {
  15. throw new TypeError(`${fnA} is not a function`)
  16. }
  17. if (typeof fnB !== "function") {
  18. throw new TypeError(`${fnB} is not a function`)
  19. }
  20. return fnA(val) || fnB(val)
  21. }
  22. export default curry(eitherOr)