all files / lib/ either.js

93.57% Statements 160/171
86.49% Branches 32/37
85.48% Functions 53/62
94.41% Lines 152/161
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229  13×     10× 26×       15× 14× 10×         32×   34×   13×                                   35× 35× 35×                               24×               37× 37× 37×                           22×                          
"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 maybe_1 = require("./maybe");
var option_1 = require("./option");
var Either = (function () {
    function Either() {
    }
    Either.prototype.isLeft = function () { return false; };
    Either.prototype.isRight = function () { return false; };
    Either.prototype.getLeft = function () { throw new ReferenceError("This either is Right."); };
    Either.prototype.getRight = function () { throw new ReferenceError("This either is Left."); };
    Either.prototype.toMaybe = function () {
        return maybe_1.Maybe.none();
    };
    Either.prototype.toOption = function () {
        return option_1.Option.none();
    };
    Either.prototype.equals = function (other) {
        if (this === other)
            return true;
        if (!other || other instanceof Either === false)
            return false;
        if (this.isRight() !== other.isRight())
            return false;
        if (this.isRight() && this.getRight() === other.getRight())
            return true;
        if (this.getLeft() && this.getLeft() === other.getLeft())
            return true;
        return false;
    };
    Either.prototype.toJSON = function () {
        return this.toObject();
    };
    Either.prototype.toString = function () {
        return JSON.stringify(this.toJSON());
    };
    return Either;
}());
exports.Either = Either;
(function (Either) {
    function left(left) {
        return new Left(left);
    }
    Either.left = left;
    function right(right) {
        return new Right(right);
    }
    Either.right = right;
    function nothing() {
        return nothingEither;
    }
    Either.nothing = nothing;
    function sequence() {
        var eithers = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            eithers[_i] = arguments[_i];
        }
        var arr = [];
        for (var i in eithers) {
            Iif (eithers[i].isLeft()) {
                return new Left(eithers[i].getLeft());
            }
            arr[i] = eithers[i].get();
        }
        return new Right(arr);
    }
    Either.sequence = sequence;
    function traverse(f) {
        return function (as) {
            var arr = [];
            for (var i in as) {
                var r = f(as[i]);
                Iif (r.isLeft()) {
                    return new Left(r.getLeft());
                }
                arr[i] = r.get();
            }
            return new Right(arr);
        };
    }
    Either.traverse = traverse;
    function lift(partialFunction) {
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            try {
                return Either.right(partialFunction.apply(partialFunction, args));
            }
            catch (err) {
                return Either.left(err);
            }
        };
    }
    Either.lift = lift;
    var Left = (function (_super) {
        __extends(Left, _super);
        function Left(left) {
            var _this = _super.call(this) || this;
            _this.left = left;
            return _this;
        }
        Left.prototype.map = function (f) {
            return new Left(this.left);
        };
        Left.prototype.fmap = function (f) {
            return new Left(this.left);
        };
        Left.prototype.applies = function (f) {
            return function (mb) {
                return new Left(this.left);
            };
        };
        Left.prototype.mbind = function (f) {
            return new Left(this.left);
        };
        Left.prototype.bimap = function (lf, rf) {
            return new Left(lf(this.left));
        };
        Left.prototype.cata = function (lf, rf) {
            return lf(this.left);
        };
        Left.prototype.flatten = function () {
            return this;
        };
        Left.prototype.isLeft = function () {
            return true;
        };
        Left.prototype.get = function () {
            throw new ReferenceError("This either is Left.");
        };
        Left.prototype.getLeft = function () {
            return this.left;
        };
        Left.prototype.getOrElse = function (f) {
            return f();
        };
        Left.prototype.getOrElseGet = function (right) {
            return right;
        };
        Left.prototype.getOrThrow = function (err) {
            throw err || new ReferenceError("This either is Left.");
        };
        Left.prototype.orElse = function (f) {
            return f();
        };
        Left.prototype.toObject = function () {
            return { left: this.left };
        };
        return Left;
    }(Either));
    Either.Left = Left;
    var Right = (function (_super) {
        __extends(Right, _super);
        function Right(right) {
            var _this = _super.call(this) || this;
            _this.right = right;
            return _this;
        }
        Right.prototype.map = function (f) {
            return new Right(f(this.right));
        };
        Right.prototype.fmap = function (f) {
            return f(this.right);
        };
        Right.prototype.applies = function (f) {
            var _this = this;
            return function (eb) { return eb.fmap(f(_this.right)); };
        };
        Right.prototype.mbind = function (f) {
            return this.applies(function (a) { return function (b) { return b(a); }; })(f);
        };
        Right.prototype.bimap = function (lf, rf) {
            return new Right(rf(this.right));
        };
        Right.prototype.cata = function (lf, rf) {
            return rf(this.right);
        };
        Right.prototype.flatten = function () {
            var val = this.get();
            if (val instanceof Either) {
                return val.flatten();
            }
            else {
                return this;
            }
        };
        Right.prototype.isRight = function () {
            return true;
        };
        Right.prototype.get = function () {
            return this.right;
        };
        Right.prototype.getRight = function () {
            return this.right;
        };
        Right.prototype.getOrElse = function (f) {
            return this.right;
        };
        Right.prototype.getOrElseGet = function (right) {
            return this.right;
        };
        Right.prototype.getOrThrow = function () {
            return this.right;
        };
        Right.prototype.orElse = function (f) {
            return this;
        };
        Right.prototype.toMaybe = function () {
            return maybe_1.Maybe.just(this.right);
        };
        Right.prototype.toOption = function () {
            return option_1.Option.some(this.right);
        };
        Right.prototype.toObject = function () {
            return { right: this.right };
        };
        return Right;
    }(Either));
    Either.Right = Right;
})(Either = exports.Either || (exports.Either = {}));
exports.Either = Either;
var nothingEither = new Either.Left(void (0));
//# sourceMappingURL=either.js.map