module Fields::AddressFieldHelper
def address_city_line_formatted(address)
def address_city_line_formatted(address)
country_iso2 = address.country&.iso2 # can be nil or empty
city = address.city
region = address.region&.name
postal_code = address.postal_code
# adapted from https://github.com/cainlevy/snail/blob/master/lib/snail.rb
# using iso2 property here because it's a port of what's used in snail gem
# will be cleaned up below if parts missing
formatted = case country_iso2
when "CN", "IN"
"#{city}, #{region} #{postal_code}"
when "BR"
"#{postal_code} #{city}#{"-" unless city.nil? || city.empty? || region.nil? || region.empty?}#{region}"
when "MX", "SK"
"#{postal_code} #{city}, #{region}"
when "IT"
"#{postal_code} #{city} #{region}"
when "BY"
"#{postal_code} #{city}#{"-" unless city.nil? || city.empty? || region.nil? || region.empty?}#{region}"
when "US", "CA", "AU", nil, ""
"#{city} #{region} #{postal_code}"
when "IL", "DK", "FI", "FR", "DE", "GR", "NO", "ES", "SE", "TR", "CY", "PT", "MK", "BA"
"#{postal_code} #{city}"
when "KW", "SY", "OM", "EE", "LU", "BE", "IS", "CH", "AT", "MD", "ME", "RS", "BG", "GE", "PL", "AM", "HR", "RO", "AZ"
"#{postal_code} #{city}"
when "NL"
"#{postal_code} #{city}"
when "IE"
"#{city}, #{region}\n#{postal_code}"
when "GB", "RU", "UA", "JO", "LB", "IR", "SA", "NZ"
"#{city} #{postal_code}" # Locally these may be on separate lines. The USPS prefers the city line above the country line, though.
when "EC"
"#{postal_code} #{city}"
when "HK", "IQ", "YE", "QA", "AL"
city.to_s
when "AE"
"#{postal_code}\n#{city}"
when "JP"
"#{city}, #{region}\n#{postal_code}"
when "EG", "ZA", "IM", "KZ", "HU"
"#{city}\n#{postal_code}"
when "LV"
"#{city}, LV-#{postal_code}".gsub(/LV-\s*$/, "") # undo if no postal code
when "LT"
"LT-#{postal_code} #{city}".gsub(/^LT-\s*/, "") # undo if no postal code
when "SI"
"SI-#{postal_code} #{city}".gsub(/^SI-\s*/, "") # undo if no postal code
when "CZ"
"#{postal_code} #{region}\n#{city}"
else
"#{city} #{region} #{postal_code}"
end
# clean up separators when missing pieces
formatted.strip # remove extra spaces and newlines before and after
.gsub(/^,\s*/, "") # remove extra comma from start
.gsub(/\s*,$/, "") # remove extra comma from end
end