-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [#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
1 parent
fe81964
commit 6cd0f58
Showing
5 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters