1 comment

[ 3.5 ms ] story [ 15.0 ms ] thread
Hi HN. I wrote an Option object for javascript and typescript.

Lots of functional programming libraries already exist, in addition to several standalone JS/TS option packages. So why another?

I wanted to avoid a large library that has lots of other things in them. And I found some of the existing standalone packages to have slightly different behavior. And it's fun to write you own.

Benefits of this package

- Excellent TypeScript support. I wanted to ensure the development experience with TS would be exceptional and tried out several ways of defining types to figure out which way works best.

- Zero production dependencies. Even the dev dependency list is extremely small.

- Better implementation of flatten method. Some packages will throw an error if flatten is called on Nones or other types. Mine handles these cases better and will never throw.

- Better implementation of contains method - this package lets users pass in an equality function for when an Option contains an object so you can pass in an accurate object comparison. By default this is === which is suitable for primitive values.

- Static (aka curried) versions of map and flatMap. This allows for converting functions that work on non option values to their equivalent that work on Optionals and allows you to reuse your existing code as much as possible.

- Great documentation throughout. I added lots of examples to the README (all of which are runnable in a TS environment), there are tests demonstrating usage, and lots of TSDocs that show (some methods also have examples that pop up) when you're writing code.

The implementation is also extremely simple and makes for a great example to follow along for anyone learning about functional programming while giving them an easy way to get running working code.

I hope you take a look and find this interesting or useful!