raiondesu-experiments/pointless-js

View on GitHub
src/curry.ts

Summary

Maintainability
A
0 mins
Test Coverage
export function curry<A, B, R>(fn: (a: A, b: B) => R): {
  (): (a: A) => (b: B) => R;
  (): (a: A, b: B) => R;
  (a: A): (b: B) => R;
  (a: A, b: B): R;
};

export function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): {
  (): (a: A) => (b: B) => (c: C) => R;
  (): (a: A) => (b: B, c: C) => R;
  (): (a: A, b: B) => (c: C) => R;
  (): (a: A, b: B, c: C) => R;
  (a: A): (b: B) => (c: C) => R;
  (a: A): (b: B, c: C) => R;
  (a: A, b: B): (c: C) => R;
  (a: A, b: B, c: C): R;
};

export function curry(fn: Function) {
  const arity = fn.length;

  return function curried(...args: any[]): any {
    if (args.length < arity) {
      return curried.bind(null, ...args);
    }

    return fn(...args);
  };
}