Gets true
if the option is a None
value.
Basic usage:
const x: Option<number> = Some(2);
assert.equal(x.isNone, false);
const y: Option<number> = None();
assert.equal(y.isNone, true);
Gets true
if the option is a Some
value.
Basic usage:
const x: Option<number> = Some(2);
assert.equal(x.isSome, true);
const y: Option<number> = None();
assert.equal(y.isSome, false);
Returns None
if the option is None
, otherwise returns optb
.
Basic usage:
const x1 = Some(2);
const y1: Option<string> = None();
assert.deepEqual(x1.and(y1), None());
const x2: Option<number> = None();
const y2 = Some('foo');
assert.deepEqual(x2.and(y2), None());
const x3 = Some(2);
const y3 = Some('foo');
assert.deepEqual(x3.and(y3), Some('foo'));
const x4: Option<number> = None();
const y4: Option<string> = None();
assert.deepEqual(x4.and(y4), None());
another option
Returns None
if the option is None
, otherwise calls cb
with the wrapped value and
returns the result.
Some languages call this operation flatmap.
Basic usage:
const sq: (x: number): Option<number> = x => Some(x * x);
const nope: () => Option<number> = () => None();
assert.deepEqual(Some(2).andThen(sq).andThen(sq), Some(16));
assert.deepEqual(Some(2).andThen(sq).andThen(nope), None());
assert.deepEqual(Some(2).andThen(nope).andThen(sq), None());
assert.deepEqual(None().andThen(sq).andThen(sq), None());
callback to map wrapped value
Unwraps an option, yielding the content of a Some
.
Basic usage:
const x = Some('value');
assert.equal(x.expect('the world is ending'), 'value');
const y: Option<string> = None();
y.expect('the world is ending'); // throws an error with `the world is ending`
Throws an error if the value is a None
with a custom error message provided by msg
.
custom error message
Maps an Option<T>
to Option<U>
by applying a function to a contained value.
Basic usage:
Convert an Option<string>
into an Option<number>
, consuming the original:
const maybeSomeString = Some('Hello, World!');
const maybeSomeLength = maybeSomeString.map(s => s.length);
assert.deepEqual(maybeSomeLength, Some(13));
callback to map wrapped value
Applies a function to the contained value (if any), or returns a def
(if not).
Basic usage:
const x = Some('foo');
assert.equal(x.mapOr(42, v => v.length), 3);
const y: Option<string> = None();
assert.equal(y.mapOr(42, v => v.length), 42);
default value
callback to map wrapped value
Applies a function to the contained value (if any), or computes a def
(if not).
Basic usage:
const k = 21;
const x = Some('foo');
assert.equal(x.mapOrElse(() => 2 * k, v => v.length), 3);
const y: Option<string> = None();
assert.equal(y.mapOrElse(() => 2 * k, v => v.length), 42);
callback to calculate default value
callback to map wrapped value
Matches option to the received matcher and executes the appropriate branch.
Basic usage:
const x1 = Some(45);
const y1 = x1.match({
Some: v => v + 5,
None: () => 10
});
assert.equal(y1, 50);
const x2 = None();
const y2 = x2.match({
Some: v => v + 5,
None: () => 10
});
assert.equal(y2, 10);
const x3 = Some('foo');
x3.match({
Some: v => console.log(v), // prints `foo`
None: () => console.log('nothing')
});
an object to perform pattern matching
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err)
.
Basic usage:
const x = Some('foo');
assert.deepEqual(x.okOr(0), Ok('foo'));
const y: Option<string> = None();
assert.deepEqual(y.okOr(0), Err(0));
custom error value
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
.
Basic usage:
const x = Some('foo');
assert.deepEqual(x.okOrElse(() => 0), Ok('foo'));
const y: Option<string> = None();
assert.deepEqual(y.okOrElse(() => 0), Err(0));
callback to calculate custom error value
Returns the option if it contains a value, otherwise returns optb
.
Basic usage:
const x1 = Some(2);
const y1 = None();
assert.deepEqual(x1.or(y1), Some(2));
const x2 = None();
const y2 = Some(100);
assert.deepEqual(x2.or(y2), Some(100));
const x3 = Some(2);
const y3 = Some(100);
assert.deepEqual(x3.or(y3), Some(2));
const x4: Option<number> = None();
const y4 = None();
assert.deepEqual(x4.or(y4), None());
assert.deepEqual(x4.or(y4), None());
another option
Returns the option if it contains a value, otherwise calls cb
and returns the result.
Basic usage:
const nobody: () => Option<string> = () => None();
const vikings: () => Option<string> = () => Some('vikings');
assert.deepEqual(Some('barbarians').orElse(vikings), Some('barbarians'));
assert.deepEqual(None().orElse(vikings), Some('vikings'));
assert.deepEqual(None().orElse(nobody), None());
callback to calculate default option
Moves the value v
out of the Option<T>
if it is Some(v)
.
In general, because this function may throw, its use is discouraged. Instead, prefer to
use Option.match and handle the None
case explicitly.
Basic usage:
const x = Some('air');
assert.equal(x.unwrap(), 'air');
const y: Option<string> = None();
assert.equal(y.unwrap(), 'air'); // fails
Throws an error if the self value equals None
.
Returns the contained value or a default.
Basic usage:
assert.equal(Some('car').unwrapOr('bike'), 'car');
assert.equal(None().unwrapOr('bike'), 'bike');
default value
Returns the contained value or computes it from a closure.
Basic usage:
const k = 10;
assert.equal(Some(4).unwrapOrElse(() => 2 * k), 4);
assert.equal(None().unwrapOrElse(() => 2 * k), 20);
callback to calculate default value
Generated using TypeDoc
The
Option
type. See the option module documentation for more.