Interface: IOption<T>
Type parameters
Name |
---|
T |
Implemented by
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.
Example
const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
Example
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Returns
Generator
<T
, any
, unknown
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1646
and
▸ and<U
>(option
): Option
<U
>
Returns None
if the option is None
, otherwise returns option
.
Example
const x: Option<number> = some(2);
const y: Option<string> = none;
assert.equal(x.and(y), none);
Example
const x: Option<number> = none;
const y: Option<string> = some('foo');
assert.equal(x.and(y), none);
Example
const x: Option<number> = some(2);
const y: Option<string> = some('foo');
assert.equal(x.and(y), some('foo'));
Example
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
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
option | Option <U > | The option. |
Returns
Option
<U
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1318
andThen
▸ andThen<U
>(cb
): Option
<U
>
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.
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
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => Option <U > | The predicate. |
Returns
Option
<U
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1338
contains
▸ contains(value
): boolean
Returns true
if the option is a Some
value containing the given value.
Example
const x: Option<number> = some(2);
assert.equal(x.contains(2), true);
Example
const x: Option<number> = some(3);
assert.equal(x.contains(2), false);
Example
const x: Option<number> = none;
assert.equal(x.contains(2), false);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.contains
Parameters
Name | Type | Description |
---|---|---|
value | T | The value to compare. |
Returns
boolean
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1465
eq
▸ eq(other
): boolean
Checks whether or not other
equals with self.
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
Parameters
Name | Type | Description |
---|---|---|
other | Option <T > | The other option to compare. |
Returns
boolean
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1588
expect
▸ expect(message
): T
Returns the contained Some
value.
Example
const x: Option<string> = some(2);
assert.equal(x.expect('Whoops!'), 2);
Example
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
Parameters
Name | 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
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1024
filter
▸ filter(predicate
): Option
<T
>
Returns None if the option is None, otherwise calls predicate
with the wrapped value and returns:
Some(t)
ifpredicate
returnstrue
(where t is the wrapped value), andNone
ifpredicate
returnsfalse
.
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
Parameters
Name | Type | Description |
---|---|---|
predicate | (value : T ) => boolean | The predicate. |
Returns
Option
<T
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1442
flatten
▸ flatten<IT
>(this
): Option
<IT
>
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
Example
const x: Option<Option<number>> = some(some(6));
assert.equal(x.flatten(), some(6));
Example
const x: Option<Option<number>> = some(none);
assert.equal(x.flatten(), none);
Example
const x: Option<Option<number>> = none;
assert.equal(x.flatten(), none);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten
Type parameters
Name |
---|
IT |
Parameters
Name | Type |
---|---|
this | Option <Option <IT >> |
Returns
Option
<IT
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1569
inspect
▸ inspect(cb
): IOption
<T
>
Calls the provided closure with a reference to the contained value (if Some
).
Seealso
inspectAsync for the awaitable version.
Example
some(2).inspect(console.log);
// Logs: 2
Example
none.inspect(console.log);
// Doesn't log
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => void | The predicate. |
Returns
IOption
<T
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1202
inspectAsync
▸ inspectAsync(cb
): Promise
<IOption
<T
>>
Calls the provided closure with a reference to the contained value (if Some
).
Seealso
inspect for the sync version.
Example
await some(2).inspectAsync(console.log);
// Logs: 2
Example
await none.inspectAsync(console.log);
// Doesn't log
Note
This is an extension not supported in Rust
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => Awaitable <void > | The predicate. |
Returns
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1221
intoPromise
▸ intoPromise(): Promise
<Option
<Awaited
<T
>>>
Returns a Promise
object with the awaited value (if Some
).
Example
let x = some(Promise.resolve(3));
assert.equal(await x.intoPromise(), some(3));
Note
This is an extension not supported in Rust
Returns
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1581
isNone
▸ isNone(): this is None
Returns true
if the option is a None
value.
Example
const x: Option<number> = some(2);
assert.equal(x.isNone(), false);
Example
const x: Option<number> = none;
assert.equal(x.isNone(), true);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none
Returns
this is None
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1002
isSome
▸ isSome(): this is Some<T>
Returns true
if the option is a Some
value.
Example
const x: Option<number> = some(2);
assert.equal(x.isSome(), true);
Example
const x: Option<number> = none;
assert.equal(x.isSome(), false);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some
Returns
this is Some<T>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:962
isSomeAnd
▸ isSomeAnd(cb
): boolean
Returns true
if the option is a Some
and the value inside of it matches a predicate.
Example
const x: Option<number> = some(2);
assert.equal(x.isSomeAnd((x) => x > 1), true);
Example
const x: Option<number> = some(0);
assert.equal(x.isSomeAnd((x) => x > 1), false);
Example
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
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => boolean | The predicate. |
Returns
boolean
Defined in
node_modules/@sapphire/result/dist/index.d.ts:985
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.
Example
const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
Example
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Returns
Generator
<T
, any
, unknown
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1286
map
▸ map<U
>(cb
): Option
<U
>
Maps an Option<T>
to Option<U>
by applying a function to a contained value.
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
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => U | The predicate. |
Returns
Option
<U
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1096
mapInto
▸ mapInto<Inner
>(cb
): Option
<Inner
>
Maps a Some<T>
to the returned Option<U>
by applying a function to a contained value, leaving None
untouched.
Example
const input: Option<string> = some('Hello, world!');
const result = input.mapInto((value) => some(value.length));
assert.equal(result, some(13));
Example
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
Type parameters
Name |
---|
Inner |
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => Option <Inner > | The predicate. |
Returns
Option
<Inner
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1119
mapNoneInto
▸ mapNoneInto<Inner
>(cb
): Option
<T
| Inner
>
Maps a None
to the returned Option<U>
by applying a function to a contained value, leaving Some<T>
untouched.
Example
const input: Option<string> = some('Hello, world!');
const result = input.mapNoneInto(() => some(13));
assert.equal(result, some('Hello, world!'));
Example
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
Type parameters
Name |
---|
Inner |
Parameters
Name | Type | Description |
---|---|---|
cb | () => Option <Inner > | The predicate. |
Returns
Option
<T
| Inner
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1183
mapOr
▸ mapOr<U
>(defaultValue
, cb
): 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.
Example
const x: Option<string> = some('hello');
assert.equal(x.mapOr(42, (value) => value.length), 5);
Example
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
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
defaultValue | U | The default value. |
cb | (value : T ) => U | The predicate. |
Returns
U
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1141
mapOrElse
▸ mapOrElse<U
>(defaultValue
, cb
): U
Computes a default function result (if none), or applies a different function to the contained value (if any).
Example
const x: Option<string> = some('hello');
assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
Example
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
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
defaultValue | () => U | The default value. |
cb | (value : T ) => U | The predicate. |
Returns
U
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1160
match
▸ match<SomeValue
, NoneValue
>(branches
): SomeValue
| NoneValue
Runs ok
function if self is Ok
, otherwise runs err
function.
Example
const option = some(4).match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 4);
Example
const option = none.match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 0);
Type parameters
Name |
---|
SomeValue |
NoneValue |
Parameters
Name | Type | Description |
---|---|---|
branches | Object | The branches to match. |
branches.none | () => NoneValue | - |
branches.some | (value : T ) => SomeValue | - |
Returns
SomeValue
| NoneValue
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1617
ne
▸ ne(other
): boolean
Checks whether or not other
doesn't equal with self.
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne
Parameters
Name | Type | Description |
---|---|---|
other | Option <T > | The other option to compare. |
Returns
boolean
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1595
okOr
▸ okOr<E
>(err
): 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.
Example
const x: Option<string> = some('hello');
assert.equal(x.okOr(0), ok('hello'));
Example
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
Type parameters
Name |
---|
E |
Parameters
Name | Type | Description |
---|---|---|
err | E | The error to be used. |
Returns
Result
<T
, E
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1242
okOrElse
▸ okOrElse<E
>(cb
): Result
<T
, E
>
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
.
Example
const x: Option<string> = some('hello');
assert.equal(x.okOrElse(() => 0), ok('hello'));
Example
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
Type parameters
Name |
---|
E |
Parameters
Name | Type | Description |
---|---|---|
cb | () => E | The error to be used. |
Returns
Result
<T
, E
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1260
or
▸ or(option
): Option
<T
>
Returns the option if it contains a value, otherwise returns option
.
Example
const x: Option<number> = some(2);
const y: Option<number> = none;
assert.equal(x.or(y), some(2));
Example
const x: Option<number> = none;
const y: Option<number> = some(100);
assert.equal(x.or(y), some(100));
Example
const x: Option<number> = some(2);
const y: Option<number> = some(100);
assert.equal(x.or(y), some(2));
Example
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
Parameters
Name | Type | Description |
---|---|---|
option | Option <T > | The option. |
Returns
Option
<T
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1370
orElse
▸ orElse(cb
): Option
<T
>
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.
Example
const nobody = (): Option<string> => none;
const vikings = (): Option<string> => some('vikings');
assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
assert.equal(none.orElse(vikings), some('vikings'));
assert.equal(none.orElse(nobody), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else
Parameters
Name | Type | Description |
---|---|---|
cb | () => Option <T > | The predicate. |
Returns
Option
<T
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1389
transpose
▸ transpose<IT
, E
>(this
): Result
<Option
<IT
>, E
>
Transposes an Option
of a Result
into a Result
of an Option
.
none
will be mapped to ok(none)
. some(ok(v))
and some(err(e))
will be mapped to ok(some(v))
and err(e)
.
Example
const x: Option<Result<number, Error>> = some(ok(5));
const y: Result<Option<number>, Error> = ok(some(5));
assert.equal(x.transpose(), y);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose
Type parameters
Name |
---|
IT |
E |
Parameters
Name | Type |
---|---|
this | Option <Result <IT , E >> |
Returns
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1547
unwrap
▸ unwrap(): T
Returns the contained Some
value.
If the value is an Err
, it throws an OptionError with the message.
Seealso
Seealso
Example
const x: Option<string> = some(2);
assert.equal(x.unwrap(), 2);
Example
const x: Option<string> = none;
assert.throws(() => x.unwrap(), {
name: 'OptionError',
message: 'Unwrap failed'
});
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap
Returns
T
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1048
unwrapOr
▸ unwrapOr<V
>(defaultValue
): T
| V
Returns the contained Some
value or a provided default.
Arguments passed to unwrapOr
are eagerly evaluated; if you are passing the result of a function call, it is
recommended to use unwrapOrElse, which is lazily evaluated.
Example
assert.equal(some(2).unwrapOr(0), 2);
Example
assert.equal(none.unwrapOr(0), 0);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or
Type parameters
Name |
---|
V |
Parameters
Name | Type |
---|---|
defaultValue | V |
Returns
T
| V
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1066
unwrapOrElse
▸ unwrapOrElse<V
>(cb
): T
| V
Returns the contained Some value or computes it from a closure.
Example
assert.equal(some(2).unwrapOrElse(() => 0), 2);
Example
assert.equal(none.unwrapOrElse(() => 0), 0);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else
Type parameters
Name |
---|
V |
Parameters
Name | Type |
---|---|
cb | () => V |
Returns
T
| V
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1081
unzip
▸ unzip<Inner
, U
>(this
): [Option
<Inner
>, Option
<U
>]
Unzips an option containing a tuple of two options.
If self is Some((a, b))
this method returns [Some(a), Some(b)]
. Otherwise, [None, None]
is returned.
Example
const x: Option<[number, string]> = some([1, 'hi']);
assert.equal(x.unzip(), [some(1), some('hi')]);
Example
const x: Option<[number, string]> = none;
assert.equal(x.unzip(), [none, none]);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip
Type parameters
Name |
---|
Inner |
U |
Parameters
Name | Type |
---|---|
this | Option <readonly [Inner , U ]> |
Returns
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1532
xor
▸ xor(option
): Option
<T
>
Returns Some
if exactly one of self or option
is Some
, otherwise returns None
.
Example
const x: Option<number> = some(2);
const y: Option<number> = none;
assert.equal(x.xor(y), some(2));
Example
const x: Option<number> = none;
const y: Option<number> = some(2);
assert.equal(x.xor(y), some(2));
Example
const x: Option<number> = some(2);
const y: Option<number> = some(2);
assert.equal(x.xor(y), none);
Example
const x: Option<number> = none;
const y: Option<number> = none;
assert.equal(x.xor(y), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.xor
Parameters
Name | Type | Description |
---|---|---|
option | Option <T > | The option to compare. |
Returns
Option
<T
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1421
zip
▸ zip<U
>(other
): Option
<[T
, U
]>
Zips self with another Option
.
If self is Some(s)
and other
is Some(o)
, this method returns Some([s, o])
. Otherwise, None
is returned.
Example
const x = some(1);
const y = some('hi');
const z = none;
assert.equal(x.zip(y), some([1, 'hi']));
assert.equal(x.zip(z), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.zip
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
other | Option <U > | The option to zip self with. |
Returns
Option
<[T
, U
]>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1484
zipWith
▸ zipWith<U
, R
>(other
, f
): Option
<R
>
Zips self and another Option
with function f
.
If self is Some(s)
and other is Some(o)
, this method returns Some(f(s, o))
. Otherwise, None
is returned.
Example
class Point {
public readonly x: number;
public readonly y: number;
public constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const x = some(17.5);
const y = some(42.7);
assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);
See
https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with
Type parameters
Name |
---|
U |
R |
Parameters
Name | Type | Description |
---|---|---|
other | Option <U > | The option to zip self with. |
f | (s : T , o : U ) => R | The function that computes the returned value. |
Returns
Option
<R
>
Defined in
node_modules/@sapphire/result/dist/index.d.ts:1513