all files / lib/data/ try.js

100% Statements 51/51
100% Branches 16/16
100% Functions 15/15
100% Lines 51/51
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                      432408× 432408×     392346×     40062×     578406×                         156224× 116179× 116179×       40045×                   125393×                 156221× 116177×   40044×       140538× 140424× 100501×   39923×       114×              
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
module.exports = (function () {
 
    'use strict';
 
    function Try(value,error) {
        this.value = value;
        this.error = error;
    }
 
    function success(value) {
        return new Try(value, null);
    }
 
    function failure(error) {
        return new Try(null, error);
    }
 
    Try.prototype.isSuccess = function () {
        return this.error === null;
    };
 
    Try.prototype.isFailure = function () {
        return !this.isSuccess();
    };
    
    Try.prototype.onSuccess = function (bindCall) {
        if (this.isSuccess()) {
            bindCall(this.value);
        }
         
        return this;
    };
 
    Try.prototype.onFailure = function (bindCall) {
        if (this.isFailure()) {
            bindCall(this.error);
        }
         
        return this;
    };
 
    Try.prototype.map = function (bindCall) {
        if (this.isSuccess()) {
            try {
                return success(bindCall(this.value));
            } catch (e) {
                return failure(e);
            }
        } else {
            return this;
        }
    };
 
    Try.prototype.flatmap = function (bindCall) {
        if (this.isSuccess()) {
            try {
                return bindCall(this.value);
            } catch (e) {
                return failure(e);
            }
        } else {
            return this;
        }
    };
 
    Try.prototype.success = function () {
        return this.value;
    };
 
    Try.prototype.failure = function () {
        return this.error;
    };
 
    Try.prototype.recoverWith = function (value) {
        if (this.isSuccess()) {
            return this.value;
        } else {
            return value;
        }
    };
 
    Try.prototype.lazyRecoverWith = function (value) {
        if (this.isSuccess()) {
            return this.value;
        } else {
            return value(this.error);
        }
    };
 
    Try.prototype.filter = function (f) {
        if (this.isSuccess()) {
            if (f(this.value)) {
                return this;
            } else {
                return failure(new Error("invalid filter"));                
            }
        }
        
        return this;
    };
 
    return {
        success : success,
        failure : failure
    };
}());