functional pattern matching in js