From aa3123d64c944c92475c90b42420c1cf3ba8433d Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Sat, 27 Jul 2024 13:35:17 +0200 Subject: [PATCH] fmt --- src/option.ts | 193 ++++++++++++++-------------- src/result.ts | 349 +++++++++++++++++++++++++------------------------- 2 files changed, 268 insertions(+), 274 deletions(-) diff --git a/src/option.ts b/src/option.ts index 3701bc2..8f4599c 100644 --- a/src/option.ts +++ b/src/option.ts @@ -4,110 +4,107 @@ import {Result} from './result' const log = logger('option') export class Option { - // ***************************************************************************************************************** - // Constructors - private constructor( - private readonly data: T | null, - private readonly _state: State, - ) {} - public static some(data: T) : Option { - return new Option(data, State.Some) - } - public static none() : Option { - return new Option(null, State.None) + // ***************************************************************************************************************** + // Constructors + private constructor(private readonly data: T | null, private readonly _state: State) {} + public static some(data: T): Option { + return new Option(data, State.Some) + } + public static none(): Option { + return new Option(null, State.None) + } + + // ***************************************************************************************************************** + // some + public is_some(): boolean { + return this._state === State.Some + } + public is_some_and(f: (data: T) => boolean): boolean { + return this._state === State.Some && f(this.data as T) + } + public expect(msg: string): T { + log.trace('expect') + + if (this._state === State.Some) { + return this.data as T } - // ***************************************************************************************************************** - // some - public is_some() : boolean { - return this._state === State.Some - } - public is_some_and(f: (data: T) => boolean) : boolean { - return this._state === State.Some && f(this.data as T) - } - public expect(msg: string) : T { - log.trace("expect") + log.warn(msg) + throw new Error(msg) + } + public unwrap(): T { + log.trace('unwrap') + return this.expect('called unwrap on a None value') + } + public unwrap_or(default_: T): T { + if (this._state === State.Some) return this.data as T + return default_ + } + public unwrap_or_else(f: () => T): T { + if (this._state === State.Some) return this.data as T + return f() + } - if (this._state === State.Some) { - return this.data as T - } + // ***************************************************************************************************************** + // none + public is_none(): boolean { + return this._state === State.None + } - log.warn(msg) - throw new Error(msg) - } - public unwrap() : T { - log.trace("unwrap") - return this.expect("called unwrap on a None value") - } - public unwrap_or(default_: T) : T { - if (this._state === State.Some) return this.data as T - return default_ - } - public unwrap_or_else(f: () => T) : T { - if (this._state === State.Some) return this.data as T - return f() - } + // ***************************************************************************************************************** + // map + public map(f: (data: T) => U): Option { + if (this._state === State.Some) return Option.some(f(this.data as T)) + return Option.none() + } + public map_or(default_: U, f: (data: T) => U): U { + if (this._state === State.Some) return f(this.data as T) + return default_ + } + public map_or_else(default_: () => U, f: (data: T) => U): U { + if (this._state === State.Some) return f(this.data as T) + return default_() + } + public ok_or(err: E): Result { + if (this._state === State.Some) return Result.ok(this.data as T) + return Result.error(err) + } + public ok_or_else(err: () => E): Result { + if (this._state === State.Some) return Result.ok(this.data as T) + return Result.error(err()) + } + public and(optb: Option): Option { + if (this._state === State.Some) return optb + return Option.none() + } + public and_then(f: (data: T) => Option): Option { + if (this._state === State.Some) return f(this.data as T) + return Option.none() + } + public filter(f: (data: T) => boolean): Option { + if (this._state === State.Some && f(this.data as T)) return this + return Option.none() + } + public or(optb: Option): Option { + if (this._state === State.Some) return this + return optb + } + public or_else(f: () => Option): Option { + if (this._state === State.Some) return this + return f() + } + public xor(optb: Option): Option { + if (this._state === optb._state) return Option.none() + else if (this._state === State.Some) return this + return optb + } - // ***************************************************************************************************************** - // none - public is_none() : boolean { - return this._state === State.None - } - - // ***************************************************************************************************************** - // map - public map(f: (data: T) => U) : Option { - if (this._state === State.Some) return Option.some(f(this.data as T)) - return Option.none() - } - public map_or(default_: U, f: (data: T) => U) : U { - if (this._state === State.Some) return f(this.data as T) - return default_ - } - public map_or_else(default_: () => U, f: (data: T) => U) : U { - if (this._state === State.Some) return f(this.data as T) - return default_() - } - public ok_or(err: E) : Result { - if (this._state === State.Some) return Result.ok(this.data as T) - return Result.error(err) - } - public ok_or_else(err: () => E) : Result { - if (this._state === State.Some) return Result.ok(this.data as T) - return Result.error(err()) - } - public and(optb: Option) : Option { - if (this._state === State.Some) return optb - return Option.none() - } - public and_then(f: (data: T) => Option) : Option { - if (this._state === State.Some) return f(this.data as T) - return Option.none() - } - public filter(f: (data: T) => boolean) : Option { - if (this._state === State.Some && f(this.data as T)) return this - return Option.none() - } - public or(optb: Option) : Option { - if (this._state === State.Some) return this - return optb - } - public or_else(f: () => Option) : Option { - if (this._state === State.Some) return this - return f() - } - public xor(optb: Option) : Option { - if (this._state === optb._state) return Option.none() - else if (this._state === State.Some) return this - return optb - } - - public get state() : State { - return this._state - } + public get state(): State { + return this._state + } } export enum State { - Some, - None, + Some, + None } diff --git a/src/result.ts b/src/result.ts index 0dc798d..69b7e77 100644 --- a/src/result.ts +++ b/src/result.ts @@ -4,199 +4,196 @@ import {Option} from './option' const log = logger('result') export class Result { - public constructor( - protected readonly _data: T | E, - private readonly _state: State, - ) {} + public constructor(protected readonly _data: T | E, private readonly _state: State) {} - // Constructors - static ok(data: T) : Result { - log.trace('New Ok') - return new Result(data, State.OK) - } - static error(err: E) : Result { - log.trace('New Error') - log.debug(`Error : ${err}`) - return new Result(err, State.ERROR) - } - static fromObject(res: Result) : Result { - // @ts-expect-error - return new Result(res._data, res._state) - } - public as_error() : Result { - return Result.error(this.unwrap_err()) + // Constructors + static ok(data: T): Result { + log.trace('New Ok') + return new Result(data, State.OK) + } + static error(err: E): Result { + log.trace('New Error') + log.debug(`Error : ${err}`) + return new Result(err, State.ERROR) + } + static fromObject(res: Result): Result { + // @ts-expect-error + return new Result(res._data, res._state) + } + public as_error(): Result { + return Result.error(this.unwrap_err()) + } + + // Getters + public state(): State { + return this._state + } + + // TODO : implements Iterable<...> + + // ***************************************************************************************************************** + // Ok + public is_ok(): boolean { + return this._state === State.OK + } + public is_ok_and(f: (data: T) => boolean): boolean { + return this._state === State.OK && f(this._data as T) + } + public expect(msg: string): T { + log.trace('expect') + + if (this._state === State.ERROR) { + log.error('unwrapped an Error') + log.error(msg) + log.debug(`Error : ${this._data}`) + throw msg } - // Getters - public state() : State { - return this._state + return this._data as T + } + public unwrap(): T { + log.trace('unwrap') + + if (this._state === State.ERROR) { + log.error('Unwrapped an Error') + log.debug(`Error : ${this._data}`) + throw 'Unwrapped an Error' } - // TODO : implements Iterable<...> + return this._data as T + } + public ok(): Option { + if (this._state === State.OK) { + return Option.some(this._data as T) + } else { + return Option.none() + } + } + public map(f: (data: T) => U): Result { + if (this._state === State.OK) { + return Result.ok(f(this._data as T)) + } else { + return Result.error(this._data as E) + } + } + public map_or(default_: U, f: (data: T) => U): U { + if (this._state === State.OK) { + return f(this._data as T) + } else { + return default_ + } + } + public map_or_else(fe: (err: E) => U, f: (data: T) => U): U { + if (this._state === State.OK) { + return f(this._data as T) + } else { + return fe(this._data as E) + } + } + public inspect(f: (data: T) => void): Result { + if (this._state === State.OK) { + f(this._data as T) + } + return this + } + public unwrap_or(default_: T): T { + if (this._state === State.OK) { + return this._data as T + } else { + return default_ + } + } + public unwrap_or_else(f: (err: E) => T): T { + if (this._state === State.OK) { + return this._data as T + } else { + return f(this._data as E) + } + } - // ***************************************************************************************************************** - // Ok - public is_ok() : boolean { - return this._state === State.OK - } - public is_ok_and(f: (data: T) => boolean) : boolean { - return this._state === State.OK && f(this._data as T) - } - public expect(msg: string) : T { - log.trace('expect') + // ***************************************************************************************************************** + // Errors + public is_err(): boolean { + return this._state === State.ERROR + } + public is_err_and(f: (err: E) => boolean): boolean { + return this._state === State.ERROR && f(this._data as E) + } + public expect_err(msg: string): E { + log.trace('expect_err') - if (this._state === State.ERROR) { - log.error('unwrapped an Error') - log.error(msg) - log.debug(`Error : ${this._data}`) - throw msg - } - - return this._data as T - } - public unwrap() : T { - log.trace('unwrap') - - if (this._state === State.ERROR) { - log.error("Unwrapped an Error") - log.debug(`Error : ${this._data}`) - throw "Unwrapped an Error" - } - - return this._data as T - } - public ok() : Option { - if (this._state === State.OK) { - return Option.some(this._data as T) - } else { - return Option.none() - } - } - public map(f: (data: T) => U) : Result { - if (this._state === State.OK) { - return Result.ok(f(this._data as T)) - } else { - return Result.error(this._data as E) - } - } - public map_or(default_: U, f: (data: T) => U) : U { - if (this._state === State.OK) { - return f(this._data as T) - } else { - return default_ - } - } - public map_or_else(fe: (err: E) => U, f: (data: T) => U) : U { - if (this._state === State.OK) { - return f(this._data as T) - } else { - return fe(this._data as E) - } - } - public inspect(f: (data: T) => void) : Result { - if (this._state === State.OK) { - f(this._data as T) - } - return this - } - public unwrap_or(default_: T) : T { - if (this._state === State.OK) { - return this._data as T - } else { - return default_ - } - } - public unwrap_or_else(f: (err: E) => T) : T { - if (this._state === State.OK) { - return this._data as T - } else { - return f(this._data as E) - } + if (this._state === State.OK) { + log.error('expect_err on Ok') + log.error(msg) + throw msg } - // ***************************************************************************************************************** - // Errors - public is_err() : boolean { - return this._state === State.ERROR - } - public is_err_and(f: (err: E) => boolean) : boolean { - return this._state === State.ERROR && f(this._data as E) - } - public expect_err(msg: string) : E { - log.trace('expect_err') + return this._data as E + } + public unwrap_err(): E { + log.trace('unwrap_err') - if (this._state === State.OK) { - log.error('expect_err on Ok') - log.error(msg) - throw msg - } - - return this._data as E - } - public unwrap_err() : E { - log.trace('unwrap_err') - - if (this._state === State.OK) { - log.error('unwrap_err on Ok') - throw "Result wasn't in error" - } - - return this._data as E - } - public err() : Option { - if (this._state === State.ERROR) { - return Option.some(this._data as E) - } else { - return Option.none() - } - } - public map_err(f: (err: E) => F) : Result { - if (this._state === State.ERROR) { - return Result.error(f(this._data as E)) - } else { - return Result.ok(this._data as T) - } - } - public inspect_err(f: (err: E) => void) : Result { - if (this._state === State.ERROR) { - f(this._data as E) - } - return this + if (this._state === State.OK) { + log.error('unwrap_err on Ok') + throw "Result wasn't in error" } - // ***************************************************************************************************************** - // Combine - public and(res: Result) : Result { - if (this._state === State.OK) { - return res - } else { - return Result.error(this._data as E) - } + return this._data as E + } + public err(): Option { + if (this._state === State.ERROR) { + return Option.some(this._data as E) + } else { + return Option.none() } - public and_then(f: (data: T) => Result) : Result { - if (this._state === State.OK) { - return f(this._data as T) - } else { - return Result.error(this._data as E) - } + } + public map_err(f: (err: E) => F): Result { + if (this._state === State.ERROR) { + return Result.error(f(this._data as E)) + } else { + return Result.ok(this._data as T) } - public or(res: Result) : Result { - if (this._state === State.OK) { - return new Result(this._data as T, State.OK) - } else { - return res - } + } + public inspect_err(f: (err: E) => void): Result { + if (this._state === State.ERROR) { + f(this._data as E) } - public or_else(f: (err: E) => Result) : Result { - if (this._state === State.OK) { - return Result.ok(this._data as T) - } else { - return f(this._data as E) - } + return this + } + + // ***************************************************************************************************************** + // Combine + public and(res: Result): Result { + if (this._state === State.OK) { + return res + } else { + return Result.error(this._data as E) } + } + public and_then(f: (data: T) => Result): Result { + if (this._state === State.OK) { + return f(this._data as T) + } else { + return Result.error(this._data as E) + } + } + public or(res: Result): Result { + if (this._state === State.OK) { + return new Result(this._data as T, State.OK) + } else { + return res + } + } + public or_else(f: (err: E) => Result): Result { + if (this._state === State.OK) { + return Result.ok(this._data as T) + } else { + return f(this._data as E) + } + } } export const enum State { - OK, - ERROR + OK, + ERROR }