Skip to content

Commit

Permalink
add: GetZipCodesByCity functionality and testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
rzp-Piyush committed Nov 25, 2024
1 parent 4ff6104 commit ad93c2d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/i18nify-go/country.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (c *Country) IsValidZipCode(pinCode string) bool {
return zipcode.IsValidZipCode(pinCode, c.Code)
}

// GetZipCodesFromCity returns all the zipcodes belonging to that city.
func (c *Country) GetZipCodesFromCity(cityName string) []string {
return zipcode.GetZipCodesFromCity(cityName, c.Code)
}

// NewCountry creates a new Country instance with the given country code.
func NewCountry(code string) ICountry {
return &Country{
Expand Down
6 changes: 6 additions & 0 deletions packages/i18nify-go/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func main() {
fmt.Println(city.Name)
}

// Get zipcodes by city
zipcodes := countryIN.GetZipCodesFromCity("indore")
for _, val := range zipcodes {
fmt.Println(val)
}

//USD
currencyUS := currency.GetCurrencyInformation("USD")
fmt.Println(currencyUS.Name) //US Dollar
Expand Down
2 changes: 2 additions & 0 deletions packages/i18nify-go/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ type ICountry interface {
GetCitiesByZipCode(zipcode string) []country_subdivisions.City
// IsValidZipCode returns whether a pinCode is valid for the country or not.
IsValidZipCode(zipcode string) bool
// GetZipCodesFromCity returns all the zipcodes belonging to that city.
GetZipCodesFromCity(cityName string) []string
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zipcode

import (
"github.com/razorpay/i18nify/packages/i18nify-go/modules/country_subdivisions"
"strings"
)

type ZipCodeDetails struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ func IsValidZipCode(zipCode string, countryCode string) bool {
}
func GetZipCodesFromCity(city string, countryCode string) []string {
zipCodeData := GetCountryZipCodeDetails(countryCode)
return zipCodeData.cityToZipCodes[city]
return zipCodeData.cityToZipCodes[strings.ToLower(city)]
}

// initializeZipCodeMap builds the zip code maps for the given CountrySubdivisions.
Expand All @@ -90,7 +91,7 @@ func initializeZipCodeMap(subdivisions country_subdivisions.CountrySubdivisions)
})
details[zipcode] = zipCodeDetail
}
cityToZipCode[city.Name] = city.Zipcodes
cityToZipCode[strings.ToLower(city.Name)] = city.Zipcodes
}
}
return &ZipCodeData{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,24 @@ func BenchmarkIsValidZipCode(b *testing.B) {
IsValidZipCode(zipCode[i%n], "IN")
}
}

// Benchmark test for GetZipCodesByCity
func BenchmarkGetZipCodesByCity(b *testing.B) {
var cities = []string{
"Mumbai", "Delhi", "Bangalore", "Hyderabad", "Ahmedabad",
"Chennai", "Kolkata", "Surat", "Pune", "Jaipur",
"Lucknow", "Kanpur", "Nagpur", "Indore", "Thane",
"Bhopal", "Visakhapatnam", "Patna", "Vadodara", "Ghaziabad",
"Ludhiana", "Agra", "Nashik", "Faridabad", "Meerut",
"Rajkot", "Kalyan-Dombivli", "Vasai-Virar", "Varanasi", "Srinagar",
"Aurangabad", "Dhanbad", "Amritsar", "Navi Mumbai", "Allahabad",
"Ranchi", "Howrah", "Coimbatore", "Jabalpur", "Gwalior",
"Vijayawada", "Jodhpur", "Madurai", "Raipur", "Kota",
"Guwahati", "Chandigarh", "Solapur", "Hubli-Dharwad", "Tiruchirappalli",
}
b.ResetTimer()
n := len(cities)
for i := 0; i < b.N; i++ {
GetZipCodesFromCity(cities[i%n], "IN")
}
}

0 comments on commit ad93c2d

Please sign in to comment.