Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 1.59 KB

README.md

File metadata and controls

67 lines (42 loc) · 1.59 KB

=> Fat Arrow · GitHub license npm version Tests

Fat Arrow is a library for Typed Functional Programming in TypeScript compatible with Node.js and all major browsers.

⚠️ Alpha release! API may change ⚠️

Installation

npm install fat-arrow-ts   

Setup Jest matchers

Be sure to have a reference to a setup file in your jest.config.ts

// In jest.config.ts

export default {
	setupFilesAfterEnv: [
	  './setupTests.ts'
	],
}

Include this in your setup file

// In setupTests.ts

import 'fat-arrow-ts/src/jest-matchers'

Quick start

import { left, right, Either } from 'fat-arrow-ts';  
  
const getDivision = (numerator: number, denominator: number): Either<Error, number> => {  
    if (denominator === 0) {  
        return left(new Error('Division by zero!'))  
    }  
  
    return right(numerator / denominator)
}
  
const addTwo = (number: number) => number + 2  
  
const print = (value: Either<Error, number>) =>  
    value.fold(  
        (error) => console.error('Doh!', error.message),  
        (result) => console.log(`Result is ${result}. Hooray!`)  
    )  
  
print(getDivision(10, 0).flatMap(addTwo)) // Doh! Division by zero!  
print(getDivision(10, 5).flatMap(addTwo)) // Result is 4. Hooray!  

Docs

SOON...