88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
import _curry2 from "./internal/_curry2.js";
|
|
import curry from "./curry.js";
|
|
import nAry from "./nAry.js";
|
|
/**
|
|
* Wraps a constructor function inside a curried function that can be called
|
|
* with the same arguments and returns the same type. The arity of the function
|
|
* returned is specified to allow using variadic constructor functions.
|
|
*
|
|
* @func
|
|
* @memberOf R
|
|
* @since v0.4.0
|
|
* @category Function
|
|
* @sig Number -> (* -> {*}) -> (* -> {*})
|
|
* @param {Number} n The arity of the constructor function.
|
|
* @param {Function} Fn The constructor function to wrap.
|
|
* @return {Function} A wrapped, curried constructor function.
|
|
* @example
|
|
*
|
|
* // Variadic Constructor function
|
|
* function Salad() {
|
|
* this.ingredients = arguments;
|
|
* }
|
|
*
|
|
* Salad.prototype.recipe = function() {
|
|
* const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
|
|
* return R.join('\n', instructions);
|
|
* };
|
|
*
|
|
* const ThreeLayerSalad = R.constructN(3, Salad);
|
|
*
|
|
* // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
|
|
* const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
|
|
*
|
|
* console.log(salad.recipe());
|
|
* // Add a dollop of Mayonnaise
|
|
* // Add a dollop of Potato Chips
|
|
* // Add a dollop of Ketchup
|
|
*/
|
|
|
|
var constructN =
|
|
/*#__PURE__*/
|
|
_curry2(function constructN(n, Fn) {
|
|
if (n > 10) {
|
|
throw new Error('Constructor with greater than ten arguments');
|
|
}
|
|
|
|
if (n === 0) {
|
|
return function () {
|
|
return new Fn();
|
|
};
|
|
}
|
|
|
|
return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
|
|
switch (arguments.length) {
|
|
case 1:
|
|
return new Fn($0);
|
|
|
|
case 2:
|
|
return new Fn($0, $1);
|
|
|
|
case 3:
|
|
return new Fn($0, $1, $2);
|
|
|
|
case 4:
|
|
return new Fn($0, $1, $2, $3);
|
|
|
|
case 5:
|
|
return new Fn($0, $1, $2, $3, $4);
|
|
|
|
case 6:
|
|
return new Fn($0, $1, $2, $3, $4, $5);
|
|
|
|
case 7:
|
|
return new Fn($0, $1, $2, $3, $4, $5, $6);
|
|
|
|
case 8:
|
|
return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
|
|
|
|
case 9:
|
|
return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
|
|
|
|
case 10:
|
|
return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
|
|
}
|
|
}));
|
|
});
|
|
|
|
export default constructN; |