Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 21, 2020
1 parent c59dc2f commit f9ff107
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
41 changes: 11 additions & 30 deletions Codec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Codec interface](#codec-interface)
- [Built-in primitive codecs](#built-in-primitive-codecs)
- [Combinators](#combinators)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand All @@ -27,34 +25,17 @@ You can build a new codec using the `make` helper
```ts
import * as C from 'io-ts/lib/Codec'
import * as D from 'io-ts/lib/Decoder'
import * as E from 'io-ts/lib/Encoder'
import { left, right } from 'fp-ts/lib/Either'

const NumberFromString: C.Codec<number> = C.make(
D.parse(D.string, (s) => {
const n = parseFloat(s)
return isNaN(n) ? left(`cannot decode ${JSON.stringify(s)}, should be parsable into a number`) : right(n)
}),
{ encode: String }
)
```
const decoder: D.Decoder<number> = D.parse(D.string, (s) => {
const n = parseFloat(s)
return isNaN(n) ? left(`cannot decode ${JSON.stringify(s)}, should be parsable into a number`) : right(n)
})

const encoder: E.Encoder<string, unknown> = {
encode: String
}

# Built-in primitive codecs

- `string: Decoder<string>`
- `number: Decoder<number>`
- `boolean: Decoder<boolean>`
- `UnknownArray: Decoder<Array<unknown>>`
- `UnknownRecord: Decoder<Record<string, unknown>>`

# Combinators

- `literal`
- `nullable`
- `type`
- `partial`
- `record`
- `array`
- `tuple`
- `intersection`
- `sum`
- `lazy`
export const NumberFromString: C.Codec<string, number> = C.make(decoder, encoder)
```
21 changes: 4 additions & 17 deletions Encoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Encoder interface](#encoder-interface)
- [Combinators](#combinators)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Encoder interface

```ts
export interface Encoder<A> {
readonly encode: (a: A) => unknown
export interface Encoder<O, A> {
readonly encode: (a: A) => O
}
```

Expand All @@ -21,21 +20,9 @@ An encoder representing a nullable value
```ts
import * as E from 'io-ts/lib/Encoder'

export function nullable<A>(or: E.Encoder<A>): E.Encoder<null | A> {
export function nullable<O, A>(or: E.Encoder<O, A>): E.Encoder<null | O, null | A> {
return {
encode: (a) => (a === null ? a : or.encode(a))
encode: (a) => (a === null ? null : or.encode(a))
}
}
```

# Combinators

- `nullable`
- `type`
- `partial`
- `record`
- `array`
- `tuple`
- `intersection`
- `sum`
- `lazy`

0 comments on commit f9ff107

Please sign in to comment.