ts-std-lib
Version:
A standard library for typescript
191 lines • 6.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Optional = exports.OptionalValueNotSetError = void 0;
const Equality_1 = require("./Equality");
const Equality_2 = require("./Equality");
const IInspectable_1 = require("./IInspectable");
const DefaultInspector_1 = require("./DefaultInspector");
const Type_1 = require("./Type");
/**
* Error for the optional values not set
*/
class OptionalValueNotSetError extends Error {
constructor() {
super('Value is null');
}
}
exports.OptionalValueNotSetError = OptionalValueNotSetError;
/**
* An object to wrap a nullable in an interface to avoid null exeptions.
*
* @see https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
* @see http://www.baeldung.com/java-optional
*/
class Optional {
constructor(value, equalityComparer, inspector) {
this._value = value === undefined ? null : value;
this._equalityComparer = equalityComparer || null;
this._inspector = inspector || null;
}
// tslint:enable:unified-signatures
[Equality_1.equals](other) {
if (!this.isValuePresent(this._value)) {
return !other.isPresent();
}
if (!other.isPresent()) {
return false;
}
const valueEqualityComparer = this._equalityComparer || other._equalityComparer || new Equality_2.DefaultEqualityComparer();
return valueEqualityComparer.equals(this._value, other.get());
}
[IInspectable_1.inspect](options) {
if (!this.isValuePresent(this._value)) {
return `<${Optional.name}> [empty]`;
}
const inspector = this._inspector || new DefaultInspector_1.DefaultInspector();
return `<${Optional.name}> ${inspector.inspect(this._value, options)}`;
}
/**
* Check if the value is null.
*
* @returns True if there is a value present, otherwise false.
*/
isPresent() {
return this.isValuePresent(this._value);
}
/**
* If a value is present, invoke the specified callback with the value, otherwise do nothing.
*
* @param callback The callback to invoke.
*/
ifPresent(callback) {
if (this.isValuePresent(this._value)) {
callback(this._value);
}
}
/**
* If a value is present, performs the given callback with the value, otherwise performs the given empty-based callback.
*
* @param callback The callback to be performed, if a value is present.
* @param emptyCallback The callback to be performed, if no value is present.
*/
ifPresentOrElse(callback, emptyCallback) {
if (this.isValuePresent(this._value)) {
return callback(this._value);
}
return emptyCallback();
}
/**
* If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
*
* @param supplier The supplying function that produces an Optional to be returned.
* @returns An Optional describing the value of this Optional, if a value is present, otherwise an Optional produced by the supplying function.
*/
or(supplier) {
if (this.isValuePresent(this._value)) {
return this;
}
return supplier();
}
/**
* Return the value if not null or a default.
*
* @param defaultValue The default value to return.
* @returns The value if present, otherwise return the default.
* @description The methods orElse and orElseGet are generally preferable to this method, as they return a substitute value if the value is absent, instead of throwing an exception.
*/
orElse(supplier) {
if (this.isValuePresent(this._value)) {
return this._value;
}
return supplier;
}
/**
* Return the value if not null or a default via a callback, to allow for lazy loading.
*
* @param callback The callback to invoke to get the default value to return.
* @returns The value if present, otherwise return the default.
*/
orElseGet(callback) {
if (this.isValuePresent(this._value)) {
return this._value;
}
return callback();
}
/**
* Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
*
* @param exceptionSupplier The callback to invoke to get the error to throw.
* @returns The value if present.
*/
orElseThrow(exceptionSupplier) {
if (this.isValuePresent(this._value)) {
return this._value;
}
throw exceptionSupplier();
}
/**
* Return the contained value, if present, otherwise return 'undefined'.
*
* @returns {Undefinable<T>}
*/
orUndefined() {
if (this.isValuePresent(this._value)) {
return this._value;
}
return undefined;
}
/**
* Return the contained value, if present, otherwise return 'null'.
*
* @returns {Undefinable<T>}
*/
orNull() {
if (this.isValuePresent(this._value)) {
return this._value;
}
return null;
}
/**
* If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
*
* @returns {T} The value if present.
*/
get() {
if (this.isValuePresent(this._value)) {
return this._value;
}
throw new OptionalValueNotSetError();
}
filter(predicate) {
if (this.isValuePresent(this._value)) {
if (predicate(this._value)) {
return this;
}
return new Optional(undefined, this._equalityComparer || undefined);
}
return new Optional(undefined, this._equalityComparer || undefined);
}
map(callbackfn, equalityComparer) {
if (this.isValuePresent(this._value)) {
const value = callbackfn(this._value);
return new Optional(value, equalityComparer);
}
return new Optional(undefined, equalityComparer);
}
chain(callbackfn, equalityComparer) {
if (this.isValuePresent(this._value)) {
const value = callbackfn(this._value);
if (Type_1.Type.isInstanceOf(Optional, value)) {
return value;
}
return new Optional(value, equalityComparer);
}
return new Optional(undefined, equalityComparer);
}
isValuePresent(value) {
return value !== null;
}
}
exports.Optional = Optional;
//# sourceMappingURL=Optional.js.map