Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport Option.orElse from effect/ #1868

Merged
merged 1 commit into from
May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,28 @@ export const getOrElseW =
*/
export const getOrElse: <A>(onNone: LazyArg<A>) => (ma: Option<A>) => A = getOrElseW

/**
* Returns the provided `Option` `that` if `self` is `None`, otherwise returns `self`.
*
* @param self - The first `Option` to be checked.
* @param that - The `Option` to return if `self` is `None`.
*
* @example
* import * as O from "fp-ts/Option"
*
* assert.deepStrictEqual(O.orElse(O.none, () => O.none), O.none)
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.none), O.some(1))
* assert.deepStrictEqual(O.orElse(O.none, () => O.some('b')), O.some('b'))
* assert.deepStrictEqual(O.orElse(O.some(1), () => O.some('b')), O.some(1))
*
* @category error handling
* @since 2.16.0
*/
export const orElse: {
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<A | B>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
} = dual(2, <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B> => (isNone(self) ? that() : self))

/**
* @category mapping
* @since 2.10.0
Expand Down