Skip to content

Instantly share code, notes, and snippets.

@kofno
Created June 25, 2017 01:32
Show Gist options
  • Select an option

  • Save kofno/9ed7c7582f110eb8bdd0a8bc4e3663bb to your computer and use it in GitHub Desktop.

Select an option

Save kofno/9ed7c7582f110eb8bdd0a8bc4e3663bb to your computer and use it in GitHub Desktop.
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');
decoder.decodeAny(undefined);
// --> Err('Unexpected data. Last failure: Expected to find a number. Instead found undefined');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment