source: node_modules/ramda/es/constructN.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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