Skip to content

A more gentle introduction? #2

Description

@gbirke

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 ) } );
} );

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions