of.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Given a constructor and a value, returns a new instance of that constructor
  4. * containing the value.
  5. *
  6. * Dispatches to the `fantasy-land/of` method of the constructor first (if present)
  7. * or to the `of` method last (if present). When neither are present, wraps the
  8. * value in an array.
  9. *
  10. * Note this `of` is different from the ES6 `of`; See
  11. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
  12. *
  13. * @func
  14. * @memberOf R
  15. * @since v0.3.0
  16. * @category Function
  17. * @sig (* -> {*}) -> a -> {a}
  18. * @param {Object} Ctor A constructor
  19. * @param {*} val any value
  20. * @return {*} An instance of the `Ctor` wrapping `val`.
  21. * @example
  22. *
  23. * R.of(Array, 42); //=> [42]
  24. * R.of(Array, [42]); //=> [[42]]
  25. * R.of(Maybe, 42); //=> Maybe.Just(42)
  26. */
  27. var of =
  28. /*#__PURE__*/
  29. _curry2(function of(Ctor, val) {
  30. return typeof Ctor['fantasy-land/of'] === 'function' ? Ctor['fantasy-land/of'](val) : typeof Ctor.of === 'function' ? Ctor.of(val) : [val];
  31. });
  32. export default of;