Created
June 25, 2017 01:32
-
-
Save kofno/9ed7c7582f110eb8bdd0a8bc4e3663bb to your computer and use it in GitHub Desktop.
onnOf decoder
This file contains hidden or 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'); | |
| 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