Code coverage report for nodash/lib/Either.js

Statements: 100% (32 / 32)      Branches: 100% (12 / 12)      Functions: 100% (12 / 12)      Lines: 100% (31 / 31)      Ignored: none     

All files » nodash/lib/ » Either.js
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      1     5   5   1 10     1 29 14   15   5   1 29 14   15   5   5   5                         5 2 3 2   1         2 1   1       2 1   1       6       6       1          
/* vim: set et sw=2 ts=2: */
'use strict';
 
module.exports = [ 'idf', 'is', 'map', 'compose', 'filter',
  function (idf, is, map, compose, filter) {
 
  var Nodash = this;
 
  var fail = function() {};
 
  function Either() {
    fail();
  }
 
  function Left(value) {
    if (!(this instanceof Left)) {
      return new Left(value);
    }
    this.value = idf(value);
  }
  Left.prototype = new Either();
 
  function Right(value) {
    if (!(this instanceof Right)) {
      return new Right(value);
    }
    this.value = idf(value);
  }
  Right.prototype = new Either();
 
  fail = function() { throw new Error(); };
 
  return {
 
    Either: Either,
 
    Left: Left,
 
    Right: Right,
 
    isLeft: is(Left),
 
    isRight: is(Right),
 
    either: function (afun, bfun, value) {
      if (is(Left, value)) {
        return afun(value.value());
      } else if (is(Right, value)) {
        return bfun(value.value());
      } else {
        fail();
      }
    },
 
    fromLeft: function (thing) {
      if (is(Left, thing)) {
        return thing.value();
      }
      fail();
    },
 
    fromRight: function (thing) {
      if (is(Right, thing)) {
        return thing.value();
      }
      fail();
    },
 
    lefts: compose(map(function (x) {
      return x.value();
    }), filter(is(Left))),
 
    rights: compose(map(function (x) {
      return x.value();
    }), filter(is(Right))),
 
    partitionEithers: function (xs) {
      return Nodash.tuple(Nodash.lefts(xs), Nodash.rights(xs));
    }
    
  };
}];