diff --git a/typescript/option/package.json b/typescript/option/package.json index dd9ded0..e2e31cd 100644 --- a/typescript/option/package.json +++ b/typescript/option/package.json @@ -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" diff --git a/typescript/option/pnpm-lock.yaml b/typescript/option/pnpm-lock.yaml index 685f1b0..7d955d7 100644 --- a/typescript/option/pnpm-lock.yaml +++ b/typescript/option/pnpm-lock.yaml @@ -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 diff --git a/typescript/option/src/helpers.ts b/typescript/option/src/helpers.ts index 68c35c3..1a989d9 100644 --- a/typescript/option/src/helpers.ts +++ b/typescript/option/src/helpers.ts @@ -9,16 +9,16 @@ import { } from './internalTypes' import { identity, none, some } from './internals' -export const isSome = (option: Option) => option._type === some -export const isNothing = (option: Option) => option._type === none +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._type === some) { - return caseSome(option.value) + if (option.__brand === some) { + return caseSome(option as T) } return _default @@ -69,16 +69,16 @@ export const forall = (predicate: Predicate, option: Option) => { } export const get = (option: Option): 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 = (mapper: Mapper, option: Option) => { - if (option._type === some) { - mapper(option.value) + if (option.__brand === some) { + mapper(option as T) } } diff --git a/typescript/option/src/types.ts b/typescript/option/src/types.ts index 83ff10e..e95dca5 100644 --- a/typescript/option/src/types.ts +++ b/typescript/option/src/types.ts @@ -1,21 +1,10 @@ import { some, none } from './internals' +import { Brand } from 'utility-types' -type NominalTyped = { - _type: T - value: U -} - -type None = NominalTyped -type Some = NominalTyped +type None = Brand +type Some = Brand export type Option = Some | None -export const None: Option = { - _type: none, - value: null -} - -export const Some = (value: T): Option => ({ - _type: some, - value -}) +export const None = undefined as None +export const Some = (value: T): Option => value as Some