maybe-optional
TypeScript library implementing Maybe type and Optional class for handling of null or undefined values
Installation
Using npm:
npm install maybe-optional
Using yarn:
yarn add maybe-optional
Usage
Maybe
Something
and Nothing
Both undefined
and null
are considered Nothing
. Anything else is considered Something
.
; ;isSomethingaNull;// => falseisNothingaNull;// => true ;isSomethinganUndefined;// => falseisNothinganUndefined;// => true ;isSomethingaNumber;// => trueisNothingaNumber;// => false ;isSomethingaString;// => trueisNothingaString;// => false ;isSomethinganObject;// => trueisNothinganObject;// => false
Maybe
The Maybe
type handles situations where it is possible for a value to be null
or undefined
.
; ;isSomethingmaybeSomething;// => trueisNothingmaybeSomething;// => false ;isSomethingmaybeNotSomething;// => falseisNothingmaybeNotSomething;// => true ;isSomethingmaybeNumber;// => trueisNothingmaybeNumber;// => false ;isSomethingmaybeString;// => trueisNothingmaybeString;// => false
ifSomething
Pass a value to a function if the value is Something
.
; ; ;ifSomethingmaybe, doSomething;// 'hello' ;ifSomethingmaybeNot, doSomething;//
orElse
Unwrap a Maybe
by returning its value, if it is Something
, or else a default value.
; // Maybe with a real value;;// => 'a value' // Maybe with an undefined value;;// => 'a default value'
orElseGet
Unwrap a Maybe
by returning its value, if it is Something
, or else returning the result of a function.
; ;;// 'hello' ;;// 'goodbye'
orElseThrow
Unwrap a Maybe
value if it is Something
or else throw an error.
; ;;// 'hello' ;;// throws MaybeNothingError
filter
Run a Maybe
through a conditional predicate. If the condition is true, return the same value still wrapped as a Maybe
.
If the condition is false, return null wrapped as a Maybe
.
; ;// => 1// => null
map
Apply a function to the Maybe
value if it is Something
. Otherwise, return null.
; ;// => 2 ;// => null
Optional
Optional
provides class-based functionality for handling situations where it is possible for a value to be null
or undefined
.
; // create empty optional;empty.isEmpty;// => trueempty.isPresent;// => false // create optional from value;opt1.isEmpty;// => falseopt1.isPresent;// => true // create optional from a Maybe;;opt2.isEmpty;// => trueopt2.isPresent;// => false
Optional.ifPresent
Pass the wrapped value to a function if the value is Something
.
; ; ;opt1.ifPresentdoSomething;// 'hello' ;opt2.ifPresentdoSomething;//
Optional.orElse
Unwrap an Optional
by returning its value, if it is Something
, or else a default value.
; // with Something;;// => 'a value' // with Nothing;;// => 'a default value'
Optional.orElseGet
Unwrap an Optional
by returning its value, if it is Something
, or else returning the result of a function.
; // with Something;;// 'hello' // with Nothing;;// 'goodbye'
Optional.orElseThrow
Unwrap an Optional
value if it is Something
or else throw an error.
; // with Something;;// 'hello' // with Nothing;;// throws OptionalEmptyError
Optional.filter
Run an Optional
's wrapped value through a conditional predicate.
If the condition is true, return the same value wrapped in a new Optional.
If the condition is false, return an empty Optional.
; ;// => Optional { maybe: 1 }// => Optional { maybe: null }
Optional.map
Apply a function to the wrapped Optional
value if it is Something
and return wrapped in new Optional.
Otherwise, return an empty Optional.
; ;// => Optional { maybe: 2 } ;// => Optional { maybe: null }
Chaining Optionals
; .mapval + 1 .filterval > 0;// => Optional { maybe: 2 }