Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

DecoderFn

DecoderFn: function

A decoder function takes an object of any type and returns a Result

Type declaration

    • (thing: any): Result<string, A>
    • Parameters

      • thing: any

      Returns Result<string, A>

Variables

Const boolean

boolean: Decoder<boolean> = new Decoder<boolean>(value => {if (typeof value !== 'boolean') {const errorMsg = `I expected to find a boolean but instead found ${stringify(value)}`;return err(errorMsg);}return ok(value);})

Boolean decoder

Const date

date: Decoder<Date> = new Decoder<Date>(value => {const errMsg = (v: any): string =>`I expected a date but instead I found ${stringify(v)}`;return ok(value).andThen(s => string.map(v => new Date(v)).decodeAny(s)).orElse(n => number.map(v => new Date(v)).decodeAny(n)).andThen(d => (isNaN(d.getTime()) ? err<any, Date>(value) : ok(d))).mapError(() => errMsg(value));})

Date decoder.

Date decoder expects a value that is a number or a string. It will then try to construct a JavaScript date object from the value.

Const number

number: Decoder<number> = new Decoder<number>(value => {if (typeof value !== 'number') {const errorMsg = `I expected to find a number but instead I found ${stringify(value)}`;return err(errorMsg);}return ok(value);})

Number decoder

Const string

string: Decoder<string> = new Decoder<string>(value => {if (typeof value !== 'string') {const stringified = stringify(value);const errorMsg = `I expected to find a string but instead I found ${stringified}`;return err(errorMsg);}return ok(value);})

String decoder

Functions

Const array

  • Applies the decoder to all of the elements of an array.

    Type parameters

    • A

    Parameters

    Returns Decoder<A[]>

Const at

  • at<A>(path: Array<number | string>, decoder: Decoder<A>): Decoder<A>
  • Decodes the value at a particular path in a nested JavaScript object.

    Type parameters

    • A

    Parameters

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

    Returns Decoder<A>

Const dict

  • Converts a JSON object to a Map<string, A>.

    I would reccomend using this as a decoder of last resort. For correctness, you are probably better off using field decoders and explicitly declaring the shape of the objects you are expecting.

    Type parameters

    • A

    Parameters

    • decoder: Decoder<A>

      The internal decoder to be applied to the object values

    Returns Decoder<Map<string, A>>

Const fail

  • fail<A>(message: string): Decoder<A>
  • Returns a decoder that always fails, returning an Err with the message passed in.

    Type parameters

    • A

    Parameters

    • message: string

    Returns Decoder<A>

Const field

  • Decodes the value at a particular field in a JavaScript object.

    Type parameters

    • A

    Parameters

    Returns Decoder<A>

Const keyValuePairs

  • Converts a JSON object to an array of key value pairs ((string, A)[]). The passed in decoder is applied to the object value. The key will always be converted to a string.

    Type parameters

    • A

    Parameters

    • decoder: Decoder<A>

      The internal decoder to be applied to the object values

    Returns Decoder<[string, A][]>

Const maybe

  • Makes any decoder optional. Be aware that this can mask a failing decoder because it makes any failed decoder result a nothing.

    Type parameters

    • A

    Parameters

    Returns Decoder<Maybe<A>>

Const nullable

  • Decodes possibly null or undefined values into types. There is overlap between nullable and maybe decoders. The difference is that maybe will always succeed, even if there is an error in the decoder.

    Maybe example:

    maybe(string).decodeAny('foo') // => Ok('foo')
    maybe(string).decodeAny(null)  // => Ok(Nothing)
    maybe(string).decodeAny(42)    // => Ok(Nothing)
    

    Nullable example:

    nullable(string).decodeAny('foo') // => Ok('foo')
    nullable(string).decodeAny(null)  // => Ok(Nothing)
    nullable(string).decodeAny(42)    // => Err...
    

    Type parameters

    • A

    Parameters

    Returns Decoder<Maybe<A>>

Const oneOf

  • Applies a series of decoders, in order, until one succeeds or they all fail.

    Type parameters

    • A

    Parameters

    Returns Decoder<A>

Const succeed

  • Returns a decoder that always succeeds, resolving to the value passed in.

    Type parameters

    • A

    Parameters

    • value: A

    Returns Decoder<A>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc