Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. /**
  2. * @module lib/compose.js
  3. */
  4.  
  5. /**
  6. * Identity function
  7. *
  8. * @param {any} arg An input
  9. * @returns {any} The original input
  10. * @private
  11. */
  12. const identity = arg => arg;
  13.  
  14. /**
  15. * Check if function is promise
  16. *
  17. * @param {function} fn Function to check
  18. * @returns {boolean} Whether function is promise
  19. * @private
  20. */
  21. const isPromise = fn => {
  22. return fn != null
  23. && typeof fn === 'object'
  24. && typeof fn.then === 'function'
  25. && typeof fn.catch === 'function';
  26. };
  27.  
  28. /**
  29. * Create composed function
  30. *
  31. * @param {function} first First function
  32. * @param {...function} rest Rest of the functions
  33. * @returns {function} Composed function
  34. */
  35. export default (first = identity, ...rest) => {
  36. return (...args) => {
  37. return rest.reduce(
  38. (computed, fn) => {
  39. return isPromise(computed)
  40. ? computed.then(fn)
  41. : fn(computed);
  42. },
  43. first(...args)
  44. );
  45. };
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement