opt-in
Optional: Utility class for differentiating null, undefined and present values.
Example
Basic Usage
var something = 5;something; // returns truesomething; // returns 5 var nothing = null;nothing; // returns falsenothing; // throws TypeError
Safe Defaults
let user = ;let userName = Optional;console;
Simple Control Flow
{ Optional ;}
API
- Optional
- new Optional([value])
- instance
- .get() ⇒
T
- .orElse([value]) ⇒
T
- .isPresent() ⇒
boolean
- .ifPresent(fn) ⇒
Optional.<T>
- .isEmpty() ⇒
boolean
- .ifEmpty(fn) ⇒
Optional.<T>
- .map(fn) ⇒
Optional.<U>
|Optional.<T>
- .flatMap(fn) ⇒
Optional.<U>
|Optional.<T>
- .filter(fn) ⇒
Optional.<T>
- .equals(other) ⇒
boolean
- .strictEquals(other) ⇒
boolean
- .get() ⇒
- static
- .of([value]) ⇒
Optional.<T>
- .empty() ⇒
Optional.<null>
- .isOptional(optional) ⇒
boolean
- .isPresent(value) ⇒
boolean
- .isEmpty(value) ⇒
boolean
- .of([value]) ⇒
- inner
- ~ifPresentCallback ⇒
undefined
- ~EmptyCallback ⇒
undefined
- ~MapCallback ⇒
U
- ~FilterCallback ⇒
boolean
- ~ifPresentCallback ⇒
new Optional([value])
Create an Optional.
Param | Type | Description |
---|---|---|
[value] | T |
value to box Optional with |
T
optional.get() ⇒ Returns present value or throws when empty.
Kind: instance method of Optional
Throws:
TypeError
T
optional.orElse([value]) ⇒ Returns present value or passed value when empty.
Kind: instance method of Optional
Param | Type | Description |
---|---|---|
[value] | T |
value to return if Optional is empty |
boolean
optional.isPresent() ⇒ Returns whether the Optional has a value present.
Kind: instance method of Optional
Optional.<T>
optional.ifPresent(fn) ⇒ Calls supplied function when one is present.
Kind: instance method of Optional
Param | Type |
---|---|
fn | ifPresentCallback |
boolean
optional.isEmpty() ⇒ Returns whether the Optional is empty.
Kind: instance method of Optional
Optional.<T>
optional.ifEmpty(fn) ⇒ Calls supplied function if Optional is empty.
Kind: instance method of Optional
Param | Type |
---|---|
fn | EmptyCallback |
Optional.<U>
| Optional.<T>
optional.map(fn) ⇒ Calls supplied function when value is present and returns an Optional of the function's result. Otherwise returns an empty Optional.
Kind: instance method of Optional
Template: U
Param | Type |
---|---|
fn | MapCallback |
Optional.<U>
| Optional.<T>
optional.flatMap(fn) ⇒ Calls supplied function when value is present and returns an Optional of the function's result. Unlike map, if supplied function returns an Optional, then it is not boxed. Otherwise returns an empty Optional.
Kind: instance method of Optional
Template: U
Param | Type |
---|---|
fn | MapCallback |
Optional.<T>
optional.filter(fn) ⇒ Returns an Optional of present value when passed function returns a truthy value. Otherwise returns an empty Optional.
Kind: instance method of Optional
Param | Type |
---|---|
fn | FilterCallback |
boolean
optional.equals(other) ⇒ Returns result of ==
comparison with unboxed value and (unboxed if Optional) argument.
Kind: instance method of Optional
Param | Type | Description |
---|---|---|
other | * |
value to compare Optional's unboxed value with |
boolean
optional.strictEquals(other) ⇒ Returns result of ===
comparison with unboxed value and (unboxed if Optional) argument.
Kind: instance method of Optional
Param | Type | Description |
---|---|---|
other | * |
value to compare Optional's unboxed value with |
Optional.<T>
Optional.of([value]) ⇒ Convenience initializer; returns an Optional of value.
Kind: static method of Optional
Template: T
Param | Type |
---|---|
[value] | T |
Optional.<null>
Optional.empty() ⇒ Convenience initializer; returns an empty Optional.
Kind: static method of Optional
boolean
Optional.isOptional(optional) ⇒ Returns whether the argument is an instance of Optional.
Kind: static method of Optional
Param | Type | Description |
---|---|---|
optional | * |
variable to test |
boolean
Optional.isPresent(value) ⇒ Returns whether value is present (not null and not undefined).
This is simply typeof value !== "undefined" && value !== null
.
Kind: static method of Optional
Param | Type |
---|---|
value | * |
boolean
Optional.isEmpty(value) ⇒ Returns whether value is empty (null or undefined).
This is simply typeof value !== "undefined" && value !== null
.
Kind: static method of Optional
Param | Type |
---|---|
value | * |
undefined
Optional~ifPresentCallback ⇒ Kind: inner typedef of Optional
Param | Type | Description |
---|---|---|
value | T |
present value |
undefined
Optional~EmptyCallback ⇒ Kind: inner typedef of Optional
U
Optional~MapCallback ⇒ Kind: inner typedef of Optional
Template: U
Param | Type |
---|---|
value | T |
boolean
Optional~FilterCallback ⇒ Kind: inner typedef of Optional
Type | Description |
---|---|
T |
the present value |