UNPKG

sussy-util

Version:
59 lines (58 loc) 2.48 kB
declare class Optional<T> { private static readonly EMPTY; private readonly value; constructor(value: T | undefined); /** * Retrieves the value if it's present, otherwise returns undefined. * @returns The value if present, otherwise undefined. */ get(): T | undefined; /** * Checks if the value is present. * @returns True if the value is present, false otherwise. */ isPresent(): boolean; /** * Retrieves the value if it's present, otherwise returns a specified default value. * @param other The default value to return if the value is absent. * @returns The value if present, otherwise the specified default value. */ orElse(other: T): T; /** * Maps the value to a new value if present, otherwise returns an empty Optional. * @param mapper A function to transform the value. * @returns An Optional containing the transformed value if present, otherwise an empty Optional. */ map<U>(mapper: (value: T) => U): Optional<U>; /** * Maps the value to a new Optional if present, otherwise returns an empty Optional. * Then applies a function to the value and flattens the result. * @param mapper A function to transform the value and return an Optional. * @returns An Optional containing the transformed value if present, otherwise an empty Optional. */ flatMap<U>(mapper: (value: T) => Optional<U>): Optional<U>; /** * Applies a function to the value if present, otherwise does nothing. * @param consumer A function to apply to the value if present. */ ifPresent(consumer: (value: T) => void): void; /** * Filters the value based on a predicate function. * @param predicate A function to test the value. * @returns An Optional containing the value if it passes the predicate, otherwise an empty Optional. */ filter(predicate: (value: T) => boolean): Optional<T>; /** * Returns an empty Optional. * @returns An empty Optional instance. */ static empty<T>(): Optional<T>; /** * Returns a new Optional with a value if it's present, otherwise an empty Optional. * @param value The value to wrap. * @returns An Optional containing the value if not undefined, otherwise an empty Optional. */ static of<T>(value: T | undefined): Optional<T>; static ofNullable<T>(value: T | undefined | null): Optional<T>; } export default Optional;