@hazae41/option
Version:
Rust-like Option for TypeScript
313 lines (310 loc) • 9.48 kB
JavaScript
import { Ok } from '@hazae41/result';
import { None } from './none.mjs';
class Some {
inner;
/**
* An existing value
* @param inner
*/
constructor(inner) {
this.inner = inner;
}
static create(inner) {
return new Some(inner);
}
static from(init) {
return new Some(init.inner);
}
/**
* Returns an iterator over the possibly contained value
* @yields `this.inner` if `Some`
*/
*[Symbol.iterator]() {
yield this.inner;
}
/**
* Type guard for `Some`
* @returns `true` if `Some`, `false` if `None`
*/
isSome() {
return true;
}
/**
* 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 await somePredicate(this.inner);
}
/**
* 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 somePredicate(this.inner);
}
/**
* Type guard for `None`
* @returns `true` if `None`, `false` if `Some`
*/
isNone() {
return false;
}
/**
* Compile-time safely get `this.inner`
* @returns `this.inner`
*/
get() {
return this.inner;
}
/**
* Get the inner value or throw an error
* @returns
*/
getOrThrow() {
return this.inner;
}
/**
* Get the inner value or `null`
* @returns
*/
getOrNull() {
return this.inner;
}
/**
* Get the inner value or a default one
* @param value
* @returns `this.inner` if `Some`, `value` if `None`
*/
getOr(value) {
return this.inner;
}
/**
* 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 this.inner;
}
/**
* Returns the contained `Some` value or computes it from a closure
* @param noneCallback
* @returns `this.inner` if `Some`, `noneCallback()` if `None`
*/
getOrElseSync(noneCallback) {
return this.inner;
}
/**
* Transform `Option<T>` into `Result<T, NoneError>`
* @returns `Ok(this.inner)` if `Some`, `Err(NoneError)` if `None`
*/
ok() {
return new Ok(this.inner);
}
/**
* Transform `Option<T>` into `Result<T, E>`
* @param error
* @returns `Ok(this.inner)` if `Some`, `Err(error)` if `None`
*/
okOr(error) {
return new Ok(this.inner);
}
/**
* 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 Ok(this.inner);
}
/**
* 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 Ok(this.inner);
}
/**
* 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) {
if (await somePredicate(this.inner))
return this;
else
return new None();
}
/**
* 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) {
if (somePredicate(this.inner))
return this;
else
return new None();
}
/**
* Transform `Option<Promise<T>>` into `Promise<Option<T>>`
* @returns `Promise<Option<T>>`
*/
async await() {
return new Some(await this.inner);
}
/**
* 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 this.inner === value;
}
/**
* Calls the given callback with the inner value if `Ok`
* @param someCallback
* @returns `this`
*/
async inspect(someCallback) {
await someCallback(this.inner);
return this;
}
/**
* Calls the given callback with the inner value if `Ok`
* @param someCallback
* @returns `this`
*/
inspectSync(someCallback) {
someCallback(this.inner);
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 new Some(await someMapper(this.inner));
}
/**
* 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 new Some(someMapper(this.inner));
}
/**
* 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 await someMapper(this.inner);
}
/**
* 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 someMapper(this.inner);
}
/**
* 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 someMapper(this.inner);
}
/**
* 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 someMapper(this.inner);
}
/**
* Returns `None` if the option is `None`, otherwise returns `value`
* @param value
* @returns `None` if `None`, `value` if `Some`
*/
and(value) {
return value;
}
/**
* 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 await someMapper(this.inner);
}
/**
* 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 someMapper(this.inner);
}
/**
* Returns `this` if `Some`, otherwise returns `value`
* @param value
* @returns `this` if `Some`, `value` if `None`
*/
or(value) {
return this;
}
/**
* 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 this;
}
/**
* Returns `this` if `Some`, otherwise calls `noneCallback` and returns the result
* @param noneCallback
* @returns `this` if `Some`, `noneCallback()` if `None`
*/
orElseSync(noneCallback) {
return this;
}
/**
* 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) {
if (value.isSome())
return new None();
else
return this;
}
/**
* 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) {
if (other.isSome())
return new Some([this.inner, other.inner]);
else
return other;
}
}
export { Some };
//# sourceMappingURL=some.mjs.map