diff --git a/country.go b/country.go index 4cf9251..7e8da59 100644 --- a/country.go +++ b/country.go @@ -138,10 +138,16 @@ func (c *Country) SubdivisionByName(name string) Subdivision { return Subdivision{} } +// HasPostalCode determines whether the country has postal codes. It returns +// true if the country has postal codes, and false if it does not. +func (c *Country) HasPostalCode() bool { + return c.PostalCodeFormat != "" +} + // MatchPostalCode returns true if postalCode has a valid format for the // country. If the country does not have a postal code, returns false. func (c *Country) MatchPostalCode(postalCode string) bool { - if c.PostalCodeFormat == "" { + if !c.HasPostalCode() { return false } r := regexp.MustCompile(c.PostalCodeFormat) diff --git a/country_test.go b/country_test.go index ce24f8f..ffe0a63 100644 --- a/country_test.go +++ b/country_test.go @@ -342,6 +342,14 @@ func TestTranslations(t *testing.T) { assert.Equal(t, "", c.Translations["xx"]) } +func TestHasPostalCode(t *testing.T) { + it := countries.Get("IT") + assert.True(t, it.HasPostalCode()) + + jm := countries.Get("JM") + assert.False(t, jm.HasPostalCode()) +} + func TestMatchPostalCode(t *testing.T) { it := countries.Get("IT") assert.True(t, it.MatchPostalCode("35018"))