Chains decoders together. Can be used when the value from a decoder is needed to decode the rest of the data. For example, if you have a versioned api, you can check the version number and then select an appropriate decoder for the rest of the data.
Also, chaining decoders is one way to build new types from decoded objects.
This is a special case of chaining. Like andThen
, assign
allows you
to combine several decoders. With assign
, we build up an object (or scope)
internally. THe benefit is that is allows you to avoid and the nesting
and callback hell typically associated with using andThen
to build objects.
The idea for assign came from this blog: https://medium.com/@dhruvrajvanshi/simulating-haskells-do-notation-in-typescript-e48a9501751c
Run the current decoder on any value
Parse the json string and run the current decoder on the resulting value. Parse errors are returned in an Result.Err, as with any decoder error.
Inject a side-effectual operation in the middle of a Decoder chain. This is a convenient mechanism for debugging decoders using console logging. I don't reccomend using this mechanusm for making API calls, or anything complex like that.
If a decoder fails, do something side-effectual
Lifts any function up to operate on the value in the Decoder context.
If a decoder fails, map over the failure message.
Returns a function that runs this docoder over any value when called. This is a convenient way to convert a decoder into a callback.
Returns a function that runs this decoder over a JSON string when called. This is a convenient way to convert a decoder into a callback.
Generated using TypeDoc
A Decoder represents a value that can be converted to a known type, either from JSON or from an typed object.