@hazae41/option
Version:
Rust-like Option for TypeScript
302 lines (299 loc) • 8.89 kB
JavaScript
import { Panic, Err } from '@hazae41/result';
class NoneError extends Error {
constructor() {
super(`Option is a None`);
}
}
class None {
inner;
/**
* An empty value
*/
constructor(inner = undefined) {
this.inner = inner;
}
static create() {
return new None();
}
static from(init) {
return new None();
}
/**
* Returns an iterator over the possibly contained value
* @yields `this.inner` if `Some`
*/
*[Symbol.iterator]() {
return;
}
/**
* Type guard for `Some`
* @returns `true` if `Some`, `false` if `None`
*/
isSome() {
return false;
}
/**
* Returns `true` if the option is a `Some` and the value inside of it matches a predicate
* @param somePredicate
* @returns `true` if `Some` and `await somePredicate(this.inner)`, `None` otherwise
*/
async isSomeAnd(somePredicate) {
return false;
}
/**
* Returns `true` if the option is a `Some` and the value inside of it matches a predicate
* @param somePredicate
* @returns `true` if `Some` and `somePredicate(this.inner)`, `None` otherwise
*/
isSomeAndSync(somePredicate) {
return false;
}
/**
* Type guard for `None`
* @returns `true` if `None`, `false` if `Some`
*/
isNone() {
return true;
}
/**
* Compile-time safely get `this.inner`
* @returns `this.inner`
*/
get() {
throw new Panic();
}
/**
* Get the inner value or throw an error
* @returns
*/
getOrThrow() {
throw new NoneError();
}
/**
* Get the inner value or `null`
* @returns
*/
getOrNull() {
return;
}
/**
* Get the inner value or a default one
* @param value
* @returns `this.inner` if `Some`, `value` if `None`
*/
getOr(value) {
return value;
}
/**
* Returns the contained `Some` value or computes it from a closure
* @param noneCallback
* @returns `this.inner` if `Some`, `await noneCallback()` if `None`
*/
async getOrElse(noneCallback) {
return await noneCallback();
}
/**
* Returns the contained `Some` value or computes it from a closure
* @param noneCallback
* @returns `this.inner` if `Some`, `noneCallback()` if `None`
*/
getOrElseSync(noneCallback) {
return noneCallback();
}
/**
* Transform `Option<T>` into `Result<T, NoneError>`
* @returns `Ok(this.inner)` if `Some`, `Err(NoneError)` if `None`
*/
ok() {
return new Err(new NoneError());
}
/**
* Transform `Option<T>` into `Result<T, E>`
* @param error
* @returns `Ok(this.inner)` if `Some`, `Err(error)` if `None`
*/
okOr(error) {
return new Err(error);
}
/**
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`
* @param noneCallback
* @returns `Ok(this.inner)` if `Some`, `Err(await noneCallback())` is `None`
*/
async okOrElse(noneCallback) {
return new Err(await noneCallback());
}
/**
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`
* @param noneCallback
* @returns `Ok(this.inner)` if `Some`, `Err(noneCallback())` is `None`
*/
okOrElseSync(noneCallback) {
return new Err(noneCallback());
}
/**
* Returns `None` if the option is `None`, otherwise calls `somePredicate` with the wrapped value
* @param somePredicate
* @returns `Some` if `Some` and `await somePredicate(this.inner)`, `None` otherwise
*/
async filter(somePredicate) {
return this;
}
/**
* Returns `None` if the option is `None`, otherwise calls `somePredicate` with the wrapped value
* @param somePredicate
* @returns `Some` if `Some` and `somePredicate(this.inner)`, `None` otherwise
*/
filterSync(somePredicate) {
return this;
}
/**
* Transform `Option<Promise<T>>` into `Promise<Option<T>>`
* @returns `Promise<Option<T>>`
*/
async await() {
return this;
}
/**
* Returns `true` if the option is a `Some` value containing the given value
* @param value
* @returns `true` if `Some` and `this.inner === value`, `None` otherwise
*/
contains(value) {
return false;
}
/**
* Calls the given callback with the inner value if `Ok`
* @param someCallback
* @returns `this`
*/
async inspect(someCallback) {
return this;
}
/**
* Calls the given callback with the inner value if `Ok`
* @param someCallback
* @returns `this`
*/
inspectSync(someCallback) {
return this;
}
/**
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value (if `Some`) or returns `None` (if `None`)
* @param someMapper
* @returns `Some(await someMapper(this.inner))` if `Some`, `this` if `None`
*/
async map(someMapper) {
return this;
}
/**
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value (if `Some`) or returns `None` (if `None`)
* @param someMapper
* @returns `Some(someMapper(this.inner))` if `Some`, `this` if `None`
*/
mapSync(someMapper) {
return this;
}
/**
* Returns the provided default result (if none), or applies a function to the contained value (if any)
* @param value
* @param someMapper
* @returns `value` if `None`, `await someMapper(this.inner)` if `Some`
*/
async mapOr(value, someMapper) {
return value;
}
/**
* Returns the provided default result (if none), or applies a function to the contained value (if any)
* @param value
* @param someMapper
* @returns `value` if `None`, `someMapper(this.inner)` if `Some`
*/
mapOrSync(value, someMapper) {
return value;
}
/**
* Computes a default function result (if none), or applies a different function to the contained value (if any)
* @param noneCallback
* @param someMapper
* @returns `await someMapper(this.inner)` if `Some`, `await noneCallback()` if `None`
*/
async mapOrElse(noneCallback, someMapper) {
return await noneCallback();
}
/**
* Computes a default function result (if none), or applies a different function to the contained value (if any)
* @param noneCallback
* @param someMapper
* @returns `someMapper(this.inner)` if `Some`, `noneCallback()` if `None`
*/
mapOrElseSync(noneCallback, someMapper) {
return noneCallback();
}
/**
* Returns `None` if the option is `None`, otherwise returns `value`
* @param value
* @returns `None` if `None`, `value` if `Some`
*/
and(value) {
return this;
}
/**
* Returns `None` if the option is `None`, otherwise calls `someMapper` with the wrapped value and returns the result
* @param someMapper
* @returns `None` if `None`, `await someMapper(this.inner)` if `Some`
*/
async andThen(someMapper) {
return this;
}
/**
* Returns `None` if the option is `None`, otherwise calls `someMapper` with the wrapped value and returns the result
* @param someMapper
* @returns `None` if `None`, `someMapper(this.inner)` if `Some`
*/
andThenSync(someMapper) {
return this;
}
/**
* Returns `this` if `Some`, otherwise returns `value`
* @param value
* @returns `this` if `Some`, `value` if `None`
*/
or(value) {
return value;
}
/**
* Returns `this` if `Some`, otherwise calls `noneCallback` and returns the result
* @param noneCallback
* @returns `this` if `Some`, `await noneCallback()` if `None`
*/
async orElse(noneCallback) {
return await noneCallback();
}
/**
* Returns `this` if `Some`, otherwise calls `noneCallback` and returns the result
* @param noneCallback
* @returns `this` if `Some`, `noneCallback()` if `None`
*/
orElseSync(noneCallback) {
return noneCallback();
}
/**
* Returns `Some` if exactly one of the options is `Some`, otherwise returns `None`
* @param value
* @returns `None` if both are `Some` or both are `None`, the only `Some` otherwise
*/
xor(value) {
return value;
}
/**
* Zips `this` with another `Option`
* @param other
* @returns `Some([this.inner, other.inner])` if both are `Some`, `None` if one of them is `None`
*/
zip(other) {
return this;
}
}
export { None, NoneError };
//# sourceMappingURL=none.mjs.map