1
Fork 0

typescript(option): refactor: split heplers in different files

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-23 13:17:51 +02:00 committed by prescientmoon
parent d1b7740878
commit 467198921d
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
22 changed files with 166 additions and 108 deletions

View file

@ -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])
}

View 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)
}

View file

@ -0,0 +1,4 @@
import { Option } from '../types'
import { isSome } from './isSome'
export const count = <T>(option: Option<T>) => Number(isSome(option))

View 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)
}

View 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'

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View 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])
}

View 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)
}

View 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`)
}

View file

@ -0,0 +1,4 @@
import { Option } from '../types'
import { none } from '../internals'
export const isNothing = <T>(option: Option<T>) => option.__brand === none

View file

@ -0,0 +1,4 @@
import { Option } from '../types'
import { some } from '../internals'
export const isSome = <T>(option: Option<T>) => option.__brand === some

View 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)
}
}

View 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)
}

View 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
}

View 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)
}

View 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)
}

View 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)
}

View file

@ -1,2 +1,2 @@
export * from './helpers'
export * from './helpers/external'
export * from './types'