Skip to content

Commit 90e9bae

Browse files
author
Marc Coquand
committed
updated decoders
1 parent 76b4e02 commit 90e9bae

File tree

10 files changed

+173
-70
lines changed

10 files changed

+173
-70
lines changed

docs/assets/js/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/classes/_index_.decoder.html

Lines changed: 112 additions & 47 deletions
Large diffs are not rendered by default.

docs/classes/_index_.validationfailederror.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ <h3>constructor</h3>
117117
<li class="tsd-description">
118118
<aside class="tsd-sources">
119119
<ul>
120-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L12">src/index.ts:12</a></li>
120+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L13">src/index.ts:13</a></li>
121121
</ul>
122122
</aside>
123123
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -139,7 +139,7 @@ <h3>error</h3>
139139
<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>
140140
<aside class="tsd-sources">
141141
<ul>
142-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L12">src/index.ts:12</a></li>
142+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L13">src/index.ts:13</a></li>
143143
</ul>
144144
</aside>
145145
</section>

docs/classes/_result_.result.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ <h3>constructor</h3>
132132
<li class="tsd-description">
133133
<aside class="tsd-sources">
134134
<ul>
135-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L65">src/result.ts:65</a></li>
135+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L65">src/result.ts:65</a></li>
136136
</ul>
137137
</aside>
138138
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -154,7 +154,7 @@ <h3>get</h3>
154154
<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>
155155
<aside class="tsd-sources">
156156
<ul>
157-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L2">src/result.ts:2</a></li>
157+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L2">src/result.ts:2</a></li>
158158
</ul>
159159
</aside>
160160
</section>
@@ -171,7 +171,7 @@ <h3>and<wbr>Then</h3>
171171
<li class="tsd-description">
172172
<aside class="tsd-sources">
173173
<ul>
174-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L24">src/result.ts:24</a></li>
174+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L24">src/result.ts:24</a></li>
175175
</ul>
176176
</aside>
177177
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -218,7 +218,7 @@ <h3>map</h3>
218218
<li class="tsd-description">
219219
<aside class="tsd-sources">
220220
<ul>
221-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L4">src/result.ts:4</a></li>
221+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L4">src/result.ts:4</a></li>
222222
</ul>
223223
</aside>
224224
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -265,7 +265,7 @@ <h3>map<wbr>Error</h3>
265265
<li class="tsd-description">
266266
<aside class="tsd-sources">
267267
<ul>
268-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L14">src/result.ts:14</a></li>
268+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L14">src/result.ts:14</a></li>
269269
</ul>
270270
</aside>
271271
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -312,7 +312,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> fail</h3>
312312
<li class="tsd-description">
313313
<aside class="tsd-sources">
314314
<ul>
315-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L61">src/result.ts:61</a></li>
315+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L61">src/result.ts:61</a></li>
316316
</ul>
317317
</aside>
318318
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -344,7 +344,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> is<wbr>Fail</h3>
344344
<li class="tsd-description">
345345
<aside class="tsd-sources">
346346
<ul>
347-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L65">src/result.ts:65</a></li>
347+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L65">src/result.ts:65</a></li>
348348
</ul>
349349
</aside>
350350
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -376,7 +376,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> is<wbr>Ok</h3>
376376
<li class="tsd-description">
377377
<aside class="tsd-sources">
378378
<ul>
379-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L64">src/result.ts:64</a></li>
379+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L64">src/result.ts:64</a></li>
380380
</ul>
381381
</aside>
382382
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -408,7 +408,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> merge</h3>
408408
<li class="tsd-description">
409409
<aside class="tsd-sources">
410410
<ul>
411-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L33">src/result.ts:33</a></li>
411+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L33">src/result.ts:33</a></li>
412412
</ul>
413413
</aside>
414414
<h4 class="tsd-type-parameters-title">Type parameters</h4>
@@ -464,7 +464,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> ok</h3>
464464
<li class="tsd-description">
465465
<aside class="tsd-sources">
466466
<ul>
467-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/result.ts#L59">src/result.ts:59</a></li>
467+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/result.ts#L59">src/result.ts:59</a></li>
468468
</ul>
469469
</aside>
470470
<h4 class="tsd-type-parameters-title">Type parameters</h4>

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ <h1>elm-decoders</h1>
6868
<h1>Decoder</h1>
6969
</a>
7070
<p>A powerful, well tested, data decoder for Typescript.</p>
71-
<p>API Documentation: <a href="https://marccoquand.github.io/Decoder/docs/index.html">Decoder</a>
71+
<p>API Documentation: <a href="https://marccoquand.github.io/Decoder/modules/_index_.html">Decoder</a>
7272
Github: <a href="https://github.com/MarcCoquand/Decoder">Decoder</a></p>
7373
<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>
7474
<a href="https://travis-ci.org/MarcCoquand/Decoder"><img src="https://travis-ci.org/MarcCoquand/Decoder.svg?branch=master" alt="Build Status"></a></p>

docs/modules/_error_.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ <h3>Decode<wbr>Error</h3>
9494
<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>
9595
<aside class="tsd-sources">
9696
<ul>
97-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L1">src/error.ts:1</a></li>
97+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L1">src/error.ts:1</a></li>
9898
</ul>
9999
</aside>
100100
</section>
@@ -111,7 +111,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> format<wbr>Index</h3>
111111
<li class="tsd-description">
112112
<aside class="tsd-sources">
113113
<ul>
114-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L12">src/error.ts:12</a></li>
114+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L12">src/error.ts:12</a></li>
115115
</ul>
116116
</aside>
117117
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -137,7 +137,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> make<wbr>Single<wbr>Error</
137137
<li class="tsd-description">
138138
<aside class="tsd-sources">
139139
<ul>
140-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/error.ts#L7">src/error.ts:7</a></li>
140+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/error.ts#L7">src/error.ts:7</a></li>
141141
</ul>
142142
</aside>
143143
<h4 class="tsd-parameters-title">Parameters</h4>

docs/modules/_index_.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ <h3>Decoded<wbr>Value</h3>
103103
<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>
104104
<aside class="tsd-sources">
105105
<ul>
106-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L585">src/index.ts:585</a></li>
106+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L633">src/index.ts:633</a></li>
107107
</ul>
108108
</aside>
109109
<div class="tsd-comment tsd-typography">
@@ -139,7 +139,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>Date</h3>
139139
<li class="tsd-description">
140140
<aside class="tsd-sources">
141141
<ul>
142-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L4">src/index.ts:4</a></li>
142+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L4">src/index.ts:4</a></li>
143143
</ul>
144144
</aside>
145145
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -162,7 +162,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> isISO</h3>
162162
<li class="tsd-description">
163163
<aside class="tsd-sources">
164164
<ul>
165-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L5">src/index.ts:5</a></li>
165+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L5">src/index.ts:5</a></li>
166166
</ul>
167167
</aside>
168168
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -185,7 +185,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>Integer</h3>
185185
<li class="tsd-description">
186186
<aside class="tsd-sources">
187187
<ul>
188-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L7">src/index.ts:7</a></li>
188+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L7">src/index.ts:7</a></li>
189189
</ul>
190190
</aside>
191191
<h4 class="tsd-parameters-title">Parameters</h4>
@@ -208,7 +208,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>String<wbr>Number</h
208208
<li class="tsd-description">
209209
<aside class="tsd-sources">
210210
<ul>
211-
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/87c91d3/src/index.ts#L8">src/index.ts:8</a></li>
211+
<li>Defined in <a href="https://github.com/MarcCoquand/Decoder/blob/67dd445/src/index.ts#L8">src/index.ts:8</a></li>
212212
</ul>
213213
</aside>
214214
<h4 class="tsd-parameters-title">Parameters</h4>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "6.0.2",
2+
"version": "6.1.0",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",

src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,33 @@ export class Decoder<T> {
493493
*/
494494
public static any: Decoder<unknown> = new Decoder((data) => Result.ok(data));
495495

496+
/**
497+
* run a decoder on each item in an array, collecting the amount of successful and failed decodings.
498+
*
499+
* ```
500+
* example:
501+
*
502+
* Decoder.number.sequence([1,3,4,"hello"]) // => {successful: [1,2,3], failed: ["hello"]}
503+
* ```
504+
*/
505+
public sequence = (
506+
array: unknown[]
507+
): { successful: T[]; failed: unknown[] } => {
508+
const failed = [];
509+
const successful = [];
510+
for (const item of array) {
511+
const result = this.run(item);
512+
switch (result.type) {
513+
case 'OK':
514+
successful.push(result.value);
515+
break;
516+
case 'FAIL':
517+
failed.push(item);
518+
}
519+
}
520+
return { failed, successful };
521+
};
522+
496523
/**
497524
* Decode values of an object where the keys are unknown.
498525
*

test/decoder.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ describe('Dict decoder', () => {
145145
});
146146
});
147147

148+
describe('Sequence', () => {
149+
it('collects failures and successes', () => {
150+
const array = [1, 2, 3, 'hello'];
151+
152+
const { successful, failed } = Decoder.number.sequence(array);
153+
154+
expect(successful).toEqual([1, 2, 3]);
155+
expect(failed).toEqual(['hello']);
156+
});
157+
});
158+
148159
describe('Any decoder', () => {
149160
it('accepts anything', () => {
150161
fc.assert(

0 commit comments

Comments
 (0)