Skip to content

Commit fa087e8

Browse files
committed
Add Decode.map example
1 parent 0908ea4 commit fa087e8

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

index.d.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ export namespace Decode {
370370
export function many<T>(child: Decoder<T>) : Decoder<T[]>
371371

372372
/**
373-
* Take the value decoded by the given decoder and do something
374-
* with it.
373+
* Take the value decoded by the given decoder and transform it.
375374
*
376375
* @param fn - Your mapping function
377376
* @param child - The decoder to run before calling your function
@@ -381,7 +380,21 @@ export namespace Decode {
381380
*
382381
* @example
383382
* ```typescript
384-
* // TODO
383+
* // Add 42 to a number
384+
* const add_42_decoder = Decode.map(value => value + 42, Decode.number);
385+
*
386+
* decode(add_42_decoder, 0) === 42
387+
* decode(add_42_decoder, -42) === 0
388+
* decode(add_42_decoder, 42) === 84
389+
* decode(add_42_decoder, 'text') // Fails
390+
*
391+
* // Convert any value to its string representation.
392+
* const to_string_decoder = Decode.map(value => String(value), Decode.unknown);
393+
*
394+
* decode(to_string_decoder, 42) === '42'
395+
* decode(to_string_decoder, {}) === '[object Object]'
396+
* decode(to_string_decoder, 'text') === 'text'
397+
* decode(to_string_decoder, Symbol('my_symbol')) === 'Symbol(my_symbol)'
385398
* ```
386399
*/
387400
export function map<A, B>(fn: (a: A) => B, child: Decoder<A>) : Decoder<B>

0 commit comments

Comments
 (0)