worldwide 2.3.0 → 3.0.0

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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +0 -6
  3. data/.github/workflows/ci.yml +1 -3
  4. data/CHANGELOG.md +17 -0
  5. data/Gemfile +1 -4
  6. data/Gemfile.lock +7 -6
  7. data/README.md +3 -1
  8. data/data/iana_to_rails_time_zone.yml +12 -12
  9. data/data/other/timezones/ar.yml +4 -3
  10. data/data/other/timezones/bg.yml +4 -3
  11. data/data/other/timezones/bn.yml +4 -3
  12. data/data/other/timezones/ca.yml +4 -3
  13. data/data/other/timezones/cs.yml +4 -3
  14. data/data/other/timezones/da.yml +4 -3
  15. data/data/other/timezones/de.yml +4 -3
  16. data/data/other/timezones/el.yml +4 -3
  17. data/data/other/timezones/en.yml +4 -3
  18. data/data/other/timezones/es.yml +4 -3
  19. data/data/other/timezones/et.yml +4 -3
  20. data/data/other/timezones/eu.yml +4 -3
  21. data/data/other/timezones/fi.yml +4 -3
  22. data/data/other/timezones/fr.yml +4 -3
  23. data/data/other/timezones/gl.yml +4 -3
  24. data/data/other/timezones/gu.yml +4 -3
  25. data/data/other/timezones/he.yml +4 -3
  26. data/data/other/timezones/hi.yml +4 -3
  27. data/data/other/timezones/hr.yml +4 -3
  28. data/data/other/timezones/hu.yml +4 -3
  29. data/data/other/timezones/id.yml +4 -3
  30. data/data/other/timezones/it.yml +4 -3
  31. data/data/other/timezones/ja.yml +4 -3
  32. data/data/other/timezones/ka.yml +4 -3
  33. data/data/other/timezones/ko.yml +4 -3
  34. data/data/other/timezones/lt.yml +4 -3
  35. data/data/other/timezones/lv.yml +4 -3
  36. data/data/other/timezones/mr.yml +4 -3
  37. data/data/other/timezones/ms.yml +4 -3
  38. data/data/other/timezones/nb.yml +4 -3
  39. data/data/other/timezones/nl.yml +4 -3
  40. data/data/other/timezones/pl.yml +4 -3
  41. data/data/other/timezones/pt-BR.yml +4 -3
  42. data/data/other/timezones/pt-PT.yml +4 -3
  43. data/data/other/timezones/ro.yml +4 -3
  44. data/data/other/timezones/ru.yml +4 -3
  45. data/data/other/timezones/sk.yml +4 -3
  46. data/data/other/timezones/sl.yml +4 -3
  47. data/data/other/timezones/sr.yml +4 -3
  48. data/data/other/timezones/sv.yml +4 -3
  49. data/data/other/timezones/ta.yml +4 -3
  50. data/data/other/timezones/te.yml +4 -3
  51. data/data/other/timezones/th.yml +4 -3
  52. data/data/other/timezones/tr.yml +4 -3
  53. data/data/other/timezones/uk.yml +4 -3
  54. data/data/other/timezones/ur.yml +4 -3
  55. data/data/other/timezones/vi.yml +4 -3
  56. data/data/other/timezones/zh-CN.yml +4 -3
  57. data/data/other/timezones/zh-TW.yml +4 -3
  58. data/data/regions/GL.yml +1 -1
  59. data/lib/worldwide/deprecated_time_zone_mapper.rb +2 -0
  60. data/lib/worldwide/region.rb +194 -3
  61. data/lib/worldwide/time_zone.rb +10 -2
  62. data/lib/worldwide/util.rb +101 -0
  63. data/lib/worldwide/version.rb +1 -1
  64. data/lib/worldwide/zip.rb +4 -3
  65. data/worldwide.gemspec +1 -1
  66. metadata +2 -2
@@ -17,6 +17,12 @@ module Worldwide
17
17
  ).to_set.freeze
18
18
  private_constant :ISO_3166_ASSIGNED_COUNTRY_CODES
19
19
 
20
+ CODE_SEPARATOR = "-"
21
+ private_constant :CODE_SEPARATOR
22
+
23
+ BLANK_CODE = /\A[[:space:]]*\z/
24
+ private_constant :BLANK_CODE
25
+
20
26
  # Accpetable format types of the postal code.
21
27
  FORMAT_TYPES = {
22
28
  ALPHANUMERIC: "ALPHANUMERIC",
@@ -164,10 +170,10 @@ module Worldwide
164
170
 
165
171
  # Other names that may be used to refer to this region.
166
172
  # E.g., "Czech Republic" is also known as "Czechia".
167
- attr_accessor :name_alternates
173
+ attr_reader :name_alternates
168
174
 
169
- # iso_code values of regions (subdivisions) within the same country that border this region.
170
- # E.g., for CA-ON, the neighbouring zones are CA-MB, CA-NU and CA-QC.
175
+ # legacy_code values of regions (subdivisions) within the same country that border this region.
176
+ # E.g., for CA-ON, the neighbouring zones are "QC" and "MB".
171
177
  attr_accessor :neighbours
172
178
 
173
179
  alias_method :neighbors, :neighbours
@@ -336,6 +342,12 @@ module Worldwide
336
342
  @zones_by_code = {}
337
343
  end
338
344
 
345
+ def name_alternates=(names)
346
+ @name_alternates = names
347
+ @handle = nil
348
+ @handle_alternates = nil
349
+ end
350
+
339
351
  def inspect
340
352
  "#<#{self.class.name}:#{object_id} #{inspected_fields}>"
341
353
  end
@@ -557,6 +569,114 @@ module Worldwide
557
569
  valid_normalized_zip?(normalized, partial_match: partial_match)
558
570
  end
559
571
 
572
+ # Is the given postal code value valid for this region, judging a province by its own zip prefixes?
573
+ #
574
+ # For a region that is not a province, this gives the same result as {#valid_zip?}.
575
+ #
576
+ # For a province, {#valid_zip?} infers one province from the zip, and accepts the zip only when that is
577
+ # this province or when `zips_crossing_provinces` lists it. This method instead accepts the zip when
578
+ # the country accepts it and one of this province's own `zip_prefixes` matches it. The two differ when:
579
+ #
580
+ # - the province has no `zip_prefixes`, but other provinces of its country do: this method accepts
581
+ # any zip that the country accepts, and {#valid_zip?} accepts only a zip that infers this province;
582
+ # - the prefixes of two provinces both match the zip, for example a partial zip such as "CH6" or a
583
+ # shared prefix such as "K1A": this method accepts the zip for each of them, and {#valid_zip?}
584
+ # accepts it for the first one that it finds, and for the others only through `zips_crossing_provinces`.
585
+ #
586
+ # @example
587
+ # Worldwide.region(code: "CA-ON").valid_zone_zip?("K2A") == false
588
+ # Worldwide.region(code: "CA-ON").valid_zone_zip?("K2A", partial_match: true) == true
589
+ #
590
+ # @param zip [String]
591
+ # @param partial_match [Boolean] also accept the first part of a zip, as `valid_zip?` does
592
+ # @return [Boolean]
593
+ def valid_zone_zip?(zip, partial_match: false)
594
+ return valid_zip?(zip, partial_match: partial_match) unless province?
595
+
596
+ country = associated_country
597
+ return false if country.nil? || country.iso_code.nil?
598
+
599
+ country_code = country.iso_code
600
+ normalized = Zip.normalize(country_code: country_code, zip: zip)
601
+ adjusted = Zip.strip_optional_country_prefix(country_code: country_code, zip: normalized)
602
+
603
+ return false unless country.send(:valid_normalized_zip?, adjusted, partial_match: partial_match)
604
+ return true if Util.blank?(zip) && !country.zip_required?
605
+ return true if Util.blank?(zip_prefixes)
606
+ return true if country.send(:cross_border_zip_includes_province?, zip: adjusted, province_code: legacy_code)
607
+
608
+ zip_prefixes.any? { |prefix| adjusted.start_with?(prefix) || prefix.start_with?(adjusted) }
609
+ end
610
+
611
+ # Does the given code identify this region?
612
+ #
613
+ # The code may be the region's `legacy_code`, one of its `code_alternates`, or its `iso_code`.
614
+ # For a region within a country, the code may also carry the country's code in place of
615
+ # the hyphen-less part: "CA-NL" matches the region with legacy code "NL" in Canada.
616
+ # And, for a region whose legacy code has a hyphen, the part after the hyphen alone is enough:
617
+ # "13" matches the region with legacy code "JP-13".
618
+ #
619
+ # The comparison ignores the case of ASCII letters.
620
+ # Callers may look a region up by code very often, so this allocates no strings.
621
+ #
622
+ # @example
623
+ # Worldwide.region(code: "CA-QC").has_code?("PQ") == true
624
+ # Worldwide.region(code: "CA-QC").has_code?("ca-qc") == true
625
+ # Worldwide.region(code: "JP-13").has_code?("13") == true
626
+ #
627
+ # @param code [String, Symbol]
628
+ # @return [Boolean]
629
+ def has_code?(code)
630
+ return false if code.nil?
631
+
632
+ code = code.to_s unless code.is_a?(String)
633
+ return false if code.empty? || BLANK_CODE.match?(code)
634
+
635
+ return true if legacy_code&.casecmp(code)&.zero?
636
+ return true if code_alternates&.any? { |code_alternate| code_alternate.casecmp(code)&.zero? }
637
+ return true if iso_code&.casecmp(code)&.zero?
638
+
639
+ match_unprefixed_code?(code) || match_suffix_only_code?(code)
640
+ end
641
+
642
+ # Does the given name identify this region?
643
+ #
644
+ # The name is compared with the region's `legacy_name` and its `name_alternates`, after
645
+ # {Util.utf8_handle} simplifies each of them, so that "Fayyum" matches "Fayyūm".
646
+ # It is not compared with the localized {#full_name}.
647
+ #
648
+ # @param name [String]
649
+ # @return [Boolean]
650
+ def has_name?(name)
651
+ has_handle?(name.nil? ? nil : Util.utf8_handle(name))
652
+ end
653
+
654
+ # Does the given handle identify this region?
655
+ #
656
+ # A handle is the result of {Util.utf8_handle}. It is compared with the handle of the region's
657
+ # `legacy_name` and with the handles of its `name_alternates`.
658
+ #
659
+ # @param handle [String]
660
+ # @return [Boolean]
661
+ def has_handle?(handle)
662
+ return false if handle.nil?
663
+
664
+ handle == name_handle || name_alternate_handles.include?(handle)
665
+ end
666
+
667
+ # Is the region with the given legacy code one of the regions that border this region?
668
+ #
669
+ # @example
670
+ # Worldwide.region(code: "CA-ON").neighbor?("QC") == true
671
+ #
672
+ # @param code [String] a legacy code, as found in {#neighbours}
673
+ # @return [Boolean]
674
+ def neighbor?(code)
675
+ return false if neighbours.nil?
676
+
677
+ neighbours.include?(code)
678
+ end
679
+
560
680
  # are zones optional for this region?
561
681
  def province_optional?
562
682
  province_optional || !@zones&.any?(&:province?)
@@ -571,6 +691,77 @@ module Worldwide
571
691
 
572
692
  private
573
693
 
694
+ def name_handle
695
+ return if legacy_name.nil?
696
+
697
+ @handle ||= Util.utf8_handle(legacy_name)
698
+ end
699
+
700
+ def name_alternate_handles
701
+ @handle_alternates ||= (name_alternates || []).map { |name| Util.utf8_handle(name) }.freeze
702
+ end
703
+
704
+ # Matches a code that has this region's country code, a hyphen, and then this region's legacy code
705
+ # or one of its code alternates. E.g., "CA-NL", to match just the "NL" part.
706
+ #
707
+ # This avoids String#split, so that it allocates no strings.
708
+ def match_unprefixed_code?(code)
709
+ return false unless province?
710
+
711
+ hyphen_index = code.byteindex(CODE_SEPARATOR)
712
+ return false unless hyphen_index && hyphen_index != 0 && hyphen_index + 1 < code.bytesize
713
+
714
+ country_code = parent_country&.iso_code
715
+ return false if country_code.nil? || country_code.empty?
716
+
717
+ return false unless Util.ascii_casecmp_substring?(
718
+ country_code,
719
+ code,
720
+ start1: 0,
721
+ length1: country_code.bytesize,
722
+ start2: 0,
723
+ length2: hyphen_index,
724
+ )
725
+
726
+ suffix_start = hyphen_index + 1
727
+ suffix_length = code.bytesize - suffix_start
728
+
729
+ return true if suffix_matches?(legacy_code, code, suffix_start, suffix_length)
730
+
731
+ !!code_alternates&.any? { |code_alternate| suffix_matches?(code_alternate, code, suffix_start, suffix_length) }
732
+ end
733
+
734
+ def suffix_matches?(candidate, code, suffix_start, suffix_length)
735
+ return false if candidate.nil? || candidate.empty?
736
+
737
+ Util.ascii_casecmp_substring?(
738
+ candidate,
739
+ code,
740
+ start1: 0,
741
+ length1: candidate.bytesize,
742
+ start2: suffix_start,
743
+ length2: suffix_length,
744
+ )
745
+ end
746
+
747
+ # Matches a code that is only the part of this region's legacy code after the hyphen,
748
+ # e.g., "13" for "JP-13". Code alternates do not take part, to avoid matching by accident.
749
+ def match_suffix_only_code?(code)
750
+ return false if legacy_code.nil?
751
+
752
+ hyphen_index = legacy_code.byteindex(CODE_SEPARATOR)
753
+ return false if hyphen_index.nil? || hyphen_index == legacy_code.bytesize - 1
754
+
755
+ Util.ascii_casecmp_substring?(
756
+ legacy_code,
757
+ code,
758
+ start1: hyphen_index + 1,
759
+ length1: legacy_code.bytesize - hyphen_index - 1,
760
+ start2: 0,
761
+ length2: code.bytesize,
762
+ )
763
+ end
764
+
574
765
  def add_zone_to_hash(zone)
575
766
  @zones_by_code[subdivision_code(zone.iso_code)] = zone if Util.present?(subdivision_code(zone.iso_code))
576
767
  @zones_by_code[zone.alpha_three] = zone if Util.present?(zone.alpha_three)
@@ -11,6 +11,14 @@ module Worldwide
11
11
  "America/Winnipeg" => -5 * 3600,
12
12
  }.freeze
13
13
 
14
+ # Rails 8.1 moved these zones to their current tz names. Older Rails still reports the old names, which
15
+ # have no labels here, so mapping them keeps names and labels the same on every Rails version.
16
+ RAILS_LEGACY_ZONE_NAMES = {
17
+ "America/Godthab" => "America/Nuuk",
18
+ "Asia/Rangoon" => "Asia/Yangon",
19
+ "Europe/Kiev" => "Europe/Kyiv",
20
+ }.freeze
21
+
14
22
  class << self
15
23
  def all
16
24
  @all ||= uniq_zone_names.map { |zone_name| new(zone_name) }
@@ -23,7 +31,7 @@ module Worldwide
23
31
 
24
32
  (ActiveSupport::TimeZone.all + additional_zones).sort_by do |time_zone|
25
33
  [ADDITIONAL_ZONE_OFFSETS.fetch(time_zone.tzinfo.name) { time_zone.utc_offset }, time_zone.name]
26
- end.map { |time_zone| time_zone.tzinfo.name }.uniq
34
+ end.map { |time_zone| RAILS_LEGACY_ZONE_NAMES.fetch(time_zone.tzinfo.name, time_zone.tzinfo.name) }.uniq
27
35
  end
28
36
  end
29
37
 
@@ -40,7 +48,7 @@ module Worldwide
40
48
  private
41
49
 
42
50
  def translated_name
43
- Cldr.t(name, scope: :timezones)
51
+ Cldr.t(RAILS_LEGACY_ZONE_NAMES.fetch(name, name), scope: :timezones)
44
52
  end
45
53
  end
46
54
  end
@@ -2,6 +2,40 @@
2
2
 
3
3
  module Worldwide
4
4
  class Util
5
+ # Characters in U+00C0..U+017F that have no decomposition into a base letter and a mark,
6
+ # mapped to an ASCII approximation.
7
+ HANDLE_APPROXIMATIONS = {
8
+ "ß" => "ss",
9
+ "æ" => "ae",
10
+ "ð" => "d",
11
+ "ø" => "o",
12
+ "þ" => "th",
13
+ "đ" => "d",
14
+ "ħ" => "h",
15
+ "ı" => "i",
16
+ "ij" => "ij",
17
+ "ĸ" => "k",
18
+ "ŀ" => "l",
19
+ "ł" => "l",
20
+ "ʼn" => "'n",
21
+ "ŋ" => "ng",
22
+ "œ" => "oe",
23
+ "ŧ" => "t",
24
+ }.freeze
25
+ private_constant :HANDLE_APPROXIMATIONS
26
+
27
+ HANDLE_LATIN_RANGE = /[\u00C0-\u017F]/
28
+ private_constant :HANDLE_LATIN_RANGE
29
+
30
+ HANDLE_QUOTES_AND_BRACKETS = "'\"()[]"
31
+ private_constant :HANDLE_QUOTES_AND_BRACKETS
32
+
33
+ HANDLE_SEPARATOR = /(?:(?!\p{Word}|\u200D|\p{Extended_Pictographic}|\p{Emoji_Modifier}).)+/m
34
+ private_constant :HANDLE_SEPARATOR
35
+
36
+ HANDLE_EDGE_HYPHENS = /\A-+|-+\z/
37
+ private_constant :HANDLE_EDGE_HYPHENS
38
+
5
39
  class << self
6
40
  def blank?(value)
7
41
  value.nil? ||
@@ -12,6 +46,73 @@ module Worldwide
12
46
  def present?(value)
13
47
  !blank?(value)
14
48
  end
49
+
50
+ # Reduces a name to a simplified, lowercase form that is suitable for comparing names.
51
+ # Latin letters with diacritics become their base letters, quotes and brackets are dropped, and each
52
+ # run of other punctuation or whitespace becomes a single hyphen.
53
+ # Letters of other scripts are kept.
54
+ #
55
+ # @example
56
+ # Worldwide::Util.utf8_handle("Al Fayyūm") == "al-fayyum"
57
+ # Worldwide::Util.utf8_handle("Ærøskøbing 日本") == "aeroskobing-日本"
58
+ #
59
+ # @param input [String]
60
+ # @return [String] a new, frozen string
61
+ def utf8_handle(input)
62
+ string = input.to_s
63
+ string = string.dup.force_encoding(Encoding::UTF_8) unless string.encoding == Encoding::UTF_8
64
+ string = ActiveSupport::Multibyte::Unicode.tidy_bytes(string) unless string.valid_encoding?
65
+
66
+ result = string.downcase.unicode_normalize(:nfc)
67
+ result = result.gsub(HANDLE_LATIN_RANGE) do |char|
68
+ HANDLE_APPROXIMATIONS[char] || char.unicode_normalize(:nfd).delete("\u0300-\u036F")
69
+ end
70
+ result.delete!(HANDLE_QUOTES_AND_BRACKETS)
71
+ result.gsub!(HANDLE_SEPARATOR, "-")
72
+ result.gsub!(HANDLE_EDGE_HYPHENS, "")
73
+ result.freeze
74
+ end
75
+
76
+ # Compares a part of one string with a part of another string, ignoring the case of ASCII letters.
77
+ # This allocates no objects, because it compares bytes with `getbyte`.
78
+ # A comparison that involves a byte outside of ASCII is always false.
79
+ #
80
+ # @param str1 [String]
81
+ # @param str2 [String]
82
+ # @param start1 [Integer] the byte offset of the part of `str1`
83
+ # @param length1 [Integer] the byte length of the part of `str1`; it must be greater than 0
84
+ # @param start2 [Integer] the byte offset of the part of `str2`
85
+ # @param length2 [Integer] the byte length of the part of `str2`; it must be greater than 0
86
+ # @return [Boolean]
87
+ # @raise [ArgumentError] when a string is nil, a length is not positive, or a start is negative
88
+ def ascii_casecmp_substring?(str1, str2, start1:, length1:, start2:, length2:)
89
+ raise ArgumentError, "str1 and str2 must not be nil" if str1.nil? || str2.nil?
90
+ raise ArgumentError, "Length must be greater than 0" if length1 <= 0 || length2 <= 0
91
+ raise ArgumentError, "Start indices must not be negative" if start1 < 0 || start2 < 0
92
+
93
+ return false if length1 != length2
94
+
95
+ length = length1
96
+ return false if start1 + length > str1.bytesize || start2 + length > str2.bytesize
97
+
98
+ # A while loop avoids allocating a Range
99
+ i = 0
100
+ while i < length
101
+ b1 = str1.getbyte(start1 + i)
102
+ b2 = str2.getbyte(start2 + i)
103
+
104
+ return false if b1 >= 128 || b2 >= 128
105
+
106
+ b1 += 32 if b1.between?(65, 90)
107
+ b2 += 32 if b2.between?(65, 90)
108
+
109
+ return false if b1 != b2
110
+
111
+ i += 1
112
+ end
113
+
114
+ true
115
+ end
15
116
  end
16
117
  end
17
118
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Worldwide
4
- VERSION = "2.3.0"
4
+ VERSION = "3.0.0"
5
5
  end
data/lib/worldwide/zip.rb CHANGED
@@ -52,7 +52,7 @@ module Worldwide
52
52
  def find_country(country_code: nil, zip:, min_confidence: 0)
53
53
  return unless Util.present?(zip)
54
54
 
55
- country = Worldwide.region(code: country_code) unless country_code.nil?
55
+ country = Worldwide.region(code: country_code) unless Util.blank?(country_code)
56
56
  return country if country&.valid_zip?(zip)
57
57
 
58
58
  adjusted_zip = zip.strip.upcase
@@ -62,7 +62,8 @@ module Worldwide
62
62
  return suggestion unless suggestion.nil? || confidence.nil? || confidence < min_confidence
63
63
 
64
64
  # If our postal code is wholly numeric, we can't make an intelligent suggestion without an alleged country.
65
- return unless adjusted_zip.match?(/[A-Z]/)
65
+ # This checks the zip as given, not `adjusted_zip`, so that lowercase letters alone do not trigger a guess.
66
+ return unless zip.match?(/[A-Z]/)
66
67
 
67
68
  # Try a broader-ranging match without considering the alleged country
68
69
  # We'll see if we have only a single suggestion and, if so, return it.
@@ -837,7 +838,7 @@ module Worldwide
837
838
  # "CH6 ".
838
839
 
839
840
  # Check for complete postcode (outcode and incode)
840
- m = stripped.match(/^([A-Z]{1,2})(\d{1,2})([A-Z])*\s*(\d)([A-Z][A-Z])$/)
841
+ m = stripped.match(/^([A-Z]{1,2})(\d{1,2})([A-Z]){0,1}(\d)([A-Z][A-Z])$/)
841
842
 
842
843
  if m&.size == 6
843
844
  # outcode
data/worldwide.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.required_ruby_version = ">= 3.1.0"
27
+ spec.required_ruby_version = ">= 3.2.0"
28
28
 
29
29
  spec.add_dependency("activesupport", ">= 7.0")
30
30
  spec.add_dependency("i18n", ">= 1.15")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worldwide
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
@@ -9556,7 +9556,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
9556
9556
  -
9557
9557
  - ">="
9558
9558
  - !ruby/object:Gem::Version
9559
- version: 3.1.0
9559
+ version: 3.2.0
9560
9560
  required_rubygems_version: !ruby/object:Gem::Requirement
9561
9561
  requirements:
9562
9562
  -