Skip to content

Commit

Permalink
[#213] Add Codec (#217)
Browse files Browse the repository at this point in the history
* [#213] Add Codec

* [#213] skip codec test

* [#213] remove period

* [#213] undo last two commits

* [#213] add coverage to jest and github prs

* chore: bump eslint version

* chore: remove coverage de-bump eslint

* chore: add Coveralls to PRs
  • Loading branch information
jacob-alford authored Dec 7, 2022
1 parent fe81964 commit 6cd0f58
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/*.ts'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
}
30 changes: 30 additions & 0 deletions src/Codec.ts
Original file line number Diff line number Diff line change
@@ -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<E, A>
extends Decoder<unknown, A>,
Encoder<E, A>,
Guard<unknown, A> {}

/**
* @since 1.0.1
* @category Interpreters
*/
export const getCodec = <E, A>(schema: SchemaExt<E, A>): Codec<E, A> => ({
...getDecoder(schema),
...getEncoder(schema),
...getGuard(schema),
})
19 changes: 19 additions & 0 deletions tests/Codec.test.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof S.Float>>)).toEqual('1')
})
test('guarding', () => {
const codec = getCodec(S.FloatFromString())
expect(codec.is(1)).toEqual(true)
})
})
})
27 changes: 17 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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==
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6cd0f58

Please sign in to comment.