All files / internalHelpers _curry.js

100% Statements 4/4
100% Branches 2/2
100% Functions 3/3
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                            18x   62x 62x         17x    
// @flow
 
// Type definitions taken from https://github.com/gcanti/flow-static-land/blob/master/src/Fun.js
type Fn1<A, B> = (a: A, ...rest: Array<void>) => B;
type Fn2<A, B, C> = (a: A, b: B, ...rest: Array<void>) => C;
type Fn3<A, B, C, D> = (a: A, b: B, c: C, ...rest: Array<void>) => D;
 
type CurriedFn2<A, B, C> =
  & Fn1<A, Fn1<B, C>>
  & Fn2<A, B, C>;
// eslint-disable-next-line no-unused-vars
type CurriedFn3<A, B, C, D> =
  & Fn1<A, CurriedFn2<B, C, D>>
  & Fn2<A, B, Fn1<C, D>>
  & Fn3<A, B, C, D>;
 
// eslint-disable-next-line no-unused-vars
declare function curry<A, B, C>(f: Fn2<A, B, C>): CurriedFn2<A, B, C>;
// eslint-disable-next-line no-redeclare
declare function curry<A, B, C, D>(f: Fn3<A, B, C, D>): CurriedFn3<A, B, C, D>;
 
function curried(f, length, acc) {
  return function fn() {
    // eslint-disable-next-line prefer-rest-params
    const combined = acc.concat(Array.prototype.slice.call(arguments))
    return combined.length >= length ? f.apply(this, combined) : curried(f, length, combined)
  }
}
 
export default function curry(f: Function) { // eslint-disable-line no-redeclare
  return curried(f, f.length, [])
}