Skip to content

pedromsilvapt/data-optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple Java8-inspired monadic class to represent optional values.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published