yarn add perfect-matcher
npm install perfect-matcher
- [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
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"
)