export type None = Readonly<{ isSome: false; }>; export type Some = Readonly<{ isSome: true; value: A; }>; /** * An optional value. If the value exists, it's of type `Some`, otherwise it's of type `None`. * * @example * * * ```ts * import { strictEqual } from "node:assert/strict"; * * import { option as O, pipe } from "@clipboard-health/util-ts"; * * function double(n: number) { * return n * 2; * } * * function inverse(n: number): O.Option { * return n === 0 ? O.none : O.some(1 / n); * } * * const result = pipe( * O.some(5), * O.map(double), * O.flatMap(inverse), * O.match( * () => "No result", * (n) => `Result is ${n}`, * ), * ); * * strictEqual(result, "Result is 0.1"); * ``` * * */ export type Option = None | Some; /** * Constructs an `Option` of `None`, representing a missing value. */ export declare const none: None; /** * Constructs an `Option` holding a `Some`, representing an optional value that exists. * * @param value - The value to wrap in a `Some` * @returns A `Some` containing the value */ export declare function some(value: A): Some; /** * Type guard that checks if an `Option` is `None`. * * @param option - The `Option` to check * @returns `true` if the `Option` is `None`, `false` if it is `Some` */ export declare function isNone(option: Option): option is None; /** * Type guard that checks if an `Option` is `Some`. * * @param option - The `Option` to check * @returns `true` if the `Option` is `Some`, `false` if it is `None` */ export declare function isSome(option: Option): option is Some; /** * Transforms the value inside an `Option` using the provided function. If the `Option` is * `Some(value)`, returns `Some(f(value))`. If the `Option` is `None`, returns `None`. * * @param f - The function to apply to the value if it exists * @returns A new `Option` containing the transformed value */ export declare function map(f: (a: A) => B): (option: Option) => Option; /** * Chains `Option` operations that return `Option`s. Unlike `map` which wraps the result in a new * `Option`, `flatMap` prevents nested `Option`s like `Some(Some(value))`. * * @param f - A function that returns an `Option` * @returns The `Option` returned by the function if the input is `Some`, `None` otherwise */ export declare function flatMap(f: (a: A) => Option): (option: Option) => Option; /** * Safely extracts the value from an `Option` with a fallback. Use this function when you need to * convert an `Option` to an `A`, providing a default value for the `None` case. * * @param defaultValue - The value to return if the `Option` is `None` * @returns The contained value if `Some`, `defaultValue` if `None` */ export declare function getOrElse(defaultValue: A): (option: Option) => A; /** * Pattern matches on an `Option`, handling both `Some` and `None` cases. * * @param onNone - Function to handle the `None` case * @param onSome - Function to handle the `Some` case * @returns The result of either `onNone` or `onSome` based on the `Option` state */ export declare function match(onNone: () => B, onSome: (value: A) => C): (option: Option) => B | C; /** * Converts a nullable value to an `Option`. If the value is `null` or `undefined`, returns `None`. * Otherwise, returns `Some(value)`. * * @param value - The value to convert * @returns An `Option` representing the nullable value */ export declare function fromNullable(value: A | null | undefined): Option;