1.0.1 • Published 4 years ago

@fernando.vasquez/either-prototype v1.0.1

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

Either abstraction module

Get Started

Load the module

// Loding the module
const Either = require("@fernando.vasquez/either-class");

Instances on Either, Right and Left

// Intance of Either
const either = new Either();

// Right object
const right = either.createRight("foo", { foo: "bar" }]);

// Left object
const left = either.createLeft("foo", { foo: "bar" }]);

Exmaple

const Either = require("@fernando.vasquez/either-class");
const either = new Either();

const prop = (key, object) =>
  key in object
    ? either.createRight(object[key])
    : either.createLeft(`Cannot read property '${key}'`);

const foobar = prop("foo", { foo: "bar" });
const bazbar = prop("baz", { foo: "bar" });

console.log(foobar.map(word => `${word}!`));                             // -> Right("bar!")
console.log(bazbar.map(word => `${word}!`));                             // -> Left("Cannot read property 'baz'")
foobar.runEither(error => console.error(new Error(error)), console.log); // -> foo
bazbar.runEither(error => console.error(new Error(error)), console.log); // -> Error: "Cannot read property 'baz'" at ...
console.log(prop("foo", { foo: "bar" }).getType());                      // -> 'Either'
console.log(prop("baz", { foo: "bar" }).getType());                      // -> 'Either'