Refactor Option type to one implementation instead of two
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2024-07-03 17:49:15 -04:00
parent 1b3e9d9796
commit 4313b923bf

View File

@ -1,175 +1,127 @@
/**
* The sad, lonely enum that should be more tightly coupled
* to the Option type...but this isn't Rust
*/
enum OptionType {
Some = 'Some',
None = 'None',
}
const isOption = <T>(v: any): v is Option<T> => v instanceof Option;
/**
* Rust-style optional type
*
* Based on https://gist.github.com/s-panferov/575da5a7131c285c0539
*/
export default interface Option<T> {
isSome(): boolean;
isNone(): boolean;
isSomeAnd(fn: (a: T) => boolean): boolean;
isNoneAnd(fn: () => boolean): boolean;
unwrap(): T | never;
unwrapOr(def: T): T;
unwrapOrElse(f: () => T): T;
map<U>(f: (a: T) => U): Option<U>;
mapOr<U>(def: U, f: (a: T) => U): U;
mapOrElse<U>(def: () => U, f: (a: T) => U): U;
and<U>(optb: Option<U>): Option<U>;
andThen<U>(f: (a: T) => Option<U>): Option<U>;
or(optb: Option<T>): Option<T>;
orElse(f: () => Option<T>): Option<T>;
}
export class Option<T> {
/**
* The placeholder for the 'None' value type
*/
private static _noneInstance: Option<any> = new Option();
class _Some<T> implements Option<T> {
private value: T;
/**
* Is this a 'Some' or a 'None'?
*/
private optionType: OptionType;
constructor(v: T) {
/**
* The value for the 'Some' type
*/
private value?: T;
private constructor(v?: T | null) {
if (v !== undefined && v !== null) {
this.optionType = OptionType.Some;
this.value = v;
}
static wrapNull<T>(value: T): Option<T> {
if (value == null) {
return None;
} else {
return new _Some<T>(value);
this.optionType = OptionType.None;
this.value = undefined;
}
}
map<U>(fn: (a: T) => U): Option<U> {
return new _Some(fn(this.value));
public static get None(): Option<any> {
return <Option<any>> Option._noneInstance;
}
mapOr<U>(_def: U, f: (a: T) => U): U {
return f(this.value);
public static Some<X>(v: X): Option<X> {
return new Option(v);
}
mapOrElse<U>(_def: () => U, f: (a: T) => U): U {
return f(this.value);
public static from<X>(v: any): Option<X> {
return (isOption(v)) ? Option.from(v.unwrap()) : new Option(v);
}
isSome(): boolean {
return true;
return this.optionType === OptionType.Some && this.value !== undefined;
}
isNone(): boolean {
return false;
return this.optionType === OptionType.None;
}
isSomeAnd(fn: (a: T) => boolean): boolean {
return fn(this.value);
}
isNoneAnd(_fn: () => boolean): boolean {
return false;
}
unwrap(): T {
return this.value;
}
unwrapOr(_def: T): T {
return this.value;
}
unwrapOrElse(_f: () => T): T {
return this.value;
}
and<U>(optb: Option<U>): Option<U> {
return optb;
}
andThen<U>(f: (a: T) => Option<U>): Option<U> {
return f(this.value);
}
or(_optb: Option<T>): Option<T> {
return this;
}
orElse(_f: () => Option<T>): Option<T> {
return this;
}
toString(): string {
return 'Some ' + this.value;
}
}
class _None<T> implements Option<T> {
constructor() {
}
map<U>(_fn: (a: T) => U): Option<U> {
return <Option<U>> _None._instance;
}
isSome(): boolean {
return false;
}
isNone(): boolean {
return true;
}
isSomeAnd(_fn: (a: T) => boolean): boolean {
return false;
return this.isSome() ? fn(this.unwrap()) : false;
}
isNoneAnd(fn: () => boolean): boolean {
return fn();
return this.isNone() ? fn() : false;
}
map<U>(fn: (a: T) => U): Option<U> {
return this.isSome() ? new Option(fn(this.unwrap())) : Option._noneInstance;
}
mapOr<U>(def: U, f: (a: T) => U): U {
return this.isSome() ? f(this.unwrap()) : def;
}
mapOrElse<U>(def: () => U, f: (a: T) => U): U {
return this.isSome() ? f(this.unwrap()) : def();
}
unwrap(): T | never {
if (this.isSome() && this.value !== undefined) {
return this.value;
}
unwrap(): never {
console.error('None.unwrap()');
throw 'None.get';
}
unwrapOr(def: T): T {
return def;
return this.isSome() ? this.unwrap() : def;
}
unwrapOrElse(f: () => T): T {
return f();
return this.isSome() ? this.unwrap() : f();
}
mapOr<U>(def: U, _f: (a: T) => U): U {
return def;
and<U>(optb: Option<U>): Option<U> {
return this.isSome() ? optb : Option._noneInstance;
}
mapOrElse<U>(def: () => U, _f: (a: T) => U): U {
return def();
}
and<U>(_optb: Option<U>): Option<U> {
return _None.instance<U>();
}
andThen<U>(_f: (a: T) => Option<U>): Option<U> {
return _None.instance<U>();
andThen<U>(f: (a: T) => Option<U>): Option<U> {
return this.isSome() ? f(this.unwrap()) : Option._noneInstance;
}
or(optb: Option<T>): Option<T> {
return optb;
return this.isNone() ? optb : this;
}
orElse(f: () => Option<T>): Option<T> {
return f();
return this.isNone() ? f() : this;
}
private static _instance: Option<any> = new _None();
toString(): string {
const innerValue = (this.value !== undefined)
? JSON.stringify(this.value)
: '';
const prefix = this.optionType.valueOf();
public static instance<X>(): Option<X> {
return <Option<X>> _None._instance;
}
public toString(): string {
return 'None';
return (innerValue.length > 0) ? `${prefix} (${innerValue})` : prefix;
}
}
export const None: Option<any> = _None.instance();
export function Some<T>(value: T): Option<T> {
return _Some.wrapNull(value);
}
export const { Some, None } = Option;
export default Option;