diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3ab3a358..27f83de5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,7 +30,6 @@ jobs: - run: yarn run eslint - run: yarn run test:ci - name: Coveralls - if: github.ref == 'refs/heads/main' uses: coverallsapp/github-action@1.1.3 env: NODE_COVERALLS_DEBUG: 1 diff --git a/jest.config.js b/jest.config.js index ed3f13d7..39cf21f3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,4 +3,12 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', testMatch: ['/tests/**/*.ts'], + coverageThreshold: { + global: { + branches: 100, + functions: 100, + lines: 100, + statements: 100, + }, + }, } diff --git a/src/Codec.ts b/src/Codec.ts new file mode 100644 index 00000000..d6ac7e31 --- /dev/null +++ b/src/Codec.ts @@ -0,0 +1,30 @@ +/** + * A Codec is a combined Decoder, Encoder, and Guard. + * + * @since 1.0.1 + */ +import { Decoder, getDecoder } from './Decoder' +import { Encoder, getEncoder } from './Encoder' +import { getGuard, Guard } from './Guard' +import { SchemaExt } from './SchemaExt' + +/** + * A Codec is a combined Decoder, Encoder, and Guard. + * + * @since 1.0.1 + * @category Model + */ +export interface Codec + extends Decoder, + Encoder, + Guard {} + +/** + * @since 1.0.1 + * @category Interpreters + */ +export const getCodec = (schema: SchemaExt): Codec => ({ + ...getDecoder(schema), + ...getEncoder(schema), + ...getGuard(schema), +}) diff --git a/tests/Codec.test.ts b/tests/Codec.test.ts new file mode 100644 index 00000000..b412d9f5 --- /dev/null +++ b/tests/Codec.test.ts @@ -0,0 +1,19 @@ +import { getCodec } from '../src/Codec' +import * as S from '../src/schemata' + +describe('Codec', () => { + describe('getCodec', () => { + test('decoding', () => { + const codec = getCodec(S.FloatFromString()) + expect(codec.decode('1')).toEqual({ _tag: 'Right', right: 1 }) + }) + test('encoding', () => { + const codec = getCodec(S.FloatFromString()) + expect(codec.encode(1 as S.TypeOf>)).toEqual('1') + }) + test('guarding', () => { + const codec = getCodec(S.FloatFromString()) + expect(codec.is(1)).toEqual(true) + }) + }) +}) diff --git a/yarn.lock b/yarn.lock index eb1b2f85..2a7c0832 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1964,9 +1964,9 @@ ajv@^6.10.0, ajv@^6.12.4: uri-js "^4.2.2" ajv@^8.0.1: - version "8.11.0" - resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.11.0.tgz#977e91dd96ca669f54a11e23e378e33b884a565f" - integrity sha512-wGgprdCvMalC0BztXvitD2hC04YffAvtsUn93JbGXYLAtCUO4xd17mCCZQxUOItiBwZvJScWo8NIvQMQ71rdpg== + version "8.11.2" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.11.2.tgz#aecb20b50607acf2569b6382167b65a96008bb78" + integrity sha512-E4bfmKAhGiSTvMfL1Myyycaub+cUEU2/IvpylXkUu7CHBkBj1f/ikdzbD7YQ6FKUbixDxeYvB/xY4fvyroDlQg== dependencies: fast-deep-equal "^3.1.1" json-schema-traverse "^1.0.0" @@ -3862,9 +3862,9 @@ globals@^11.1.0: integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA== globals@^13.6.0, globals@^13.9.0: - version "13.17.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-13.17.0.tgz#902eb1e680a41da93945adbdcb5a9f361ba69bd4" - integrity sha512-1C+6nQRb1GwGMKm2dH/E7enFAMxGTmGI7/dEdhy/DNelv85w9B72t3uc5frtMNXIbzrarJJ/lTCjcaZwbLJmyw== + version "13.18.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-13.18.0.tgz#fb224daeeb2bb7d254cd2c640f003528b8d0c1dc" + integrity sha512-/mR4KI8Ps2spmoc0Ulu9L7agOF0du1CZNQ3dke8yItYlyKNmGrkONemBbd6V8UTc1Wgcqn21t3WYB7dbRmh6/A== dependencies: type-fest "^0.20.2" @@ -6666,7 +6666,7 @@ saxes@^5.0.1: resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== -semver@7.x, semver@^7.2.1, semver@^7.3.2, semver@^7.3.5, semver@^7.3.7: +semver@7.x, semver@^7.3.2, semver@^7.3.5, semver@^7.3.7: version "7.3.7" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f" integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g== @@ -6678,6 +6678,13 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0: resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== +semver@^7.2.1: + version "7.3.8" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798" + integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A== + dependencies: + lru-cache "^6.0.0" + set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" @@ -7090,9 +7097,9 @@ synckit@^0.8.3: tslib "^2.4.0" table@^6.0.9: - version "6.8.0" - resolved "https://registry.yarnpkg.com/table/-/table-6.8.0.tgz#87e28f14fa4321c3377ba286f07b79b281a3b3ca" - integrity sha512-s/fitrbVeEyHKFa7mFdkuQMWlH1Wgw/yEXMt5xACT4ZpzWFluehAxRtUUQKPuWhaLAWhFcVx6w3oC8VKaUfPGA== + version "6.8.1" + resolved "https://registry.yarnpkg.com/table/-/table-6.8.1.tgz#ea2b71359fe03b017a5fbc296204471158080bdf" + integrity sha512-Y4X9zqrCftUhMeH2EptSSERdVKt/nEdijTOacGD/97EKjhQ/Qs8RTlEGABSJNNN8lac9kheH+af7yAkEWlgneA== dependencies: ajv "^8.0.1" lodash.truncate "^4.4.2"