curry-named-args
v0.2.1
Published
curryNamed is a JavaScript utility that turns any function that receives a single object parameter into a variadic curried version of that function
Downloads
2
Maintainers
Readme
curry-named-args
curryNamed is a JavaScript utility that turns any function that receives a single object parameter into a variadic curried version of that function
Examples
The required parameters for the function to be executed need to be specified:
import curryNamed, { ParamTypes } from 'curry-named-args';
function sumNumbers({a, b, c}) {
return a + b + c;
}
const sumThreeNumbers = curryNamed(sumNumbers);
sumThreeNumbers.paramTypes = {
a: ParamTypes.isRequired,
b: ParamTypes.isRequired,
c: ParamTypes.isRequired,
};
const sum5ToTwoNumbers = sumThreeNumbers({a: 5});
const sum8ToANumber = sum5ToTwoNumbers({b: 3});
sum8ToANumber({c: 4}); // 12
It allows to create multiple functions from an original one:
const sum5ToTwoNumbers = sumThreeNumbers({a: 5});
const sum3ToTwoNumbers = sumThreeNumbers({a: 3});
sum5ToTwoNumbers({b: 1, c: 2}); // 8
sum3ToTwoNumbers({b: 1, c: 2}); // 6
It allows to pass any number of arguments to the returned function:
sumThreeNumbers({a: 1, b: 3, c: 5}); // 9
sum5ToTwoNumbers({b: 2, c: 4}); // 11
It allows to pass the arguments in any order:
sumThreeNumbers({b: 2})({a: 4})({c: 8}); // 14
Because the required parameters need to be specified, optional parameters can be passed and the function is executed only when all required parameters are passed:
import curryNamed, { ParamTypes } from 'curry-named-args';
function sumNumbers({a, b, c, msg}) {
if (msg) {
console.log(msg);
}
return a + b + c;
}
const sumThreeNumbers = curryNamed(sumNumbers);
sumThreeNumbers.paramTypes = {
a: ParamTypes.isRequired,
b: ParamTypes.isRequired,
c: ParamTypes.isRequired,
};
sumThreeNumbers({a: 1})({b: 2})({c: 3}); // 6
sumThreeNumbers({a: 1, msg: 'summing...'})({b: 2})({c: 3});
// summing...
// 6