Class: Some<T>
Type Parameters
Type Parameter |
---|
T |
Implements
IOption
<T
>
Constructors
new Some()
new Some<
T
>(value
:T
):Some
<T
>
Parameters
Parameter | Type |
---|---|
value | T |
Returns
Some
<T
>
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:12
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:181
and()
and<
R
>(option
:R
):R
Returns None
if the option is None
, otherwise returns option
.
Type Parameters
Type Parameter |
---|
R extends Option <any > |
Parameters
Parameter | Type | Description |
---|---|---|
option | R | The option. |
Returns
R
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:92
andThen()
andThen<
R
>(cb
: (value
:T
) =>R
):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
Type Parameter |
---|
R extends Option <any > |
Parameters
Parameter | 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
projects/utilities/packages/result/src/lib/Option/Some.ts:96
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:124
eq()
eq(other)
eq(
other
:None
):false
Checks whether or not other
equals with self.
Parameters
Parameter | 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
projects/utilities/packages/result/src/lib/Option/Some.ts:165
eq(other)
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
Implementation of
IOption.eq
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:166
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:28
filter()
filter(predicate)
filter(
predicate
: (value
:T
) =>true
):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
Parameter | 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
projects/utilities/packages/result/src/lib/Option/Some.ts:117
filter(predicate)
filter(
predicate
: (value
:T
) =>false
):None
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
Parameter | Type | Description |
---|---|---|
predicate | (value : T ) => false | The predicate. |
Returns
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
IOption.filter
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:118
filter(predicate)
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
Parameter | Type | Description |
---|---|---|
predicate | (value : T ) => boolean | The predicate. |
Returns
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
IOption.filter
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:119
flatten()
flatten<
Inner
>(this
:Some
<Inner
>):Inner
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
Type Parameters
Type Parameter |
---|
Inner extends Option <any > |
Parameters
Parameter | Type |
---|---|
this | Some <Inner > |
Returns
Inner
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:157
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:68
inspectAsync()
Calls the provided closure with a reference to the contained value (if Some
).
Parameters
Parameter | Type | Description |
---|---|---|
cb | (value : T ) => unknown | 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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:73
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:161
isNone()
isNone():
false
Returns true
if the option is a None
value.
Returns
false
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:24
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:16
isSomeAnd()
isSomeAnd<
R
>(cb
: (value
:T
) =>R
):R
Returns true
if the option is a Some
and the value inside of it matches a predicate.
Type Parameters
Type Parameter |
---|
R extends boolean |
Parameters
Parameter | Type | Description |
---|---|---|
cb | (value : T ) => R | The predicate. |
Returns
R
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:20
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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:88
map()
map<
U
>(cb
: (value
:T
) =>U
):Some
<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
Some
<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
Implementation of
Defined in
projects/utilities/packages/result/src/lib/Option/Some.ts:47
mapInto()
mapInto<
R
>(cb
: (value
:T
) =>R
):R
Maps a Some<T>
to the returned Option<U>
by applying a function to a contained value, leaving None
untouched.
Type Parameters
Type Parameter |
---|
R extends Option <any > |
Parameters
Parameter | Type | Description |
---|---|---|
cb | (value : T ) => R | The predicate. |
Returns
R
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