phonelib 0.10.23 → 0.10.24

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: 5b2633488b369e12463c7027ee816032ccc733067cca9292c74ec88f3116b734
4
- data.tar.gz: cd120fe2caa70e770c290209cd9293170cb92993c33047d2829c0abcfda36cd8
3
+ metadata.gz: b57ecb9e7c4eeabbaabb6ae82a15dc4334e0eb9749b87a70cdd3f37be143a6c3
4
+ data.tar.gz: 3f5c9003d13f07654412a6455a54defd4719d0bf56aec23df720cdb3005b5f5b
5
5
  SHA512:
6
- metadata.gz: 55754cb48b2c2aad42b617910105dcd66caf92ce0a5b6b8d3987eaa095ba2fd36a42b1a5e08047ae1091c59e2fc66c636f436c31d4de616a2579b8cfc1664d92
7
- data.tar.gz: 5ec7b9b789d4e9876d0432c5864415f8227dd3c25fc21fd90dd25715010f63660b5cdf98d640da35edfa960009ead66afb4bf5f3239211774418f8ea48b51fd1
6
+ metadata.gz: b9d13eb82977f48c7024aaf7f0f5677fd03889e945b8ef8e067975d5b72466903a1d1c46ac7ea483beb41c2f3cd78b8af58da98f1012a41a302b297dde27ea88
7
+ data.tar.gz: 82cdc51eebad9516e64633116410c3e3056747c32049ad028bdac7359ce8ea5c61dc318cdc8dafcf32b3596b0ddec9226a46d3c0d124ec26cb828334703084d1
data/README.md CHANGED
@@ -246,10 +246,11 @@ Also you can fetch all matched countries
246
246
 
247
247
  ``` ruby
248
248
  phone.countries # returns array of all matched countries
249
- phone.country # returns first element from array of all matched countries
249
+ phone.country # returns valid_country when there is one, otherwise the main country for the code among all matched countries
250
250
  phone.valid_countries # returns array of countries where phone was matched against valid pattern
251
- phone.valid_country # returns first valid country from array of valid countries
251
+ phone.valid_country # returns the main country for the code among valid countries, or the first of them
252
252
  phone.country_code # returns country phone prefix
253
+ phone.national_number # returns national number of the country returned by phone.country
253
254
  ```
254
255
 
255
256
  Also it is possible to get formatted phone number
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -195,6 +195,10 @@ module Phonelib
195
195
  def additional_regexes=(data)
196
196
  return unless data.is_a?(Array)
197
197
  @@additional_regexes = {}
198
+ # regexes are baked into the cached phone data, so it has to be dropped
199
+ # even when no new regex is added (add_additional_regex does it too),
200
+ # otherwise stale capture groups desync the national number offset
201
+ @@phone_data = @@data_by_country_codes = nil
198
202
  data.each do |row|
199
203
  next if row.size != 3
200
204
  add_additional_regex(*row)
@@ -32,8 +32,10 @@ module Phonelib
32
32
  @data = {}
33
33
  else
34
34
  @data = analyze(sanitized, passed_country(country))
35
- first = @data.values.first
36
- @national_number = first ? first[:national] : sanitized
35
+ # the national number must come from the country #country reports:
36
+ # another entry can be a possible-only match, and mixing entries makes
37
+ # e164 splice one country's code onto another's number (issue #355)
38
+ @national_number = country_data ? country_data[:national] : sanitized
37
39
  end
38
40
  end
39
41
 
@@ -93,7 +95,7 @@ module Phonelib
93
95
  # Returns all countries that matched valid patterns
94
96
  # @return [Array] Possible ISO2 country codes array
95
97
  def countries
96
- @data.map { |iso2, _data| iso2 }
98
+ @countries ||= @data.keys
97
99
  end
98
100
 
99
101
  # Return countries with valid patterns
@@ -107,19 +109,25 @@ module Phonelib
107
109
  # Return valid country
108
110
  # @return [String] valid ISO2 country code
109
111
  def valid_country
110
- @valid_country ||= main_country(valid_countries)
112
+ return @valid_country if defined?(@valid_country)
113
+
114
+ @valid_country = main_country(valid_countries)
111
115
  end
112
116
 
113
117
  # Returns first country that matched valid patterns
114
118
  # @return [String] valid country ISO2 code or first matched country code
115
119
  def country
116
- @country ||= valid_country || main_country(countries)
120
+ return @country if defined?(@country)
121
+
122
+ @country = valid_country || main_country(countries)
117
123
  end
118
124
 
119
125
  # Returns whether a current parsed phone number is valid
120
126
  # @return [Boolean] parsed phone is valid
121
127
  def valid?
122
- @valid ||= @data.select { |_iso2, data| data[:valid].any? }.any?
128
+ return @valid if defined?(@valid)
129
+
130
+ @valid = @data.any? { |_iso2, data| data[:valid].any? }
123
131
  end
124
132
 
125
133
  # Returns whether a current parsed phone number is invalid
@@ -131,7 +139,9 @@ module Phonelib
131
139
  # Returns whether a current parsed phone number is possible
132
140
  # @return [Boolean] parsed phone is possible
133
141
  def possible?
134
- @possible ||= @data.select { |_iso2, data| data[:possible].any? }.any?
142
+ return @possible if defined?(@possible)
143
+
144
+ @possible = @data.any? { |_iso2, data| data[:possible].any? }
135
145
  end
136
146
 
137
147
  # Returns whether a current parsed phone number is impossible
@@ -178,6 +188,13 @@ module Phonelib
178
188
 
179
189
  private
180
190
 
191
+ # @private analyzing results of the country that #country resolved to.
192
+ # Every value derived from a single country (national number, format,
193
+ # national prefix) must be read through here so they cannot disagree.
194
+ def country_data
195
+ @data[country]
196
+ end
197
+
181
198
  # @private extracts extension from passed phone number if provided
182
199
  def separate_extension(original)
183
200
  return [original, ''] unless Phonelib.extension_separate_symbols
@@ -151,8 +151,11 @@ module Phonelib
151
151
  end
152
152
 
153
153
  def country_code_candidates_for(phone)
154
+ prefixes = (1..3).map { |length| phone[0, length] }
155
+ return prefixes if !Phonelib.ignore_plus && Core::PLUS_SIGN == @original[0]
156
+
154
157
  stripped_phone = phone.gsub(cr("Phonelib.phone_data_int_prefixes") { /^(#{Phonelib.phone_data_int_prefixes})/ }, '')
155
- ((1..3).map { |length| phone[0, length] } + (1..3).map { |length| stripped_phone[0, length] }).uniq
158
+ (prefixes + (1..3).map { |length| stripped_phone[0, length] }).uniq
156
159
  end
157
160
 
158
161
  # Create phone representation in e164 format
@@ -17,7 +17,7 @@ module Phonelib
17
17
  end
18
18
 
19
19
  def original_starts_with_plus_or_double_zero?
20
- original_s[0] == Core::PLUS_SIGN || original_s[0..1] == '00'
20
+ significant_original_s[0] == Core::PLUS_SIGN || significant_original_s[0..1] == '00'
21
21
  end
22
22
 
23
23
  # converts symbols in phone to numbers
@@ -37,8 +37,9 @@ module Phonelib
37
37
  # defines if to validate against single country or not
38
38
  def passed_country(country)
39
39
  code = country_prefix(country)
40
- if !Phonelib.ignore_plus && Core::PLUS_SIGN == @original[0] && code && !sanitized.start_with?(code)
41
- # in case number passed with + but it doesn't start with passed
40
+ if !Phonelib.ignore_plus && original_starts_with_plus_or_double_zero? && code &&
41
+ !sanitized.delete_prefix('00').start_with?(code)
42
+ # in case number passed with + or 00 but it doesn't start with passed
42
43
  # country prefix
43
44
  country = nil
44
45
  end
@@ -98,6 +99,15 @@ module Phonelib
98
99
  @original_s ||= @original.is_a?(String) ? @original : ''
99
100
  end
100
101
 
102
+ # Returns original number without the leading characters that libphonenumber
103
+ # drops in extractPossibleNumber, so a plus or 00 written behind punctuation
104
+ # like "(+501) 623-6848" is still recognized as leading. Vanity conversion
105
+ # runs first, the same way +sanitized+ applies it, so leading letters that
106
+ # convert to significant digits are not stripped as junk.
107
+ def significant_original_s
108
+ @significant_original_s ||= vanity_converted(original_s).sub(cr('\A[^0-9+]+'), '')
109
+ end
110
+
101
111
  # Get country that was provided or default country in needable format
102
112
  #
103
113
  # ==== Attributes
@@ -57,7 +57,7 @@ module Phonelib
57
57
  return "#{prefix}#{sanitized}" unless possible?
58
58
  return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
59
59
 
60
- fmt = @data[country][:format]
60
+ fmt = country_data[:format]
61
61
  national = @national_number
62
62
  if (matches = @national_number.match(cr(fmt[Core::PATTERN])))
63
63
  fmt = fmt[:intl_format] || fmt[:format]
@@ -129,7 +129,7 @@ module Phonelib
129
129
  return false if impossible?
130
130
 
131
131
  # has national prefix
132
- return false unless @data[country][Core::NATIONAL_PREFIX] || country == 'IT'
132
+ return false unless country_data[Core::NATIONAL_PREFIX] || country == 'IT'
133
133
  # fixed or mobile
134
134
  return false unless Core::AREA_CODE_TYPES.include?(type)
135
135
  # mobile && mexico, argentina, brazil
@@ -148,7 +148,7 @@ module Phonelib
148
148
  def formatting_data
149
149
  return @formatting_data if defined?(@formatting_data)
150
150
 
151
- data = @data[country]
151
+ data = country_data
152
152
  format = data[:format]
153
153
  prefix = data[Core::NATIONAL_PREFIX]
154
154
  rule = format[Core::NATIONAL_PREFIX_RULE] ||
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Phonelib
4
4
  # @private
5
- VERSION = '0.10.23'
5
+ VERSION = '0.10.24'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phonelib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.23
4
+ version: 0.10.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-07-27 00:00:00.000000000 Z
11
+ date: 2026-08-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Google libphonenumber library was taken as a basis for
@@ -40,7 +40,7 @@ homepage: https://github.com/daddyz/phonelib
40
40
  licenses:
41
41
  - MIT
42
42
  metadata:
43
- changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.23
43
+ changelog_uri: https://github.com/daddyz/phonelib/releases/tag/v0.10.24
44
44
  post_install_message:
45
45
  rdoc_options:
46
46
  - " --no-private - CHANGELOG.md --readme README.md"