typescript(option): refactor: refactored everything to use unwrap instead of match
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
6dc868c5fb
commit
69dcaa5e91
|
@ -1,10 +1,10 @@
|
|||
import { Binder } from '../internalTypes'
|
||||
import { Option, None } from '../types'
|
||||
import { match } from './match'
|
||||
import { unwrap } from './unwrap'
|
||||
|
||||
export const bind = <T, U>(
|
||||
binder: Binder<T, U>,
|
||||
option: Option<T>
|
||||
): Option<U> => {
|
||||
return match(binder, None, option)
|
||||
return unwrap(None, binder, option)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { Mapper } from '../internalTypes'
|
||||
import { Option, None } from '../types'
|
||||
import { match } from './match'
|
||||
import { unwrap } from './unwrap'
|
||||
|
||||
export const bindAsync = <T, U>(
|
||||
binder: Mapper<T, Promise<Option<U>>>,
|
||||
option: Option<T>
|
||||
): Promise<Option<U>> => {
|
||||
return match(binder, Promise.resolve(None), option)
|
||||
return unwrap(Promise.resolve(None), binder, option)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { match } from './match'
|
||||
import { unwrap } from './unwrap'
|
||||
import { Predicate } from '../internalTypes'
|
||||
import { Option } from '../types'
|
||||
|
||||
export const exists = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||
return match(predicate, false, option)
|
||||
return unwrap(false, predicate, option)
|
||||
}
|
||||
|
|
|
@ -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 = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||
return match(v => (predicate(v) ? Some(v) : None), None, option)
|
||||
return unwrap(None, v => (predicate(v) ? Some(v) : None), option)
|
||||
}
|
||||
|
|
|
@ -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 = <T, U>(
|
|||
initial: U,
|
||||
option: Option<T>
|
||||
) => {
|
||||
return match(v => folder(initial, v), initial, option)
|
||||
return unwrap(initial, v => folder(initial, v), option)
|
||||
}
|
||||
|
|
|
@ -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 = <T, U>(
|
|||
option: Option<T>,
|
||||
initial: U
|
||||
) => {
|
||||
return match(v => folder(v, initial), initial, option)
|
||||
return unwrap(initial, v => folder(v, initial), option)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { match } from './match'
|
||||
import { unwrap } from './unwrap'
|
||||
import { Predicate } from '../internalTypes'
|
||||
import { Option } from '../types'
|
||||
|
||||
export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||
return match(predicate, true, option)
|
||||
return unwrap(true, predicate, option)
|
||||
}
|
||||
|
|
|
@ -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 = <T, U>(
|
|||
mapper: Mapper<T, U>,
|
||||
option: Option<T>
|
||||
): Option<U> => {
|
||||
return match(v => Some(mapper(v)), None, option)
|
||||
return unwrap(None, v => Some(mapper(v)), option)
|
||||
}
|
||||
|
|
|
@ -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 = <T, U>(
|
||||
mapper: Mapper<T, Promise<U>>,
|
||||
option: Option<T>
|
||||
) => {
|
||||
return match(
|
||||
value => mapper(value).then(Some),
|
||||
return unwrap(
|
||||
Promise.resolve(None),
|
||||
value => mapper(value).then(Some),
|
||||
option
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
import { Option } from '../types'
|
||||
import { Mapper } from '../internalTypes'
|
||||
import { isSome } from './isSome'
|
||||
|
||||
export const match = <T, U>(
|
||||
caseSome: Mapper<T, U>,
|
||||
_default: U,
|
||||
option: Option<T>
|
||||
) => {
|
||||
if (isSome(option)) {
|
||||
return caseSome(option as T)
|
||||
}
|
||||
|
||||
return _default
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
import { match } from './match'
|
||||
import { unwrap } from './unwrap'
|
||||
import { Option } from '../types'
|
||||
|
||||
export const toArray = <T>(option: Option<T>) => {
|
||||
return match(v => [v], [], option)
|
||||
return unwrap([], v => [v], option)
|
||||
}
|
||||
|
|
|
@ -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 = <T>(option: Option<T>) => {
|
||||
return match(identity, null, option)
|
||||
return unwrap(null, identity, option)
|
||||
}
|
||||
|
|
|
@ -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 = <T, U>(
|
||||
_default: U,
|
||||
mapper: Mapper<T, U>,
|
||||
caseSome: Mapper<T, U>,
|
||||
option: Option<T>
|
||||
) => {
|
||||
return withDefault(_default, map(mapper, option))
|
||||
if (isSome(option)) {
|
||||
return caseSome(option as T)
|
||||
}
|
||||
|
||||
return _default
|
||||
}
|
||||
|
|
|
@ -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 = <T>(_default: T, option: Option<T>) => {
|
||||
return match(identity, _default, option)
|
||||
return unwrap(_default, identity, option)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue