index.js 457 B

1234567891011121314151617181920212223
  1. import arityN from 'arity-n';
  2. let compose2 = (f, g) => (...args) => f(g(...args));
  3. export default function compose(...functions) {
  4. const funcs = functions.filter(fn => typeof fn === 'function');
  5. let lastIdx = funcs.length - 1;
  6. let arity = 0;
  7. if (funcs.length <= 0) {
  8. throw new Error('No funcs passed');
  9. }
  10. if (lastIdx >= 0 && funcs[lastIdx]) {
  11. arity = funcs[lastIdx].length;
  12. }
  13. return arityN(funcs.reduce(compose2), arity);
  14. }