diff --git a/typescript/option/src/helpers/external.ts b/typescript/option/src/helpers/external.ts index 7995d8d..408312a 100644 --- a/typescript/option/src/helpers/external.ts +++ b/typescript/option/src/helpers/external.ts @@ -16,3 +16,4 @@ export * from './map' export * from './toArray' export * from './toNullable' export * from './withDefault' +export * from './mapAsync' diff --git a/typescript/option/src/helpers/mapAsync.ts b/typescript/option/src/helpers/mapAsync.ts new file mode 100644 index 0000000..05b8ea4 --- /dev/null +++ b/typescript/option/src/helpers/mapAsync.ts @@ -0,0 +1,18 @@ +import { Option, None, Some } from '../types' +import { Mapper } from '../internalTypes' +import { match } from './match' + +export const mapAsync = ( + mapper: Mapper>, + option: Option +) => { + return match( + async value => { + const output = await mapper(value) + + return Some(output) + }, + Promise.resolve(None), + option + ) +}