1
Fork 0

typescript(option): feat: added a "flat" helper

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-19 14:34:48 +02:00 committed by prescientmoon
parent 17569d9417
commit c9cc0b0f22
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 6 additions and 2 deletions

View file

@ -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) 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 # Contributing

View file

@ -1,7 +1,7 @@
import { Option, Some, None } from './types' import { Option, Some, None } from './types'
import { Binder, Folder, Mapper, Predicate, BackFolder } from './internalTypes' import { Binder, Folder, Mapper, Predicate, BackFolder } from './internalTypes'
import { always, identity } from './internalHelperts' import { always, identity } from './internalHelperts'
import Internals, { SomeClass } from './internals' import Internals, { SomeClass, isOption } from './internals'
export const isSome = <T>(option: Option<T>) => export const isSome = <T>(option: Option<T>) =>
option instanceof Internals.SomeClass option instanceof Internals.SomeClass
@ -89,3 +89,7 @@ export const toNullable = <T>(option: Option<T>) => {
export const withDefault = <T>(_default: T, option: Option<T>) => { export const withDefault = <T>(_default: T, option: Option<T>) => {
return match(option, identity, always(_default)) return match(option, identity, always(_default))
} }
export const flat = <T>(option: Option<Option<T>>) => {
return bind(inner => (isSome(inner) ? flat(inner) : inner), option)
}