From b65790d8f4ce36ea0d940ecb926ff5ac90fcd05d Mon Sep 17 00:00:00 2001 From: Leon Davis Date: Sat, 26 Mar 2022 17:58:54 +0000 Subject: [PATCH] feat/chore: convert to interfaces... ...and remove examples. --- option.ts | 155 ++++-------------------------------------------------- result.ts | 148 +++------------------------------------------------ 2 files changed, 16 insertions(+), 287 deletions(-) diff --git a/option.ts b/option.ts index 8abfc6e..10fca76 100644 --- a/option.ts +++ b/option.ts @@ -1,30 +1,9 @@ -/** - * Represents a value that exists. - * - * ## Examples - * - * ```ts - * import { Some } from "./option.ts"; - * - * const num = Some(1); - * ``` - */ +/** Represents a value that exists. */ export function Some(value: T): Option { return new SomeImpl(value); } -/** - * Represents a value that does not exist. - * - * ## Examples - * - * ```ts - * import { None } from "./option.ts"; - * - * const unk = None(); - * const num = None(); - * ``` - */ +/** Represents a value that does not exist. */ export function None(): Option { return new NoneImpl(); } @@ -33,158 +12,44 @@ export function None(): Option { * Represents an optional value that either exists ({@linkcode Some}) or does * not exist ({@linkcode None}). */ -export type Option = { - /** - * Returns `true` if the `Option` is {@linkcode Some}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).isSome() === true; - * None().isSome() === false; - * ``` - */ +export interface Option { + /** Returns `true` if the `Option` is {@linkcode Some}. */ isSome(): boolean; - /** - * Returns `true` if the `Option` is {@linkcode None}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).isNone() === false; - * None().isNone() === true; - * ``` - */ + /** Returns `true` if the `Option` is {@linkcode None}. */ isNone(): boolean; /** * Returns `true` of the `Option` is a {@linkcode Some} containing `value`. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).contains(1) === true; - * None().contains(1) === false; - * ``` */ contains(value: U): boolean; /** * Returns the contained {@linkcode Some} value. * - * ## Throws - * * Throws if the `Option` is a {@linkcode None} with the provided `message`. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).expect("returns") === 1; - * None().expect("throws") === 1; - * ``` */ expect(message: string): T; /** * Returns the contained {@linkcode Some} value. * - * ## Throws - * * Throws if the `Option` is a {@linkcode None}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).unwrap() === 1; - * None().unwrap() === 1; // throws - * ``` */ unwrap(): T; - /** - * Returns a new `Option` where the value is mapped with `fn`. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * Some(1).map((v) => v + 1).contains(2) === true; - * None().map((v) => v + 1).contains(2) === false; - * ``` - */ + /** Returns a new `Option` where the value is mapped with `fn`. */ map(fn: ((_: T) => U)): Option; - /** - * Returns `other` if this and `other` are {@linkcode Some}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * const one = Some(1); - * const two = Some(2); - * const none = None(); - * - * one.and(two) === two; - * one.and(none).isNone() === true; - * none.and(two).isNone() === true; - * none.and(none).isNone() === true; - * ``` - */ + /** Returns `other` if this and `other` are {@linkcode Some}. */ and(other: Option): Option; - /** - * Returns this or `other` if either is {@linkcode Some}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * const one = Some(1); - * const two = Some(2); - * const none = None(); - * - * one.or(two) === one; - * one.or(none) === one; - * none.or(two) === two; - * none.or(none).isNone() === true; - * ``` - */ + /** Returns this or `other` if either is {@linkcode Some}. */ or(other: Option): Option; - /** - * Returns this or `other` if only one is {@linkcode Some}. - * - * ## Examples - * - * ```ts - * import { None, Some } from "./option.ts"; - * - * const one = Some(1); - * const two = Some(2); - * const none = None(); - * - * one.or(two).isNone === true; - * one.or(none) === one; - * none.or(two) === two; - * none.or(none).isNone() === true; - */ + /** Returns this or `other` if only one is {@linkcode Some}. */ xor(other: Option): Option; -}; +} class SomeImpl implements Option { #value: T; diff --git a/result.ts b/result.ts index 5cfa77c..7e7246c 100644 --- a/result.ts +++ b/result.ts @@ -34,203 +34,67 @@ export function Err(value: E): Result { * Represents a result value that is either successful ({@linkcode Ok}) or * erroneous ({@linkcode Err}). */ -export type Result = { - /** - * Returns `true` if the `Result` is {@linkcode Ok}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).isOk() === true; - * Err(1).isOk() === false; - * ``` - */ +export interface Result { + /** Returns `true` if the `Result` is {@linkcode Ok}. */ isOk(): boolean; - /** - * Returns `true` if the `Result` is {@linkcode Err}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).isErr() === false; - * Err(1).isErr() === true; - * ``` - */ + /** Returns `true` if the `Result` is {@linkcode Err}. */ isErr(): boolean; /** * Returns `true` of the `Result` is an {@linkcode Ok} containing `value`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).contains(1) === true; - * Err(1).contains(1) === false; - * ``` */ contains(value: U): boolean; /** * Returns `true` of the `Result` is an {@linkcode Err} containing `value`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).containsErr(1) === false; - * Err(1).containsErr(1) === true; - * ``` */ containsErr(value: F): boolean; /** * Returns the contained {@linkcode Ok} value. * - * ## Throws - * * Throws if the `Result` is an {@linkcode Err} with the provided `message`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).expect("returns") === 1; - * Err(1).expect("throws") === 1; - * ``` */ expect(message: string): T; /** * Returns the contained {@linkcode Err} value. * - * ## Throws - * * Throws if the `Result` is an {@linkcode Ok} with the provided `message`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).expect("throws") === 1; - * Err(1).expect("returns") === 1; - * ``` */ expectErr(message: string): E; /** * Returns the contained {@linkcode Ok} value. * - * ## Throws - * * Throws if the `Result` is an {@linkcode Err}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).unwrap() === 1; - * Err(1).unwrap() === 1; // throws - * ``` */ unwrap(): T; /** * Returns the contained {@linkcode Err} value. * - * ## Throws - * * Throws if the `Result` is an {@linkcode Ok}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).unwrap() === 1; // throws - * Err(1).unwrap() === 1; - * ``` */ unwrapErr(): E; /** * Returns a new `Result` where the {@linkcode Ok} value is mapped with `fn`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).map((v) => v + 1).contains(2) === true; - * Err(1).map((v) => v + 1).contains(2) === false; - * ``` */ map(fn: ((_: T) => U)): Result; /** * Returns a new `Result` where the {@linkcode Err} value is mapped with `fn`. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * Ok(1).mapErr((v) => v + 1).containsErr(2) === false; - * Err(1).mapErr((v) => v + 1).containsErr(2) === true; - * ``` */ mapErr(fn: ((_: E) => F)): Result; - /** - * Returns `other` if this and `other` are {@linkcode Ok}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * const one = Ok(1); - * const two = Ok(2); - * const err = Err(1); - * - * one.and(two) === two; - * one.and(err).isErr() === true; - * err.and(two).isErr() === true; - * err.and(err).isErr() === true; - * ``` - */ + /** Returns `other` if this and `other` are {@linkcode Ok}. */ and(other: Result): Result; - /** - * Returns this or `other` if either is {@linkcode Ok}. - * - * ## Examples - * - * ```ts - * import { Err, Ok } from "./result.ts"; - * - * const one = Ok(1); - * const two = Ok(2); - * const err = Err(1); - * - * one.or(two) === one; - * one.or(err) === one; - * err.or(two) === two; - * err.or(err).isErr() === true; - * ``` - */ + /** Returns this or `other` if either is {@linkcode Ok}. */ or(other: Result): Result; -}; +} class OkImpl implements Result { #value: T;