construct-n

v0.9.6arrow_drop_down
v0.9.6
STATUS
Passing
DOWNLOADS
3
VISIBILITY
Public
PUBLISHED
5 years ago
SIZE
1 KB
Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type.
1 contributor
Install construct-n as a package?
Copied
npm i @bit/justin-capalbo.ramda.construct-n
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
No preview available
modifieddraft
chevron_left
chevron_right
constructN (
n:Number,
Fn:Function
) : Function

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.

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
Arguments
n: Number

The arity of the constructor function.

Fn: Function

The constructor function to wrap.

Returns
Function

A wrapped, curried constructor function.

Help and resources