javascript: pattern guards for typematching