forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurryN.ts
More file actions
33 lines (32 loc) · 936 Bytes
/
curryN.ts
File metadata and controls
33 lines (32 loc) · 936 Bytes
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
import { curryN } from '../typings/types';
/**
* Returns a curried equivalent of the provided function, with the specified
* arity. If `g` is `curryN(3, f)`, the
* following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* @param {Number} arity The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @example
*
* var sumArgs = (...args) => sum(args);
*
* var curriedAddFourNumbers = curryN(4, sumArgs);
* var f = curriedAddFourNumbers(1, 2);
* var g = f(3);
* g(4); //=> 10
*/
export default ((arity, fn) =>
function curried(...args) {
if (args.length >= arity) {
return fn.apply(this, args);
}
return function (...newArgs) {
return curried.apply(this, args.concat(newArgs));
};
}) as typeof curryN;