Option

Option represents an optional value: every Option is either Some, and contains a value, or None, and it's empty.

It is commonly used to represent the result of a function that may not return a value due to failure or missing data, such as a network request, a file read, or a database query.

Constructor

some

Constructs a Some Option, representing an optional value that exists.

import { Option } from 'funkcia';

//       β”Œβ”€β”€β”€ Option<number>
//       β–Ό
const option = Option.some(10);
// Output: Some(10)

of

Alias of Option.some

Constructs a Some Option, representing an optional value that exists.

import { Option } from 'funkcia';

//       β”Œβ”€β”€β”€ Option<number>
//       β–Ό
const option = Option.of(10);
// Output: Some(10)

none

Constructs a None Option, representing an optional value that does not exist.

fromNullable

Constructs an Option from a nullable value.

If the value is null or undefined, it returns an Option.None. Otherwise, it returns an Option.Some containing the value.

fromFalsy

Constructs an Option from a falsy value.

If the value is falsy, it returns an Option.None. Otherwise, it returns an Option.Some with the value.

try

Constructs an Option from a function that may throw.

If the function throws or returns null or undefined, it returns an Option.None. Otherwise, it returns an Option.Some with the value.

firstSomeOf

Returns the first Option.Some value in the iterable. If all values are Option.None, returns Option.None.

predicate

Returns a function that asserts that a value passes the test implemented by the provided function, creating an Option.Some narrowing down the value to the provided type predicate if the predicate is fulfilled. If the test fails, returns an Option.None instead.

fun

Declare a function that always returns an Option.

enhance

Converts a function that may throw or return a nullable value to an enhanced function that returns an Option.

values

Given an array of Options, returns an array containing only the values inside Some.

is

Asserts that an unknown value is an Option.

Instance methods

map

Applies a callback function to the value of the Option when it is Some, returning a new Option containing the new value.

andThen

Applies a callback function to the value of the Option when it is Some, and returns the new value. Similar to chain (also known as flatMap), with the difference that the callback must return an Option, not a raw value.

filter

Asserts that the Option value passes the test implemented by the provided function. If the test fails, the value is filtered out of the Option, returning a None instead.

or

Replaces the current Option with the provided fallback Option when it is None.

If the current Option is Some, it returns the current Option.

zip

Combines two Options into a single Option containing a tuple of their values, if both Options are Some variants, otherwise, returns None.

zipWith

Combines two Options into a single Option. The new value is produced by applying the given function to both values, if both Options are Some variants, otherwise, returns None.

match

Compare the Option against the possible patterns and then execute code based on which pattern matches.

unwrap

Unwraps the Option value.

unwrapOr

Unwraps the Option value.

If the Option is None, returns the result of the provided callback.

unwrapOrNull

Use this method at the edges of the system, when storing values in a database or serializing to JSON.

Unwraps the value of the Option if it is a Some, otherwise returns null.

unwrapOrUndefined

Use this method at the edges of the system, when storing values in a database or serializing to JSON.

Unwraps the value of the Option if it is a Some, otherwise returns undefined.

expect

Unwraps the Option value.

contains

Verifies if the Option contains a value that passes the test implemented by the provided function.

  • Returns true if the predicate is fullfiled by the wrapped value.

  • If the predicate is not fullfiled or if the Option is None, returns false.

toResult

Converts an Option to a Result.

If Option is Some, returns a Result.ok. If Option is None, returns a Result.error with a NoValueError or a custom error.

toAsyncOption

Converts the Option to an AsyncOption.

toAsyncResult

Converts the Option to an AsyncResult.

toArray

Converts an Option to an array.

If Option is Some, returns an array with the value. If Option is None, returns an empty array.

tap

Calls the function with the Option value, then returns the Option itself.

The return value of the provided function is ignored. This allows "tapping into" a function sequence in a pipe, to perform side effects on intermediate results

isSome

Returns true if the Option contains a value.

isNone

Returns true if the Option is empty.

equals

By default, it uses referential equality to compare the values, but you can provide a custom equality function for more complex cases.

Compares the Option with another Option and returns true if they are equal.

Last updated

Was this helpful?