Maybe.ts
This library provides means for representing values that may or may not exist. This can come handy for representing optional arguments, error handling, and records with optional fields.
Usage
Library represents values that may or may not exist via Maybe<a>
type.
Existing value is repsented as Just<a>
and absense as Nothing
:
Given javascript / typescript semantics Nothing
is just a type alias for union
of primitives representing absence of value:
For the same reason Just<a>
is just a type alias for a
:
Chosen approach makes optional function argument & optional record fields of
type a
be fully compatible with Maybe<a>
. There for all of the functions
exposed are applicable.
This also avoids necessity to box existing values for the sake of typing.
Import
toValue(fallback:a, maybe:Maybe<a>):a
Provide a fallback
value, turning an optional value into a normal value.
Maybe.toValue5, Maybe.just9 // => 9Maybe.toValue6, Maybe.nothing // => 6
map(f:(input:a) => b, maybe:Maybe<a>):Maybe<b>
Transform a Maybe value with a given function:
Maybe.mapMath.sqrt, Maybe.just9 // => Maybe.just(3)Maybe.mapMath.sqrt, Maybe.nothing // => Maybe.nothing
chain(then:(input:a) => Maybe<b>, maybe:Maybe<a>):Maybe<b>
Utility to chain together two computations that may fail (return Nothing).
greetMaybe.nothing // => Maybe.nothinggreetMaybe.just'world' // => Maybe.just('Hello world!')
and(left:Maybe<a>, right:Maybe<b>):Maybe<b>
Returns Nothing
if the left Maybe is Nothing
, otherwise returns the
right Maybe.
Maybe.andMaybe.just2, Maybe.nothing // => Maybe.nothingMaybe.andMaybe.nothing, Maybe.just'foo' // => Maybe.nothingMaybe.andMaybe.just2, Maybe.just'foo' // => Maybe.just('foo')Maybe.andMaybe.nothing, Maybe.nothing // => Maybe.nothing
or(left:Maybe<a>, right:Maybe<a>):Maybe<a>
Returns the left Maybe if it is a Just value, otherwise returns right Maybe.
Maybe.orMaybe.just2, Maybe.nothing // => Maybe.just(2)Maybe.orMaybe.nothing, Maybe.just'foo' // => Maybe.just('foo')Maybe.orMaybe.just2, Maybe.just100 // => Maybe.just(100)Maybe.orMaybe.nothing, Maybe.nothing // => Maybe.nothing
isJust(maybe:Maybe<a>):maybe is Just<a>
Predicate that refines given Maybe. If returns true
given Maybe
is
refined to Just
, otherwise refined to Nothing
.
if isJustname
isNothing = <a> (maybe:Maybe<a>):maybe is Nothing
Predicate that refines given Maybe. If returns true
given Maybe
is
refined to Nothing
, otherwise refined to Just
if isNothingname else
just(value:a) => Maybe<a>
Turns arbitrary value
of type a
into Maybe<a>
. Not that in most cases you dont need to do this and value a
can be passed in place for Maybe<a>
. Still sometimes it's convinent to downcast Just<a>
(which is alias for a
) to Maybe<a>
and this function does just that:
Maybe.just4 // => 4
nothing:Maybe<any>
This is just an exported null
and you can use null
, or undefined
or null|undefined|void|a
in place for Maybe<a>
but sometimes code will read and communiacte intent a lot better when you do use explicit names.
Install
yarn
yarn add --save maybe.ts
npm
npm install --save maybe.ts