From c9cc0b0f2239e8d39abca471f04157e6900adb0e Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 19 Dec 2019 14:34:48 +0200 Subject: [PATCH] typescript(option): feat: added a "flat" helper Signed-off-by: prescientmoon --- typescript/option/README.md | 2 +- typescript/option/src/helpers.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/typescript/option/README.md b/typescript/option/README.md index 7aefdc9..7c24f01 100644 --- a/typescript/option/README.md +++ b/typescript/option/README.md @@ -16,7 +16,7 @@ npm install @adrielus/option Curently there are no docs, but all functions have the same type definition as the ones from [fsharp](https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/core.option-module-%5Bfsharp%5D) (except the ones from my package are not curreied) -There are also a few original helpers (match, and withDefault), but I'ts pretty easy to guess what those do +There are also a few original helpers (match, flat and withDefault), but I'ts pretty easy to guess what those do # Contributing diff --git a/typescript/option/src/helpers.ts b/typescript/option/src/helpers.ts index 7e4017c..2351ffc 100644 --- a/typescript/option/src/helpers.ts +++ b/typescript/option/src/helpers.ts @@ -1,7 +1,7 @@ import { Option, Some, None } from './types' import { Binder, Folder, Mapper, Predicate, BackFolder } from './internalTypes' import { always, identity } from './internalHelperts' -import Internals, { SomeClass } from './internals' +import Internals, { SomeClass, isOption } from './internals' export const isSome = (option: Option) => option instanceof Internals.SomeClass @@ -89,3 +89,7 @@ export const toNullable = (option: Option) => { export const withDefault = (_default: T, option: Option) => { return match(option, identity, always(_default)) } + +export const flat = (option: Option>) => { + return bind(inner => (isSome(inner) ? flat(inner) : inner), option) +}