Skip to content

Commit

Permalink
Add HasPostalCode method on Country type
Browse files Browse the repository at this point in the history
  • Loading branch information
pioz committed Oct 4, 2023
1 parent 7864191 commit d8088f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion country.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions country_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit d8088f7

Please sign in to comment.