Pattern matching in Javascript