Skip to content

chefomar/optionull

Repository files navigation

Optionull

Build Status

Basic Implementation for Optional/Nullables in Javascript

Table of Contents

Installation

npm install --save optionull

Optional Class

<!-- In the browser -->
<script src="https://unpkg.com/optionull"></script>
const Optional = require('optionull').Optional;
// or
const { Optional } = require('optionull');
// or
import { Optional } from 'optionull';

ofNullable(value)

Creates and returns a new optional with the value provided.

const opt = Optional.ofNullable('somevalue');

//this is basically an empty optional.
const nullOpt = Optional.ofNullable(null);

empty()

Creates and returns a new optional with empty value.

const emptyOpt = Optional.empty();

isDefined()

Returns true if the value is defined.

opt.isDefined(); // true
nullOpt.isDefined(); // also true

emptyOpt.isDefined(); // false
Optional.ofNullable(undefined).isDefined(); // false

isNotNull()

Returns true if the value is not null.

opt.isNotNull(); // true
nullOpt.isNotNull(); // false

emptyOpt.isNotNull(); // true
Optional.ofNullable(undefined).isNotNull(); // true

isPresent()

Returns true if the value defined and not null.

opt.isPresent(); // true
nullOpt.isPresent(); // false

emptyOpt.isPresent(); // false
Optional.ofNullable(undefined).isPresent(); // false

ifPresent(fn)

Calls the function fn if the value is present.

opt.ifPresent(val => console.log(val)); // prints 'somevalue' in the console

emptyOpt.ifPresent(val => console.log(val)); // nothing happens

ifPresentOrElse(fn, elseFn)

Calls the function fn if the value is present, otherwise calls the function elseFn.

opt.ifPresentOrElse(
    val => console.log('found'),
    () => console.log('not found')
); // prints 'found' in the console

emptyOpt.ifPresentOrElse(
    val => console.log('found'),
    () => console.log('not found')
); // prints 'not found' in the console

getOrElse(fnOrVal)

Returns the value if present. Otherwise returns the value of fnOrVal.

opt.getOrElse(() => 'someother value'); // returns 'somevalue'

emptyOpt.getOrElse('someother value'); // returns 'someother value'
emptyOpt.getOrElse(() => 'someother value'); // returns 'someother value'

get()

Returns the value of the optional. (Could be null or undefined)

opt.get(); // 'somevalue'
emptyOpt.get(); // undefined
nullOpt.get(); // null

map(fn)

If the value is present, applies the mapping function and returns its result wrapped in a new optional. Returns empty optional otherwise.

opt.map(val => val.length).get(); // 9
opt.get(); // 'somevalue' (note the original optional did not change)

emptyOpt.map(val => val.length).isPresent(); // false 

flatMap(fn)

If the value is present, applies the mapping function then if the function returns optional the optional is returned else the result is wrapped in a new optional and returned. Return empty optional if the value is not present.

opt.flatMap(val => val.length).get(); // 9
opt.flatMap(val => Optional.ofNullable(val.length)).get(); // 9

filter(fnOrVal)

Returns the value wrapped in a new optional if the value is preset and the value matches the parameter or the parameter function returns truthy value. Returns an empty optional if the value is not present or the parameter function returns a falsy value or the value did not match the paramter.

opt.filter('somevalue').get(); // somevalue
opt.filter(val => val === 'somevalue').get(); // somevalue
opt.filter('something else').isPresent(); // false
opt.filter(val => val.length === 1).isPresent(); //false