A new Optional<S2>
resulting from the mapping, or
Optional.None
if the input was Optional.None
.
const numberOptional = Optional.some(21);
const mapped = Optional.map(numberOptional, (value) => value * 2);
assert.deepStrictEqual(mapped, Optional.some(42));
const mapToLength = Optional.map((text: string) => text.length);
assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));
assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);
Maps an Optional to OptionalmapFn
to the
value in Optional.Some
and returns a new Optional.Some
with the
result.
The type of the value returned by the mapping function.
A new Optional<S2>
resulting from the mapping, or
Optional.None
if the input was Optional.None
.
const numberOptional = Optional.some(21);
const mapped = Optional.map(numberOptional, (value) => value * 2);
assert.deepStrictEqual(mapped, Optional.some(42));
const mapToLength = Optional.map((text: string) => text.length);
assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));
assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);
Maps an Optional
to Optional by applying a function to
a contained value. If the Optional is Optional.None, it
returns Optional.none. Otherwise, it applies the mapFn
to the value inOptional.Some
and returns a newOptional.Some
with the result.