const x = new Some(2);
const y = new None();
expect(x.and(y)).toEqual(new None());
const x = new None();
const y = new Some("foo");
expect(x.and(y)).toEqual(new None());
const x = new Some(2);
const y = new Some("foo");
expect(x.and(y)).toEqual(new Some("foo"));
const x = new None();
const y = new None();
expect(x.and(y)).toEqual(new None());
Returns None
if the option is a None
, otherwise calls fn
with the
wrapped value and returns the result.
Some languages call this operation flatMap.
const sq = (x: number) => new Some(x * x);
const nope = (x: number) => new None();
expect(new Some(2).andThen(sq).andThen(sq)).toEqual(new Some(16));
expect(new Some(2).andThen(sq).andThen(nope)).toEqual(new None());
expect(new Some(2).andThen(nope).andThen(sq)).toEqual(new None());
expect(new None().andThen(sq).andThen(sq)).toEqual(new None());
Returns the contained Some
value.
Some(t)
if predicate
returns true
(where t
is the wrapped value), andNone
if predicate
returns false
.const isEven = (x: number) => x % 2 === 0;
expect(new None().filter(isEven)).toEqual(new None());
expect(new Some(3).filter(isEven)).toEqual(new None());
expect(new Some(4).filter(isEven)).toEqual(new Some(4));
Maps an Option<T>
to an Option<U>
by applying a function to a contained value.
Converts an Option<string> into an
Option
const maybeSomeString = new Some ("Hello, World!");
const maybeSomeLength = maybeSomeString.map(s => s.length);
expect (maybeSomeLength).toEqual(new Some(13));
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.
const x = new Some("foo");
expect(x.mapOr(42, s => s.length)).toBe(3);
const x = new None();
expect(x.mapOr(42, s => s.length)).toBe(42);
Computes a default function result (if none), or applies a different function to the contained value (if any).
const x = new Some("foo");
expect(x.mapOrElse(() => 2 * 21, s => s.length)).toBe(3);
const x = new None();
expect(x.mapOrElse(() => 2 * 21, s => s.length)).toBe(42);
Returns the option if it contains a value, otherwise returns optB
.
Arguments passed to or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use orElse
, which is
lazily evaluated.
const x = new Some(2);
const y = new None();
expect(x.or(y)).toEqual(new Some(2));
const x = new None();
const y = new Some(100);
expect(x.or(y)).toEqual(new Some(100));
const x = new Some(2);
const y = new Some(100);
expect(x.or(y)).toEqual(new Some(2));
const x = new None();
const y = new None();
expect(x.or(y)).toEqual(new None());
Returns the option if it contains a value, otherwise calls fn
and
returns the result.
const nobody = () => new None();
const vikings = () => new Some("vikings");
expect(new Some("barbarians").orElse(vikings)).toEqual(new Some("barbarians"));
expect(new None().orElse(vikings)).toEqual(new Some("vikings"));
expect(new None().orElse(nobody)).toEqual(new None());
Returns the contained Some
value.
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.
expect(new Some("car").unwrapOr("bike")).toBe("car");
expect(new None().unwrapOr("bike")).toBe("bike");
const x = new Some(2);
const y = new None();
expect(x.xor(y)).toEqual(new Some(2));
const x = new None();
const y = new Some(2);
expect(x.xor(y)).toEqual(new Some(2));
const x = new Some(2);
const y = new Some(2);
expect(x.xor(y)).toEqual(new None());
const x = new None();
const y = new None();
expect(x.xor(y)).toEqual(new None());
Zips this
with another Option
.
If this
is Some(s)
and other
is Some(o)
, this method returns Some([s, o])
.
Otherwise, it returns None
.
const x = new Some(1);
const y = new Some("hi");
const z = new None();
expect(x.zip(y)).toEqual(new Some([1, "hi"]));
expect(x.zip(z)).toEqual(new None());
Zips this
and another Option
with function fn
.
If this
is Some(s)
and other
is Some(o)
, this method returns Some(fn([s, o]))
.
Otherwise, it returns None
.
const x = new Some(17.5);
const y = new Some(42.7);
const point = (x: number, y: number) => ({ x, y });
expect(x.zipWith(y, point)).toEqual(new Some({ x: 17.5, y: 42.7 }));
expect(x.zipWith(new None(), point)).toEqual(new None());
Generated using TypeDoc
A
Some
is anOption
that contains a value.