Skip to main content
Deno 2 is finally here 🎉️
Learn more
Go to Latest
class matches.matches
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.4.0.0-alpha2/lib/esm/dependencies.js";
const { matches } = matches;

Given an object, we want to make sure the key exists and that the value on the key matches the parser

Constructors

new
ShapeParser(
parserMap,
isPartial,
parserKeys?,
description?,
)

Methods

parse(a, onParse)

Constructors

new
ConcatParsers(
parent,
otherParser,
description?,
)

Methods

parse(a, onParse)

Static Methods

of(parent, otherParser)

Constructors

new
NumberParser(description?)

Methods

parse(a, onParse)

Constructors

new
BoolParser(description?)

Methods

parse(a, onParse)

Constructors

new
Validator(parser, description?)

Methods

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

concat(otherParser)

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(defaultValue)

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)

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

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

map(fn, mappingName)

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)

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)

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(otherParser)

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

parse(a, onParse)

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

refine(refinementTest, otherName?)

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

unsafeCast(value)

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, otherName)

We want to test value with a test eg isEven

Static Methods

isA(checkIsA, name)

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)

Trying to convert the parser into a string representation

Constructors

new
Parser(parser, description?)

Methods

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

concat(otherParser)

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(defaultValue)

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)

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

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

map(fn, mappingName)

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)

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)

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(otherParser)

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

parse(a, onParse)

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

refine(refinementTest, otherName?)

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

unsafeCast(value)

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, otherName)

We want to test value with a test eg isEven

Static Methods

isA(checkIsA, name)

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)

Trying to convert the parser into a string representation

Constructors

new
ArrayOfParser(parser, description?)

Methods

parse(a, onParse)

Constructors

new
NamedParser(
parent,
name,
description?,
)

Methods

parse(a, onParse)

Constructors

new
FunctionParser(description?)

Methods

parse(a, onParse)

Constructors

new
MappedAParser(
parent,
map,
mappingName?,
description?,
)

Methods

parse(a, onParse)

Constructors

new
ArrayParser(description?)

Methods

parse(a, onParse)

Constructors

new
OrParsers(
parent,
otherParser,
description?,
)

Methods

parse(a, onParse)

Constructors

new
AnyParser(description?)

Methods

parse(a, onParse)

Constructors

new
ObjectParser(description?)

Methods

parse(a, onParse)

Constructors

new
LiteralsParser(values, description?)

Methods

parse(a, onParse)

Constructors

new
StringParser(description?)

Methods

parse(a, onParse)

Constructors

new
GuardParser(
checkIsA,
typeName,
description?,
)

Methods

parse(a, onParse)

Constructors

new
NilParser(description?)

Methods

parse(a, onParse)
variable matches.matches
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.4.0.0-alpha2/lib/esm/dependencies.js";
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

(...parsers) => unknown

type

(classCreator) => unknown

type

(testShape) => unknown

type

(tester) => unknown
function matches.matches
Re-export
import { matches } from "https://deno.land/x/embassyd_sdk@v0.3.4.0.0-alpha2/lib/esm/dependencies.js";
const { matches } = matches;

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

Parameters

validator

What is the validator for the values in the array