UNPKG

2.41 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4
5var curry =
6/*#__PURE__*/
7require("./curry");
8
9var nAry =
10/*#__PURE__*/
11require("./nAry");
12/**
13 * Wraps a constructor function inside a curried function that can be called
14 * with the same arguments and returns the same type. The arity of the function
15 * returned is specified to allow using variadic constructor functions.
16 *
17 * @func
18 * @memberOf R
19 * @since v0.4.0
20 * @category Function
21 * @sig Number -> (* -> {*}) -> (* -> {*})
22 * @param {Number} n The arity of the constructor function.
23 * @param {Function} Fn The constructor function to wrap.
24 * @return {Function} A wrapped, curried constructor function.
25 * @example
26 *
27 * // Variadic Constructor function
28 * function Salad() {
29 * this.ingredients = arguments;
30 * }
31 *
32 * Salad.prototype.recipe = function() {
33 * const instructions = R.map(ingredient => 'Add a dollop of ' + ingredient, this.ingredients);
34 * return R.join('\n', instructions);
35 * };
36 *
37 * const ThreeLayerSalad = R.constructN(3, Salad);
38 *
39 * // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
40 * const salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup');
41 *
42 * console.log(salad.recipe());
43 * // Add a dollop of Mayonnaise
44 * // Add a dollop of Potato Chips
45 * // Add a dollop of Ketchup
46 */
47
48
49var constructN =
50/*#__PURE__*/
51_curry2(function constructN(n, Fn) {
52 if (n > 10) {
53 throw new Error('Constructor with greater than ten arguments');
54 }
55
56 if (n === 0) {
57 return function () {
58 return new Fn();
59 };
60 }
61
62 return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
63 switch (arguments.length) {
64 case 1:
65 return new Fn($0);
66
67 case 2:
68 return new Fn($0, $1);
69
70 case 3:
71 return new Fn($0, $1, $2);
72
73 case 4:
74 return new Fn($0, $1, $2, $3);
75
76 case 5:
77 return new Fn($0, $1, $2, $3, $4);
78
79 case 6:
80 return new Fn($0, $1, $2, $3, $4, $5);
81
82 case 7:
83 return new Fn($0, $1, $2, $3, $4, $5, $6);
84
85 case 8:
86 return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
87
88 case 9:
89 return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
90
91 case 10:
92 return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
93 }
94 }));
95});
96
97module.exports = constructN;
\No newline at end of file