typescript(option): refactor: created some internal helpers to clean some of the code
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
c226f17987
commit
0458b226c7
|
@ -1,5 +1,6 @@
|
||||||
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 Internals, { SomeClass } from './internals'
|
import Internals, { SomeClass } from './internals'
|
||||||
|
|
||||||
export const isSome = <T>(option: Option<T>) =>
|
export const isSome = <T>(option: Option<T>) =>
|
||||||
|
@ -9,8 +10,8 @@ export const isNothing = <T>(option: Option<T>) =>
|
||||||
|
|
||||||
export const match = <T, U>(
|
export const match = <T, U>(
|
||||||
option: Option<T>,
|
option: Option<T>,
|
||||||
caseSome: (v: T) => U,
|
caseSome: Mapper<T, U>,
|
||||||
caseNone: () => U
|
caseNone: Mapper<void, U>
|
||||||
) => {
|
) => {
|
||||||
if (isSome(option)) {
|
if (isSome(option)) {
|
||||||
return caseSome((option as SomeClass<T>)[Internals.someValue])
|
return caseSome((option as SomeClass<T>)[Internals.someValue])
|
||||||
|
@ -23,32 +24,24 @@ export const bind = <T, U>(
|
||||||
binder: Binder<T, U>,
|
binder: Binder<T, U>,
|
||||||
option: Option<T>
|
option: Option<T>
|
||||||
): Option<U> => {
|
): Option<U> => {
|
||||||
return match(option, binder, () => None)
|
return match(option, binder, always(None))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const map = <T, U>(
|
export const map = <T, U>(
|
||||||
mapper: Mapper<T, U>,
|
mapper: Mapper<T, U>,
|
||||||
option: Option<T>
|
option: Option<T>
|
||||||
): Option<U> => {
|
): Option<U> => {
|
||||||
return match(
|
return match(option, v => Some(mapper(v)), always(None))
|
||||||
option,
|
|
||||||
v => Some(mapper(v)),
|
|
||||||
() => None
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const count = <T>(option: Option<T>) => Number(isSome(option))
|
export const count = <T>(option: Option<T>) => Number(isSome(option))
|
||||||
|
|
||||||
export const exists = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
export const exists = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||||
return match(option, predicate, () => false)
|
return match(option, predicate, always(false))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const filter = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
export const filter = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||||
return match(
|
return match(option, v => (predicate(v) ? Some(v) : None), always(None))
|
||||||
option,
|
|
||||||
v => (predicate(v) ? Some(v) : None),
|
|
||||||
() => None
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fold = <T, U>(
|
export const fold = <T, U>(
|
||||||
|
@ -56,11 +49,7 @@ export const fold = <T, U>(
|
||||||
initial: U,
|
initial: U,
|
||||||
option: Option<T>
|
option: Option<T>
|
||||||
) => {
|
) => {
|
||||||
match(
|
match(option, v => folder(initial, v), always(initial))
|
||||||
option,
|
|
||||||
v => folder(initial, v),
|
|
||||||
() => initial
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const foldback = <T, U>(
|
export const foldback = <T, U>(
|
||||||
|
@ -68,11 +57,7 @@ export const foldback = <T, U>(
|
||||||
option: Option<T>,
|
option: Option<T>,
|
||||||
initial: U
|
initial: U
|
||||||
) => {
|
) => {
|
||||||
return match(
|
return match(option, v => folder(v, initial), always(initial))
|
||||||
option,
|
|
||||||
v => folder(v, initial),
|
|
||||||
() => initial
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||||
|
@ -94,17 +79,9 @@ export const iter = <T>(mapper: Mapper<T, void>, option: Option<T>) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const toArray = <T>(option: Option<T>) => {
|
export const toArray = <T>(option: Option<T>) => {
|
||||||
return match(
|
return match(option, v => [v], always([]))
|
||||||
option,
|
|
||||||
v => [v],
|
|
||||||
() => []
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const toNullable = <T>(option: Option<T>) => {
|
export const toNullable = <T>(option: Option<T>) => {
|
||||||
return match(
|
return match(option, identity, always(null))
|
||||||
option,
|
|
||||||
v => v,
|
|
||||||
() => null
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
2
typescript/option/src/internalHelperts.ts
Normal file
2
typescript/option/src/internalHelperts.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const always = <T>(v: T) => () => v
|
||||||
|
export const identity = <T>(v: T) => v
|
Loading…
Reference in a new issue