[d24f17c] | 1 | function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
---|
| 2 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
---|
| 3 | 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, _toPropertyKey(descriptor.key), descriptor); } }
|
---|
| 4 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
---|
| 5 | function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
---|
| 6 | function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
---|
| 7 | import { empty as emptyR } from 'ramda';
|
---|
| 8 | import * as fl from './mapping';
|
---|
| 9 | import { applyTrait, functorTrait, setoidTrait, semigroupTrait, chainTrait, ordTrait } from './traits';
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * The simplest {@link https://github.com/fantasyland/fantasy-land|fantasy-land}
|
---|
| 13 | * compatible monad which attaches no information to values.
|
---|
| 14 | *
|
---|
| 15 | * The Identity type is a very simple type that has no interesting side effects and
|
---|
| 16 | * is effectively just a container of some value. So why does it exist ?
|
---|
| 17 | * The Identity type is often used as the base monad of a monad
|
---|
| 18 | * transformer when no other behaviour is required.
|
---|
| 19 | *
|
---|
| 20 | * @memberOf RA
|
---|
| 21 | * @implements
|
---|
| 22 | * {@link https://github.com/fantasyland/fantasy-land#apply|Apply},
|
---|
| 23 | * {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative},
|
---|
| 24 | * {@link https://github.com/fantasyland/fantasy-land#functor|Functor},
|
---|
| 25 | * {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid},
|
---|
| 26 | * {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup},
|
---|
| 27 | * {@link https://github.com/fantasyland/fantasy-land#chain|Chain},
|
---|
| 28 | * {@link https://github.com/fantasyland/fantasy-land#monad|Monad},
|
---|
| 29 | * {@link https://github.com/fantasyland/fantasy-land#ord|Ord},
|
---|
| 30 | * {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*},
|
---|
| 31 | * {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant}
|
---|
| 32 | * @since {@link https://char0n.github.io/ramda-adjunct/1.8.0|v1.8.0}
|
---|
| 33 | */
|
---|
| 34 | var Identity = /*#__PURE__*/function (_fl$of, _fl$ap, _fl$map, _fl$equals, _fl$concat, _fl$chain, _fl$lte, _fl$empty, _fl$contramap) {
|
---|
| 35 | /**
|
---|
| 36 | * Private constructor. Use {@link RA.Identity.of|Identity.of} instead.
|
---|
| 37 | *
|
---|
| 38 | * @param {*} value
|
---|
| 39 | * @return {RA.Identity}
|
---|
| 40 | */
|
---|
| 41 | function Identity(value) {
|
---|
| 42 | _classCallCheck(this, Identity);
|
---|
| 43 | this.value = value;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Catamorphism for a value.
|
---|
| 48 | * @returns {*}
|
---|
| 49 | * @example
|
---|
| 50 | *
|
---|
| 51 | * const a = Identity.of(1);
|
---|
| 52 | * a.get(); //=> 1
|
---|
| 53 | */
|
---|
| 54 | _createClass(Identity, [{
|
---|
| 55 | key: "get",
|
---|
| 56 | value: function get() {
|
---|
| 57 | return this.value;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#apply|Apply} specification.
|
---|
| 62 | *
|
---|
| 63 | * @sig ap :: Apply f => f a ~> f (a -> b) -> f b
|
---|
| 64 | * @param {RA.Identity} applyWithFn
|
---|
| 65 | * @return {RA.Identity}
|
---|
| 66 | * @example
|
---|
| 67 | *
|
---|
| 68 | * const a = Identity.of(1);
|
---|
| 69 | * const b = Identity.of(1).map(a => b => a + b);
|
---|
| 70 | *
|
---|
| 71 | * a.ap(b); //=> Identity(2)
|
---|
| 72 | */
|
---|
| 73 | }, {
|
---|
| 74 | key: _fl$ap,
|
---|
| 75 | value: function value(applyWithFn) {
|
---|
| 76 | return applyTrait[fl.ap].call(this, applyWithFn);
|
---|
| 77 | }
|
---|
| 78 | }, {
|
---|
| 79 | key: "ap",
|
---|
| 80 | value: function ap(applyWithFn) {
|
---|
| 81 | return this[fl.ap](applyWithFn);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#functor|Functor} specification.
|
---|
| 86 | *
|
---|
| 87 | * @sig map :: Functor f => f a ~> (a -> b) -> f b
|
---|
| 88 | * @param {Function} fn
|
---|
| 89 | * @return {RA.Identity}
|
---|
| 90 | * @example
|
---|
| 91 | *
|
---|
| 92 | * const a = Identity.of(1);
|
---|
| 93 | * a.map(a => a + 1); //=> Identity(2)
|
---|
| 94 | */
|
---|
| 95 | }, {
|
---|
| 96 | key: _fl$map,
|
---|
| 97 | value: function value(fn) {
|
---|
| 98 | return functorTrait[fl.map].call(this, fn);
|
---|
| 99 | }
|
---|
| 100 | }, {
|
---|
| 101 | key: "map",
|
---|
| 102 | value: function map(fn) {
|
---|
| 103 | return this[fl.map](fn);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#setoid|Setoid} specification.
|
---|
| 108 | *
|
---|
| 109 | * @sig equals :: Setoid a => a ~> a -> Boolean
|
---|
| 110 | * @param {RA.Identity} setoid
|
---|
| 111 | * @return {boolean}
|
---|
| 112 | * @example
|
---|
| 113 | *
|
---|
| 114 | * const a = Identity.of(1);
|
---|
| 115 | * const b = Identity.of(1);
|
---|
| 116 | * const c = Identity.of(2);
|
---|
| 117 | *
|
---|
| 118 | * a.equals(b); //=> true
|
---|
| 119 | * a.equals(c); //=> false
|
---|
| 120 | */
|
---|
| 121 | }, {
|
---|
| 122 | key: _fl$equals,
|
---|
| 123 | value: function value(setoid) {
|
---|
| 124 | return setoidTrait[fl.equals].call(this, setoid);
|
---|
| 125 | }
|
---|
| 126 | }, {
|
---|
| 127 | key: "equals",
|
---|
| 128 | value: function equals(setoid) {
|
---|
| 129 | return this[fl.equals](setoid);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#semigroup|Semigroup} specification.
|
---|
| 134 | *
|
---|
| 135 | * @sig concat :: Semigroup a => a ~> a -> a
|
---|
| 136 | * @param {RA.Identity} semigroup
|
---|
| 137 | * @return {RA.Identity}
|
---|
| 138 | * @example
|
---|
| 139 | *
|
---|
| 140 | * const a = Identity.of(1);
|
---|
| 141 | * const b = Identity.of(1);
|
---|
| 142 | * a.concat(b); //=> 2
|
---|
| 143 | *
|
---|
| 144 | * const c = Identity.of('c');
|
---|
| 145 | * const d = Identity.of('d');
|
---|
| 146 | * c.concat(d); //=> 'cd'
|
---|
| 147 | *
|
---|
| 148 | * const e = Identity.of(['e']);
|
---|
| 149 | * const f = Identity.of(['f']);
|
---|
| 150 | * e.concat(f); //=> ['e', 'f']
|
---|
| 151 | */
|
---|
| 152 | }, {
|
---|
| 153 | key: _fl$concat,
|
---|
| 154 | value: function value(semigroup) {
|
---|
| 155 | return semigroupTrait[fl.concat].call(this, semigroup);
|
---|
| 156 | }
|
---|
| 157 | }, {
|
---|
| 158 | key: "concat",
|
---|
| 159 | value: function concat(semigroup) {
|
---|
| 160 | return this[fl.concat](semigroup);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#chain|Chain} specification.
|
---|
| 165 | *
|
---|
| 166 | * @sig chain :: Chain m => m a ~> (a -> m b) -> m b
|
---|
| 167 | * @param {Function} fn Function returning the value of the same {@link https://github.com/fantasyland/fantasy-land#semigroup|Chain}
|
---|
| 168 | * @return {RA.Identity}
|
---|
| 169 | * @example
|
---|
| 170 | *
|
---|
| 171 | * const a = Identity.of(1);
|
---|
| 172 | * const fn = val => Identity.of(val + 1);
|
---|
| 173 | *
|
---|
| 174 | * a.chain(fn).chain(fn); //=> Identity(3)
|
---|
| 175 | */
|
---|
| 176 | }, {
|
---|
| 177 | key: _fl$chain,
|
---|
| 178 | value: function value(fn) {
|
---|
| 179 | return chainTrait[fl.chain].call(this, fn);
|
---|
| 180 | }
|
---|
| 181 | }, {
|
---|
| 182 | key: "chain",
|
---|
| 183 | value: function chain(fn) {
|
---|
| 184 | return this[fl.chain](fn);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#ord|Ord} specification.
|
---|
| 189 | *
|
---|
| 190 | * @sig lte :: Ord a => a ~> a -> Boolean
|
---|
| 191 | * @param {RA.Identity} ord
|
---|
| 192 | * @return {boolean}
|
---|
| 193 | * @example
|
---|
| 194 | *
|
---|
| 195 | * const a = Identity.of(1);
|
---|
| 196 | * const b = Identity.of(1);
|
---|
| 197 | * const c = Identity.of(2);
|
---|
| 198 | *
|
---|
| 199 | * a.lte(b); //=> true
|
---|
| 200 | * a.lte(c); //=> true
|
---|
| 201 | * c.lte(a); //=> false
|
---|
| 202 | */
|
---|
| 203 | }, {
|
---|
| 204 | key: _fl$lte,
|
---|
| 205 | value: function value(ord) {
|
---|
| 206 | return ordTrait[fl.lte].call(this, ord);
|
---|
| 207 | }
|
---|
| 208 | }, {
|
---|
| 209 | key: "lte",
|
---|
| 210 | value: function lte(ord) {
|
---|
| 211 | return this[fl.lte](ord);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#monoid|Monoid*} specification.
|
---|
| 216 | * Partial implementation of Monoid specification. `empty` method on instance only, returning
|
---|
| 217 | * identity value of the wrapped type. Using `R.empty` under the hood.
|
---|
| 218 | *
|
---|
| 219 | *
|
---|
| 220 | * @sig empty :: Monoid m => () -> m
|
---|
| 221 | * @return {RA.Identity}
|
---|
| 222 | * @example
|
---|
| 223 | *
|
---|
| 224 | * const a = Identity.of('test');
|
---|
| 225 | * const i = a.empty();
|
---|
| 226 | *
|
---|
| 227 | * a.concat(i); //=> Identity('string');
|
---|
| 228 | * i.concat(a); //=> Identity('string');
|
---|
| 229 | */
|
---|
| 230 | }, {
|
---|
| 231 | key: _fl$empty,
|
---|
| 232 | value: function value() {
|
---|
| 233 | return this.constructor.of(emptyR(this.value));
|
---|
| 234 | }
|
---|
| 235 | }, {
|
---|
| 236 | key: "empty",
|
---|
| 237 | value: function empty() {
|
---|
| 238 | return this[fl.empty]();
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
| 242 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#contravariant|Contravariant} specification.
|
---|
| 243 | *
|
---|
| 244 | * @sig contramap :: Contravariant f => f a ~> (b -> a) -> f b
|
---|
| 245 | * @param {Function} fn
|
---|
| 246 | * @return {RA.Identity}
|
---|
| 247 | * @example
|
---|
| 248 | *
|
---|
| 249 | * const identity = a => a;
|
---|
| 250 | * const add1 = a => a + 1;
|
---|
| 251 | * const divide2 = a => a / 2;
|
---|
| 252 | *
|
---|
| 253 | * Identity.of(divide2).contramap(add1).get()(3); //=> 2
|
---|
| 254 | * Identity.of(identity).contramap(divide2).contramap(add1).get()(3); //=> 2
|
---|
| 255 | * Identity.of(identity).contramap(a => divide2(add1(a))).get()(3); //=> 2
|
---|
| 256 | */
|
---|
| 257 | }, {
|
---|
| 258 | key: _fl$contramap,
|
---|
| 259 | value: function value(fn) {
|
---|
| 260 | var _this = this;
|
---|
| 261 | return this.constructor.of(function (value) {
|
---|
| 262 | return _this.value(fn(value));
|
---|
| 263 | });
|
---|
| 264 | }
|
---|
| 265 | }, {
|
---|
| 266 | key: "contramap",
|
---|
| 267 | value: function contramap(fn) {
|
---|
| 268 | return this[fl.contramap](fn);
|
---|
| 269 | }
|
---|
| 270 | }], [{
|
---|
| 271 | key: _fl$of,
|
---|
| 272 | value:
|
---|
| 273 | /**
|
---|
| 274 | * Fantasy land {@link https://github.com/fantasyland/fantasy-land#applicative|Applicative} specification.
|
---|
| 275 | *
|
---|
| 276 | * @static
|
---|
| 277 | * @sig of :: Applicative f => a -> f a
|
---|
| 278 | * @param {*} value
|
---|
| 279 | * @returns {RA.Identity}
|
---|
| 280 | * @example
|
---|
| 281 | *
|
---|
| 282 | * const a = Identity.of(1); //=> Identity(1)
|
---|
| 283 | */
|
---|
| 284 | function value(_value) {
|
---|
| 285 | return new Identity(_value);
|
---|
| 286 | }
|
---|
| 287 | }, {
|
---|
| 288 | key: "of",
|
---|
| 289 | value: function of(value) {
|
---|
| 290 | return new Identity(value);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /**
|
---|
| 294 | * @static
|
---|
| 295 | */
|
---|
| 296 | }, {
|
---|
| 297 | key: '@@type',
|
---|
| 298 | get: function get() {
|
---|
| 299 | return 'RA/Identity';
|
---|
| 300 | }
|
---|
| 301 | }]);
|
---|
| 302 | return Identity;
|
---|
| 303 | }(fl.of, fl.ap, fl.map, fl.equals, fl.concat, fl.chain, fl.lte, fl.empty, fl.contramap);
|
---|
| 304 | export default Identity; |
---|