Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "src/index"

This module can be used to safely decode JSON into a typed value. All of the decoders in this module are pure and reusable.

Index

Type aliases

Dict

Dict: object

Object with arbitrary keys and a single value type. Used by dict.

Type declaration

  • [index: string]: T

EntryDecoder

EntryDecoder: [string, Decoder<T>]

Represents the property of an object to decode. See object for more details.

Functions

andThen

  • Intelligently decode a value based on the result of another decoder. Useful decoding on object whose type or structure is not immediately known. The first decoder can be used to look at part of the object, then the second one can can use the result of that decoder to decide on how to actually decode the object.

    Type parameters

    • A

    • B

    Parameters

    • ad: Decoder<A>

      First decoder to run against the value.

    • cb: function

      Callback that receives the previous result and returns a decoder.

    Returns Decoder<B>

    The decoder returned by from the callback function.

array

  • Decode an array using another decoder for each element. Can only decode arrays of a single type. If this feels like a limitation, you may be looking for tuple, or perhaps andThen paired with TypeScript's union types.

    Type parameters

    • T

    Parameters

    • element: Decoder<T>

      A Decoder that decodes the element type of an array.

    Returns Decoder<T[]>

    A Decoder that will decode an array of elements of the given type.

at

  • at<T>(keyPath: Array<string | number>, decoder: Decoder<T>): Decoder<T>
  • Reaches into a nested data structure and decodes the value there. Useful if you only care about a single value in a nested data structure, or if you want to skip past a top level data key. Can perform lookups on nested arrays and objects.

    Type parameters

    • T

    Parameters

    • keyPath: Array<string | number>

      Path of key lookups into a nested object.

    • decoder: Decoder<T>

      Decoder to use on the nested value.

    Returns Decoder<T>

    A decoder that decodes the value of the nested object.

boolean

dict

  • Decode an object with arbitrary keys. Values must be of the same type.

    Type parameters

    • T

    Parameters

    • value: Decoder<T>

      A Decoder that will be used to decode each value.

    Returns Decoder<Dict>

    A Decoder will decode an object with arbitrary keys.

equal

  • Decode a value and make sure the result equals another value. Useful for checking for null.

    const decoder = object(
      ['shouldBeNull', equal(null)],
      (shouldBeNull) => ({shouldBeNull})
    )
    decoder.decodeJSON('{"shouldBeNull": null}')
    

    Type parameters

    • T

    Parameters

    • value: T

      Value that the input must equal (===).

    Returns Decoder<T>

    A Decoder that decodes a value that equals the given value.

fail

  • fail(message: string): Decoder<never>
  • A decoder that always fails with the given error message.

    Parameters

    • message: string

      Message to throw when this decoder is used.

    Returns Decoder<never>

    Never since it always throws an error.

lazy

  • lazy<T>(thunk: function): Decoder<T>
  • Decode a value with the decoder returned by the given function. This is useful if you need to decode a recursive data structure. TypeScript does not allow the direct use of variables within their definition:

    interface Comment {
      msg: string
      replies: Comment[]
    }
    const decoder: Decoder<Comment> = object(
      ['msg', string()],
      ['replies', array(decoder)],
      (msg, replies) => ({msg, replies})
    )
    // Block-scoped variable 'decoder' used before its declaration.
    // Variable 'decoder' is used before being assigned.
    

    However, a variable can be used within its definition if it is not immediately evaluated, or in other words, wrapped in an anonymous function:

    const decoder: Decoder<Comment> = object(
      ['msg', string()],
      ['replies', array(lazy(() => decoder))],
      (msg, replies) => ({msg, replies})
    )
    

    Type parameters

    • T

    Parameters

    • thunk: function

      A function that will return the decoder.

    Returns Decoder<T>

    A decoder that lazily calls thunk to get the decoder when needed.

map

  • Maps a function over the value returned by a decoder. Useful for creating fancier objects from builtin types.

    import Immutable from 'immutable'
    
    const decoder = map(
      Immutable.List,
      array(number())
    )
    

    Type parameters

    • T1

    • T2

    Parameters

    • transform: function
        • (v: T1): T2
        • Parameters

          • v: T1

          Returns T2

    • decoder: Decoder<T1>

    Returns Decoder<T2>

    A decoder that will apply the function after decoding the input.

number

object

oneOf

  • Try multiple decoders on a value. Useful when paired with union types, or as a way to provide a default value if something goes wrong. No decoders after the first successful one will be tried. In that way oneOf can be thought of a short circuit OR operator for decoders.

    The first/rest split is to make sure at least one decoder is specified.

    Type parameters

    • T

    Parameters

    • first: Decoder<T>

      First decoder to try.

    • Rest ...rest: Decoder<T>[]

      Fallback decoders to try in order if the first fails.

    Returns Decoder<T>

    A decoder or throws an Error of no decoders succeeded.

string

succeed

  • A decoder that always succeeds with the given value.

    Type parameters

    • T

    Parameters

    • value: T

      Value that always gets returned when this decoder is used.

    Returns Decoder<T>

    A decoder that always returns the given value.

tuple

union

Generated using TypeDoc