optionize
v0.0.7
Published
Optional values for JavaScript
Downloads
10
Readme
optionize
:sparkles: Optional values for JavaScript
Why?
Because null is evil for a variety of reasons and other languages, such as Scala,
address this issue through a simple Option
type, where any boxed value is either Some
or None
. This article covers the generic behaviors of Some
and None
using the Scala API.
How?
optionize
simulates this behavior in JavaScript with an extremely simple and idiomatic API:
get
: returns the value without consideration ofSome
/None
getOrElse
: returns the value if it isSome
, otherwise returns the provided fallbackmap
: maps the provided function against the value if it isSome
forEach
: applies the provided function to the value if it isSome
That's it!
Example
By declaring values as either Some
or None
, you no longer need toconditionally access your data depending on if a value is present or not and can work with a consistent interface:
import { none, some } from 'optionize'
function badFoo (bar) {
if (bar) {
return bar.baz
}
return {}
}
function goodFoo (bar) {
return bar.getOrElse({}).baz
}
badFoo({ baz: 'boo' }) // returns 'boo'
badFoo(null) // returns {}
goodFoo(some({ baz: 'win' })) // returns 'win'
goodFoo(none) // returns {}
Downsides
Nothing is perfect and everything comes with tradeoffs. Here's what isn't great about using Some
and None
:
- Doesn't play with truthy/falsy, so you can't utilize
||
or&&
- Due to JavaScript's limited pattern matching abilities, you still end up using
if
or ternary conditions to wrap a value as eitherSome
orNone
(this can probably be improved with a simple implicit method, TODO)
Install
npm install optionize
Contributing
If you are interested in contributing, simply open up a well-justified PR or email me at [email protected].
License
MIT