pattern-match
pattern-match copied to clipboard
A more gentle introduction?
I was struggling to understand how this library works. Your example does not provide the sample data the matches are compared against. Reading the test code helped, but maybe you can start with a less complicated example. Here is an example that helped me understand better:
var match = require("pattern-match")
let myobj = { a: 1, b: 2 }
// Just check if object properties exist and do something
match(myobj, ( when ) => {
when({c:match.any}, () => { console.log('object with "c" property detected') } );
when({a:match.any}, () => { console.log('object with "a" property detected') } );
// This will never be reached since the previous check matched
when({a:match.any, b: match.any}, () => { console.log('object with "a" and "b" properties detected') } );
} );
// capture the property values with new names
match(myobj, ( when ) => {
when({a:match.var('foo'), b: match.var('bar')}, ( submatch ) => { console.log('Sub-Match object is', submatch ) } );
} );