fenzhi-utils
Version:
分值前端项目的js函数库
28 lines (26 loc) • 641 B
JavaScript
/**
* 函数柯力化
* @param {function} fn
* @returns {function}
*/
/**
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = CustomCurry(add);
console.log(curriedAdd(1)(2)(3)); // Output: 6
console.log(curriedAdd(1, 2)(3)); // Output: 6
console.log(curriedAdd(1)(2, 3)); // Output: 6
console.log(curriedAdd(1, 2, 3)); // Output: 6
*/
function CustomCurry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
return function (...args2) {
return curried.apply(this, args.concat(args2));
};
}
};
}