diff --git a/typescript/option/src/helpers/bind.ts b/typescript/option/src/helpers/bind.ts index 8dd234d..8f16419 100644 --- a/typescript/option/src/helpers/bind.ts +++ b/typescript/option/src/helpers/bind.ts @@ -1,10 +1,10 @@ import { Binder } from '../internalTypes' import { Option, None } from '../types' -import { match } from './match' +import { unwrap } from './unwrap' export const bind = ( binder: Binder, option: Option ): Option => { - return match(binder, None, option) + return unwrap(None, binder, option) } diff --git a/typescript/option/src/helpers/bindAsync.ts b/typescript/option/src/helpers/bindAsync.ts index 30f8955..32d55df 100644 --- a/typescript/option/src/helpers/bindAsync.ts +++ b/typescript/option/src/helpers/bindAsync.ts @@ -1,10 +1,10 @@ import { Mapper } from '../internalTypes' import { Option, None } from '../types' -import { match } from './match' +import { unwrap } from './unwrap' export const bindAsync = ( binder: Mapper>>, option: Option ): Promise> => { - return match(binder, Promise.resolve(None), option) + return unwrap(Promise.resolve(None), binder, option) } diff --git a/typescript/option/src/helpers/exists.ts b/typescript/option/src/helpers/exists.ts index 9b70af7..c984532 100644 --- a/typescript/option/src/helpers/exists.ts +++ b/typescript/option/src/helpers/exists.ts @@ -1,7 +1,7 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Predicate } from '../internalTypes' import { Option } from '../types' export const exists = (predicate: Predicate, option: Option) => { - return match(predicate, false, option) + return unwrap(false, predicate, option) } diff --git a/typescript/option/src/helpers/filter.ts b/typescript/option/src/helpers/filter.ts index 592b60d..04891a6 100644 --- a/typescript/option/src/helpers/filter.ts +++ b/typescript/option/src/helpers/filter.ts @@ -1,7 +1,7 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Some, None, Option } from '../types' import { Predicate } from '../internalTypes' export const filter = (predicate: Predicate, option: Option) => { - return match(v => (predicate(v) ? Some(v) : None), None, option) + return unwrap(None, v => (predicate(v) ? Some(v) : None), option) } diff --git a/typescript/option/src/helpers/fold.ts b/typescript/option/src/helpers/fold.ts index 99f67b8..7ddb023 100644 --- a/typescript/option/src/helpers/fold.ts +++ b/typescript/option/src/helpers/fold.ts @@ -1,4 +1,4 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Option } from '../types' import { Folder } from '../internalTypes' @@ -7,5 +7,5 @@ export const fold = ( initial: U, option: Option ) => { - return match(v => folder(initial, v), initial, option) + return unwrap(initial, v => folder(initial, v), option) } diff --git a/typescript/option/src/helpers/foldback.ts b/typescript/option/src/helpers/foldback.ts index 564dda4..dedcf88 100644 --- a/typescript/option/src/helpers/foldback.ts +++ b/typescript/option/src/helpers/foldback.ts @@ -1,4 +1,4 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Option } from '../types' import { BackFolder } from '../internalTypes' @@ -7,5 +7,5 @@ export const foldback = ( option: Option, initial: U ) => { - return match(v => folder(v, initial), initial, option) + return unwrap(initial, v => folder(v, initial), option) } diff --git a/typescript/option/src/helpers/forall.ts b/typescript/option/src/helpers/forall.ts index 6321261..634d0d2 100644 --- a/typescript/option/src/helpers/forall.ts +++ b/typescript/option/src/helpers/forall.ts @@ -1,7 +1,7 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Predicate } from '../internalTypes' import { Option } from '../types' export const forall = (predicate: Predicate, option: Option) => { - return match(predicate, true, option) + return unwrap(true, predicate, option) } diff --git a/typescript/option/src/helpers/map.ts b/typescript/option/src/helpers/map.ts index 672b6a8..5c53f16 100644 --- a/typescript/option/src/helpers/map.ts +++ b/typescript/option/src/helpers/map.ts @@ -1,4 +1,4 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Mapper } from '../internalTypes' import { Option, Some, None } from '../types' @@ -6,5 +6,5 @@ export const map = ( mapper: Mapper, option: Option ): Option => { - return match(v => Some(mapper(v)), None, option) + return unwrap(None, v => Some(mapper(v)), option) } diff --git a/typescript/option/src/helpers/mapAsync.ts b/typescript/option/src/helpers/mapAsync.ts index ebf7989..08c124c 100644 --- a/typescript/option/src/helpers/mapAsync.ts +++ b/typescript/option/src/helpers/mapAsync.ts @@ -1,14 +1,14 @@ import { Option, None, Some } from '../types' import { Mapper } from '../internalTypes' -import { match } from './match' +import { unwrap } from './unwrap' export const mapAsync = ( mapper: Mapper>, option: Option ) => { - return match( - value => mapper(value).then(Some), + return unwrap( Promise.resolve(None), + value => mapper(value).then(Some), option ) } diff --git a/typescript/option/src/helpers/match.ts b/typescript/option/src/helpers/match.ts deleted file mode 100644 index dda77b6..0000000 --- a/typescript/option/src/helpers/match.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Option } from '../types' -import { Mapper } from '../internalTypes' -import { isSome } from './isSome' - -export const match = ( - caseSome: Mapper, - _default: U, - option: Option -) => { - if (isSome(option)) { - return caseSome(option as T) - } - - return _default -} diff --git a/typescript/option/src/helpers/toArray.ts b/typescript/option/src/helpers/toArray.ts index e135960..d704e2d 100644 --- a/typescript/option/src/helpers/toArray.ts +++ b/typescript/option/src/helpers/toArray.ts @@ -1,6 +1,6 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { Option } from '../types' export const toArray = (option: Option) => { - return match(v => [v], [], option) + return unwrap([], v => [v], option) } diff --git a/typescript/option/src/helpers/toNullable.ts b/typescript/option/src/helpers/toNullable.ts index 7a7d072..2275211 100644 --- a/typescript/option/src/helpers/toNullable.ts +++ b/typescript/option/src/helpers/toNullable.ts @@ -1,7 +1,7 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { identity } from '@thi.ng/compose' import { Option } from '../types' export const toNullable = (option: Option) => { - return match(identity, null, option) + return unwrap(null, identity, option) } diff --git a/typescript/option/src/helpers/unwrap.ts b/typescript/option/src/helpers/unwrap.ts index 4757468..75ddaee 100644 --- a/typescript/option/src/helpers/unwrap.ts +++ b/typescript/option/src/helpers/unwrap.ts @@ -1,7 +1,6 @@ -import { Mapper } from '../internalTypes' import { Option } from '../types' -import { withDefault } from './withDefault' -import { map } from './map' +import { Mapper } from '../internalTypes' +import { isSome } from './isSome' /** * Apply the function to the value in the Maybe and return it unwrapped. @@ -13,8 +12,12 @@ import { map } from './map' */ export const unwrap = ( _default: U, - mapper: Mapper, + caseSome: Mapper, option: Option ) => { - return withDefault(_default, map(mapper, option)) + if (isSome(option)) { + return caseSome(option as T) + } + + return _default } diff --git a/typescript/option/src/helpers/withDefault.ts b/typescript/option/src/helpers/withDefault.ts index ca855db..9a6db91 100644 --- a/typescript/option/src/helpers/withDefault.ts +++ b/typescript/option/src/helpers/withDefault.ts @@ -1,4 +1,4 @@ -import { match } from './match' +import { unwrap } from './unwrap' import { identity } from '@thi.ng/compose' import { Option } from '../types' @@ -9,5 +9,5 @@ import { Option } from '../types' * @param option The option to get the default of. */ export const withDefault = (_default: T, option: Option) => { - return match(identity, _default, option) + return unwrap(_default, identity, option) }