data-optional
v0.0.2
Published
Simple Java8-inspired monadic class to represent optional values
Downloads
5,073
Maintainers
Readme
Optional
Simple Java8-inspired monadic class to represent optional values
Installation
npm install --save data-optional
Usage
import Optional from 'data-optional';
const empty = Optional.empty();
const value = Optional.of( 20 );
empty.isPresent(); // false
value.isPresent(); // true
You can also convert nullable types into optionals.
import { Optional } from 'data-optional';
const empty = Optional.ofNullable( null );
const value = Optional.ofNullable( 1 );
empty.isPresent(); // false
value.isPresent(); // true
There are also a number of computations that can be done functionally with this module.
import { Optional } from 'data-optional';
value = Optional.ofNullable( 1 );
value = value.map( n => n * 3 ); // Optional.of( 3 )
value = value.flatMap( n => n % 2 === 0 ? Optional.of( n ) : Optional.empty() ); // Optional.empty()
value.orElse( 0 ); // 0