From ad93c2ddd093e7873379fae6a7318ce39943931b Mon Sep 17 00:00:00 2001 From: rzp-Piyush Date: Mon, 25 Nov 2024 14:15:27 +0530 Subject: [PATCH] add: GetZipCodesByCity functionality and testcases --- packages/i18nify-go/country.go | 5 +++++ packages/i18nify-go/example/example.go | 6 ++++++ packages/i18nify-go/interface.go | 2 ++ .../country_subdivisions/zipcode/zipcode.go | 5 +++-- .../zipcode/zipcode_test.go | 21 +++++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/i18nify-go/country.go b/packages/i18nify-go/country.go index 4b1dd4ec..d35df269 100644 --- a/packages/i18nify-go/country.go +++ b/packages/i18nify-go/country.go @@ -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{ diff --git a/packages/i18nify-go/example/example.go b/packages/i18nify-go/example/example.go index d836da18..a0d1bbac 100644 --- a/packages/i18nify-go/example/example.go +++ b/packages/i18nify-go/example/example.go @@ -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 diff --git a/packages/i18nify-go/interface.go b/packages/i18nify-go/interface.go index a494617f..3e53f2df 100644 --- a/packages/i18nify-go/interface.go +++ b/packages/i18nify-go/interface.go @@ -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 } diff --git a/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode.go b/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode.go index 73a4cbc7..eeae2b8c 100644 --- a/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode.go +++ b/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode.go @@ -3,6 +3,7 @@ package zipcode import ( "github.com/razorpay/i18nify/packages/i18nify-go/modules/country_subdivisions" + "strings" ) type ZipCodeDetails struct { @@ -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. @@ -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{ diff --git a/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode_test.go b/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode_test.go index e263ec51..916e1e5c 100644 --- a/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode_test.go +++ b/packages/i18nify-go/modules/country_subdivisions/zipcode/zipcode_test.go @@ -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") + } +}