typescript(option): refactor: split heplers in different files
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
d1b7740878
commit
467198921d
|
@ -1,107 +0,0 @@
|
||||||
import { Option, Some, None } from './types'
|
|
||||||
import {
|
|
||||||
Binder,
|
|
||||||
Folder,
|
|
||||||
Mapper,
|
|
||||||
Predicate,
|
|
||||||
BackFolder,
|
|
||||||
Nullable
|
|
||||||
} from './internalTypes'
|
|
||||||
import { identity, none, some } from './internals'
|
|
||||||
|
|
||||||
export const isSome = <T>(option: Option<T>) => option.__brand === some
|
|
||||||
export const isNothing = <T>(option: Option<T>) => option.__brand === none
|
|
||||||
|
|
||||||
const match = <T, U>(
|
|
||||||
caseSome: Mapper<T, U>,
|
|
||||||
_default: U,
|
|
||||||
option: Option<T>
|
|
||||||
) => {
|
|
||||||
if (option.__brand === some) {
|
|
||||||
return caseSome(option as T)
|
|
||||||
}
|
|
||||||
|
|
||||||
return _default
|
|
||||||
}
|
|
||||||
|
|
||||||
export const bind = <T, U>(
|
|
||||||
binder: Binder<T, U>,
|
|
||||||
option: Option<T>
|
|
||||||
): Option<U> => {
|
|
||||||
return match(binder, None, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const map = <T, U>(
|
|
||||||
mapper: Mapper<T, U>,
|
|
||||||
option: Option<T>
|
|
||||||
): Option<U> => {
|
|
||||||
return match(v => Some(mapper(v)), None, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const count = <T>(option: Option<T>) => Number(isSome(option))
|
|
||||||
|
|
||||||
export const exists = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
|
||||||
return match(predicate, false, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const filter = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
|
||||||
return match(v => (predicate(v) ? Some(v) : None), None, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fold = <T, U>(
|
|
||||||
folder: Folder<T, U>,
|
|
||||||
initial: U,
|
|
||||||
option: Option<T>
|
|
||||||
) => {
|
|
||||||
return match(v => folder(initial, v), initial, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const foldback = <T, U>(
|
|
||||||
folder: BackFolder<T, U>,
|
|
||||||
option: Option<T>,
|
|
||||||
initial: U
|
|
||||||
) => {
|
|
||||||
return match(v => folder(v, initial), initial, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
|
||||||
return match(predicate, true, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const get = <T>(option: Option<T>): T => {
|
|
||||||
if (option.__brand === some) {
|
|
||||||
return option as T
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Cannot get value of None`)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const iter = <T>(mapper: Mapper<T, void>, option: Option<T>) => {
|
|
||||||
if (option.__brand === some) {
|
|
||||||
mapper(option as T)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const toArray = <T>(option: Option<T>) => {
|
|
||||||
return match(v => [v], [], option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const toNullable = <T>(option: Option<T>) => {
|
|
||||||
return match(identity, null, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const withDefault = <T>(_default: T, option: Option<T>) => {
|
|
||||||
return match(identity, _default, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const flat = <T>(option: Option<Option<T>>): Option<T> => {
|
|
||||||
return bind(identity, option)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fromNullable = <T>(value: Nullable<T>): Option<T> => {
|
|
||||||
return value === null ? None : Some(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fromArray = <T>(value: [T] | []): Option<T> => {
|
|
||||||
return value[0] === undefined ? None : Some(value[0])
|
|
||||||
}
|
|
10
typescript/option/src/helpers/bind.ts
Normal file
10
typescript/option/src/helpers/bind.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { Binder } from '../internalTypes'
|
||||||
|
import { Option, None } from '../types'
|
||||||
|
import { match } from './match'
|
||||||
|
|
||||||
|
export const bind = <T, U>(
|
||||||
|
binder: Binder<T, U>,
|
||||||
|
option: Option<T>
|
||||||
|
): Option<U> => {
|
||||||
|
return match(binder, None, option)
|
||||||
|
}
|
4
typescript/option/src/helpers/count.ts
Normal file
4
typescript/option/src/helpers/count.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { isSome } from './isSome'
|
||||||
|
|
||||||
|
export const count = <T>(option: Option<T>) => Number(isSome(option))
|
7
typescript/option/src/helpers/exists.ts
Normal file
7
typescript/option/src/helpers/exists.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Predicate } from '../internalTypes'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const exists = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||||
|
return match(predicate, false, option)
|
||||||
|
}
|
18
typescript/option/src/helpers/external.ts
Normal file
18
typescript/option/src/helpers/external.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
export * from './bind'
|
||||||
|
export * from './count'
|
||||||
|
export * from './exists'
|
||||||
|
export * from './filter'
|
||||||
|
export * from './flat'
|
||||||
|
export * from './fold'
|
||||||
|
export * from './foldback'
|
||||||
|
export * from './forall'
|
||||||
|
export * from './fromArray'
|
||||||
|
export * from './fromNullable'
|
||||||
|
export * from './get'
|
||||||
|
export * from './isNone'
|
||||||
|
export * from './isSome'
|
||||||
|
export * from './iter'
|
||||||
|
export * from './map'
|
||||||
|
export * from './toArray'
|
||||||
|
export * from './toNullable'
|
||||||
|
export * from './withDefault'
|
7
typescript/option/src/helpers/filter.ts
Normal file
7
typescript/option/src/helpers/filter.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { match } from './match'
|
||||||
|
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)
|
||||||
|
}
|
7
typescript/option/src/helpers/flat.ts
Normal file
7
typescript/option/src/helpers/flat.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { bind } from './bind'
|
||||||
|
import { identity } from '../internals'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const flat = <T>(option: Option<Option<T>>): Option<T> => {
|
||||||
|
return bind(identity, option)
|
||||||
|
}
|
11
typescript/option/src/helpers/fold.ts
Normal file
11
typescript/option/src/helpers/fold.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { Folder } from '../internalTypes'
|
||||||
|
|
||||||
|
export const fold = <T, U>(
|
||||||
|
folder: Folder<T, U>,
|
||||||
|
initial: U,
|
||||||
|
option: Option<T>
|
||||||
|
) => {
|
||||||
|
return match(v => folder(initial, v), initial, option)
|
||||||
|
}
|
11
typescript/option/src/helpers/foldback.ts
Normal file
11
typescript/option/src/helpers/foldback.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { BackFolder } from '../internalTypes'
|
||||||
|
|
||||||
|
export const foldback = <T, U>(
|
||||||
|
folder: BackFolder<T, U>,
|
||||||
|
option: Option<T>,
|
||||||
|
initial: U
|
||||||
|
) => {
|
||||||
|
return match(v => folder(v, initial), initial, option)
|
||||||
|
}
|
7
typescript/option/src/helpers/forall.ts
Normal file
7
typescript/option/src/helpers/forall.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Predicate } from '../internalTypes'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
||||||
|
return match(predicate, true, option)
|
||||||
|
}
|
5
typescript/option/src/helpers/fromArray.ts
Normal file
5
typescript/option/src/helpers/fromArray.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { None, Some, Option } from '../types'
|
||||||
|
|
||||||
|
export const fromArray = <T>(value: [T] | []): Option<T> => {
|
||||||
|
return value[0] === undefined ? None : Some(value[0])
|
||||||
|
}
|
6
typescript/option/src/helpers/fromNullable.ts
Normal file
6
typescript/option/src/helpers/fromNullable.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { Nullable } from '../internalTypes'
|
||||||
|
import { Some, None, Option } from '../types'
|
||||||
|
|
||||||
|
export const fromNullable = <T>(value: Nullable<T>): Option<T> => {
|
||||||
|
return value === null ? None : Some(value)
|
||||||
|
}
|
10
typescript/option/src/helpers/get.ts
Normal file
10
typescript/option/src/helpers/get.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { some } from '../internals'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const get = <T>(option: Option<T>): T => {
|
||||||
|
if (option.__brand === some) {
|
||||||
|
return option as T
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Cannot get value of None`)
|
||||||
|
}
|
4
typescript/option/src/helpers/isNone.ts
Normal file
4
typescript/option/src/helpers/isNone.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { none } from '../internals'
|
||||||
|
|
||||||
|
export const isNothing = <T>(option: Option<T>) => option.__brand === none
|
4
typescript/option/src/helpers/isSome.ts
Normal file
4
typescript/option/src/helpers/isSome.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { some } from '../internals'
|
||||||
|
|
||||||
|
export const isSome = <T>(option: Option<T>) => option.__brand === some
|
9
typescript/option/src/helpers/iter.ts
Normal file
9
typescript/option/src/helpers/iter.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import { some } from '../internals'
|
||||||
|
import { Mapper } from '../internalTypes'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const iter = <T>(mapper: Mapper<T, void>, option: Option<T>) => {
|
||||||
|
if (option.__brand === some) {
|
||||||
|
mapper(option as T)
|
||||||
|
}
|
||||||
|
}
|
10
typescript/option/src/helpers/map.ts
Normal file
10
typescript/option/src/helpers/map.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Mapper } from '../internalTypes'
|
||||||
|
import { Option, Some, None } from '../types'
|
||||||
|
|
||||||
|
export const map = <T, U>(
|
||||||
|
mapper: Mapper<T, U>,
|
||||||
|
option: Option<T>
|
||||||
|
): Option<U> => {
|
||||||
|
return match(v => Some(mapper(v)), None, option)
|
||||||
|
}
|
15
typescript/option/src/helpers/match.ts
Normal file
15
typescript/option/src/helpers/match.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { Option } from '../types'
|
||||||
|
import { Mapper } from '../internalTypes'
|
||||||
|
import { some } from '../internals'
|
||||||
|
|
||||||
|
export const match = <T, U>(
|
||||||
|
caseSome: Mapper<T, U>,
|
||||||
|
_default: U,
|
||||||
|
option: Option<T>
|
||||||
|
) => {
|
||||||
|
if (option.__brand === some) {
|
||||||
|
return caseSome(option as T)
|
||||||
|
}
|
||||||
|
|
||||||
|
return _default
|
||||||
|
}
|
6
typescript/option/src/helpers/toArray.ts
Normal file
6
typescript/option/src/helpers/toArray.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const toArray = <T>(option: Option<T>) => {
|
||||||
|
return match(v => [v], [], option)
|
||||||
|
}
|
7
typescript/option/src/helpers/toNullable.ts
Normal file
7
typescript/option/src/helpers/toNullable.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { identity } from '../internals'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const toNullable = <T>(option: Option<T>) => {
|
||||||
|
return match(identity, null, option)
|
||||||
|
}
|
7
typescript/option/src/helpers/withDefault.ts
Normal file
7
typescript/option/src/helpers/withDefault.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { match } from './match'
|
||||||
|
import { identity } from '../internals'
|
||||||
|
import { Option } from '../types'
|
||||||
|
|
||||||
|
export const withDefault = <T>(_default: T, option: Option<T>) => {
|
||||||
|
return match(identity, _default, option)
|
||||||
|
}
|
|
@ -1,2 +1,2 @@
|
||||||
export * from './helpers'
|
export * from './helpers/external'
|
||||||
export * from './types'
|
export * from './types'
|
||||||
|
|
Loading…
Reference in a new issue