Fat Arrow is a library for Typed Functional Programming in TypeScript compatible with Node.js and all major browsers.
npm install fat-arrow-ts
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'
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!
SOON...