UNPKG

ramda-adjunct

Version:

Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.

334 lines (306 loc) 9.65 kB
"use strict"; exports.__esModule = true; exports["default"] = void 0; var _ramda = require("ramda"); var _mapping = _interopRequireDefault(require("./mapping")); var _traits = require("./traits"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } // we do this here for jsdocs generate properly var of = _mapping["default"].of, _ap = _mapping["default"].ap, _map = _mapping["default"].map, _equals = _mapping["default"].equals, _concat = _mapping["default"].concat, _chain = _mapping["default"].chain, _lte = _mapping["default"].lte, _empty = _mapping["default"].empty, _contramap = _mapping["default"].contramap; /** * The simplest {@link https://github.com/fantasyland/fantasy-land|fantasy-land} * compatible monad which attaches no information to values. * * The Identity type is a very simple type that has no interesting side effects and * is effectively just a container of some value. So why does it exist ? * The Identity type is often used as the base monad of a monad * transformer when no other behaviour is required. * * @memberOf RA * @implements * {@link https://github.com/fantasyland/fantasy-land#apply|Apply}, * {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative}, * {@link https://github.com/fantasyland/fantasy-land#functor|Functor}, * {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid}, * {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup}, * {@link https://github.com/fantasyland/fantasy-land#chain|Chain}, * {@link https://github.com/fantasyland/fantasy-land#monad|Monad}, * {@link https://github.com/fantasyland/fantasy-land#ord|Ord}, * {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*}, * {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant} * @since {@link https://char0n.github.io/ramda-adjunct/1.8.0|v1.8.0} */ var Identity = /*#__PURE__*/function () { _createClass(Identity, null, [{ key: of, /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative} specification. * * @static * @sig of :: Applicative f => a -> f a * @param {*} value * @returns {RA.Identity} * @example * * const a = Identity.of(1); //=> Identity(1) */ value: function value(_value) { return new Identity(_value); } }, { key: "of", value: function of(value) { return new Identity(value); } /** * @static */ }, { key: '@@type', get: function get() { return 'RA/Identity'; } /** * Private constructor. Use {@link RA.Identity.of|Identity.of} instead. * * @private * @param {*} value * @return {RA.Identity} */ }]); function Identity(value) { _classCallCheck(this, Identity); this.value = value; } /** * Catamorphism for a value. * @returns {*} * @example * * const a = Identity.of(1); * a.get(); //=> 1 */ _createClass(Identity, [{ key: "get", value: function get() { return this.value; } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#apply|Apply} specification. * * @sig ap :: Apply f => f a ~> f (a -> b) -> f b * @param {RA.Identity} applyWithFn * @return {RA.Identity} * @example * * const a = Identity.of(1); * const b = Identity.of(1).map(a => b => a + b); * * a.ap(b); //=> Identity(2) */ }, { key: _ap, value: function value(applyWithFn) { return _traits.applyTrait[_ap].call(this, applyWithFn); } }, { key: "ap", value: function ap(applyWithFn) { return this[_ap](applyWithFn); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#functor|Functor} specification. * * @sig map :: Functor f => f a ~> (a -> b) -> f b * @param {Function} fn * @return {RA.Identity} * @example * * const a = Identity.of(1); * a.map(a => a + 1); //=> Identity(2) */ }, { key: _map, value: function value(fn) { return _traits.functorTrait[_map].call(this, fn); } }, { key: "map", value: function map(fn) { return this[_map](fn); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid} specification. * * @sig equals :: Setoid a => a ~> a -> Boolean * @param {RA.Identity} setoid * @return {boolean} * @example * * const a = Identity.of(1); * const b = Identity.of(1); * const c = Identity.of(2); * * a.equals(b); //=> true * a.equals(c); //=> false */ }, { key: _equals, value: function value(setoid) { return _traits.setoidTrait[_equals].call(this, setoid); } }, { key: "equals", value: function equals(setoid) { return this[_equals](setoid); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup} specification. * * @sig concat :: Semigroup a => a ~> a -> a * @param {RA.Identity} semigroup * @return {RA.Identity} * @example * * const a = Identity.of(1); * const b = Identity.of(1); * a.concat(b); //=> 2 * * const c = Identity.of('c'); * const d = Identity.of('d'); * c.concat(d); //=> 'cd' * * const e = Identity.of(['e']); * const f = Identity.of(['f']); * e.concat(f); //=> ['e', 'f'] */ }, { key: _concat, value: function value(semigroup) { return _traits.semigroupTrait[_concat].call(this, semigroup); } }, { key: "concat", value: function concat(semigroup) { return this[_concat](semigroup); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#chain|Chain} specification. * * @sig chain :: Chain m => m a ~> (a -> m b) -> m b * @param {Function} fn Function returning the value of the same {@link https://github.com/fantasyland/fantasy-land#semigroup|Chain} * @return {RA.Identity} * @example * * const a = Identity.of(1); * const fn = val => Identity.of(val + 1); * * a.chain(fn).chain(fn); //=> Identity(3) */ }, { key: _chain, value: function value(fn) { return _traits.chainTrait[_chain].call(this, fn); } }, { key: "chain", value: function chain(fn) { return this[_chain](fn); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#ord|Ord} specification. * * @sig lte :: Ord a => a ~> a -> Boolean * @param {RA.Identity} ord * @return {boolean} * @example * * const a = Identity.of(1); * const b = Identity.of(1); * const c = Identity.of(2); * * a.lte(b); //=> true * a.lte(c); //=> true * c.lte(a); //=> false */ }, { key: _lte, value: function value(ord) { return _traits.ordTrait[_lte].call(this, ord); } }, { key: "lte", value: function lte(ord) { return this[_lte](ord); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*} specification. * Partial implementation of Monoid specification. `empty` method on instance only, returning * identity value of the wrapped type. Using `R.empty` under the hood. * * * @sig empty :: Monoid m => () -> m * @return {RA.Identity} * @example * * const a = Identity.of('test'); * const i = a.empty(); * * a.concat(i); //=> Identity('string'); * i.concat(a); //=> Identity('string'); */ }, { key: _empty, value: function value() { return this.constructor.of((0, _ramda.empty)(this.value)); } }, { key: "empty", value: function empty() { return this[_empty](); } /** * Fantasy land {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant} specification. * * @sig contramap :: Contravariant f => f a ~> (b -> a) -> f b * @param {Function} fn * @return {RA.Identity} * @example * * const identity = a => a; * const add1 = a => a + 1; * const divide2 = a => a / 2; * * Identity.of(divide2).contramap(add1).get()(3); //=> 2 * Identity.of(identity).contramap(divide2).contramap(add1).get()(3); //=> 2 * Identity.of(identity).contramap(a => divide2(add1(a))).get()(3); //=> 2 */ }, { key: _contramap, value: function value(fn) { var _this = this; return this.constructor.of(function (value) { return _this.value(fn(value)); }); } }, { key: "contramap", value: function contramap(fn) { return this[_contramap](fn); } }]); return Identity; }(); var _default = Identity; exports["default"] = _default;