Pattern matching in javascript