typescript(option): refactor: Ditched the cusom NominalType type
typescript(option): typescript(option): Instead of defining my own NominalType now I use the Brand type from typescript(option): utility types. Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
aed68688a1
commit
d1b7740878
|
@ -29,10 +29,11 @@
|
|||
"@types/node": "^12.12.21",
|
||||
"@wessberg/rollup-plugin-ts": "^1.1.83",
|
||||
"rimraf": "^3.0.0",
|
||||
"rollup": "^1.27.13",
|
||||
"rollup": "^1.27.14",
|
||||
"rollup-plugin-terser": "^5.1.3",
|
||||
"semantic-release": "^15.14.0",
|
||||
"typescript": "^3.7.4"
|
||||
"typescript": "^3.7.4",
|
||||
"utility-types": "^3.10.0"
|
||||
},
|
||||
"author": "Matei Adriel",
|
||||
"license": "SEE LICENSE IN LICENSE"
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
devDependencies:
|
||||
'@rollup/plugin-commonjs': 11.0.0_rollup@1.27.13
|
||||
'@rollup/plugin-node-resolve': 6.0.0_rollup@1.27.13
|
||||
'@rollup/plugin-commonjs': 11.0.0_rollup@1.27.14
|
||||
'@rollup/plugin-node-resolve': 6.0.0_rollup@1.27.14
|
||||
'@types/node': 12.12.21
|
||||
'@wessberg/rollup-plugin-ts': 1.1.83_rollup@1.27.13+typescript@3.7.4
|
||||
'@wessberg/rollup-plugin-ts': 1.1.83_rollup@1.27.14+typescript@3.7.4
|
||||
rimraf: 3.0.0
|
||||
rollup: 1.27.13
|
||||
rollup-plugin-terser: 5.1.3_rollup@1.27.13
|
||||
rollup: 1.27.14
|
||||
rollup-plugin-terser: 5.1.3_rollup@1.27.14
|
||||
semantic-release: 15.14.0_semantic-release@15.14.0
|
||||
typescript: 3.7.4
|
||||
utility-types: 3.10.0
|
||||
lockfileVersion: 5.1
|
||||
packages:
|
||||
/@babel/code-frame/7.5.5:
|
||||
|
@ -821,14 +822,14 @@ packages:
|
|||
dev: true
|
||||
resolution:
|
||||
integrity: sha512-StASIL2lgT3TRjxv17z9pAqbnI7HGu9DrJlg3sEBFfCLaMEqp+O3IQPUF6EZtQ4xkAu2ml6kMBBCtGxjvmtmuQ==
|
||||
/@rollup/plugin-commonjs/11.0.0_rollup@1.27.13:
|
||||
/@rollup/plugin-commonjs/11.0.0_rollup@1.27.14:
|
||||
dependencies:
|
||||
'@rollup/pluginutils': 3.0.1_rollup@1.27.13
|
||||
'@rollup/pluginutils': 3.0.1_rollup@1.27.14
|
||||
estree-walker: 0.6.1
|
||||
is-reference: 1.1.4
|
||||
magic-string: 0.25.4
|
||||
resolve: 1.14.1
|
||||
rollup: 1.27.13
|
||||
rollup: 1.27.14
|
||||
dev: true
|
||||
engines:
|
||||
node: '>= 8.0.0'
|
||||
|
@ -836,14 +837,14 @@ packages:
|
|||
rollup: ^1.20.0
|
||||
resolution:
|
||||
integrity: sha512-jnm//T5ZWOZ6zmJ61fReSCBOif+Ax8dHVoVggA+d2NA7T4qCWgQ3KYr+zN2faGEYLpe1wa03IzvhR+sqVLxUWg==
|
||||
/@rollup/plugin-node-resolve/6.0.0_rollup@1.27.13:
|
||||
/@rollup/plugin-node-resolve/6.0.0_rollup@1.27.14:
|
||||
dependencies:
|
||||
'@rollup/pluginutils': 3.0.1_rollup@1.27.13
|
||||
'@rollup/pluginutils': 3.0.1_rollup@1.27.14
|
||||
'@types/resolve': 0.0.8
|
||||
builtin-modules: 3.1.0
|
||||
is-module: 1.0.0
|
||||
resolve: 1.14.1
|
||||
rollup: 1.27.13
|
||||
rollup: 1.27.14
|
||||
dev: true
|
||||
engines:
|
||||
node: '>= 8.0.0'
|
||||
|
@ -851,10 +852,10 @@ packages:
|
|||
rollup: ^1.20.0
|
||||
resolution:
|
||||
integrity: sha512-GqWz1CfXOsqpeVMcoM315+O7zMxpRsmhWyhJoxLFHVSp9S64/u02i7len/FnbTNbmgYs+sZyilasijH8UiuboQ==
|
||||
/@rollup/pluginutils/3.0.1_rollup@1.27.13:
|
||||
/@rollup/pluginutils/3.0.1_rollup@1.27.14:
|
||||
dependencies:
|
||||
estree-walker: 0.6.1
|
||||
rollup: 1.27.13
|
||||
rollup: 1.27.14
|
||||
dev: true
|
||||
engines:
|
||||
node: '>= 8.0.0'
|
||||
|
@ -1034,7 +1035,7 @@ packages:
|
|||
node: '>=8.0.0'
|
||||
resolution:
|
||||
integrity: sha512-Ba1Q36FZhe1KrLWDkxUetrWCEIdmyPvndKp4zTirWHYnTfNyvr4adDIrQaIq1h49DkPDgQEkBw7PcMRSvuFA3g==
|
||||
/@wessberg/rollup-plugin-ts/1.1.83_rollup@1.27.13+typescript@3.7.4:
|
||||
/@wessberg/rollup-plugin-ts/1.1.83_rollup@1.27.14+typescript@3.7.4:
|
||||
dependencies:
|
||||
'@babel/core': 7.7.7
|
||||
'@babel/plugin-transform-runtime': 7.7.6_@babel+core@7.7.7
|
||||
|
@ -1051,7 +1052,7 @@ packages:
|
|||
magic-string: 0.25.4
|
||||
mkdirp: 0.5.1
|
||||
resolve: 1.14.1
|
||||
rollup: 1.27.13
|
||||
rollup: 1.27.14
|
||||
rollup-pluginutils: 2.8.2
|
||||
slash: 3.0.0
|
||||
tslib: 1.10.0
|
||||
|
@ -1630,7 +1631,7 @@ packages:
|
|||
human-signals: 1.1.1
|
||||
is-stream: 2.0.0
|
||||
merge-stream: 2.0.0
|
||||
npm-run-path: 4.0.0
|
||||
npm-run-path: 4.0.1
|
||||
onetime: 5.1.0
|
||||
p-finally: 2.0.1
|
||||
signal-exit: 3.0.2
|
||||
|
@ -2421,14 +2422,14 @@ packages:
|
|||
node: '>=4'
|
||||
resolution:
|
||||
integrity: sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=
|
||||
/npm-run-path/4.0.0:
|
||||
/npm-run-path/4.0.1:
|
||||
dependencies:
|
||||
path-key: 3.1.1
|
||||
dev: true
|
||||
engines:
|
||||
node: '>=8'
|
||||
resolution:
|
||||
integrity: sha512-8eyAOAH+bYXFPSnNnKr3J+yoybe8O87Is5rtAQ8qRczJz1ajcsjg8l2oZqP+Ppx15Ii3S1vUTjQN2h4YO2tWWQ==
|
||||
integrity: sha512-S48WzZW777zhNIrn7gxOlISNAqi9ZC/uQFnRdbeIHhZhCA6UqpkOT8T1G7BvfdgP4Er8gF4sUbaS0i7QvIfCWw==
|
||||
/npm/6.13.4:
|
||||
bundledDependencies:
|
||||
- abbrev
|
||||
|
@ -3023,11 +3024,11 @@ packages:
|
|||
hasBin: true
|
||||
resolution:
|
||||
integrity: sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
|
||||
/rollup-plugin-terser/5.1.3_rollup@1.27.13:
|
||||
/rollup-plugin-terser/5.1.3_rollup@1.27.14:
|
||||
dependencies:
|
||||
'@babel/code-frame': 7.5.5
|
||||
jest-worker: 24.9.0
|
||||
rollup: 1.27.13
|
||||
rollup: 1.27.14
|
||||
rollup-pluginutils: 2.8.2
|
||||
serialize-javascript: 2.1.2
|
||||
terser: 4.4.3
|
||||
|
@ -3042,7 +3043,7 @@ packages:
|
|||
dev: true
|
||||
resolution:
|
||||
integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==
|
||||
/rollup/1.27.13:
|
||||
/rollup/1.27.14:
|
||||
dependencies:
|
||||
'@types/estree': 0.0.40
|
||||
'@types/node': 12.12.21
|
||||
|
@ -3050,7 +3051,7 @@ packages:
|
|||
dev: true
|
||||
hasBin: true
|
||||
resolution:
|
||||
integrity: sha512-hDi7M07MpmNSDE8YVwGVFA8L7n8jTLJ4lG65nMAijAyqBe//rtu4JdxjUBE7JqXfdpqxqDTbCDys9WcqdpsQvw==
|
||||
integrity: sha512-DuDjEyn8Y79ALYXMt+nH/EI58L5pEw5HU9K38xXdRnxQhvzUTI/nxAawhkAHUQeudANQ//8iyrhVRHJBuR6DSQ==
|
||||
/run-parallel/1.1.9:
|
||||
dev: true
|
||||
resolution:
|
||||
|
@ -3514,6 +3515,12 @@ packages:
|
|||
dev: true
|
||||
resolution:
|
||||
integrity: sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
|
||||
/utility-types/3.10.0:
|
||||
dev: true
|
||||
engines:
|
||||
node: '>= 4'
|
||||
resolution:
|
||||
integrity: sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg==
|
||||
/validate-npm-package-license/3.0.4:
|
||||
dependencies:
|
||||
spdx-correct: 3.1.0
|
||||
|
@ -3626,7 +3633,8 @@ specifiers:
|
|||
'@types/node': ^12.12.21
|
||||
'@wessberg/rollup-plugin-ts': ^1.1.83
|
||||
rimraf: ^3.0.0
|
||||
rollup: ^1.27.13
|
||||
rollup: ^1.27.14
|
||||
rollup-plugin-terser: ^5.1.3
|
||||
semantic-release: ^15.14.0
|
||||
typescript: ^3.7.4
|
||||
utility-types: ^3.10.0
|
||||
|
|
|
@ -9,16 +9,16 @@ import {
|
|||
} from './internalTypes'
|
||||
import { identity, none, some } from './internals'
|
||||
|
||||
export const isSome = <T>(option: Option<T>) => option._type === some
|
||||
export const isNothing = <T>(option: Option<T>) => option._type === none
|
||||
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._type === some) {
|
||||
return caseSome(option.value)
|
||||
if (option.__brand === some) {
|
||||
return caseSome(option as T)
|
||||
}
|
||||
|
||||
return _default
|
||||
|
@ -69,16 +69,16 @@ export const forall = <T>(predicate: Predicate<T>, option: Option<T>) => {
|
|||
}
|
||||
|
||||
export const get = <T>(option: Option<T>): T => {
|
||||
if (option._type === some) {
|
||||
return option.value
|
||||
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._type === some) {
|
||||
mapper(option.value)
|
||||
if (option.__brand === some) {
|
||||
mapper(option as T)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,10 @@
|
|||
import { some, none } from './internals'
|
||||
import { Brand } from 'utility-types'
|
||||
|
||||
type NominalTyped<T, U> = {
|
||||
_type: T
|
||||
value: U
|
||||
}
|
||||
|
||||
type None = NominalTyped<typeof none, null>
|
||||
type Some<T> = NominalTyped<typeof some, T>
|
||||
type None = Brand<void, typeof none>
|
||||
type Some<T> = Brand<T, typeof some>
|
||||
|
||||
export type Option<T> = Some<T> | None
|
||||
|
||||
export const None: Option<any> = {
|
||||
_type: none,
|
||||
value: null
|
||||
}
|
||||
|
||||
export const Some = <T>(value: T): Option<T> => ({
|
||||
_type: some,
|
||||
value
|
||||
})
|
||||
export const None = undefined as None
|
||||
export const Some = <T>(value: T): Option<T> => value as Some<T>
|
||||
|
|
Loading…
Reference in a new issue