Skip to content

Commit

Permalink
Merge pull request #58 from Shopify/zone_lookup_by_name_alternate_202…
Browse files Browse the repository at this point in the history
…3-12-08

(bugfix) Zone lookup by name
  • Loading branch information
rochlefebvre authored Dec 11, 2023
2 parents fec32c6 + f7f5642 commit 1e1da69
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Allow building number in address2 for DK [#53](https://github.com/Shopify/worldwide/pull/53)
- Avoid .present? and .blank? so we don't require Rails [#57](https://github.com/Shopify/worldwide/pull/57)
- (bugfix) Zone lookup by name [#58](https://github.com/Shopify/worldwid/pull/58)

---

Expand Down
7 changes: 4 additions & 3 deletions lib/worldwide/region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,10 @@ def zone(code: nil, name: nil, zip: nil)
search_name = name.upcase

zones.find do |region|
search_name == region.legacy_name.upcase ||
search_name == region.full_name.upcase ||
search_name == I18n.with_locale(:en) { region.full_name.upcase }
search_name == region.legacy_name&.upcase ||
region.name_alternates&.any? { |a| search_name == a.upcase } ||
search_name == region.full_name&.upcase ||
search_name == I18n.with_locale(:en) { region.full_name&.upcase }
end
else # Worldwide::Util.present?(zip)
zone_by_normalized_zip(Zip.normalize(country_code: iso_code, zip: zip))
Expand Down
6 changes: 6 additions & 0 deletions test/worldwide/region_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class RegionTest < ActiveSupport::TestCase
end
end

test "can look up a zone by its alternate names" do
["Baleares", "Illes Balears", "Islas Baleares"].each do |name|
assert_equal "ES-PM", Worldwide.region(code: "ES").zone(name: name).iso_code
end
end

test "can look up a zone by CLDR code" do
zone = Worldwide.region(code: "MY").zone(code: "my14")

Expand Down

0 comments on commit 1e1da69

Please sign in to comment.