worldwide 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 246832b6ca4adeaa2bb6e1dd5b236962316581b22ec49fa28db9d461e0065995
4
- data.tar.gz: '0569123a25ae7afae4dd9d09e7049e2c86dd26c8924f15a935916dbe982f128f'
3
+ metadata.gz: 378551ef286d7b7f398ee9ca149e417ac0f85efbba591a97fa8373de299258fd
4
+ data.tar.gz: 2a26ebd6f73862b5cc01bdae65ed710dbb7af5666a65f11d224bb529acb32d15
5
5
  SHA512:
6
- metadata.gz: 93b987051a980e244e817f1194e69e5e61a61544064029a5fabfe8d1739e385e803b19608bc3f0e3487c707f4deebd7186db584f8ba560d29f6c9cdb34272963
7
- data.tar.gz: 4bd8196e846f3339f0f50607efe8a288a670563a087665e2082ed90d180d9f89b11b21c7976e3449961f9fc517f44022a1029d4c5b470ff06810455e312c0304
6
+ metadata.gz: 6487ee321f130d34da0da8f58bf0bcc38815eecbec6f502358204a859b22c31afc6e9bfe3d8ee002b4e57e3b50040aca9fda443fc05889d62165755c706b449f
7
+ data.tar.gz: 1caf34f3c88a07fb3aa532f8192caf1a448af2795b710369645c1841cad6de722fe525833fff4b25a0c992fdb853e6fadf2ffc4244ee4aec0ee6073953147744
data/CHANGELOG.md CHANGED
@@ -27,10 +27,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
27
27
 
28
28
  ## [Unreleased]
29
29
 
30
- - nil
30
+ - nil.
31
31
 
32
32
  ---
33
33
 
34
+ [0.6.2] - 2023-12-11
35
+
36
+ - Zone lookup by Hash [#61](https://github.com/Shopify/worldwide/pull/61)
37
+
38
+ [0.6.1] - 2023-12-11
39
+
40
+ - Allow building number in address2 for DK [#53](https://github.com/Shopify/worldwide/pull/53)
41
+ - Avoid .present? and .blank? so we don't require Rails [#57](https://github.com/Shopify/worldwide/pull/57)
42
+ - (bugfix) Zone lookup by name [#58](https://github.com/Shopify/worldwid/pull/58)
43
+
34
44
  [0.6.0] - 2023-12-08
35
45
 
36
46
  - Add localized concern messages for address1 + 2 warnings and address may not exist message [#54](https://github.com/Shopify/worldwide/pull/54)
data/Gemfile.lock CHANGED
@@ -13,7 +13,7 @@ GIT
13
13
  PATH
14
14
  remote: .
15
15
  specs:
16
- worldwide (0.6.0)
16
+ worldwide (0.6.2)
17
17
  activesupport (~> 7.0)
18
18
  i18n (~> 1.12.0)
19
19
  phonelib (~> 0.8)
@@ -18,6 +18,7 @@ zip_regex: "^(DK( |-)?)?(?!38\\d{2}|39\\d{2})((0[89]\\d{2})|([1-9]\\d{3}))$"
18
18
  zip_example: '8660'
19
19
  phone_number_prefix: 45
20
20
  building_number_required: true
21
+ building_number_may_be_in_address2: true
21
22
  week_start_day: monday
22
23
  languages:
23
24
  - da
@@ -249,6 +249,7 @@ module Worldwide
249
249
 
250
250
  @parents = [].to_set
251
251
  @zones = []
252
+ @zones_by_code = {}
252
253
  end
253
254
 
254
255
  def inspect
@@ -262,6 +263,7 @@ module Worldwide
262
263
 
263
264
  region.parents << self
264
265
  @zones.append(region)
266
+ add_zone_to_hash(region)
265
267
  end
266
268
 
267
269
  # Attributes
@@ -320,6 +322,7 @@ module Worldwide
320
322
  # A user-facing name in the currently-active locale's language.
321
323
  def full_name(locale: I18n.locale)
322
324
  lookup_code = cldr_code
325
+
323
326
  if /^[0-9]+$/.match?(lookup_code) || lookup_code.length < 3
324
327
  Worldwide::Cldr.t("territories.#{lookup_code}", locale: locale, default: legacy_name)
325
328
  else
@@ -358,24 +361,22 @@ module Worldwide
358
361
  if Worldwide::Util.present?(code)
359
362
  search_code = code.to_s.upcase
360
363
  alt_search_code = "#{search_code[0..1]}-#{search_code[2..-1]}"
364
+ zone = nil
361
365
 
362
- zones.find do |region|
363
- [search_code, alt_search_code].any? do |candidate|
364
- candidate == subdivision_code(region.iso_code) ||
365
- candidate == region.alpha_three ||
366
- candidate == region.iso_code ||
367
- candidate == region.legacy_code ||
368
- candidate == region.numeric_three ||
369
- region&.code_alternates&.any?(candidate)
370
- end
366
+ [search_code, alt_search_code].each do |candidate|
367
+ zone = @zones_by_code[candidate]
368
+ break if zone
371
369
  end
370
+
371
+ return zone unless zone.nil?
372
372
  elsif Worldwide::Util.present?(name)
373
373
  search_name = name.upcase
374
374
 
375
375
  zones.find do |region|
376
- search_name == region.legacy_name.upcase ||
377
- search_name == region.full_name.upcase ||
378
- search_name == I18n.with_locale(:en) { region.full_name.upcase }
376
+ search_name == region.legacy_name&.upcase ||
377
+ region.name_alternates&.any? { |a| search_name == a.upcase } ||
378
+ search_name == region.full_name&.upcase ||
379
+ search_name == I18n.with_locale(:en) { region.full_name&.upcase }
379
380
  end
380
381
  else # Worldwide::Util.present?(zip)
381
382
  zone_by_normalized_zip(Zip.normalize(country_code: iso_code, zip: zip))
@@ -413,6 +414,17 @@ module Worldwide
413
414
 
414
415
  private
415
416
 
417
+ def add_zone_to_hash(zone)
418
+ @zones_by_code[subdivision_code(zone.iso_code)] = zone if Util.present?(subdivision_code(zone.iso_code))
419
+ @zones_by_code[zone.alpha_three] = zone if Util.present?(zone.alpha_three)
420
+ @zones_by_code[zone.iso_code] = zone if Util.present?(zone.iso_code)
421
+ @zones_by_code[zone.legacy_code] = zone if Util.present?(zone.legacy_code)
422
+ @zones_by_code[zone.numeric_three] = zone if Util.present?(zone.numeric_three)
423
+ zone.code_alternates&.each do |code|
424
+ @zones_by_code[code] = zone
425
+ end
426
+ end
427
+
416
428
  def answers_to_cldr_code(search_code)
417
429
  return false if Util.blank?(search_code) || Util.blank?(cldr_code)
418
430
  return true if search_code.casecmp(cldr_code).zero?
@@ -444,7 +456,7 @@ module Worldwide
444
456
  # Returns true if this country has zones defined, and has postal code prefix data for the zones
445
457
  def has_zip_prefixes?
446
458
  @zones&.any? do |zone|
447
- zone.zip_prefixes.present?
459
+ Util.present?(zone.zip_prefixes)
448
460
  end
449
461
  end
450
462
 
@@ -471,7 +483,7 @@ module Worldwide
471
483
 
472
484
  adjusted_value = partial_match ? value.strip : value
473
485
 
474
- Regexp.new(regex_prefix).match(adjusted_value).present?
486
+ Util.present?(Regexp.new(regex_prefix).match(adjusted_value))
475
487
  end
476
488
 
477
489
  # Search a list of zip prefixes (by province or timezone) to find the element that corresponds to the zip
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Worldwide
4
- VERSION = "0.6.0"
4
+ VERSION = "0.6.2"
5
5
  end
data/lib/worldwide/zip.rb CHANGED
@@ -50,7 +50,7 @@ module Worldwide
50
50
  # @param min_confidence [Integer] The minimum confidence level (between 0-100) that is accepted from a suggestion (optional)
51
51
  # @return [Region] which is a "country" if we have a suggestion, or `nil` if we do not.
52
52
  def find_country(country_code: nil, zip:, min_confidence: 0)
53
- return nil unless zip.present?
53
+ return nil unless Util.present?(zip)
54
54
 
55
55
  country = Worldwide.region(code: country_code) unless country_code.nil?
56
56
  return country if country&.valid_zip?(zip)
@@ -68,8 +68,8 @@ module Worldwide
68
68
  # We'll see if we have only a single suggestion and, if so, return it.
69
69
  # In cases where there's more than one possible match, we'll return nil.
70
70
  suggestions = find_country_using_zip_alone(adjusted_zip)
71
- suggestion = suggestions.first[0] unless suggestions.blank?
72
- confidence = suggestions.first[1] unless suggestions.blank?
71
+ suggestion = suggestions.first[0] unless Util.blank?(suggestions)
72
+ confidence = suggestions.first[1] unless Util.blank?(suggestions)
73
73
 
74
74
  return suggestion if suggestions.length == 1 && confidence && confidence >= min_confidence
75
75
 
@@ -90,7 +90,7 @@ module Worldwide
90
90
 
91
91
  if allow_autofill
92
92
  autofill = country.autofill_zip
93
- return autofill if autofill.present?
93
+ return autofill if Util.present?(autofill)
94
94
  end
95
95
 
96
96
  return nil if zip.nil?
@@ -163,7 +163,7 @@ module Worldwide
163
163
  end
164
164
 
165
165
  def strip_optional_country_prefix(country_code:, zip:)
166
- return zip if zip.blank?
166
+ return zip if Util.blank?(zip)
167
167
 
168
168
  unless OPTIONAL_PREFIX_COUNTRIES.include?(country_code&.to_sym)
169
169
  return zip
@@ -604,7 +604,7 @@ module Worldwide
604
604
  return zip
605
605
  end
606
606
 
607
- return zip if zip.blank?
607
+ return zip if Util.blank?(zip)
608
608
 
609
609
  autocorrected_zips = []
610
610
  input = zip
@@ -727,7 +727,7 @@ module Worldwide
727
727
  end
728
728
 
729
729
  def normalize_for_bd(zip:)
730
- return zip if zip.blank?
730
+ return zip if Util.blank?(zip)
731
731
 
732
732
  m = zip.match(/^(GPO:?|DHAKA)(\d{4})$/)
733
733
  if m.nil?
@@ -834,10 +834,10 @@ module Worldwide
834
834
  return zip if zip.nil?
835
835
 
836
836
  m = zip.match(/^0?0?0?0?([1-9])0?0?$/)
837
- return "00#{m[1]}00" if m.present?
837
+ return "00#{m[1]}00" if Util.present?(m)
838
838
 
839
839
  m = zip.match(/^0?0?1([1-9])0?0?$/)
840
- return "01#{m[1]}00" if m.present?
840
+ return "01#{m[1]}00" if Util.present?(m)
841
841
 
842
842
  if zip.match?(/^[1-9][0-9]00$/)
843
843
  "#{zip}0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worldwide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-08 00:00:00.000000000 Z
11
+ date: 2023-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport