Skip to content

dherman/match.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

match.js

A pattern-matching library for JS.

A simple example:

cases(on({ foo: Number, bar: Number }, function(o) { return o.foo + o.bar }),
      on([String, String, String]), function(a) { return a[0] + a[1] + a[2] })
 .match(f())

The same example, using ES6 destructuring:

cases(on({ foo: Number, bar: Number }, function({ foo, bar }) { return foo + bar }),
      on([String, String, String]), function([a, b, c]) { return a + b + c })
 .match(f())

Same example again, using arrow syntax (which makes me go "squee!"):

cases(on({ foo: Number, bar: Number }, ({ foo, bar }) => foo + bar),
      on([String, String, String]), ([a, b, c]) => a + b + c)
 .match(f())

The pattern API

pattern ::=                              match condition                        match result
                                         -----------------------------------    --------------------
  the Boolean function                   typeof x === "boolean"                 x
| the Number function                    typeof x === "number"                  x
| the String function                    typeof x === "string"                  x
| the Date function                      x.[[Class]] === "Date"                 x
| the Object function                    typeof x === "object" &&               x
                                           x !== null                           
| the Function function                  typeof x === "function"                x
| the Array function                     x.[[Class]] === "Array"                x
| function                               f(x)                                   x
| null                                   x === null                             null
| undefined                              x === void 0                           undefined
| boolean                                x === b                                b
| NaN                                    isNaN(x)                               NaN
| number other than NaN                  x === n                                n
| string                                 x === s                                s
| Date                                   x.[[Class]] === "Date" &&              d
                                           +x === +d                            
| regexp                                 r.exec(x)                              result of r.exec(x)
| { x1: pattern1, ..., xn: patternn }    typeof x === "object" &&               object w/ each match
                                           for each i, x.xi matches patterni    
| [ pattern1, ..., patternn ]            typeof x === "object" &&               array w/ each match
                                         x.length > n &&                        
                                           for each i, x[i] matches patterni    
| Pattern                                p.match(x)                             result of p.match(x)

Pattern objects

A Pattern<a> is a Pattern object whose match method returns values of type a on successful matches.

A pattern<a> is pattern syntax value that produces a Pattern<a>.

on(patt [, action])

Type: (pattern<a>[, (a) -> b]) -> Pattern<b>

cases(patt, ...)

Type: (Pattern<a>, ...) -> Pattern<a>

Pattern.prototype.match(x)

Type: (this: Pattern<a>, any) -> a

Any

Type: Pattern<true>

None

Type: Pattern<nothing>

Predicates

instance(ctor)

Type: (Function) -> (any) -> boolean

Returns a predicate that checks for instances of ctor.

hasType(x)

hasClass(x)

equals(x)

License

Copyright © 2012 Dave Herman

Licensed under the MIT License.

About

Pattern matching for JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published