This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map<B>(f: (a: A) => B): Decoder<B>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Applies the `decoder` to all of the elements of an array. | |
*/ | |
export declare const array: <A>(decoder: Decoder<A>) => Decoder<A[]>; | |
/** | |
* Decodes the value at a particular field in a JavaScript object. | |
*/ | |
export declare const field: <A>(name: string, decoder: Decoder<A>) => Decoder<A>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { oneOf, string, number } from 'jsonous'; | |
const numberToString = number().map(n => n.toString()); | |
const decoder = oneOf([string(), numberToString]); | |
decoder.decodeAny('forty-two'); | |
// --> Ok('forty-two'); | |
decoder.decodeAny(42); | |
// --> Ok('42'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { maybe, string, field } from 'jsonous'; | |
const decoder = field('foo', maybe(string())); | |
decoder.decodeAny({ foo: 'bar' }) | |
// --> Ok(Just('bar')) | |
decoder.decodeAny({ foo: null }) | |
// --> Ok(Nothing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { at, number } from 'jsonous'; | |
at(['foo', 0, 'bar'], number()).decodeAny({ foo: [{ bar: 42 }] }) | |
// --> Ok(42) | |
at(['foo', 1, 'bar'], number()).decodeAny({ foo: [{ bar: 42 }] }) | |
// --> Err('Path failure: Expected to find path '["foo",1]' in {"foo":[{"bar":42}]}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { field, string, boolean, number, succeed } from 'jsonous'; | |
const decoder = | |
field('foo', string()).map(s => s + '-ish').andThen(foo => | |
field('bar', number()).map(n => n*2).andThen(bar => | |
field('baz', boolean()).map(b => !b).andThen(baz => | |
succeed({ foo, bar, baz })))); | |
decoder.decodeAny({ foo: 'hello', bar: 42, baz: true }); | |
// --> Ok({ foo: 'hello-ish', bar: 84, baz: false }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { string } from 'jsonous'; | |
const decoder = string().map(s => s + '-ish'); | |
decoder.decodeAny('soon'); // --> Ok('soon-ish') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { string, field } from 'jsonous'; | |
const decoder = field('foo', string()); | |
decoder.decodeAny({ foo: 'bar' }); // --> Ok('bar') | |
decoder.decodeAny({ baz: 'bar' }); // --> Err('Expected to find an object with key 'foo'. Instead found { baz: 'bar' }') | |
decoder.decodeAny({ foo: 8 }); // --> Err('Error found in field 'foo' of { foo: 8 }: Expected a string. Instead found 8.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { array, string } from 'jsonous'; | |
const decoder = array(string()); | |
decoder.decodeAny([ 'foo', 'bar' ]); // --> Ok(['foo', 'bar']) | |
decoder.decodeAny('foo'); // --> Err('Expected an array. Instead found foo') | |
decoder.decodeAny([ 'foo', 42, 'bar']); // --> Err('Error found in array at [1]: Expected a string. Instead found 42') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { string } from 'jsonous'; | |
const decoder = string(); | |
decoder.decodeAny("foo"); // --> Ok("foo") | |
decoder.decodeAnt(8); // --> Err("Expected to find a string. Instead found 8") |