You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
860 B

5 years ago
  1. /**
  2. * Similar to compose but performs from left-to-right function composition.<br/>
  3. * {@link https://30secondsofcode.org/function#composeright see also}
  4. * @param {...[function]} fns) - list of unary function
  5. * @returns {*} result of the computation
  6. */
  7. export const composeRight = (...fns) => compose(...fns.reverse())
  8. /**
  9. * Performs right-to-left function composition.<br/>
  10. * Use Array.prototype.reduce() to perform right-to-left function composition.<br/>
  11. * The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.<br/>
  12. * {@link https://30secondsofcode.org/function#compose original source code}
  13. * @param {...[function]} fns) - list of unary function
  14. * @returns {*} result of the computation
  15. */
  16. export default function compose(...fns) {
  17. return fns.reduce((f, g) => (...args) => f(g(...args)))
  18. }