From 4916289b10a120640c9c85688d7210ec184fa893 Mon Sep 17 00:00:00 2001 From: George Wilson Date: Fri, 19 May 2023 18:15:35 +1000 Subject: [PATCH] Backport Option.orElse from effect --- src/Option.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Option.ts b/src/Option.ts index 2d074d841..a76886eab 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -901,6 +901,28 @@ export const getOrElseW = */ export const getOrElse: (onNone: LazyArg) => (ma: Option) => 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: { + (that: LazyArg>): (self: Option) => Option + (self: Option, that: LazyArg>): Option +} = dual(2, (self: Option, that: LazyArg>): Option => (isNone(self) ? that() : self)) + /** * @category mapping * @since 2.10.0