Skip to content

Commit

Permalink
updated decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Coquand committed Apr 20, 2021
1 parent 76b4e02 commit 90e9bae
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/assets/js/search.json

Large diffs are not rendered by default.

159 changes: 112 additions & 47 deletions docs/classes/_index_.decoder.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes/_index_.validationfailederror.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ <h3>constructor</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L12">src/index.ts:12</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L13">src/index.ts:13</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -139,7 +139,7 @@ <h3>error</h3>
<div class="tsd-signature tsd-kind-icon">error<span class="tsd-signature-symbol">:</span> <a href="../modules/_error_.html#decodeerror" class="tsd-signature-type">DecodeError</a></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L12">src/index.ts:12</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L13">src/index.ts:13</a></li>
</ul>
</aside>
</section>
Expand Down
20 changes: 10 additions & 10 deletions docs/classes/_result_.result.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h3>constructor</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L65">src/result.ts:65</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L65">src/result.ts:65</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -154,7 +154,7 @@ <h3>get</h3>
<div class="tsd-signature tsd-kind-icon">get<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{ </span>type<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">"OK"</span><span class="tsd-signature-symbol">; </span>value<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol"> }</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-symbol">{ </span>error<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">E</span><span class="tsd-signature-symbol">; </span>type<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">"FAIL"</span><span class="tsd-signature-symbol"> }</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L2">src/result.ts:2</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L2">src/result.ts:2</a></li>
</ul>
</aside>
</section>
Expand All @@ -171,7 +171,7 @@ <h3>and<wbr>Then</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L24">src/result.ts:24</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L24">src/result.ts:24</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -218,7 +218,7 @@ <h3>map</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L4">src/result.ts:4</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L4">src/result.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -265,7 +265,7 @@ <h3>map<wbr>Error</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L14">src/result.ts:14</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L14">src/result.ts:14</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -312,7 +312,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> fail</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L61">src/result.ts:61</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L61">src/result.ts:61</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -344,7 +344,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> is<wbr>Fail</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L65">src/result.ts:65</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L65">src/result.ts:65</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -376,7 +376,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> is<wbr>Ok</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L64">src/result.ts:64</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L64">src/result.ts:64</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -408,7 +408,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> merge</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L33">src/result.ts:33</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L33">src/result.ts:33</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -464,7 +464,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> ok</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L59">src/result.ts:59</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L59">src/result.ts:59</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1>elm-decoders</h1>
<h1>Decoder</h1>
</a>
<p>A powerful, well tested, data decoder for Typescript.</p>
<p>API Documentation: <a href="https://marccoquand.github.io/Decoder/docs/index.html">Decoder</a>
<p>API Documentation: <a href="https://marccoquand.github.io/Decoder/modules/_index_.html">Decoder</a>
Github: <a href="https://github.com/MarcCoquand/Decoder">Decoder</a></p>
<p><a href="https://coveralls.io/github/MarcCoquand/Decoder?branch=master"><img src="https://coveralls.io/repos/github/MarcCoquand/Decoder/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://travis-ci.org/MarcCoquand/Decoder"><img src="https://travis-ci.org/MarcCoquand/Decoder.svg?branch=master" alt="Build Status"></a></p>
Expand Down
6 changes: 3 additions & 3 deletions docs/modules/_error_.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h3>Decode<wbr>Error</h3>
<div class="tsd-signature tsd-kind-icon">Decode<wbr>Error<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-symbol">{}</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-symbol">{ </span>error<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> | </span><a href="_error_.html#decodeerror" class="tsd-signature-type">DecodeError</a><span class="tsd-signature-symbol">; </span>index<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">; </span>value<span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">any</span><span class="tsd-signature-symbol"> }</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-symbol">[</span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">, </span><a href="_error_.html#decodeerror" class="tsd-signature-type">DecodeError</a><span class="tsd-signature-symbol">]</span><span class="tsd-signature-symbol"> | </span><a href="_error_.html#decodeerror" class="tsd-signature-type">DecodeError</a><span class="tsd-signature-symbol">[]</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L1">src/error.ts:1</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L1">src/error.ts:1</a></li>
</ul>
</aside>
</section>
Expand All @@ -111,7 +111,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> format<wbr>Index</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L12">src/error.ts:12</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L12">src/error.ts:12</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -137,7 +137,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> make<wbr>Single<wbr>Error</
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L7">src/error.ts:7</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L7">src/error.ts:7</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
10 changes: 5 additions & 5 deletions docs/modules/_index_.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h3>Decoded<wbr>Value</h3>
<div class="tsd-signature tsd-kind-icon">Decoded<wbr>Value&lt;T&gt;<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">T</span><span class="tsd-signature-symbol"> extends </span><span class="tsd-signature-type">Decoder</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-symbol">infer </span> U<span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol"> ? </span><span class="tsd-signature-type">U</span><span class="tsd-signature-symbol"> : </span><span class="tsd-signature-type">never</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L585">src/index.ts:585</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L633">src/index.ts:633</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -139,7 +139,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>Date</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L4">src/index.ts:4</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L4">src/index.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -162,7 +162,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> isISO</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L5">src/index.ts:5</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L5">src/index.ts:5</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -185,7 +185,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>Integer</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L7">src/index.ts:7</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L7">src/index.ts:7</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -208,7 +208,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>String<wbr>Number</h
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L8">src/index.ts:8</a></li>
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L8">src/index.ts:8</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "6.0.2",
"version": "6.1.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,33 @@ export class Decoder<T> {
*/
public static any: Decoder<unknown> = new Decoder((data) => Result.ok(data));

/**
* run a decoder on each item in an array, collecting the amount of successful and failed decodings.
*
* ```
* example:
*
* Decoder.number.sequence([1,3,4,"hello"]) // => {successful: [1,2,3], failed: ["hello"]}
* ```
*/
public sequence = (
array: unknown[]
): { successful: T[]; failed: unknown[] } => {
const failed = [];
const successful = [];
for (const item of array) {
const result = this.run(item);
switch (result.type) {
case 'OK':
successful.push(result.value);
break;
case 'FAIL':
failed.push(item);
}
}
return { failed, successful };
};

/**
* Decode values of an object where the keys are unknown.
*
Expand Down
11 changes: 11 additions & 0 deletions test/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ describe('Dict decoder', () => {
});
});

describe('Sequence', () => {
it('collects failures and successes', () => {
const array = [1, 2, 3, 'hello'];

const { successful, failed } = Decoder.number.sequence(array);

expect(successful).toEqual([1, 2, 3]);
expect(failed).toEqual(['hello']);
});
});

describe('Any decoder', () => {
it('accepts anything', () => {
fc.assert(
Expand Down

0 comments on commit 90e9bae

Please sign in to comment.