1.0.1 • Published 5 years ago
perfect-matcher v1.0.1
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
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"
)