Skip to main content
Go to Latest
class matches.matches
implements IParser<A, B2>
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.3.0.5/dependencies.ts";
const { matches } = matches;

A Parser is usually a function that takes a value and returns a Parsed value. For this class we have that as our main reason but we want to be able to have other methods including testing and showing text representations.

The main function unsafeCast which will take in a value A (usually unknown) and will always return a B. If it cannot it will throw an error.

The parse function is the lower level function that will take in a value and a dictionary of what to do with success and failure.

Constructors

new
private
ConcatParsers(
parent: Parser<A, B>,
otherParser: Parser<B, B2>,
description?,
)

Methods

parse<C, D>(a: A, onParse: OnParse<A, B2, C, D>): C | D

Static Methods

of<A, B, B2>(parent: Parser<A, B>, otherParser: Parser<B, B2>)

Constructors

new
OrParsers(
parent: Parser<A, B>,
otherParser: Parser<A2, B2>,
description?,
)

Methods

parse<C, D>(a: A & A2, onParse: OnParse<A | A2, B | B2, C, D>): C | D

Constructors

new
StringParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, string, C, D>): C | D

Constructors

new
LiteralsParser(values: B, description?)

Type Parameters

B extends unknown[]

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, OneOf<B>, C, D>): C | D

Constructors

new
ObjectParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, object, C, D>): C | D

Constructors

new
Validator(parser: IParser<A, B>, description?)

Properties

readonly
_TYPE: B
test: (value: A) => value is A & B

Use this as a guard clause, useful for escaping during the error cases. https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types

Methods

castPromise(value: A): Promise<B>

This is the like the unsafe parser, it assumes the happy path and will throw and return a failed promise during failure.

concat<C>(otherParser: IParser<B, C>): Parser<A, C>

Use this when you want to combine two parsers into one. This will make sure that both parsers will run against the same value.

defaultTo<C>(defaultValue: C): Parser<Optional<A>, C | NonNull<B, C>>

There are times that we would like to bring in a value that we know as null or undefined and want it to go to a default value

enumParsed(value: A): { value: B; } | { error: ISimpleParsedError; }

This is another type of parsing that will return a value that is a discriminated union of the success and failure cases. https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions

errorMessage(input: A): void | string

When we want to get the error message from the input, to know what is wrong

map<C>(fn: (apply: B) => C, mappingName?: string): Parser<A, C>

Use this that we want to do transformations after the value is valid and parsed. A use case would be parsing a string, making sure it can be parsed to a number, and then convert to a number

name(nameString: string): Parser<A, B>

Use this when we want to give the parser a name, and we want to be able to use the name in the error messages.

optional(name?: string): Parser<Optional<A>, Optional<B>>

When we want to make sure that we handle the null later on in a monoid fashion, and this ensures we deal with the value https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

orParser<C>(otherParser: IParser<A, C>): Parser<A, B | C>

Use this to combine parsers into one. This will make sure that one or the other parsers will run against the value.

parse<C, D>(a: A, onParse: OnParse<A, B, C, D>): C | D

Use this when you want to decide what happens on the succes and failure cases of parsing

refine<C = B>(refinementTest: (value: B) => value is B & C, otherName?: string): Parser<A, B & C>

We want to refine to a new type given an original type, like isEven, or casting to a more specific type

unsafeCast(value: A): B

This is the most useful parser, it assumes the happy path and will throw an error if it fails.

Return the unwrapped parser/ IParser

validate(isValid: (value: B) => boolean, otherName: string): Parser<A, B>

We want to test value with a test eg isEven

Static Properties

validatorErrorAsString: <A, B>(error: ISimpleParsedError) => string

This is the line of code that could be over written if One would like to have a custom error as any shape

Static Methods

isA<A, B extends A>(checkIsA: (value: A) => value is B, name: string): Parser<A, B>

This is a constructor helper that can use a predicate tester in the form of a guard function, and will return a parser that will only parse if the predicate returns true. https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types

parserAsString(parserComingIn: IParser<unknown, unknown>): string

Trying to convert the parser into a string representation

Constructors

new
Parser(parser: IParser<A, B>, description?)

Properties

readonly
_TYPE: B
test: (value: A) => value is A & B

Use this as a guard clause, useful for escaping during the error cases. https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types

Methods

castPromise(value: A): Promise<B>

This is the like the unsafe parser, it assumes the happy path and will throw and return a failed promise during failure.

concat<C>(otherParser: IParser<B, C>): Parser<A, C>

Use this when you want to combine two parsers into one. This will make sure that both parsers will run against the same value.

defaultTo<C>(defaultValue: C): Parser<Optional<A>, C | NonNull<B, C>>

There are times that we would like to bring in a value that we know as null or undefined and want it to go to a default value

enumParsed(value: A): { value: B; } | { error: ISimpleParsedError; }

This is another type of parsing that will return a value that is a discriminated union of the success and failure cases. https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions

errorMessage(input: A): void | string

When we want to get the error message from the input, to know what is wrong

map<C>(fn: (apply: B) => C, mappingName?: string): Parser<A, C>

Use this that we want to do transformations after the value is valid and parsed. A use case would be parsing a string, making sure it can be parsed to a number, and then convert to a number

name(nameString: string): Parser<A, B>

Use this when we want to give the parser a name, and we want to be able to use the name in the error messages.

optional(name?: string): Parser<Optional<A>, Optional<B>>

When we want to make sure that we handle the null later on in a monoid fashion, and this ensures we deal with the value https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

orParser<C>(otherParser: IParser<A, C>): Parser<A, B | C>

Use this to combine parsers into one. This will make sure that one or the other parsers will run against the value.

parse<C, D>(a: A, onParse: OnParse<A, B, C, D>): C | D

Use this when you want to decide what happens on the succes and failure cases of parsing

refine<C = B>(refinementTest: (value: B) => value is B & C, otherName?: string): Parser<A, B & C>

We want to refine to a new type given an original type, like isEven, or casting to a more specific type

unsafeCast(value: A): B

This is the most useful parser, it assumes the happy path and will throw an error if it fails.

Return the unwrapped parser/ IParser

validate(isValid: (value: B) => boolean, otherName: string): Parser<A, B>

We want to test value with a test eg isEven

Static Properties

validatorErrorAsString: <A, B>(error: ISimpleParsedError) => string

This is the line of code that could be over written if One would like to have a custom error as any shape

Static Methods

isA<A, B extends A>(checkIsA: (value: A) => value is B, name: string): Parser<A, B>

This is a constructor helper that can use a predicate tester in the form of a guard function, and will return a parser that will only parse if the predicate returns true. https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types

parserAsString(parserComingIn: IParser<unknown, unknown>): string

Trying to convert the parser into a string representation

Constructors

new
ShapeParser(
parserMap: [key in keyof B]: Parser<unknown, B[key]>,
isPartial: boolean,
parserKeys?,
description?,
)

Type Parameters

A extends unknown
Key extends string | number | symbol
B

Methods

parse<C, D>(a: unknown, onParse: OnParse<A, [key in Key]?: B, C, D>): C | D

Constructors

new
BoolParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, boolean, C, D>): C | D

Constructors

new
AnyParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, any, C, D>): C | D

Constructors

new
NumberParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, number, C, D>): C | D

Constructors

new
NamedParser(
parent: Parser<A, B>,
name: string,
description?,
)

Methods

parse<C, D>(a: A, onParse: OnParse<A, B, C, D>): C | D

Constructors

new
GuardParser(
checkIsA: (value: A) => value is A & B,
typeName: string,
description?,
)

Methods

parse<C, D>(a: A, onParse: OnParse<A, B, C, D>): C | D

Constructors

new
NilParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, null | undefined, C, D>): C | D

Constructors

new
FunctionParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, Function, C, D>): C | D

Constructors

new
MappedAParser(
parent: Parser<A, B>,
map: (value: B) => B2,
mappingName?,
description?,
)

Methods

parse<C, D>(a: A, onParse: OnParse<A, B2, C, D>): C | D

Constructors

new
ArrayOfParser(parser: Parser<unknown, B>, description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, B[], C, D>): C | D

Constructors

new
ArrayParser(description?)

Methods

parse<C, D>(a: unknown, onParse: OnParse<unknown, Array<unknown>, C, D>): C | D
interface matches.matches
Re-export
import { type matches } from "https://deno.land/x/embassyd_sdk@v0.3.3.0.5/dependencies.ts";
const { matches } = matches;

Type Parameters

In
optional
OutcomeType = never

Methods

when<A, B>(...args: In extends never ? never : WhenArgs<A, B>): ChainMatches<Exclude<In, A>, OutcomeType | B>
defaultTo<B>(value: B): B | OutcomeType
defaultToLazy<B>(getValue: () => B): B | OutcomeType
unwrap(): OutcomeType
type alias matches.matches
Re-export
import { type matches } from "https://deno.land/x/embassyd_sdk@v0.3.3.0.5/dependencies.ts";
const { matches } = matches;
definition: ISimpleParsedError
definition: { readonly description: Readonly<Description & { }>; parse<C, D>(
this: IParser<A, B>,
a: A,
onParse: OnParse<A, B, C, D>,
): C | D; }
definition:
| "Any"
| "Array"
| "ArrayOf"
| "Boolean"
| "Concat"
| "Default"
| "Deferred"
| "Named"
| "Dictionary"
| "Function"
| "Guard"
| "Literal"
| "Mapped"
| "Maybe"
| "Named"
| "Null"
| "Number"
| "Partial"
| "Object"
| "Or"
| "Recursive"
| "Shape"
| "String"
| "Tuple"
| "Unknown"
| "Wrapper"
definition: ((a: In) => Out) | (() => Out) | Out
definition: ExtendsSimple<A> | Parser<unknown, A>
definition: A extends
| string
| number
| boolean
| null
| undefined
? A : never
variable matches.matches
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.3.0.5/dependencies.ts";
const { matches } = matches;

Want to be able to bring in the declarative nature that a functional programming language feature of the pattern matching and the switch statement. With the destructors the only thing left was to find the correct structure then move move forward. Using a structure in chainable fashion allows for a syntax that works with typescript while looking similar to matches statements in other languages

Use: matches('a value').when(matches.isNumber, (aNumber) => aNumber + 4).defaultTo('fallback value')

type

<ParserSets extends [Parser<unknown, unknown>, Parser<unknown, unknown>][]>(...parsers: ParserSets) => Parser<unknown, _<DictionaryShaped<[...ParserSets]>>>

type

<C>(classCreator: { new (...args: any[]): C; }) => Parser<unknown, C>

type

(tester: RegExp) => unknown
function matches.matches
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.3.0.5/dependencies.ts";
const { matches } = matches;

We would like to validate that all of the array is of the same type

Parameters

validator: Parser<unknown, A>

What is the validator for the values in the array

Returns

Parser<unknown, A[]>

Type Parameters

A extends { }
Overwrites extends keyof A

Parameters

testShape: [key in keyof A]: Parser<unknown, A[key]>
optionals: Overwrites[]

Returns

Parser<unknown, MergeAll<[K in keyof Omit<A, Overwrites>]: A[K] & [K in keyof Pick<A, Overwrites>]?: A[K]>>

Type Parameters

A extends { }
Overwrites extends keyof A
Defaults extends [K in Overwrites]?: A[K]

Parameters

testShape: [key in keyof A]: Parser<unknown, A[key]>
optionals: Overwrites[]
defaults: Defaults

Returns

Parser<unknown, MergeAll<[K in keyof Omit<A, Overwrites>]: A[K] & [K in keyof Omit<Pick<A, Overwrites>, keyof Defaults>]?: A[K] & [K in keyof Pick<Pick<A, Overwrites>, keyof Defaults & Overwrites>]: A[K]>>

Type Parameters

A extends { }

Parameters

testShape: [key in keyof A]: Parser<unknown, A[key]>

Returns

Parser<unknown, A>