Skip to content

Commit

Permalink
Merge pull request #57 from Shopify/present_and_blank_2023-12-08
Browse files Browse the repository at this point in the history
Avoid `.present?` and `.blank?` so we don't require Rails
  • Loading branch information
rochlefebvre authored Dec 11, 2023
2 parents d63024b + fdf1ade commit fec32c6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]

- 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)

---

Expand Down
4 changes: 2 additions & 2 deletions lib/worldwide/region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def cross_border_zip_includes_province?(zip:, province_code:)
# Returns true if this country has zones defined, and has postal code prefix data for the zones
def has_zip_prefixes?
@zones&.any? do |zone|
zone.zip_prefixes.present?
Util.present?(zone.zip_prefixes)
end
end

Expand All @@ -471,7 +471,7 @@ def passes_country_zip_regexp?(value:, partial_match: false)

adjusted_value = partial_match ? value.strip : value

Regexp.new(regex_prefix).match(adjusted_value).present?
Util.present?(Regexp.new(regex_prefix).match(adjusted_value))
end

# Search a list of zip prefixes (by province or timezone) to find the element that corresponds to the zip
Expand Down
18 changes: 9 additions & 9 deletions lib/worldwide/zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def gb_style?(country_code:)
# @param min_confidence [Integer] The minimum confidence level (between 0-100) that is accepted from a suggestion (optional)
# @return [Region] which is a "country" if we have a suggestion, or `nil` if we do not.
def find_country(country_code: nil, zip:, min_confidence: 0)
return nil unless zip.present?
return nil unless Util.present?(zip)

country = Worldwide.region(code: country_code) unless country_code.nil?
return country if country&.valid_zip?(zip)
Expand All @@ -68,8 +68,8 @@ def find_country(country_code: nil, zip:, min_confidence: 0)
# We'll see if we have only a single suggestion and, if so, return it.
# In cases where there's more than one possible match, we'll return nil.
suggestions = find_country_using_zip_alone(adjusted_zip)
suggestion = suggestions.first[0] unless suggestions.blank?
confidence = suggestions.first[1] unless suggestions.blank?
suggestion = suggestions.first[0] unless Util.blank?(suggestions)
confidence = suggestions.first[1] unless Util.blank?(suggestions)

return suggestion if suggestions.length == 1 && confidence && confidence >= min_confidence

Expand All @@ -90,7 +90,7 @@ def normalize(country_code:, zip:, allow_autofill: true, strip_extraneous_charac

if allow_autofill
autofill = country.autofill_zip
return autofill if autofill.present?
return autofill if Util.present?(autofill)
end

return nil if zip.nil?
Expand Down Expand Up @@ -163,7 +163,7 @@ def outcode(country_code:, zip:)
end

def strip_optional_country_prefix(country_code:, zip:)
return zip if zip.blank?
return zip if Util.blank?(zip)

unless OPTIONAL_PREFIX_COUNTRIES.include?(country_code&.to_sym)
return zip
Expand Down Expand Up @@ -604,7 +604,7 @@ def replace_letters_and_numbers_for_alphanumeric(country_code:, zip:)
return zip
end

return zip if zip.blank?
return zip if Util.blank?(zip)

autocorrected_zips = []
input = zip
Expand Down Expand Up @@ -727,7 +727,7 @@ def replace_ohs_and_zeros_for_gb(zip:)
end

def normalize_for_bd(zip:)
return zip if zip.blank?
return zip if Util.blank?(zip)

m = zip.match(/^(GPO:?|DHAKA)(\d{4})$/)
if m.nil?
Expand Down Expand Up @@ -834,10 +834,10 @@ def normalize_for_lk(zip:)
return zip if zip.nil?

m = zip.match(/^0?0?0?0?([1-9])0?0?$/)
return "00#{m[1]}00" if m.present?
return "00#{m[1]}00" if Util.present?(m)

m = zip.match(/^0?0?1([1-9])0?0?$/)
return "01#{m[1]}00" if m.present?
return "01#{m[1]}00" if Util.present?(m)

if zip.match?(/^[1-9][0-9]00$/)
"#{zip}0"
Expand Down
4 changes: 2 additions & 2 deletions rake/cldr/pluralization_subkey_expander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def cardinal_pluralization_context?(parent, children, locale)
# pluralization keywords collide with the cardinal pluralization keywords.
required_keys_for_locale = Worldwide::Plurals.keys(locale).map(&:to_s)
!ordinal_pluralization_context?(parent) &&
(required_keys_for_locale & children.keys).present?
Util.present?(required_keys_for_locale & children.keys)
end

def ordinal_pluralization_context?(parent)
Expand All @@ -63,7 +63,7 @@ def ordinal_pluralization_context?(parent)
def expand_pluralization_children!(parent, children, locale)
required_keys_for_locale = Worldwide::Plurals.keys(locale).map(&:to_s)
missing_keys = (required_keys_for_locale - children.keys).sort
if missing_keys.present?
if Util.present?(missing_keys)
missing_keys.each do |missing_key|
new_value = value_from_parent_locale(locale, parent + [missing_key]) ||
value_from_siblings(locale, missing_key, children)
Expand Down
2 changes: 1 addition & 1 deletion test/worldwide/regions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RegionsTest < ActiveSupport::TestCase
assert_equal name, region.legacy_name
assert_equal zones_count, region.zones.count

if zone_name.present?
if Util.present?(zone_name)
assert_equal zone_name, region.zones.first&.legacy_name
else
assert_nil region.zones.first&.legacy_name
Expand Down

0 comments on commit fec32c6

Please sign in to comment.