import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(10,2).map(n=>n*2).unwrapOr(0);console.log(result1);// 10constresult2=calculate(10,0).map(n=>n*2).unwrapOr(0);console.log(result2);// 0
mapLeft<M>(fn: (err: L) => M): Either<M, R>
Maps over the Left value. Does nothing for Right.
1 2 3 4 5 6 7 8 91011121314151617181920212223
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(10,2).map(n=>n*2).mapLeft(e=>console.log(`Error: ${e}`))// No printing here.unwrapOr(0);console.log(result1);// 10constresult2=calculate(10,0).map(n=>n*2).mapLeft(e=>console.log(`Error: ${e}`))// Prints "Error: Division by zero".unwrapOr(0);console.log(result2);// 0
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1).mapLeft(e=>console.log(`Error: ${e}`))// Not run.unwrapOr(0);console.log(result1);// 13constresult2=calculate(10,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1).mapLeft(e=>console.log(`Error: ${e}`))// Prints "Error: Result is too small".unwrapOr(0);console.log(result2);// 0
unwrapOr(defaultValue: R): R
Returns the value of Right, or the default value for Left.
1 2 3 4 5 6 7 8 9101112131415
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).unwrapOr(0);console.log(result1);// 6constresult2=calculate(10,0).unwrapOr(-1);console.log(result2);// -1
isRight(): boolean
Checks if the value is Right.
1 2 3 4 5 6 7 8 9101112131415
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).isRight();console.log(result1);// trueconstresult2=calculate(10,0).isRight();console.log(result2);// false
isLeft(): boolean
Checks if the value is Left.
1 2 3 4 5 6 7 8 9101112131415
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).isLeft();console.log(result1);// falseconstresult2=calculate(10,0).isLeft();console.log(result2);// true
match<T>(cases: { left: (left: L) => T; right: (right: R) => T }): T
Matches the value to execute either the left or right case.
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1).match({right:n=>n,left:e=>{console.log(`Error: ${e}`);// Not runreturn0;}});console.log(result1);// 13constresult2=calculate(10,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1).match({right:n=>n,left:e=>{console.log(`Error: ${e}`);// Prints "Error: Result is too small"return0;}});console.log(result2);// 0
equals(other: Either<L, R>): boolean
Compares this to another Either, returns false if the values inside are different.
1 2 3 4 5 6 7 8 9101112131415161718192021
import{Either,Left,Right}from"holo-fn/either";constcalculate=(a:number,b:number):Either<string,number>=>{if(b===0){returnnewLeft("Division by zero");}returnnewRight(a/b);};constresult1=calculate(12,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1);console.log(result1.equals(newRight(13)));// trueconstresult2=calculate(10,2).chain(n=>n>5?newRight(n*2):newLeft("Result is too small")).map(n=>n+1);console.log(result2.equals(newRight(0)));// false
Helpers
left<L, R = never>(value: L): Either<L, R>
Creates a Left value, representing an error or failure in an operation.
1234
import{left}from'holo-fn/either';consteitherValue=left<string,string>("Error");console.log(eitherValue.unwrapOr("No error"));// "No error"
right<L, R>(value: R): Either<L, R>
Creates a Right value, representing a success in an operation.
Wraps a potentially throwing function in an Either.
1 2 3 4 5 6 7 8 9101112131415161718192021
import{tryCatch}from'holo-fn/either';constinput='{"name": "John Doe"}'interfaceUser{name:string;}constconvertToJson=(obj:unknown):User=>{if(typeofobj==='object'&&obj!==null&&'name'inobj){returnobjasUser;}return{name:'anonymous'};}constparsed=tryCatch(()=>JSON.parse(input),e=>'Invalid JSON').map(convertToJson).unwrapOr({name:'anonymous 1'});console.log(parsed.name)// John Doe
Returns Right<R> if fn() succeeds
Returns Left<L> if it throws, using onError if provided