Matches.js exports one function,
var pattern = require"matches"pattern;
The simplest way is to give it an object with the keys being patterns, and the values being functions. Each pattern will be tried in order until a match is found.
var arrayElems = patternreturn "This array is empty.";return "This array has one element: " + x;return "This array has two elements: " + x + " and " + y;return "This array is long. The first two elements are: " + x + " and " + y;;arrayElems1 2 3;
You can create individual pattern and function pairs.
var emptyArray = pattern'' return "Empty array" ;// "Empty array"emptyArray;// TypeError: "All patterns exhausted"emptyArray12;
You can also create your own custom pattern functions. The
an array of arguments, and should return
false for no match, or a new array
of arguments to forward on to the
varif args0 >= 42 return args0;return false;;var customPattern = patterngreater42console.logx;;// Logs 54customPattern54;// TypeError: "All patterns exhausted"customPattern12;
You can combine any of these methods to create unique match chains using the
var wildcard = pattern'_' return "No matches."; ;var mychain = pattern'1' return "One";altreturn "Two";return "Three";altwildcard;// 'One'mychain1;// 'Two'mychain2;// 'No matches.'mychain5;
var myfn = pattern// Matches on the first three arguments. If more are passed, they are ignored.return a;// Matches on the first two arguments, ignoring the restreturn fna;// Matches anythingreturn null;;// 12myfn6 return x * 2; "foo" "bar";// nullmyfn1 2 3 4;
Check for specific values using number or string literals,
var myfn = pattern// Null// Undefined// Numbers// Strings// Escape sequences must be double escaped.;
Underscores will match successfully on any value but ignore it. A single underscore works well as a catch all pattern.
var myfn = pattern// Match if second argument is 12, ignoring the first// Match on anything;
Patterns that start with lowercase letters will pass the value to the function. Values are passed to the function in the same right to left order they are declared in the pattern.
var myfn = pattern// Pass on the second argument if the first is 12// Pass on the first and third argument, ignoring the second;
Match on the entire array, or only a few values.
var myfn = pattern// Empty array// Strict comparison on contents// Grab the first value, ignoring the rest// Split it into a head and tail// Grab the first and last items, ignoring the middle// Grab the last item// Make a shallow clone// Grab the first item, but also pass on the whole array;
Matches.js currently supports strict object comparison, meaning it will match if the object contains the specified keys and no others.
var myfn = pattern// Empty object// Check that an object has only two keys 'x' and 'y', and pass to the function// Check that an object has a key that contains an array
Typecheck arguments using
var myfn = pattern// Takes a function and an array// Takes a function and an object;
// Create a new adt.js typevar Tree = adtdataEmpty : adtsingleNode : adtrecord"val" "left" "right";var mytree = TreeNode12 TreeEmpty TreeNode42 TreeEmpty TreeEmpty;var myfn = pattern// Match on an Empty tree node// Match on a Node with a value of 12// Match on a Node that has non-Empty children// Match on a Node that has a left child Node of 42 and an Empty right node;
Find out more about adt.js: https://github.com/natefaubion/adt.js