Options
All
  • Public
  • Public/Protected
  • All
Menu

ParTSing

Index

Type aliases

DecodeError

Union type of all possible decoding errors.

DecodeResult

DecodeResult<In, Out, Err>: DecodeSuccess<In, Out, Err> | DecodeFailure<In, Out, Err>

DecodeResult it's an either type, aunion type of two possible constructors. It can either be:

  • DecodeSuccess: a decoder was able to successfully decode a value
  • DecodeFailure: a decoder failed to decode a value

A DecodeResult brings three type parameters like in <a href="classes/decoder.html">Decoder</a>:

  • In: The input stream for a decoder.
  • Out: The expected result from a decoder.
  • Err: The type that contains information on why a decoder failed.

Type parameters

  • In

  • Out

  • Err

Decoding

Decoding<In, Out, Err>: function

Type signature for a function that takes an input and decodes it into a result object.

Type parameters

  • In

  • Out

  • Err

Type declaration

Error

Error<T>: T["_E"]

Extracts the Err type from a Decoder type.

Type parameters

  • T: object

Input

Input<T>: T["_I"]

Extracts the Int type from a Decoder type.

Type parameters

  • T: object

MarkOptionalFields

MarkOptionalFields<T, U>: MarkOptionalFields<T, U>

Given a type for an object T and an array U of field names from U, return a new object type with the specified fields U marked as optional.

Type parameters

  • T

  • U: any[]

Output

Output<T>: T["_O"]

Extracts the Out type from a Decoder type.

Type parameters

  • T: object

TextDecoder

TextDecoder<T>: Decoder<TextInput, T, DecodeError>

Type alias for a decoder specialized in consuming values of type <a href="interfaces/textinput.html">TextInput</a> and generate errors of type <a href="globals.html#decodeerror">DecodeError</a>.

Type parameters

  • T

TupleToUnion

TupleToUnion<T>: T[number] | never

Transform a tuple type into the union of all the types in the tuple.

Type parameters

  • T: any[]

ValueDecoder

ValueDecoder<T>: Decoder<ValueInput, T, DecodeError>

Type alias for a decoder specialized in consuming values of type <a href="interfaces/valueinput.html">ValueInput</a> and generate errors of type <a href="globals.html#decodeerror">DecodeError</a>.

Type parameters

  • T

Variables

Const anyArrayValue

Decoder that matches an array. Element values are not decoded and are typed as any.

Const anyValue

Decoder that always retun the input value as an untyped (any) value.

Const booleanValue

Decoder that matches a boolean value.

Const char

char: Decoder<TextInput, unknown, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = make((input: TextInput) => {if (input.index < input.input.length) {const c = input.input.charAt(input.index)return success({ ...input, index: input.index + 1 }, c)} else {// no more charactersreturn failure(input, DecodeError.expectedOnce(Entity.CHARACTER))}})

Decoder that matches one single character. This decoder will only fail if it encounters the EOI (end of input).

Const currentPath

currentPath: Decoder<ValueInput, (string | number)[], CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = make(input => success(input, input.path))

A decoder that doesn't consume any input but does return the current path position as its result value.

Const digit

digit: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = regexp(digitPattern).withFailure(DecodeError.expectedOnce(Entity.DIGIT))

Match a single digit character.

digitPattern

digitPattern: RegExp

digitsPattern

digitsPattern: (Anonymous function)

Const eoi

eoi: Decoder<TextInput, void, DecodeError> = make(input => {const index = input.input.lengthif (input.index === index) {return success({ ...input, index }, undefined)} else {return failure(input, DecodeError.expectedEoi)}})

A decoder that doesn't consume anything from the TextInput and produces undefined if it matches the end of the input.

Const finiteNumberValue

finiteNumberValue: Decoder<ValueInput, number, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = numberValue.test(Number.isFinite,DecodeError.expectedMatch(Entity.TYPE, 'finite number'))

Decoder that matches a finite-number value.

Const integerValue

integerValue: Decoder<ValueInput, number, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = numberValue.test(Number.isInteger, DecodeError.expectedMatch(Entity.TYPE, 'integer'))

Decoder that matches an integer value.

Const isToken

isToken: RegExp = /^[a-z$_]+$/i

Pattern to recognize if a field name is in a format that doesn't require quotes.

Const letter

letter: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = regexp(letterPattern).withFailure(DecodeError.expectedOnce(Entity.LETTER))

Match any alphabetical character in a case insensitive matter.

letterPattern

letterPattern: RegExp

lettersPattern

lettersPattern: (Anonymous function)

Const lowerCaseLetter

lowerCaseLetter: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = regexp(lowerCaseLetterPattern).withFailure(DecodeError.expectedOnce(Entity.LOWER_CASE_LETTER))

Match any alphabetical lower-ccase-character.

lowerCaseLetterPattern

lowerCaseLetterPattern: RegExp

lowerCaseLettersPattern

lowerCaseLettersPattern: (Anonymous function)

Const nullValue

nullValue: Decoder<ValueInput, null, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = testValue<null>(v => v === null, 'null').withResult(null).withFailure(DecodeError.expectedMatch(Entity.TYPE, 'null'))

Decoder that matches a null value.

Const numberValue

Decoder that matches a numeric value.

Const optionalWhitespace

Match an optional whitespace character. If no whitespace is matched, the result is the empty string "".

optionalWhitespacePattern

optionalWhitespacePattern: RegExp

Const rest

rest: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = make(input => {const value = input.input.substring(input.index)return success({ ...input, index: input.input.length }, value)})

A decoder that produces all the remaining characters in TextInput.

Const safeIntegerValue

safeIntegerValue: Decoder<ValueInput, number, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = numberValue.test(Number.isSafeInteger,DecodeError.expectedMatch(Entity.TYPE, 'safe integer'))

Decoder that matches a safe-integer value.

Const stringValue

Decoder that matches a string value.

Const testObject

testObject: Decoder<ValueInput, unknown, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = testType<{}>('object').flatMap(value => {return Decoder.of(input => {if (Array.isArray(value)) {return failure(input, DecodeError.expectedMatch(Entity.TYPE, 'not array'))} else {return success(input, value)}})})

Decoder that validates a value to be an object.

Const undefinedValue

Decoder that matches an undefined value.

Const upperCaseLetter

upperCaseLetter: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = regexp(upperCaseLetterPattern).withFailure(DecodeError.expectedOnce(Entity.UPPERCASE_LETTER))

Match any alphabetical upper-case-character.

upperCaseLetterPattern

upperCaseLetterPattern: RegExp

upperCaseLettersPattern

upperCaseLettersPattern: (Anonymous function)

Const whitespace

whitespace: Decoder<TextInput, string, CustomError | ExpectedAnyOf | ExpectedAtLeast | ExpectedField | ExpectedMatch | ExpectedNoneOf | ExpectedOnce | ExpectedWithinRange | PatternMismatch | ExpectedEoi | UnexpectedEoi> = regexp(whitespacePattern).withFailure(DecodeError.expectedAtLeast(1, Entity.WHITESPACE))

Match a single whitespace character.

whitespacePattern

whitespacePattern: RegExp

Const withPosition

A decoder that doesn't consume any portion of the string but does return the current index position as its result value.

Functions

Const arrayValue

Const autoQuote

  • autoQuote(value: string): string
  • Adds double quotes to a string if it is not a simple sequence of alpha-numeric characters.

    Parameters

    • value: string

    Returns string

Const concatOr

  • concatOr(values: string[]): string
  • Utility function to generate a comma separate list of values where the last one is concatenated by or.

    Parameters

    • values: string[]

    Returns string

Const decodeText

  • decodeText<T>(decoder: TextDecoder<T>): (Anonymous function)
  • Helper function that return a function to decode from an input of type string to a DecodeResult.

    The function takes a TextDecoder as the only argument.

    This convenience function exists because TextDecoder requires an input of type TextInput and not string.

    Type parameters

    • T

    Parameters

    Returns (Anonymous function)

Const decodeValue

  • decodeValue<T>(decoder: ValueDecoder<T>): (Anonymous function)
  • Helper function that return a function to decode from an input of type any to a DecodeResult.

    The function takes a ValueDecoder as the only argument.

    This convenience function exists because ValueDecoder requires an input of type ValueInput and not just any.

    Type parameters

    • T

    Parameters

    Returns (Anonymous function)

Const digits

  • digits(min?: number, max?: undefined | number): TextDecoder<string>
  • Match a sequence of digit characters. It expects at least one occurance. The boundaries min and max are both inclusive and optional.

    Parameters

    • Default value min: number = 1
    • Optional max: undefined | number

    Returns TextDecoder<string>

Const entityToString

  • entityToString(entity: Entity, qt: number): "character" | "character code" | "predicate" | "letter" | "literal" | "uppercase letter" | "lowercase letter" | "digit" | "string" | "string insensitive" | "type" | "whitespace" | "characters" | "character codes" | "predicates" | "letters" | "literals" | "uppercase letters" | "lowercase letters" | "digits" | "strings" | "strings insensitive" | "types" | "whitespaces"
  • Make Entity into english words accounting for pluralization by using qt.

    Parameters

    Returns "character" | "character code" | "predicate" | "letter" | "literal" | "uppercase letter" | "lowercase letter" | "digit" | "string" | "string insensitive" | "type" | "whitespace" | "characters" | "character codes" | "predicates" | "letters" | "literals" | "uppercase letters" | "lowercase letters" | "digits" | "strings" | "strings insensitive" | "types" | "whitespaces"

Const fail

  • fail<In, Out, Err>(err: Err): Decoder<In, Out, Err>
  • Returns a decoder that always fails with the given error.

    Type parameters

    • In

    • Out

    • Err

    Parameters

    • err: Err

    Returns Decoder<In, Out, Err>

Const failure

  • failure<In, Out, Err>(input: In, ...failures: Err[]): DecodeResult<In, Out, Err>
  • Helper function to create an instance of DecodeResult from a failed decoding.

    Type parameters

    • In

    • Out

    • Err

    Parameters

    • input: In
    • Rest ...failures: Err[]

    Returns DecodeResult<In, Out, Err>

Const failureToString

Const lazy

  • lazy<In, Out, Err>(f: function): Decoder<In, Out, Err>
  • Often time decoders are defined recursively. The language and type-system do not allow for recursion. To work around that limit you create a lazy decoder that wraps the desired decoder into a lazy function.

    The decoder function f is only invoked once and its result is reused mutliple times if needed.

    Type parameters

    • In

    • Out

    • Err

    Parameters

    Returns Decoder<In, Out, Err>

Const letters

  • letters(min?: number, max?: undefined | number): TextDecoder<string>
  • Match a sequence of case-insensitve alphabetical characters. It expects at least one occurance. The boundaries min and max are both inclusive and optional.

    Parameters

    • Default value min: number = 1
    • Optional max: undefined | number

    Returns TextDecoder<string>

Const literalValue

Const lowerCaseLetters

  • lowerCaseLetters(min?: number, max?: undefined | number): TextDecoder<string>
  • Match a sequence of lower-case alphabetical characters. It expects at least one occurance. The boundaries min and max are both inclusive and optional.

    Parameters

    • Default value min: number = 1
    • Optional max: undefined | number

    Returns TextDecoder<string>

Const make

  • Utility function to generate a decoder of type <a href="globals.html#valuedecoder">ValueDecoder</a> from a function f. Utility function to generate a decoder of type <a href="globals.html#valuedecoder">ValueDecoder</a> from a function f.

    Type parameters

    • T

    Parameters

    Returns TextDecoder<T>

  • Utility function to generate a decoder of type <a href="globals.html#valuedecoder">ValueDecoder</a> from a function f. Utility function to generate a decoder of type <a href="globals.html#valuedecoder">ValueDecoder</a> from a function f.

    Type parameters

    • T

    Parameters

    Returns ValueDecoder<T>

Const match

  • Create a decoder that consume and produces an exact string match.

    Type parameters

    • V: string

    Parameters

    • s: V

    Returns TextDecoder<V>

Const matchAnyCharOf

  • Match any single char from a list of possible values anyOf.

    Parameters

    • anyOf: string

    Returns TextDecoder<string>

Const matchChar

  • Match exactly one char given its char code value as a number.

    Parameters

    • char: string

    Returns TextDecoder<string>

Const matchCharCode

  • Match exactly one char given its char code value as a number.

    Parameters

    • charCode: number

    Returns TextDecoder<string>

Const matchInsensitive

Const matchNoCharOf

  • Match any single char that is not included in the list of possible values noneOf.

    Parameters

    • noneOf: string

    Returns TextDecoder<string>

Const nullableValue

objectValue

  • Create a decoder to validate objects.

    Each field in the passed object argument fieldDecoders is a distinct decoder for a matching field in the input object.

    The second argument optionalFields, marks the fields to consider optionals.

    Type parameters

    • T: __type

    • K: keyof T

    Parameters

    • fieldDecoders: object
    • optionalFields: [Array]

    Returns ValueDecoder<MarkOptionalFields<T, Array>>

Const oneOf

  • oneOf<U, D>(...decoders: D & object): Decoder<D[0]["_I"], U[number], D[0]["_E"]>
  • Given an array of decoders, it traverses them all until one succeeds or they all fail.

    Type parameters

    • U: any[]

    • D: object

    Parameters

    • Rest ...decoders: D & object

    Returns Decoder<D[0]["_I"], U[number], D[0]["_E"]>

Const optionalValue

Const pathToString

  • pathToString(path: (string | number)[]): string
  • Pretty prints a ValueInput.path value.

    Parameters

    • path: (string | number)[]

    Returns string

Const recordValue

  • Decodes an object applying a decoder for the keys and one for values of each field pair.

    Type parameters

    • K: string | number | symbol

    • T

    Parameters

    Returns ValueDecoder<Record<K, T>>

Const regexp

  • regexp(pattern: RegExp, group?: number): TextDecoder<string>
  • Generate a TextDecoder that uses a RegExp pattern to match a result.

    By default, this decoder will capture the first group (group 0) returned from the regular expression. This can be changed by providing a different value to group.

    If the JS runtime you are working with allows it, it is suggested to use the stycky modifier y. Its main advantage is that it doesn't need to reallocate a substring of the original input to perform its matching.

    Parameters

    • pattern: RegExp
    • Default value group: number = 0

    Returns TextDecoder<string>

Const sequence

  • sequence<U, D>(...decoders: D & object): Decoder<D[0]["_I"], object, D[0]["_E"]>
  • Given an array of decoders it tries to apply them all in sequence. If they all succeed it returns a typed n tuple where each element type matches the expected type of the corresponding decoder.

    Type parameters

    • U: any[]

    • D: object

    Parameters

    • Rest ...decoders: D & object

    Returns Decoder<D[0]["_I"], object, D[0]["_E"]>

Const stringRecordValue

Const succeed

  • succeed<In, Out, Err>(result: Out): Decoder<In, Out, Err>
  • Returns a decoder that always succeeds with the given result. The decoder doesn't consume anything from the input.

    Type parameters

    • In

    • Out

    • Err

    Parameters

    • result: Out

    Returns Decoder<In, Out, Err>

Const success

  • success<In, Out, Err>(input: In, result: Out): DecodeResult<In, Out, Err>
  • Helper function to create an instance of DecodeResult from a succeeded decoding.

    Type parameters

    • In

    • Out

    • Err

    Parameters

    • input: In
    • result: Out

    Returns DecodeResult<In, Out, Err>

Const takeCharBetween

  • takeCharBetween(f: function, min: number, max: number): TextDecoder<string>
  • Same as takeCharWhile but with a minimum (min) and maximum (max) constraints. They are both inclusive and mandatory.

    Parameters

    • f: function
        • (c: string): boolean
        • Parameters

          • c: string

          Returns boolean

    • min: number
    • max: number

    Returns TextDecoder<string>

Const takeCharWhile

  • takeCharWhile(f: function, atLeast?: number): TextDecoder<string>
  • Take a sequence of char that all satisfy the predicate f. It works much like testChar but for a sequence of charecters. The default is that the decoder should match at least one result. That can be changed by passing the second optional argument atLeast with a different value.

    Parameters

    • f: function
        • (c: string): boolean
        • Parameters

          • c: string

          Returns boolean

    • Default value atLeast: number = 1

    Returns TextDecoder<string>

Const testChar

  • Produces a decoder similar to {@link char} but the char is tested through the predicate function f. The character value is produced only if the predicate succeeds.

    Parameters

    • f: function
        • (c: string): boolean
        • Parameters

          • c: string

          Returns boolean

    Returns TextDecoder<string>

Const testType

Const testValue

Const tupleValue

Const undefineableValue

Const upperCaseLetters

  • upperCaseLetters(min?: number, max?: undefined | number): TextDecoder<string>
  • Match a sequence of upper-case alphabetical characters. It expects at least one occurance. The boundaries min and max are both inclusive and optional.

    Parameters

    • Default value min: number = 1
    • Optional max: undefined | number

    Returns TextDecoder<string>

valueToString

  • valueToString(value: any): string
  • Tries to convert any value into a human readable string.

    Parameters

    • value: any

    Returns string

Object literals

Const DecodeError

DecodeError: object

Static object that contains utility functions to generate any of the existing decoding errors typed as DecodeError instances.

expectedEoi

Generate an instance of <a href="classes/customerror.html">CustomError</a>.

unexpectedEoi

Generate an instance of <a href="classes/unexpectedeoi.html">UnexpectedEoi</a>.

custom

expectedAnyOf

expectedAtLeast

expectedField

expectedMatch

expectedNoneOf

expectedOnce

expectedWithinRange

patternMismatch