all files / lib/ option.js

98.97% Statements 96/97
88.89% Branches 24/27
97.22% Functions 35/36
98.94% Lines 93/94
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      14×           12×         11×   19×                 20×                   11× 11× 11×   11×                      
"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 Option = (function () {
    function Option() {
    }
    Option.prototype.isDefined = function () {
        return false;
    };
    Option.prototype.isEmpty = function () {
        return false;
    };
    Option.prototype.get = function () {
        throw new ReferenceError("This is option is None.");
    };
    Option.prototype.toEither = function () {
        return either_1.Either.left(new ReferenceError("This either is Left."));
    };
    Option.prototype.equals = function (other) {
        if (!other || other instanceof Option === 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();
    };
    Option.prototype.toJSON = function () {
        return this.toObject();
    };
    Option.prototype.toString = function () {
        return JSON.stringify(this.toJSON());
    };
    return Option;
}());
exports.Option = Option;
(function (Option) {
    function some(value) {
        return new Some(value);
    }
    Option.some = some;
    function none() {
        return new None();
    }
    Option.none = none;
    function nothing() {
        return nothingOption;
    }
    Option.nothing = nothing;
    function lift(partialFunction) {
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            try {
                return Option.some(partialFunction.apply(partialFunction, args));
            }
            catch (err) {
                return Option.none();
            }
        };
    }
    Option.lift = lift;
    var None = (function (_super) {
        __extends(None, _super);
        function None() {
            return _super.call(this) || this;
        }
        None.prototype.isEmpty = function () {
            return true;
        };
        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 { some: null };
        };
        return None;
    }(Option));
    Option.None = None;
    var Some = (function (_super) {
        __extends(Some, _super);
        function Some(value) {
            var _this = _super.call(this) || this;
            _this.value = value;
            return _this;
        }
        Some.prototype.isDefined = function () {
            return true;
        };
        Some.prototype.get = function () {
            return this.value;
        };
        Some.prototype.getOrElse = function (value) {
            return this.value;
        };
        Some.prototype.getOrElseGet = function (value) {
            return this.value;
        };
        Some.prototype.getOrThrow = function (err) {
            return this.value;
        };
        Some.prototype.orElse = function (o) {
            return this;
        };
        Some.prototype.toEither = function () {
            return either_1.Either.right(this.value);
        };
        Some.prototype.toObject = function () {
            return { some: this.value };
        };
        return Some;
    }(Option));
    Option.Some = Some;
    var nothingOption = new None();
})(Option = exports.Option || (exports.Option = {}));
exports.Option = Option;
//# sourceMappingURL=option.js.map