Optional: Utility class for differentiating null, undefined and present values.
var something = new Optional(5);
something.isPresent(); // returns true
something.get(); // returns 5
var nothing = new Optional(null);
nothing.isPresent(); // returns false
nothing.get(); // throws TypeError
let user = getUser();
let userName = Optional.of(user.name).orElse("Anonymous User");
console.log(`Hello ${userName}`);
function handleRequest(request, response) {
Optional.of(request.headers["Authorization"])
.ifEmpty(() => {
response.statusCode = 401;
response.end("You need an Authorization header to access this."); })
.map(getUserForAuthToken)
.ifPresent((user) => {
response.statusCode = 200;
response.end("Thanks friend!"); })
.ifEmpty(() => {
response.statusCode(500);
response.end("We couldn't find a user behind that token."); });
}
- 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 ⇒
Create an Optional.
Param | Type | Description |
---|---|---|
[value] | T |
value to box Optional with |
Returns present value or throws when empty.
Kind: instance method of Optional
Throws:
TypeError
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 |
Returns whether the Optional has a value present.
Kind: instance method of Optional
Calls supplied function when one is present.
Kind: instance method of Optional
Param | Type |
---|---|
fn | ifPresentCallback |
Returns whether the Optional is empty.
Kind: instance method of Optional
Calls supplied function if Optional is empty.
Kind: instance method of Optional
Param | Type |
---|---|
fn | EmptyCallback |
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 |
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 |
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 |
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 |
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 |
Convenience initializer; returns an Optional of value.
Kind: static method of Optional
Template: T
Param | Type |
---|---|
[value] | T |
Convenience initializer; returns an empty Optional.
Kind: static method of Optional
Returns whether the argument is an instance of Optional.
Kind: static method of Optional
Param | Type | Description |
---|---|---|
optional | * |
variable to test |
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 | * |
Returns whether value is empty (null or undefined).
This is simply typeof value !== "undefined" && value !== null
.
Kind: static method of Optional
Param | Type |
---|---|
value | * |
Kind: inner typedef of Optional
Param | Type | Description |
---|---|---|
value | T |
present value |
Kind: inner typedef of Optional
Kind: inner typedef of Optional
Template: U
Param | Type |
---|---|
value | T |
Kind: inner typedef of Optional
Type | Description |
---|---|
T |
the present value |