perfect-matcher
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

TypeScript pattern matching

Install

yarn add perfect-matcher

npm install perfect-matcher

Features

  • [x] match by value (include deep compare with objects, Date, array, typed arrays)
  • [x] match by type
  • [x] match by instance
  • [x] match NaN, regexp, positive/negative number
  • [x] 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

Readme

Keywords

none

Package Sidebar

Install

npm i perfect-matcher

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

32.5 kB

Total Files

10

Last publish

Collaborators

  • sawaxon