UNPKG

5.7 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.lift = exports.Nothing = exports.Just = exports.Maybe = void 0;
7
8var R = _interopRequireWildcard(require("ramda"));
9
10function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
11
12function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
13
14function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
15
16function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
17
18function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
19
20function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
21
22function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
23
24function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
26function _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); } }
27
28function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
29
30var Maybe =
31/*#__PURE__*/
32function () {
33 function Maybe(v) {
34 _classCallCheck(this, Maybe);
35
36 this._value = v;
37 }
38
39 _createClass(Maybe, [{
40 key: "isNothing",
41 get: function get() {
42 return false;
43 }
44 }, {
45 key: "isJust",
46 get: function get() {
47 return true;
48 }
49 }], [{
50 key: "just",
51 value: function just(v) {
52 return new Just(v);
53 }
54 }, {
55 key: "nothing",
56 value: function nothing() {
57 return new Nothing();
58 }
59 }, {
60 key: "fromNullable",
61 value: function fromNullable(v) {
62 return [null, undefined].some(function (_) {
63 return Object.is(v, _);
64 }) ? Maybe.nothing(v) : Maybe.just(v);
65 }
66 }, {
67 key: "of",
68 value: function of(v) {
69 return just(v);
70 }
71 }]);
72
73 return Maybe;
74}();
75
76exports.Maybe = Maybe;
77
78var Just =
79/*#__PURE__*/
80function (_Maybe) {
81 _inherits(Just, _Maybe);
82
83 function Just(v) {
84 _classCallCheck(this, Just);
85
86 return _possibleConstructorReturn(this, _getPrototypeOf(Just).call(this, v));
87 }
88
89 _createClass(Just, [{
90 key: "map",
91 value: function map(f) {
92 return Maybe.fromNullable(f(this._value));
93 }
94 }, {
95 key: "getOrElse",
96 value: function getOrElse() {
97 return this._value;
98 }
99 }, {
100 key: "filter",
101 value: function filter(f) {
102 return Maybe.fromNullable(f(this._value)) ? this._value : null;
103 }
104 }, {
105 key: "chain",
106 value: function chain(f) {
107 return f(this._value);
108 }
109 }, {
110 key: "toString",
111 value: function toString() {
112 return "Maybe.Just(".concat(this._value, ")");
113 }
114 }, {
115 key: "value",
116 get: function get() {
117 return this._value;
118 }
119 }]);
120
121 return Just;
122}(Maybe);
123
124exports.Just = Just;
125
126var Nothing =
127/*#__PURE__*/
128function (_Maybe2) {
129 _inherits(Nothing, _Maybe2);
130
131 function Nothing(v) {
132 _classCallCheck(this, Nothing);
133
134 return _possibleConstructorReturn(this, _getPrototypeOf(Nothing).call(this, v));
135 }
136
137 _createClass(Nothing, [{
138 key: "map",
139 value: function map(f) {
140 return this;
141 }
142 }, {
143 key: "getOrElse",
144 value: function getOrElse(v) {
145 return v;
146 }
147 }, {
148 key: "filter",
149 value: function filter(f) {
150 return null;
151 }
152 }, {
153 key: "chain",
154 value: function chain(f) {
155 return this;
156 }
157 }, {
158 key: "toString",
159 value: function toString() {
160 return "Maybe.Nothing";
161 }
162 }, {
163 key: "value",
164 get: function get() {
165 throw new TypeError("It's nothing. You can not get from it");
166 }
167 }, {
168 key: "isNothing",
169 get: function get() {
170 return true;
171 }
172 }, {
173 key: "isJust",
174 get: function get() {
175 return false;
176 }
177 }]);
178
179 return Nothing;
180}(Maybe);
181
182exports.Nothing = Nothing;
183var lift = R.curry(function (f, value) {
184 return Maybe.fromNullable(value).map(f);
185});
186exports.lift = lift;
\No newline at end of file