Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "decoders"

Index

Type aliases

Decoder combinators Variables

Decoder combinators Functions

Primitive decoders Functions

Type aliases

Decoder

Decoder<T>: function

Type parameters

  • T

Type declaration

    • (input: unknown): T
    • Parameters

      • input: unknown

      Returns T

Decoder combinators Variables

Const attempt

attempt: try_ = try_

Synonym for try_

Decoder combinators Functions

Const array

  • Transform a Decoder<T> into a Decoder<Array<T>>.

    Type parameters

    • T

    Parameters

    Returns Decoder<T[]>

Const assoc

  • assoc<K, V, T>(keyDecoder: Decoder<K>, valueDecoder: Decoder<V>): (Anonymous function)
  • Create a decoder for arbitrary key/value pairs from a key decoder and a value decoder. Effectively transforms a Decoder<K> and a Decoder<V> into a Decoder<Array<{ key: K, value: V }>>.

    Type parameters

    • K

    • V

    • T: object

    Parameters

    Returns (Anonymous function)

Const at

  • at<T>(path: Array<string | number> | string | number, decoder: Decoder<T>): Decoder<T>
  • Apply the provided decoder at the specified path in a complex object. Path can be either:

    • a string (when querying a top level field)
    • a number (when querying an array index)
    • an Array<string | number> (to query an arbitrarily nested field)

    Type parameters

    • T

    Parameters

    • path: Array<string | number> | string | number
    • decoder: Decoder<T>

    Returns Decoder<T>

Const nullable

  • nullable<T>(decoder: Decoder<T>): function
  • Transform a Decoder<T> into a Decoder<T | null>.

    Type parameters

    • T

    Parameters

    Returns function

      • (input: unknown): T
      • Parameters

        • input: unknown

        Returns T

oneOf

Const optional

  • optional<T>(decoder: Decoder<T>): function
  • Transform a Decoder<T> into a Decoder<T | null | undefined>.

    Type parameters

    • T

    Parameters

    Returns function

      • (input: unknown): T
      • Parameters

        • input: unknown

        Returns T

Const props

  • props<T>(obj: T): (Anonymous function)
  • Create a decoder for some complex structure from an object where each value is a Decoder and the keys correspond to the desired output structure.

    const personDecoder = decod.props({
      first_name: decod.at('first_name', decod.string),
      last_name: decod.at('last_name', decod.string),
      age: decod.at('age', decod.number),
    });

    Type parameters

    • T: object

    Parameters

    • obj: T

    Returns (Anonymous function)

try_

  • Try a decoder, and fallback to a specified value (if provided) if the decoder fails. If no default value is specified, fallback to undefined.

    Type parameters

    • T

    Parameters

    Returns Decoder<T | undefined>

  • Type parameters

    • T

    Parameters

    • decoder: Decoder<T>
    • defaultValue: T

    Returns Decoder<T>

Primitive decoders Functions

Const boolean

  • boolean(input: unknown): boolean

Const date

  • date(input: unknown): Date

Const is

  • is<T>(expectedValue: T): Decoder<T>
  • Decode an unknown value if it matches the expected value. Otherwise, throws a ScalarDecoderError.

    It is mostly intended to be used in conjunction with oneOf for decoding groups of specific values.

    const droidDecoder = decod.oneOf(
      decod.is('r2d2'),
      decod.is('c3po')
    );

    Type parameters

    • T: string | number | boolean | null | undefined

    Parameters

    • expectedValue: T

    Returns Decoder<T>

Const null_

  • null_(input: unknown): null

Const number

  • number(input: unknown): number

Const string

  • string(input: unknown): string

Const undefined_

  • undefined_(input: unknown): undefined

Generated using TypeDoc