Skip to content

Instantly share code, notes, and snippets.

@kofno
kofno / map.d.ts
Created June 25, 2017 14:52
Decoder map sig
map<B>(f: (a: A) => B): Decoder<B>;
@kofno
kofno / structured.d.ts
Created June 25, 2017 14:36
Structured decoder signature
/**
* 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>;
@kofno
kofno / oneOf.js
Created June 25, 2017 01:32
onnOf decoder
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');
@kofno
kofno / maybe.js
Created June 25, 2017 01:23
The maybe decoder
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)
@kofno
kofno / at.js
Created June 25, 2017 01:11
The at decoder
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}]}')
@kofno
kofno / andThen.js
Last active June 25, 2017 17:30
andThen applied to decoders
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 })
@kofno
kofno / stringish.js
Last active June 25, 2017 00:23
Map applied to decoders
import { string } from 'jsonous';
const decoder = string().map(s => s + '-ish');
decoder.decodeAny('soon'); // --> Ok('soon-ish')
@kofno
kofno / field-example.js
Created June 24, 2017 21:11
Field decoder example Jsonous
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.')
@kofno
kofno / arra-example.js
Created June 24, 2017 18:24
Array decoder
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')
@kofno
kofno / string-decoder.js
Created June 24, 2017 14:29
string decoder
import { string } from 'jsonous';
const decoder = string();
decoder.decodeAny("foo"); // --> Ok("foo")
decoder.decodeAnt(8); // --> Err("Expected to find a string. Instead found 8")