2024-07-03 16:13:29 -04:00
|
|
|
/**
|
2024-07-03 17:49:15 -04:00
|
|
|
* The sad, lonely enum that should be more tightly coupled
|
|
|
|
* to the Option type...but this isn't Rust
|
2024-07-03 16:13:29 -04:00
|
|
|
*/
|
2024-07-03 17:49:15 -04:00
|
|
|
enum OptionType {
|
|
|
|
Some = 'Some',
|
|
|
|
None = 'None',
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
const isOption = <T>(v: any): v is Option<T> => v instanceof Option;
|
2024-07-03 16:13:29 -04:00
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
/**
|
|
|
|
* Rust-style optional type
|
|
|
|
*
|
|
|
|
* Based on https://gist.github.com/s-panferov/575da5a7131c285c0539
|
|
|
|
*/
|
|
|
|
export class Option<T> {
|
|
|
|
/**
|
|
|
|
* The placeholder for the 'None' value type
|
|
|
|
*/
|
|
|
|
private static _noneInstance: Option<any> = new Option();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a 'Some' or a 'None'?
|
|
|
|
*/
|
|
|
|
private optionType: OptionType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2024-07-03 16:13:29 -04:00
|
|
|
} else {
|
2024-07-03 17:49:15 -04:00
|
|
|
this.optionType = OptionType.None;
|
|
|
|
this.value = undefined;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
public static get None(): Option<any> {
|
|
|
|
return <Option<any>> Option._noneInstance;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
public static Some<X>(v: X): Option<X> {
|
|
|
|
return new Option(v);
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
public static from<X>(v: any): Option<X> {
|
|
|
|
return (isOption(v)) ? Option.from(v.unwrap()) : new Option(v);
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isSome(): boolean {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.optionType === OptionType.Some && this.value !== undefined;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isNone(): boolean {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.optionType === OptionType.None;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isSomeAnd(fn: (a: T) => boolean): boolean {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.isSome() ? fn(this.unwrap()) : false;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
isNoneAnd(fn: () => boolean): boolean {
|
|
|
|
return this.isNone() ? fn() : false;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
map<U>(fn: (a: T) => U): Option<U> {
|
|
|
|
return this.isSome() ? new Option(fn(this.unwrap())) : Option._noneInstance;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
mapOr<U>(def: U, f: (a: T) => U): U {
|
|
|
|
return this.isSome() ? f(this.unwrap()) : def;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
mapOrElse<U>(def: () => U, f: (a: T) => U): U {
|
|
|
|
return this.isSome() ? f(this.unwrap()) : def();
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
unwrap(): T | never {
|
|
|
|
if (this.isSome() && this.value !== undefined) {
|
|
|
|
return this.value;
|
|
|
|
}
|
2024-07-03 16:13:29 -04:00
|
|
|
|
|
|
|
console.error('None.unwrap()');
|
|
|
|
throw 'None.get';
|
|
|
|
}
|
|
|
|
|
|
|
|
unwrapOr(def: T): T {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.isSome() ? this.unwrap() : def;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unwrapOrElse(f: () => T): T {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.isSome() ? this.unwrap() : f();
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
and<U>(optb: Option<U>): Option<U> {
|
|
|
|
return this.isSome() ? optb : Option._noneInstance;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
andThen<U>(f: (a: T) => Option<U>): Option<U> {
|
|
|
|
return this.isSome() ? f(this.unwrap()) : Option._noneInstance;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
or(optb: Option<T>): Option<T> {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.isNone() ? optb : this;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
orElse(f: () => Option<T>): Option<T> {
|
2024-07-03 17:49:15 -04:00
|
|
|
return this.isNone() ? f() : this;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
toString(): string {
|
|
|
|
const innerValue = (this.value !== undefined)
|
|
|
|
? JSON.stringify(this.value)
|
|
|
|
: '';
|
|
|
|
const prefix = this.optionType.valueOf();
|
2024-07-03 16:13:29 -04:00
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
return (innerValue.length > 0) ? `${prefix} (${innerValue})` : prefix;
|
2024-07-03 16:13:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:49:15 -04:00
|
|
|
export const { Some, None } = Option;
|
|
|
|
export default Option;
|