@fernando.vasquez/either-factory
v1.0.1
Published
Either abstraction implementation.
Downloads
4
Readme
Either abstraction module
Get Started
Load the module
// Loding the module
const { createLeft, createRight } = require("@fernando.vasquez/either-class");
Instances on Right and Left
// Right object
const right = createRight("foo", { foo: "bar" }]);
// Left object
const left = createLeft("foo", { foo: "bar" }]);
Exmaple
const { createLeft, createRight } = require("@fernando.vasquez/either-class");
const prop = (key, object) =>
key in object
? createRight(object[key])
: 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