construct.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import _curry1 from "./internal/_curry1.js";
  2. import constructN from "./constructN.js";
  3. /**
  4. * Wraps a constructor function inside a curried function that can be called
  5. * with the same arguments and returns the same type.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.1.0
  10. * @category Function
  11. * @sig (* -> {*}) -> (* -> {*})
  12. * @param {Function} fn The constructor function to wrap.
  13. * @return {Function} A wrapped, curried constructor function.
  14. * @see R.invoker
  15. * @example
  16. *
  17. * // Constructor function
  18. * function Animal(kind) {
  19. * this.kind = kind;
  20. * };
  21. * Animal.prototype.sighting = function() {
  22. * return "It's a " + this.kind + "!";
  23. * }
  24. *
  25. * const AnimalConstructor = R.construct(Animal)
  26. *
  27. * // Notice we no longer need the 'new' keyword:
  28. * AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
  29. *
  30. * const animalTypes = ["Lion", "Tiger", "Bear"];
  31. * const animalSighting = R.invoker(0, 'sighting');
  32. * const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
  33. * R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]
  34. */
  35. var construct =
  36. /*#__PURE__*/
  37. _curry1(function construct(Fn) {
  38. return constructN(Fn.length, Fn);
  39. });
  40. export default construct;