Options
All
  • Public
  • Public/Protected
  • All
Menu

TypeScript pattern matching

Install

yarn add perfect-matcher

npm install perfect-matcher

Features

  • match by value (include deep compare with objects, Date, array, typed arrays)
  • match by type
  • match by instance
  • match NaN, regexp, positive/negative number
  • match tuple
  • optional matching objects

Docs

z81.github.io/matcher

Example

console.log(
  match(7)(
    value('str', v => console.log(`This is string "${v}"`)),
    value(7, v => console.log('This is number 7') && 'return this'),
    value({ a: '2' }, v => console.log("This is {a: '2'}", v)),
    value({ a: '3' }, v => console.log("This is {a: '3'}")),
    value(null, v => console.log('This is null')),
    value(false, v => console.log('This is false')),
    type(0, () => console.log('This is number')),
    string(() => console.log('This is string')),
    boolean(() => console.log('This is boolean')),
    regexp(() => console.log('This is RegExp')),
    nan(() => console.log('This is NaN')),
    array(() => console.log('This is array')),
    instance(Store, () => console.log('This is Store'))
  )
)
console.log(
  match(['firstName'] as [string, string?])(
    tuple((firstName, lastName) => `${firstName} ${lastName}`),
    tuple(firstName => `${firstName}`)
  )
  // "firstName"
)

npm version Build Status Coverage Status semantic-release

Index

Type aliases

Matcher

Matcher<T>: [function, MatcherClb<T>]

Type parameters

  • T

Functions

Const any

  • any<T>(clb: MatcherClb<T>): Matcher<T>

Const array

  • array(clb: MatcherClb<Array<any>>): Matcher<Array<any>>

Const boolean

  • boolean(clb: MatcherClb<boolean>): [function, function]
  • Parameters

    • clb: MatcherClb<boolean>

    Returns [function, function]

Const instance

  • instance<T>(value: T, clb: MatcherClb<T>): Matcher<T>
  • Type parameters

    • T: Function

    Parameters

    • value: T
    • clb: MatcherClb<T>

    Returns Matcher<T>

Const match

  • match<T>(value: T): (Anonymous function)
  • Type parameters

    • T

    Parameters

    • value: T

    Returns (Anonymous function)

Const matchAll

  • matchAll<T>(value: T): (Anonymous function)
  • Type parameters

    • T

    Parameters

    • value: T

    Returns (Anonymous function)

Const nan

  • nan(clb: MatcherClb<number>): Matcher<number>

Const negative

  • negative(clb: MatcherClb<number>): Matcher<number>

Const number

  • number(clb: MatcherClb<number>): [function, function]
  • Parameters

    • clb: MatcherClb<number>

    Returns [function, function]

Const object

  • object(clb: MatcherClb<object>): [function, function]
  • Parameters

    • clb: MatcherClb<object>

    Returns [function, function]

Const positive

  • positive(clb: MatcherClb<number>): Matcher<number>

Const regexp

  • regexp(clb: MatcherClb<RegExp>): Matcher<RegExp>

Const string

  • string(clb: MatcherClb<string>): [function, function]
  • Parameters

    • clb: MatcherClb<string>

    Returns [function, function]

Const tuple

  • tuple<T>(clb: function): Matcher<T>
  • Type parameters

    • T: Array<any>

    Parameters

    • clb: function
        • (...args: Partial<T>): any
        • Parameters

          • Rest ...args: Partial<T>

          Returns any

    Returns Matcher<T>

Const type

  • type<T>(value: T, clb: MatcherClb<T>): Matcher<T>
  • Type parameters

    • T

    Parameters

    • value: T
    • clb: MatcherClb<T>

    Returns Matcher<T>

Const value

  • value<T>(value: T, clb: MatcherClb<T>): Matcher<T>
  • Type parameters

    • T

    Parameters

    • value: T
    • clb: MatcherClb<T>

    Returns Matcher<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc