Refactor hasClass utility function