Pattern matching implementation in JS.