This is another library that mimicks standard Maybe monad known from many functional languages.
It it supplied with standard API such as: Maybe.of
, Maybe#map
, Maybe#flatMap
. For more info, look at the tests.
For documentation, look at the tests. All the cases are tested there along with TS typings.
There is just one interesting thing that might be more useful than other libraries out there. I wanted to be able to have this monad "as an input". This would be useful that I could write some safe transformation at one place:
function FilePickerComponent({
doSomethingWithTheFile,
}: {
doSomethingWithTheFile: (file: File) => void;
}) {
return (
<input
type="file"
onChange={Maybe.asInput((event: ChangeEvent<HTMLInputElement>) =>
Maybe.fromFirst(Array.from(event.target.files ?? [])).map(
doSomethingWithTheFile
)
)}
/>
);
}
This enables you to define the Maybe in a callback and you can even prepare this function before and memoize it.
npm i --save actual-maybe
yarn add actual-maybe
pnpm i --save actual-maybe
bun i --save actual maybe
Standard script:
npm run test --all
yarn test
bun run test
You are free to use this library propose any change in a pull request.
Cheers.