Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more gentle introduction? #2

Open
gbirke opened this issue Oct 14, 2016 · 0 comments
Open

A more gentle introduction? #2

gbirke opened this issue Oct 14, 2016 · 0 comments

Comments

@gbirke
Copy link

gbirke commented Oct 14, 2016

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 ) } );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant