Skip to content

Commit b6f4aaf

Browse files
author
Elias Mulhall
committed
[chore] rebuild docs
1 parent fc14fe0 commit b6f4aaf

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

docs/classes/_decoder_.decoder.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Alternatively, the main decoder `run()` method returns an object of type `Result
3333
* [run](_decoder_.decoder.md#run)
3434
* [runPromise](_decoder_.decoder.md#runpromise)
3535
* [runWithException](_decoder_.decoder.md#runwithexception)
36+
* [where](_decoder_.decoder.md#where)
3637
* [anyJson](_decoder_.decoder.md#anyjson)
3738
* [array](_decoder_.decoder.md#array)
3839
* [boolean](_decoder_.decoder.md#boolean)
@@ -103,9 +104,11 @@ ___
103104

104105
**andThen**B(f: *`function`*): [Decoder](_decoder_.decoder.md)<`B`>
105106

106-
Chain together a sequence of decoders. The first decoder will run, and then the function will determine what decoder to run second. If the result of the first decoder succeeds then `f` will be applied to the decoded value. If it fails the error will propagate through. One use case for `andThen` is returning a custom error message.
107+
Chain together a sequence of decoders. The first decoder will run, and then the function will determine what decoder to run second. If the result of the first decoder succeeds then `f` will be applied to the decoded value. If it fails the error will propagate through.
107108

108-
Example:
109+
This is a very powerful method -- it can act as both the `map` and `where` methods, can improve error messages for edge cases, and can be used to make a decoder for custom types.
110+
111+
Example of adding an error message:
109112

110113
```
111114
const versionDecoder = valueAt(['version'], number());
@@ -127,14 +130,24 @@ decoder.run({version: 5, x: 'abc'})
127130
// =>
128131
// {
129132
// ok: false,
130-
// error: {
131-
// ...
132-
// at: 'input',
133-
// message: 'Unable to decode info, version 5 is not supported.'
134-
// }
133+
// error: {... message: 'Unable to decode info, version 5 is not supported.'}
135134
// }
136135
```
137136

137+
Example of decoding a custom type:
138+
139+
```
140+
// nominal type for arrays with a length of at least one
141+
type NonEmptyArray<T> = T[] & { __nonEmptyArrayBrand__: void };
142+
143+
const nonEmptyArrayDecoder = <T>(values: Decoder<T>): Decoder<NonEmptyArray<T>> =>
144+
array(values).andThen(arr =>
145+
arr.length > 0
146+
? succeed(createNonEmptyArray(arr))
147+
: fail(`expected a non-empty array, got an empty array`)
148+
);
149+
```
150+
138151
**Type parameters:**
139152

140153
#### B
@@ -243,6 +256,41 @@ Run the decoder and return the value on success, or throw an exception with a fo
243256

244257
**Returns:** `A`
245258

259+
___
260+
<a id="where"></a>
261+
262+
### where
263+
264+
**where**(test: *`function`*, errorMessage: *`string`*): [Decoder](_decoder_.decoder.md)<`A`>
265+
266+
Add constraints to a decoder _without_ changing the resulting type. The `test` argument is a predicate function which returns true for valid inputs. When `test` fails on an input, the decoder fails with the given `errorMessage`.
267+
268+
```
269+
const chars = (length: number): Decoder<string> =>
270+
string().where(
271+
(s: string) => s.length === length,
272+
`expected a string of length ${length}`
273+
);
274+
275+
chars(5).run('12345')
276+
// => {ok: true, result: '12345'}
277+
278+
chars(2).run('HELLO')
279+
// => {ok: false, error: {... message: 'expected a string of length 2'}}
280+
281+
chars(12).run(true)
282+
// => {ok: false, error: {... message: 'expected a string, got a boolean'}}
283+
```
284+
285+
**Parameters:**
286+
287+
| Param | Type |
288+
| ------ | ------ |
289+
| test | `function` |
290+
| errorMessage | `string` |
291+
292+
**Returns:** [Decoder](_decoder_.decoder.md)<`A`>
293+
246294
___
247295
<a id="anyjson"></a>
248296

0 commit comments

Comments
 (0)