Package Exports
- javascript-either
- javascript-either/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (javascript-either) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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