diff --git a/CHANGELOG.md b/CHANGELOG.md index 2db19ba37..f49f37c71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) --- diff --git a/lib/worldwide/region.rb b/lib/worldwide/region.rb index 7568ab2ae..3e5623dd3 100644 --- a/lib/worldwide/region.rb +++ b/lib/worldwide/region.rb @@ -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 @@ -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 diff --git a/lib/worldwide/zip.rb b/lib/worldwide/zip.rb index 2851cecd9..a8c711679 100644 --- a/lib/worldwide/zip.rb +++ b/lib/worldwide/zip.rb @@ -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) @@ -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 @@ -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? @@ -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 @@ -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 @@ -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? @@ -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" diff --git a/rake/cldr/pluralization_subkey_expander.rb b/rake/cldr/pluralization_subkey_expander.rb index 0df6d6c3e..a91936387 100644 --- a/rake/cldr/pluralization_subkey_expander.rb +++ b/rake/cldr/pluralization_subkey_expander.rb @@ -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) @@ -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) diff --git a/test/worldwide/regions_test.rb b/test/worldwide/regions_test.rb index 679c7c367..39f9ecd4f 100644 --- a/test/worldwide/regions_test.rb +++ b/test/worldwide/regions_test.rb @@ -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