Skip to main content
Module

x/fun/decoder.ts

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
import * as fun from "https://deno.land/x/fun@v2.0.0-alpha.10/decoder.ts";

The Decoder module contains tools for the course parsing of javascript objects into well typed structures. In the event that parsing fails a Decoder returns a DecodeError data structure which contains detailed information on how and where parsing failed.

Variables

An internal literal instance over null used in the nullable combinator.

An internal literal instance over undefined used in the undefinable combinator.

The canonical implementation of Alt for Decoder. It contains the methods alt and map

The canonical implementation of Applicative for Decoder. It contains the methods of, ap, and map.

The canonical implementation of Apply for Decoder. It contains the methods ap and map.

A Decoder that validates booleans.

The canonical implementation of Chain for Decoder. It contains the methods ap, map, and chain.

The canonical implementation of Functor for Decoder. It contains the method map.

The canonical instance of Monad for Decoded. It contains the methods of, ap, map, join, and chain.

The canonical implementation of Monad for Decoder. It contains the methods of, ap, map, join, and chain.

The canonical implementation of Monoid for DecodeError. It contains the methods concat and empty.

A Decoder that validates numbers.

The optional tag denotes that a property in a struct or tuple is optional. This means that the key or index may not exist on the struct or tuple.

The required tag denotes that a property in a struct or tuple is required. This means that the key or index must exist on the struct or tuple.

The canonical implementation of Schemable for Decoder. It contains the methods unknown, string, number, boolean, literal, nullable, undefinable, record, array, tuple, struct, partial, intersect, union, and lazy.

The canonical implementation of Semigroup for DecodeError. It contains the method concat.

A Decoder that validates strings.

A Decoder that always returns true and casts the result to unknown.

Functions

Provide an alternative Decoder to fallback against if the first one fails.

Annotate the DecodeError output of an existing Decoder if it fails while parsing. Internally, this uses the DecodeError Wrap constructor.

Given a Decoder returning a function A => I and a Decoder returning a value A, combine them into a Decoder returning I.

A decoder against an array with only values that adhere to the passed in items decoder.

A Decoder that checks that an object is both an Array and that it's length is N.

Chain the result of one decoder into a new decoder.

Compose two Decoders where the input to second aligns with the output of first.

Combine two DecodeErrors into one. If both DecodeErrors are Unions then they are merged into a Union, if they are both Intersections then are merged into an Intersection, otherwise they are wrapped in a Many.

Map over the input of a Decoder contravariantly. This allows one to use an existing decoder against a transformed input.

A Decoder that attempts to decode a Date using new Date(value). If the process of calling new Date throws or the getTime method on the new date object returns NaN, then a failure is returned. If a Date can be derived from the object then a Date object is returned.

Map over the input and output of a Decoder. This is effectively a combination of map and contramap in a single operator.

Given a DecodeError, this function will produce a printable tree of errors.

Construct an empty DecodeError. This is a many that contains no errors.

A combinator over Decoded that maps a DecodeError into a printable tree.

Construct a Decoded failure. Specifically, this constructs a Leaf DecodeError and wraps it in Left.

Construct a Decoded from a DecodeError. This allows one to construct a Decoded failure directly from DecodeErrors other than Leaf.

Construct a Decoder<A, A> from a Predicate and a reason for failure or a Decoder<A, B> from a Refinement<A, B> and a reason for failure. While decoding, the value A is passed to the predicate/refinement. If it returns true then the result is wrapped in Success, otherwise the value and reason are wrapped in Failure.

Construct an Index from an index and an existing DecodeError.

A Decoder combinator that intersects two existing decoders. The resultant decoder ensures that an input matches both decoders. Nested intersection combinators will combine and flatten their error trees.

Construct an Intersection from two or more DecodeErrors.

A refinement that returns true when a DecodeError is an Index.

A refinement that returns true when a DecodeError is an Intersection.

A refinement that returns true when a DecodeError is a Key.

A refinement that returns true when a DecodeError is a Leaf.

A refinement that returns true when a DecodeError is a Many.

A refinement that returns true when a DecodeError is a Union.

A refinement that returns true when a DecodeError is a Wrap.

Collapse a Decoder that returns a Decoder into a new Decoder.

A Decoder combinator that will check that a value is a string and then attempt to parse it as JSON.

Construct a Key from a key and an existing DecodeError.

The Lazy decoder combinator allows for the creation of recursive or mutually recursive decoding. The passed decoder thunk is memoized to keep the vm from falling into an infinite loop.

Construct a Lead from an unknown value and a reason for the decode error.

Create a Decoder from a list of literal values. Literal values can be strings, numbers, booleans, null, or undefined. This decoder will only return Right if the value being decoded has object equality with one of the literals supplied.

Construct a Many from zero or more DecodeErrors.

Map over the output of a Decoder.

Construct a catamorphism over DecodeError, mapping each case of a DecodeError into the single type O.

A decoder combinator that modifies an existing decoder to accept null as an input value and a successful return value.

Create a Decoder<D, A> from a constant value A.

A decoder over a heterogenous record type. This struct can have different values at each key or the key can not exist or the value can be undefined, and the resultant decoder will ensure that each key matches its corresponding decoder.

A decoder against a record with string keys and values that match the items decoder.

Apply a refinement or predicate to the output of an existing Decoder. This is useful for building complicated Decoders.

A decoder over a heterogenous record type. This struct can have different values at each key, and the resultant decoder will ensure that each key matches its corresponding decoder.

Construct a Decoded from a value A.

A decoder over a heterogenous tuple. This tuple can have different values for each tuple element, but is constrained to a specific size and order.

A decoder combinator that modifies an existing decoder to accept undefined as an input value and a successful return value.

Provide an alternative Decoder to fallback against if the first one fails. This is an alias of alt.

Construct a Union from two or more DecodeErrors.

Construct a Wrap from context and an existing DecodeError.

Interfaces

Specifies Decoded as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions.

Specifies Decoder as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions and contravariant paramter D corresponding to the 0th index of any substitutions.

Specifies Decoder<unknown, A> as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions. This is a specific Kind used to construct the Schemable Decoder.

Type Aliases

A type that matches any decoder type.

The Decoded type is an alias of Either<DecodeError, A>. This is the output of a Decoder when the parsing fails.

The DecodeError type is a discriminated union of potential contextualized errors that are encountered while decoding.

The Decoder<D, A> type represents a function that parses some input D into a value A or a DecodeError. This isn't true parsing of a grammar but instead a combination of refinement and error tracking.

The Index type is used to contextualize an error that occurred at an index in an Array or Tuple.

The Intersection type is used to associate two or more DecodeErrors as an intersection of errors.

The Key type is used to contextualize an error that occurred at a key in a struct.

The Leaf type is the simplest of DecodeErrors. It indicates that some concrete value did not match the expected value. The reason field is used to indicate the expected value.

The Many type is used to represent zero or more DecodeErrors without context. It's purpose is to be used as both Empty and a target for concatenation of DecodeErrors that are neither Union or Intersection types. This allows us to build a Monoid over DecodeError.

The Property type is a type level denotation that specifies whether a key or index is required or optional.

The Union type is used to associate two or more DecoderErrors as a union of errors.

The Wrap type is used to give context to an existing DecodeError. This can be as simple as wrapping an array decode error to indicate that we first check for an array before we check that the array matches a tuple definition. It can also be used to constrain or annotate.