1.0.5 • Published 8 years ago
@t0bst4r/optional v1.0.5
@t0bst4r/optional
Add java-like optional functionality to your application.
Installation and Usage
Installation via npm
npm install @t0bst4r/optionalUsage
Import the package to your project files:
import { Optional } from "@t0bst4r/optional";Create an optional
To create a new optional use Optional.Of(yourValue) or Optional.OfNullable(yourValue).
You could also create an empty optional with Optional.Empty().
Methods
mapyour optional to an other typed value- check if the optional
isPresent() get()the value of the optional - throws an exception if the optional is not present- if the optional is Empty
- get a fallback value with
orElse(fallbackValue) - get a fallback value using a supplier with
orElseGet(fallbackSupplier) - get another optional with
or(anotherFallbackOptional) - get another optional using a supplier with
orGet(fallbackSupplier)
- get a fallback value with
- execute a piece of code if the optional is present with
ifPresent(callback)