Class: Some<T>
Type parameters
Name |
---|
T |
Implements
IOption
<T
>
Constructors
constructor
• new Some<T
>(value
): Some
<T
>
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
value | T |
Returns
Some
<T
>
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1651
Properties
value
• Private
Readonly
value: any
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1650
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
>
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1700
and
▸ and<R
>(option
): R
Returns None
if the option is None
, otherwise returns option
.
Type parameters
Name | Type |
---|---|
R | extends Option <any > |
Parameters
Name | Type | Description |
---|---|---|
option | R | The option. |
Returns
R
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1669
andThen
▸ andThen<R
>(cb
): R
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
Name | Type |
---|---|
R | extends Option <any > |
Parameters
Name | Type | Description |
---|---|---|
cb | (value : T ) => R | The predicate. |
Returns
R
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1670
contains
▸ contains(value
): boolean
Returns true
if the option is a Some
value containing the given value.
Parameters
Name | Type | Description |
---|---|---|
value | T | The value to compare. |
Returns
boolean
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1679
eq
▸ eq(other
): false
Checks whether or not other
equals with self.
Parameters
Name | Type | Description |
---|---|---|
other | None | The other option to compare. |
Returns
false
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1692
▸ eq(other
): boolean
Parameters
Name | Type |
---|---|
other | Option <T > |
Returns
boolean
Implementation of
IOption.eq
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1693
expect
▸ expect(message
): T
Returns the contained Some
value.
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
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1655
filter
▸ filter(predicate
): this
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
.
Parameters
Name | Type | Description |
---|---|---|
predicate | (value : T ) => true | The predicate. |
Returns
this
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
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1676
▸ filter(predicate
): None
Parameters
Name | Type |
---|---|
predicate | (value : T ) => false |
Returns
Implementation of
IOption.filter
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1677