12/29/2015

match-when - Pattern Matching for Modern JavaScript

I was working on an internal tool at iAdvize during our hack days (fr). This tool is representing our micro-service architecture and every of its components in a near real-time graph (I may open-source it in the future).

Somewhere in the graph relationship code I thought wait a minute, would it be awesome to use some pattern-matching there? So I tried to write some syntax that could exist for it...


... and of course it failed.

It was 6PM so the good news was I would have all the time I wanted to see if it could be possible to implement a pattern-matching solution that was both syntaxically short, would work well with functional-style code while using JavaScript ES6 features for conciseness.


I ended up with this syntax:


Which works also that way:


Underneath it uses enhanced object literals, symbols and, sadly, JSON serialization/deserialization to pass object properties.


Later that night match-when was available on Github.

« »
 
 
Made with on a hot august night from an airplane the 19th of March 2017.