Skip to main content
Module

x/fuzzy_octo_guacamole/mod.ts>Pattern.Pattern

A playground for testing CI setup that deploys on Deno and NPM
Go to Latest
type alias Pattern.Pattern
Re-export
import { type Pattern } from "https://deno.land/x/fuzzy_octo_guacamole@v2.0.0/mod.ts";
const { Pattern } = Pattern;

Pattern<a> is the generic type for patterns matching a value of type a. A pattern can be any (nested) javascript value.

They can also be wildcards, like P._, P.string, P.number, or other matchers, like P.when(predicate), P.not(pattern), etc.

Read Patterns documentation on GitHub

Examples

const pattern: P.Pattern = { name: P.string }

definition: Matcher<a, unknown, any, any> | (a extends Primitives ? a : unknown extends a ? UnknownPattern : a extends readonly (infer i)[] ? a extends readonly [any, ...any] ? readonly [index in keyof a]: Pattern<a[index]> : readonly [] | readonly [Pattern<i>, ...Pattern<i>[]] : a extends Map<infer k, infer v> ? Map<k, Pattern<v>> : a extends Set<infer v> ? Set<Pattern<v>> : a extends object ? readonly [k in keyof a]?: Pattern<Exclude<a[k], undefined>> : a)

Examples

const pattern: P.Pattern = { name: P.string }

definition: Matcher<a, unknown, any, any> | (a extends Primitives ? a : unknown extends a ? UnknownPattern : a extends readonly (infer i)[] ? a extends readonly [any, ...any] ? readonly [index in keyof a]: Pattern<a[index]> : readonly [] | readonly [Pattern<i>, ...Pattern<i>[]] : a extends Map<infer k, infer v> ? Map<k, Pattern<v>> : a extends Set<infer v> ? Set<Pattern<v>> : a extends object ? readonly [k in keyof a]?: Pattern<Exclude<a[k], undefined>> : a)

Examples

const userPattern = { name: P.string } type User = P.infer

Type Parameters

p extends Pattern<any>
definition: InvertPattern<p>
variable Pattern.Pattern
Re-export
import { Pattern } from "https://deno.land/x/fuzzy_octo_guacamole@v2.0.0/mod.ts";
const { Pattern } = Pattern;

P.any is a wildcard pattern, matching any value.

Read P.any documentation on GitHub

function Pattern.Pattern
Re-export
import { Pattern } from "https://deno.land/x/fuzzy_octo_guacamole@v2.0.0/mod.ts";
const { Pattern } = Pattern;

P.optional(subpattern) takes a sub pattern and returns a pattern which matches if the key is undefined or if it is defined and the sub pattern matches its value.

Read P.optional documentation on GitHub

Examples

match(value) .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')

Type Parameters

input
p extends unknown extends input ? UnknownPattern : Pattern<input>

Parameters

pattern: p

Returns

OptionalP<input, p>

P.when((value) => boolean) returns a pattern which matches if the predicate returns true for the current input.

Read P.when documentation on GitHub

Examples

match<{ age: number }>(value) .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21' )

Type Parameters

input
p extends (value: input) => unknown

Parameters

predicate: p

Returns

GuardP<input, p extends (value: any) => value is infer narrowed ? narrowed : never>

Type Parameters

input
narrowed extends input
excluded

Parameters

predicate: (input: input) => input is narrowed

Returns

GuardExcludeP<input, narrowed, excluded>

P.select() is a pattern which will always match, and will inject the selected piece of input in the handler function.

Read P.select documentation on GitHub

Examples

match<{ age: number }>(value) .with({ age: P.select() }, (age) => 'age: number' )

Returns

AnonymousSelectP

Type Parameters

input
patternOrKey extends string | (unknown extends input ? UnknownPattern : Pattern<input>)

Returns

patternOrKey extends string ? SelectP<patternOrKey> : SelectP<symbols.anonymousSelectKey, input, patternOrKey>

Type Parameters

input
p extends unknown extends input ? UnknownPattern : Pattern<input>
k extends string

Parameters

key: k
pattern: p

Returns

SelectP<k, input, p>