From 467198921dd90974905bc52ce39cd62ed96e391c Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Mon, 23 Dec 2019 13:17:51 +0200 Subject: [PATCH] typescript(option): refactor: split heplers in different files Signed-off-by: prescientmoon --- typescript/option/src/helpers.ts | 107 ------------------ typescript/option/src/helpers/bind.ts | 10 ++ typescript/option/src/helpers/count.ts | 4 + typescript/option/src/helpers/exists.ts | 7 ++ typescript/option/src/helpers/external.ts | 18 +++ typescript/option/src/helpers/filter.ts | 7 ++ typescript/option/src/helpers/flat.ts | 7 ++ typescript/option/src/helpers/fold.ts | 11 ++ typescript/option/src/helpers/foldback.ts | 11 ++ typescript/option/src/helpers/forall.ts | 7 ++ typescript/option/src/helpers/fromArray.ts | 5 + typescript/option/src/helpers/fromNullable.ts | 6 + typescript/option/src/helpers/get.ts | 10 ++ typescript/option/src/helpers/isNone.ts | 4 + typescript/option/src/helpers/isSome.ts | 4 + typescript/option/src/helpers/iter.ts | 9 ++ typescript/option/src/helpers/map.ts | 10 ++ typescript/option/src/helpers/match.ts | 15 +++ typescript/option/src/helpers/toArray.ts | 6 + typescript/option/src/helpers/toNullable.ts | 7 ++ typescript/option/src/helpers/withDefault.ts | 7 ++ typescript/option/src/index.ts | 2 +- 22 files changed, 166 insertions(+), 108 deletions(-) delete mode 100644 typescript/option/src/helpers.ts create mode 100644 typescript/option/src/helpers/bind.ts create mode 100644 typescript/option/src/helpers/count.ts create mode 100644 typescript/option/src/helpers/exists.ts create mode 100644 typescript/option/src/helpers/external.ts create mode 100644 typescript/option/src/helpers/filter.ts create mode 100644 typescript/option/src/helpers/flat.ts create mode 100644 typescript/option/src/helpers/fold.ts create mode 100644 typescript/option/src/helpers/foldback.ts create mode 100644 typescript/option/src/helpers/forall.ts create mode 100644 typescript/option/src/helpers/fromArray.ts create mode 100644 typescript/option/src/helpers/fromNullable.ts create mode 100644 typescript/option/src/helpers/get.ts create mode 100644 typescript/option/src/helpers/isNone.ts create mode 100644 typescript/option/src/helpers/isSome.ts create mode 100644 typescript/option/src/helpers/iter.ts create mode 100644 typescript/option/src/helpers/map.ts create mode 100644 typescript/option/src/helpers/match.ts create mode 100644 typescript/option/src/helpers/toArray.ts create mode 100644 typescript/option/src/helpers/toNullable.ts create mode 100644 typescript/option/src/helpers/withDefault.ts diff --git a/typescript/option/src/helpers.ts b/typescript/option/src/helpers.ts deleted file mode 100644 index 1a989d9..0000000 --- a/typescript/option/src/helpers.ts +++ /dev/null @@ -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 = (option: Option) => option.__brand === some -export const isNothing = (option: Option) => option.__brand === none - -const match = ( - caseSome: Mapper, - _default: U, - option: Option -) => { - if (option.__brand === some) { - return caseSome(option as T) - } - - return _default -} - -export const bind = ( - binder: Binder, - option: Option -): Option => { - return match(binder, None, option) -} - -export const map = ( - mapper: Mapper, - option: Option -): Option => { - return match(v => Some(mapper(v)), None, option) -} - -export const count = (option: Option) => Number(isSome(option)) - -export const exists = (predicate: Predicate, option: Option) => { - return match(predicate, false, option) -} - -export const filter = (predicate: Predicate, option: Option) => { - return match(v => (predicate(v) ? Some(v) : None), None, option) -} - -export const fold = ( - folder: Folder, - initial: U, - option: Option -) => { - return match(v => folder(initial, v), initial, option) -} - -export const foldback = ( - folder: BackFolder, - option: Option, - initial: U -) => { - return match(v => folder(v, initial), initial, option) -} - -export const forall = (predicate: Predicate, option: Option) => { - return match(predicate, true, option) -} - -export const get = (option: Option): T => { - if (option.__brand === some) { - return option as T - } - - throw new Error(`Cannot get value of None`) -} - -export const iter = (mapper: Mapper, option: Option) => { - if (option.__brand === some) { - mapper(option as T) - } -} - -export const toArray = (option: Option) => { - return match(v => [v], [], option) -} - -export const toNullable = (option: Option) => { - return match(identity, null, option) -} - -export const withDefault = (_default: T, option: Option) => { - return match(identity, _default, option) -} - -export const flat = (option: Option>): Option => { - return bind(identity, option) -} - -export const fromNullable = (value: Nullable): Option => { - return value === null ? None : Some(value) -} - -export const fromArray = (value: [T] | []): Option => { - return value[0] === undefined ? None : Some(value[0]) -} diff --git a/typescript/option/src/helpers/bind.ts b/typescript/option/src/helpers/bind.ts new file mode 100644 index 0000000..8dd234d --- /dev/null +++ b/typescript/option/src/helpers/bind.ts @@ -0,0 +1,10 @@ +import { Binder } from '../internalTypes' +import { Option, None } from '../types' +import { match } from './match' + +export const bind = ( + binder: Binder, + option: Option +): Option => { + return match(binder, None, option) +} diff --git a/typescript/option/src/helpers/count.ts b/typescript/option/src/helpers/count.ts new file mode 100644 index 0000000..4f8c017 --- /dev/null +++ b/typescript/option/src/helpers/count.ts @@ -0,0 +1,4 @@ +import { Option } from '../types' +import { isSome } from './isSome' + +export const count = (option: Option) => Number(isSome(option)) diff --git a/typescript/option/src/helpers/exists.ts b/typescript/option/src/helpers/exists.ts new file mode 100644 index 0000000..9b70af7 --- /dev/null +++ b/typescript/option/src/helpers/exists.ts @@ -0,0 +1,7 @@ +import { match } from './match' +import { Predicate } from '../internalTypes' +import { Option } from '../types' + +export const exists = (predicate: Predicate, option: Option) => { + return match(predicate, false, option) +} diff --git a/typescript/option/src/helpers/external.ts b/typescript/option/src/helpers/external.ts new file mode 100644 index 0000000..7995d8d --- /dev/null +++ b/typescript/option/src/helpers/external.ts @@ -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' diff --git a/typescript/option/src/helpers/filter.ts b/typescript/option/src/helpers/filter.ts new file mode 100644 index 0000000..592b60d --- /dev/null +++ b/typescript/option/src/helpers/filter.ts @@ -0,0 +1,7 @@ +import { match } from './match' +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) +} diff --git a/typescript/option/src/helpers/flat.ts b/typescript/option/src/helpers/flat.ts new file mode 100644 index 0000000..e23283b --- /dev/null +++ b/typescript/option/src/helpers/flat.ts @@ -0,0 +1,7 @@ +import { bind } from './bind' +import { identity } from '../internals' +import { Option } from '../types' + +export const flat = (option: Option>): Option => { + return bind(identity, option) +} diff --git a/typescript/option/src/helpers/fold.ts b/typescript/option/src/helpers/fold.ts new file mode 100644 index 0000000..99f67b8 --- /dev/null +++ b/typescript/option/src/helpers/fold.ts @@ -0,0 +1,11 @@ +import { match } from './match' +import { Option } from '../types' +import { Folder } from '../internalTypes' + +export const fold = ( + folder: Folder, + initial: U, + option: Option +) => { + return match(v => folder(initial, v), initial, option) +} diff --git a/typescript/option/src/helpers/foldback.ts b/typescript/option/src/helpers/foldback.ts new file mode 100644 index 0000000..564dda4 --- /dev/null +++ b/typescript/option/src/helpers/foldback.ts @@ -0,0 +1,11 @@ +import { match } from './match' +import { Option } from '../types' +import { BackFolder } from '../internalTypes' + +export const foldback = ( + folder: BackFolder, + option: Option, + initial: U +) => { + return match(v => folder(v, initial), initial, option) +} diff --git a/typescript/option/src/helpers/forall.ts b/typescript/option/src/helpers/forall.ts new file mode 100644 index 0000000..6321261 --- /dev/null +++ b/typescript/option/src/helpers/forall.ts @@ -0,0 +1,7 @@ +import { match } from './match' +import { Predicate } from '../internalTypes' +import { Option } from '../types' + +export const forall = (predicate: Predicate, option: Option) => { + return match(predicate, true, option) +} diff --git a/typescript/option/src/helpers/fromArray.ts b/typescript/option/src/helpers/fromArray.ts new file mode 100644 index 0000000..0d29b1e --- /dev/null +++ b/typescript/option/src/helpers/fromArray.ts @@ -0,0 +1,5 @@ +import { None, Some, Option } from '../types' + +export const fromArray = (value: [T] | []): Option => { + return value[0] === undefined ? None : Some(value[0]) +} diff --git a/typescript/option/src/helpers/fromNullable.ts b/typescript/option/src/helpers/fromNullable.ts new file mode 100644 index 0000000..6ebb984 --- /dev/null +++ b/typescript/option/src/helpers/fromNullable.ts @@ -0,0 +1,6 @@ +import { Nullable } from '../internalTypes' +import { Some, None, Option } from '../types' + +export const fromNullable = (value: Nullable): Option => { + return value === null ? None : Some(value) +} diff --git a/typescript/option/src/helpers/get.ts b/typescript/option/src/helpers/get.ts new file mode 100644 index 0000000..7ed7e2a --- /dev/null +++ b/typescript/option/src/helpers/get.ts @@ -0,0 +1,10 @@ +import { some } from '../internals' +import { Option } from '../types' + +export const get = (option: Option): T => { + if (option.__brand === some) { + return option as T + } + + throw new Error(`Cannot get value of None`) +} diff --git a/typescript/option/src/helpers/isNone.ts b/typescript/option/src/helpers/isNone.ts new file mode 100644 index 0000000..a55f27a --- /dev/null +++ b/typescript/option/src/helpers/isNone.ts @@ -0,0 +1,4 @@ +import { Option } from '../types' +import { none } from '../internals' + +export const isNothing = (option: Option) => option.__brand === none diff --git a/typescript/option/src/helpers/isSome.ts b/typescript/option/src/helpers/isSome.ts new file mode 100644 index 0000000..cbc07ca --- /dev/null +++ b/typescript/option/src/helpers/isSome.ts @@ -0,0 +1,4 @@ +import { Option } from '../types' +import { some } from '../internals' + +export const isSome = (option: Option) => option.__brand === some diff --git a/typescript/option/src/helpers/iter.ts b/typescript/option/src/helpers/iter.ts new file mode 100644 index 0000000..8788b09 --- /dev/null +++ b/typescript/option/src/helpers/iter.ts @@ -0,0 +1,9 @@ +import { some } from '../internals' +import { Mapper } from '../internalTypes' +import { Option } from '../types' + +export const iter = (mapper: Mapper, option: Option) => { + if (option.__brand === some) { + mapper(option as T) + } +} diff --git a/typescript/option/src/helpers/map.ts b/typescript/option/src/helpers/map.ts new file mode 100644 index 0000000..672b6a8 --- /dev/null +++ b/typescript/option/src/helpers/map.ts @@ -0,0 +1,10 @@ +import { match } from './match' +import { Mapper } from '../internalTypes' +import { Option, Some, None } from '../types' + +export const map = ( + mapper: Mapper, + option: Option +): Option => { + return match(v => Some(mapper(v)), None, option) +} diff --git a/typescript/option/src/helpers/match.ts b/typescript/option/src/helpers/match.ts new file mode 100644 index 0000000..4e3e4d2 --- /dev/null +++ b/typescript/option/src/helpers/match.ts @@ -0,0 +1,15 @@ +import { Option } from '../types' +import { Mapper } from '../internalTypes' +import { some } from '../internals' + +export const match = ( + caseSome: Mapper, + _default: U, + option: Option +) => { + if (option.__brand === some) { + return caseSome(option as T) + } + + return _default +} diff --git a/typescript/option/src/helpers/toArray.ts b/typescript/option/src/helpers/toArray.ts new file mode 100644 index 0000000..e135960 --- /dev/null +++ b/typescript/option/src/helpers/toArray.ts @@ -0,0 +1,6 @@ +import { match } from './match' +import { Option } from '../types' + +export const toArray = (option: Option) => { + return match(v => [v], [], option) +} diff --git a/typescript/option/src/helpers/toNullable.ts b/typescript/option/src/helpers/toNullable.ts new file mode 100644 index 0000000..07baa84 --- /dev/null +++ b/typescript/option/src/helpers/toNullable.ts @@ -0,0 +1,7 @@ +import { match } from './match' +import { identity } from '../internals' +import { Option } from '../types' + +export const toNullable = (option: Option) => { + return match(identity, null, option) +} diff --git a/typescript/option/src/helpers/withDefault.ts b/typescript/option/src/helpers/withDefault.ts new file mode 100644 index 0000000..b9c0f6d --- /dev/null +++ b/typescript/option/src/helpers/withDefault.ts @@ -0,0 +1,7 @@ +import { match } from './match' +import { identity } from '../internals' +import { Option } from '../types' + +export const withDefault = (_default: T, option: Option) => { + return match(identity, _default, option) +} diff --git a/typescript/option/src/index.ts b/typescript/option/src/index.ts index 493732d..37d7557 100644 --- a/typescript/option/src/index.ts +++ b/typescript/option/src/index.ts @@ -1,2 +1,2 @@ -export * from './helpers' +export * from './helpers/external' export * from './types'