1
Fork 0

typescript(option): fix: fixed the flat helper returning a non-option value

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-19 15:12:01 +02:00 committed by prescientmoon
parent c9cc0b0f22
commit 1fda9e8242
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4

View file

@ -90,6 +90,18 @@ 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>>) => { const checkIfOption = <T>(x): x is Option<T> => x[isOption]
return bind(inner => (isSome(inner) ? flat(inner) : inner), option)
export const flat = <T>(option: Option<T>) => {
return match(
option,
inner => {
if (checkIfOption(inner)) {
return flat(inner)
} else {
return Some(inner)
}
},
always(None)
)
} }