all files / lib/ maybe.js

94.7% Statements 143/151
86.49% Branches 32/37
88.89% Functions 48/54
94.48% Lines 137/145
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212  15×     10×         11×         26×   17×                                             19×                                         28× 28× 28×                     11×   21×                    
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) Eif (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var either_1 = require("./either");
var Maybe = (function () {
    function Maybe() {
    }
    Maybe.prototype.isDefined = function () {
        return false;
    };
    Maybe.prototype.isEmpty = function () {
        return false;
    };
    Maybe.prototype.toEither = function () {
        return either_1.Either.left(new ReferenceError("This either is Left."));
    };
    Maybe.prototype.equals = function (other) {
        if (!other || other instanceof Maybe === false)
            return false;
        if (this === other)
            return true;
        if (this.isDefined() === false && other.isDefined() === false)
            return true;
        return this.isDefined() === other.isDefined() && this.get() === other.get();
    };
    Maybe.prototype.toJSON = function () {
        return this.toObject();
    };
    Maybe.prototype.toString = function () {
        return JSON.stringify(this.toJSON());
    };
    return Maybe;
}());
exports.Maybe = Maybe;
(function (Maybe) {
    function just(value) {
        return new Just(value);
    }
    Maybe.just = just;
    function none() {
        return new None();
    }
    Maybe.none = none;
    function nothing() {
        return nothingMaybe;
    }
    Maybe.nothing = nothing;
    function fromNull(value) {
        if (value === null || value === undefined) {
            return Maybe.none();
        }
        else {
            return Maybe.just(value);
        }
    }
    Maybe.fromNull = fromNull;
    function sequence() {
        var maybes = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            maybes[_i] = arguments[_i];
        }
        var arr = [];
        for (var i in maybes) {
            Iif (maybes[i].isEmpty()) {
                return new None();
            }
            arr[i] = maybes[i].get();
        }
        return new Just(arr);
    }
    Maybe.sequence = sequence;
    function traverse(f) {
        return function (as) {
            var arr = [];
            for (var i in as) {
                var r = f(as[i]);
                Iif (r.isEmpty()) {
                    return new None();
                }
                arr[i] = r.get();
            }
            return new Just(arr);
        };
    }
    Maybe.traverse = traverse;
    function lift(partialFunction) {
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            try {
                return Maybe.just(partialFunction.apply(partialFunction, args));
            }
            catch (err) {
                return Maybe.none();
            }
        };
    }
    Maybe.lift = lift;
    var None = (function (_super) {
        __extends(None, _super);
        function None() {
            return _super.call(this) || this;
        }
        None.prototype.map = function (f) {
            return new None();
        };
        None.prototype.fmap = function (f) {
            return new None();
        };
        None.prototype.applies = function (f) {
            return function (mb) {
                return new None();
            };
        };
        None.prototype.mbind = function (f) {
            return new None();
        };
        None.prototype.flatten = function () {
            return this;
        };
        None.prototype.isEmpty = function () {
            return true;
        };
        None.prototype.get = function () {
            throw new ReferenceError("This is option is None.");
        };
        None.prototype.getOrElse = function (f) {
            return f();
        };
        None.prototype.getOrElseGet = function (value) {
            return value;
        };
        None.prototype.getOrThrow = function (err) {
            throw err || new ReferenceError("This option is None.");
        };
        None.prototype.orElse = function (f) {
            return f();
        };
        None.prototype.toEither = function () {
            return either_1.Either.left(new ReferenceError("This either is Left."));
        };
        None.prototype.toObject = function () {
            return { just: null };
        };
        return None;
    }(Maybe));
    Maybe.None = None;
    var Just = (function (_super) {
        __extends(Just, _super);
        function Just(value) {
            var _this = _super.call(this) || this;
            _this.value = value;
            return _this;
        }
        Just.prototype.map = function (f) {
            return new Just(f(this.value));
        };
        Just.prototype.fmap = function (f) {
            return f(this.value);
        };
        Just.prototype.applies = function (f) {
            var _this = this;
            return function (mb) { return mb.fmap(f(_this.value)); };
        };
        Just.prototype.mbind = function (f) {
            return this.applies(function (a) { return function (b) { return b(a); }; })(f);
        };
        Just.prototype.flatten = function () {
            var val = this.get();
            if (val instanceof Maybe) {
                return val.flatten();
            }
            else {
                return this;
            }
        };
        Just.prototype.isDefined = function () {
            return true;
        };
        Just.prototype.get = function () {
            return this.value;
        };
        Just.prototype.getOrElse = function (value) {
            return this.value;
        };
        Just.prototype.getOrElseGet = function (value) {
            return this.value;
        };
        Just.prototype.getOrThrow = function (err) {
            return this.value;
        };
        Just.prototype.orElse = function (o) {
            return this;
        };
        Just.prototype.toEither = function () {
            return either_1.Either.right(this.value);
        };
        Just.prototype.toObject = function () {
            return { just: this.value };
        };
        return Just;
    }(Maybe));
    Maybe.Just = Just;
    var nothingMaybe = new None();
})(Maybe = exports.Maybe || (exports.Maybe = {}));
exports.Maybe = Maybe;
//# sourceMappingURL=maybe.js.map