1.0.0 • Published 6 years ago

@ts-util/optional v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

Optional

Container object used to may or may not contain not-null objects. It will helps you with null related errors and exceptions.

Install

$ npm install @ts-util/optional

Usage

import { Optional } from 'optional';

function getOptionalValue(empty?: boolean): Optional<string> {
    return empty ? Optional.empty() : Optional.of('Hello, World');
}

// Optional with existing value
getOptionalValue().get();                    // returns value 'Hello, World'
getOptionalValue().isPresent();              // returns true
getOptionalValue().ifPresent((value) => {}); // calls your own function if optional value is present

// Optional with null value (can be created with Optional.empty() or Optional.ofNullable(null))
getOptionalValue(true).get();                                    // throws TypeError
getOptionalValue(true).isPresent();                              // returns false
getOptionalValue(true).isEmpty();                                // returns true
getOptionalValue(true).ifPresentOrElse((value) => {}, () => {}); // calls function from second argument