Interface: IOption<T>
Type parameters
| Type parameter |
|---|
T |
Methods
[iterator]()
[iterator]():
Generator<T,any,unknown>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Some, otherwise none.
Returns
Generator<T, any, unknown>
Examples
const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:738
and()
Returns None if the option is None, otherwise returns option.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
option | Option<U> | The option. |
Returns
Option<U>
Examples
const x: Option<number> = some(2);
const y: Option<string> = none;
assert.equal(x.and(y), none);
const x: Option<number> = none;
const y: Option<string> = some('foo');
assert.equal(x.and(y), none);
const x: Option<number> = some(2);
const y: Option<string> = some('foo');
assert.equal(x.and(y), some('foo'));
const x: Option<number> = none;
const y: Option<string> = none;
assert.equal(x.and(y), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.and
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:397
andThen()
Calls cb if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on Result values.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => Option<U> | The predicate. |
Returns
Option<U>
Example
function fractionOf4(value: number) {
return value === 0 ? none : some(4 / value);
}
assert.equal(some(2).andThen(fractionOf4), some(4));
assert.equal(some(0).andThen(fractionOf4), none);
assert.equal(none.andThen(fractionOf4), none);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:418
contains()
contains(
value:T):boolean
Returns true if the option is a Some value containing the given value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | T | The value to compare. |
Returns
boolean
Examples
const x: Option<number> = some(2);
assert.equal(x.contains(2), true);
const x: Option<number> = some(3);
assert.equal(x.contains(2), false);
const x: Option<number> = none;
assert.equal(x.contains(2), false);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.contains
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:550
eq()
eq(
other:Option<T>):boolean
Checks whether or not other equals with self.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Option<T> | The other option to compare. |
Returns
boolean
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:680
expect()
expect(
message:string):T
Returns the contained Some value.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message for the error. If the value is an Err, it throws an OptionError with the given message. |
Returns
T
Examples
const x: Option<string> = some(2);
assert.equal(x.expect('Whoops!'), 2);
const x: Option<string> = none;
assert.throws(() => x.expect('Whoops!'), {
name: 'OptionError',
message: 'Whoops'
});
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.expect
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:89
filter()
filter(
predicate: (value:T) =>boolean):Option<T>
Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:
Some(t)ifpredicatereturnstrue(where t is the wrapped value), andNoneifpredicatereturnsfalse.
Parameters
| Parameter | Type | Description |
|---|---|---|
predicate | (value: T) => boolean | The predicate. |
Returns
Option<T>
Example
function isEven(value: number) {
return n % 2 === 0;
}
assert.equal(none.filter(isEven), none);
assert.equal(some(3).filter(isEven), none);
assert.equal(some(4).filter(isEven), some(4));
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.filter
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:526
flatten()
Converts from Result<Result<T, E>, E> to Result<T, E>.
Type parameters
| Type parameter |
|---|
IT |
Parameters
| Parameter | Type |
|---|---|
this | Option <Option<IT>> |
Returns
Option<IT>
Examples
const x: Option<Option<number>> = some(some(6));
assert.equal(x.flatten(), some(6));
const x: Option<Option<number>> = some(none);
assert.equal(x.flatten(), none);
const x: Option<Option<number>> = none;
assert.equal(x.flatten(), none);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:659
inspect()
inspect(
cb: (value:T) =>void):this
Calls the provided closure with a reference to the contained value (if Some).
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => void | The predicate. |
Returns
this
Seealso
inspectAsync for the awaitable version.
Examples
some(2).inspect(console.log);
// Logs: 2
none.inspect(console.log);
// Doesn't log
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:276
inspectAsync()
inspectAsync(
cb: (value:T) =>Awaitable<void>):Promise<IOption<T>>
Calls the provided closure with a reference to the contained value (if Some).
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => Awaitable<void> | The predicate. |
Returns
Seealso
inspect for the sync version.
Examples
await some(2).inspectAsync(console.log);
// Logs: 2
await none.inspectAsync(console.log);
// Doesn't log
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:296
intoPromise()
Returns a Promise object with the awaited value (if Some).
Returns
Example
let x = some(Promise.resolve(3));
assert.equal(await x.intoPromise(), some(3));
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:672
isNone()
isNone():
this is None
Returns true if the option is a None value.
Returns
this is None
Examples
const x: Option<number> = some(2);
assert.equal(x.isNone(), false);
const x: Option<number> = none;
assert.equal(x.isNone(), true);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:66
isSome()
isSome():
this is Some<T>
Returns true if the option is a Some value.
Returns
this is Some<T>
Examples
const x: Option<number> = some(2);
assert.equal(x.isSome(), true);
const x: Option<number> = none;
assert.equal(x.isSome(), false);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:24
isSomeAnd()
isSomeAnd(
cb: (value:T) =>boolean):boolean
Returns true if the option is a Some and the value inside of it matches a predicate.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => boolean | The predicate. |
Returns
boolean
Examples
const x: Option<number> = some(2);
assert.equal(x.isSomeAnd((x) => x > 1), true);
const x: Option<number> = some(0);
assert.equal(x.isSomeAnd((x) => x > 1), false);
const x: Option<number> = none;
assert.equal(x.isSomeAnd((x) => x > 1), false);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:48
iter()
iter():
Generator<T,any,unknown>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Some, otherwise none.
Returns
Generator<T, any, unknown>
Examples
const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:364
map()
map<
U>(cb: (value:T) =>U):Option<U>
Maps an Option<T> to Option<U> by applying a function to a contained value.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => U | The predicate. |
Returns
Option<U>
Example
const maybeSomeString = some('Hello, world!');
const maybeSomeLength = maybeSomeString.map((value) => value.length);
assert.equal(maybeSomeLength, some(13));
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.map
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:165
mapInto()
mapInto<
Inner>(cb: (value:T) =>Option<Inner>):Option<Inner>
Maps a Some<T> to the returned Option<U> by applying a function to a contained value, leaving None
untouched.
Type parameters
| Type parameter |
|---|
Inner |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => Option<Inner> | The predicate. |
Returns
Option<Inner>
Examples
const input: Option<string> = some('Hello, world!');
const result = input.mapInto((value) => some(value.length));
assert.equal(result, some(13));
const input: Option<string> = none;
const result = input.mapInto((value) => some(value.length));
assert.equal(result, none);
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:189
mapNoneInto()
mapNoneInto<
Inner>(cb: () =>Option<Inner>):Option<T|Inner>
Maps a None to the returned Option<U> by applying a function to a contained value, leaving Some<T>
untouched.
Type parameters
| Type parameter |
|---|
Inner |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | () => Option<Inner> | The predicate. |
Returns
Option<T | Inner>
Examples
const input: Option<string> = some('Hello, world!');
const result = input.mapNoneInto(() => some(13));
assert.equal(result, some('Hello, world!'));
const input: Option<string> = none;
const result = input.mapNoneInto(() => some(13));
assert.equal(result, some(13));
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:256
mapOr()
mapOr<
U>(defaultValue:U,cb: (value:T) =>U):U
Returns the provided default result (if none), or applies a function to the contained value (if any).
Arguments passed to mapOr are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use mapOrElse, which is lazily evaluated.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
defaultValue | U | The default value. |
cb | (value: T) => U | The predicate. |
Returns
U
Examples
const x: Option<string> = some('hello');
assert.equal(x.mapOr(42, (value) => value.length), 5);
const x: Option<string> = none;
assert.equal(x.mapOr(42, (value) => value.length), 42);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:212
mapOrElse()
mapOrElse<
U>(defaultValue: () =>U,cb: (value:T) =>U):U
Computes a default function result (if none), or applies a different function to the contained value (if any).
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
defaultValue | () => U | The default value. |
cb | (value: T) => U | The predicate. |
Returns
U
Examples
const x: Option<string> = some('hello');
assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
const x: Option<string> = none;
assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:232
match()
match<
SomeValue,NoneValue>(branches:object):SomeValue|NoneValue
Runs ok function if self is Ok, otherwise runs err function.
Type parameters
| Type parameter |
|---|
SomeValue |
NoneValue |
Parameters
| Parameter | Type | Description |
|---|---|---|
branches | object | The branches to match. |
branches.none | - | |
branches.some | - |
Returns
SomeValue | NoneValue
Examples
const option = some(4).match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 4);
const option = none.match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 0);
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:711
ne()
ne(
other:Option<T>):boolean
Checks whether or not other doesn't equal with self.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Option<T> | The other option to compare. |
Returns
boolean
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:688
okOr()
okOr<
E>(err:E):Result<T,E>
Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).
Arguments passed to okOr are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use okOrElse, which is lazily evaluated.
Type parameters
| Type parameter |
|---|
E |
Parameters
| Parameter | Type | Description |
|---|---|---|
err | E | The error to be used. |
Returns
Result<T, E>
Examples
const x: Option<string> = some('hello');
assert.equal(x.okOr(0), ok('hello'));
const x: Option<string> = none;
assert.equal(x.okOr(0), err(0));
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:318
okOrElse()
okOrElse<
E>(cb: () =>E):Result<T,E>
Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).
Type parameters
| Type parameter |
|---|
E |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | () => E | The error to be used. |
Returns
Result<T, E>
Examples
const x: Option<string> = some('hello');
assert.equal(x.okOrElse(() => 0), ok('hello'));
const x: Option<string> = none;
assert.equal(x.okOrElse(() => 0), err(0));
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:337
or()
Returns the option if it contains a value, otherwise returns option.
Parameters
| Parameter | Type | Description |
|---|---|---|
option | Option<T> | The option. |
Returns
Option<T>
Examples
const x: Option<number> = some(2);
const y: Option<number> = none;
assert.equal(x.or(y), some(2));
const x: Option<number> = none;
const y: Option<number> = some(100);
assert.equal(x.or(y), some(100));
const x: Option<number> = some(2);
const y: Option<number> = some(100);
assert.equal(x.or(y), some(2));
const x: Option<number> = none;
const y: Option<number> = none;
assert.equal(x.or(y), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.or
Source
projects/utilities/packages/result/src/lib/Option/IOption.ts:451