Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/mandarinets/plugins/optional.ts>Optional

Mandarine.TS is a typescript, decorator-driven framework that allows you to create server-side applications. Mandarine.TS provides a range of built-in solutions such as Dependency Injection, Components, ORM and more. Under its umbrella, Mandarine.TS has 4 modules: Core, Data, Security and MVC, these modules will offer you the requirements to build a Mandarine-powered application.
Latest
class Optional
import { Optional } from "https://deno.land/x/mandarinets@v2.3.2/plugins/optional.ts";

The optional class is used to avoid repetitive validation with If's. If an object is present, you can verify this with Optional.isPresent() If An object is not present but you would like to get a default value, you can do this with Optional.orElseGet(defaultValue); This classs allows you to avoid null exceptions.

Constructors

new
Optional()

Properties

private
value: T | null

"Optionable" object.

Methods

get(): T

Gets the value of the Optional only if it is present. If value is not present, an exception will be thrown.

ifPresent(): boolean

Returns a boolean value of whether the optional value is present.

orElseGet(value: Optional<T> | T): T

Returns the value present in the Optional instance. if @param value is an Optional, it will invoke "get", otherwise it will return the whole value. If not value is present, it will return undefined.

orElseThrows(exception: Error)

Tries to get the value in Optional. If value is not present, it will throw an exception.

toString(): String

Static Properties

private
readonly
EMPTY: Optional<any>

Default empty optional

Static Methods

Returns a empty optional

of<T>(value: T): Optional<T>

Creates a new optional from a value which will be the optional's object.

ofNullable<T>(value: T): Optional<T>

Creates a Optional instance. if @param value is provided but it is either null or undefined, then it creates an empty instance of Optional.