javascript-either
v1.0.3
Published
An implementation of Either pattern in javascript
Downloads
4
Maintainers
Readme
Either Implementation
English
Introduction
This implementation represents a type that can hold either a value of type L
(Left) or a value of type R
(Right). It is a common pattern in functional programming to handle computations that can result in two different types of outcomes, such as success or failure.
Usage
Creating an Either
instance
To create an Either
instance, you can use the static methods left
and right
:
const { Either } = require('./either');
// Create a Left value
const leftValue = Either.left('Error occurred');
// Create a Right value
const rightValue = Either.right(42);
Checking the type of the value
if (leftValue.isLeft) {
console.log('This is a Left value');
}
if (rightValue.isRight) {
console.log('This is a Right value');
}
Output
const left = leftValue.left(); // 'Error occurred'
const right = rightValue.right(); // 42