libphonenumber-js 1.12.17 → 1.12.18

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 (175) hide show
  1. package/METADATA.md +2 -0
  2. package/README.md +209 -158
  3. package/build/AsYouType.js +6 -8
  4. package/build/AsYouType.js.map +1 -1
  5. package/build/AsYouType.test.js +13 -14
  6. package/build/AsYouType.test.js.map +1 -1
  7. package/build/AsYouTypeFormatter.complete.js +4 -4
  8. package/build/AsYouTypeFormatter.complete.js.map +1 -1
  9. package/build/AsYouTypeFormatter.js +4 -4
  10. package/build/AsYouTypeFormatter.js.map +1 -1
  11. package/build/AsYouTypeParser.js +42 -29
  12. package/build/AsYouTypeParser.js.map +1 -1
  13. package/build/AsYouTypeState.js +21 -17
  14. package/build/AsYouTypeState.js.map +1 -1
  15. package/build/PhoneNumber.js +2 -2
  16. package/build/PhoneNumber.js.map +1 -1
  17. package/build/findNumbers/Leniency.js +0 -1
  18. package/build/findNumbers/Leniency.js.map +1 -1
  19. package/build/format.js +4 -4
  20. package/build/format.js.map +1 -1
  21. package/build/formatPhoneNumberForMobileDialing.js +2 -2
  22. package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
  23. package/build/helpers/checkNumberLength.js +39 -7
  24. package/build/helpers/checkNumberLength.js.map +1 -1
  25. package/build/helpers/checkNumberLength.test.js +51 -12
  26. package/build/helpers/checkNumberLength.test.js.map +1 -1
  27. package/build/helpers/extractCountryCallingCode.js +13 -10
  28. package/build/helpers/extractCountryCallingCode.js.map +1 -1
  29. package/build/helpers/extractCountryCallingCode.test.js +3 -3
  30. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  31. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  32. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  33. package/build/helpers/extractNationalNumber.js +15 -5
  34. package/build/helpers/extractNationalNumber.js.map +1 -1
  35. package/build/helpers/extractNationalNumber.test.js +10 -2
  36. package/build/helpers/extractNationalNumber.test.js.map +1 -1
  37. package/build/helpers/getCountryByCallingCode.js +3 -2
  38. package/build/helpers/getCountryByCallingCode.js.map +1 -1
  39. package/build/helpers/getCountryByNationalNumber.js +27 -6
  40. package/build/helpers/getCountryByNationalNumber.js.map +1 -1
  41. package/build/helpers/matchesEntirely.js +4 -4
  42. package/build/helpers/matchesEntirely.js.map +1 -1
  43. package/build/isPossible.js +4 -4
  44. package/build/isPossible.js.map +1 -1
  45. package/build/isPossible.test.js +7 -0
  46. package/build/isPossible.test.js.map +1 -1
  47. package/build/isPossiblePhoneNumber.test.js +4 -0
  48. package/build/isPossiblePhoneNumber.test.js.map +1 -1
  49. package/build/metadata.js +2 -2
  50. package/build/metadata.js.map +1 -1
  51. package/build/parse.js +9 -7
  52. package/build/parse.js.map +1 -1
  53. package/build/parseIncompletePhoneNumber.js +7 -7
  54. package/build/parseIncompletePhoneNumber.js.map +1 -1
  55. package/build/parsePhoneNumber.test.js +12 -0
  56. package/build/parsePhoneNumber.test.js.map +1 -1
  57. package/build/validatePhoneNumberLength.js +1 -1
  58. package/build/validatePhoneNumberLength.js.map +1 -1
  59. package/build-scripts/check-for-metadata-changes-and-create-pull-request.js +87 -0
  60. package/{runnable/metadata-update-and-release.js → build-scripts/check-for-metadata-changes-and-release.js} +10 -4
  61. package/{runnable/json-to-js.js → build-scripts/create-js-file-for-json-file.js} +2 -2
  62. package/{runnable/metadata-branch.js → build-scripts/create-metadata-update-branch.js} +6 -1
  63. package/{runnable/generate-country-codes.js → build-scripts/generate-country-codes-list-in-typescript.js} +1 -0
  64. package/{runnable/generate.js → build-scripts/generate-metadata-from-google-metadata.js} +8 -14
  65. package/{runnable/modules → build-scripts/helpers}/commit.js +1 -0
  66. package/{runnable/modules → build-scripts/helpers}/exec.js +1 -0
  67. package/{runnable/modules/update-metadata.js → build-scripts/helpers/update-metadata-from-google-metadata.js} +9 -7
  68. package/{runnable/download.js → build-scripts/pull-google-metadata.js} +12 -4
  69. package/bundle/libphonenumber-js.min.js +1 -1
  70. package/bundle/libphonenumber-js.min.js.map +1 -1
  71. package/bundle/libphonenumber-max.js +1 -1
  72. package/bundle/libphonenumber-max.js.map +1 -1
  73. package/bundle/libphonenumber-min.js +1 -1
  74. package/bundle/libphonenumber-min.js.map +1 -1
  75. package/bundle/libphonenumber-mobile.js +1 -1
  76. package/bundle/libphonenumber-mobile.js.map +1 -1
  77. package/core/index.d.cts +1 -1
  78. package/core/index.d.ts +1 -1
  79. package/custom.d.ts +1 -1
  80. package/es6/AsYouType.js +6 -8
  81. package/es6/AsYouType.js.map +1 -1
  82. package/es6/AsYouType.test.js +13 -14
  83. package/es6/AsYouType.test.js.map +1 -1
  84. package/es6/AsYouTypeFormatter.complete.js +4 -4
  85. package/es6/AsYouTypeFormatter.complete.js.map +1 -1
  86. package/es6/AsYouTypeFormatter.js +4 -4
  87. package/es6/AsYouTypeFormatter.js.map +1 -1
  88. package/es6/AsYouTypeParser.js +42 -29
  89. package/es6/AsYouTypeParser.js.map +1 -1
  90. package/es6/AsYouTypeState.js +21 -17
  91. package/es6/AsYouTypeState.js.map +1 -1
  92. package/es6/PhoneNumber.js +1 -2
  93. package/es6/PhoneNumber.js.map +1 -1
  94. package/es6/findNumbers/Leniency.js +0 -1
  95. package/es6/findNumbers/Leniency.js.map +1 -1
  96. package/es6/format.js +4 -4
  97. package/es6/format.js.map +1 -1
  98. package/es6/formatPhoneNumberForMobileDialing.js +2 -2
  99. package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
  100. package/es6/helpers/checkNumberLength.js +39 -7
  101. package/es6/helpers/checkNumberLength.js.map +1 -1
  102. package/es6/helpers/checkNumberLength.test.js +49 -12
  103. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  104. package/es6/helpers/extractCountryCallingCode.js +13 -10
  105. package/es6/helpers/extractCountryCallingCode.js.map +1 -1
  106. package/es6/helpers/extractCountryCallingCode.test.js +3 -3
  107. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  108. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  109. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  110. package/es6/helpers/extractNationalNumber.js +15 -5
  111. package/es6/helpers/extractNationalNumber.js.map +1 -1
  112. package/es6/helpers/extractNationalNumber.test.js +10 -2
  113. package/es6/helpers/extractNationalNumber.test.js.map +1 -1
  114. package/es6/helpers/getCountryByCallingCode.js +3 -2
  115. package/es6/helpers/getCountryByCallingCode.js.map +1 -1
  116. package/es6/helpers/getCountryByNationalNumber.js +28 -6
  117. package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
  118. package/es6/helpers/matchesEntirely.js +4 -4
  119. package/es6/helpers/matchesEntirely.js.map +1 -1
  120. package/es6/isPossible.js +4 -4
  121. package/es6/isPossible.js.map +1 -1
  122. package/es6/isPossible.test.js +7 -0
  123. package/es6/isPossible.test.js.map +1 -1
  124. package/es6/isPossiblePhoneNumber.test.js +4 -0
  125. package/es6/isPossiblePhoneNumber.test.js.map +1 -1
  126. package/es6/metadata.js +2 -2
  127. package/es6/metadata.js.map +1 -1
  128. package/es6/parse.js +9 -7
  129. package/es6/parse.js.map +1 -1
  130. package/es6/parseIncompletePhoneNumber.js +7 -7
  131. package/es6/parseIncompletePhoneNumber.js.map +1 -1
  132. package/es6/parsePhoneNumber.test.js +12 -0
  133. package/es6/parsePhoneNumber.test.js.map +1 -1
  134. package/es6/validatePhoneNumberLength.js +1 -1
  135. package/es6/validatePhoneNumberLength.js.map +1 -1
  136. package/index.d.cts +1 -1
  137. package/index.d.ts +1 -1
  138. package/max/index.d.cts +1 -1
  139. package/max/index.d.ts +1 -1
  140. package/min/index.d.cts +1 -1
  141. package/min/index.d.ts +1 -1
  142. package/mobile/index.d.cts +1 -1
  143. package/mobile/index.d.ts +1 -1
  144. package/package.json +16 -23
  145. package/source/AsYouType.js +8 -10
  146. package/source/AsYouType.test.js +13 -14
  147. package/source/AsYouTypeFormatter.complete.js +4 -4
  148. package/source/AsYouTypeFormatter.js +4 -4
  149. package/source/AsYouTypeParser.js +45 -28
  150. package/source/AsYouTypeState.js +21 -17
  151. package/source/PhoneNumber.js +1 -2
  152. package/source/findNumbers/Leniency.js +0 -1
  153. package/source/format.js +4 -4
  154. package/source/formatPhoneNumberForMobileDialing.js +2 -2
  155. package/source/helpers/checkNumberLength.js +40 -7
  156. package/source/helpers/checkNumberLength.test.js +54 -12
  157. package/source/helpers/extractCountryCallingCode.js +15 -10
  158. package/source/helpers/extractCountryCallingCode.test.js +3 -3
  159. package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +19 -8
  160. package/source/helpers/extractNationalNumber.js +15 -5
  161. package/source/helpers/extractNationalNumber.test.js +11 -2
  162. package/source/helpers/getCountryByCallingCode.js +2 -2
  163. package/source/helpers/getCountryByNationalNumber.js +27 -6
  164. package/source/helpers/matchesEntirely.js +4 -4
  165. package/source/isPossible.js +4 -4
  166. package/source/isPossible.test.js +9 -0
  167. package/source/isPossiblePhoneNumber.test.js +5 -0
  168. package/source/metadata.js +2 -2
  169. package/source/parse.js +9 -5
  170. package/source/parseIncompletePhoneNumber.js +7 -7
  171. package/source/parsePhoneNumber.test.js +14 -0
  172. package/source/validatePhoneNumberLength.js +1 -1
  173. package/runnable/metadata-update-and-push-and-pull-request.js +0 -47
  174. package/runnable/metadata-update-and-push.js +0 -30
  175. /package/{runnable/create-commonjs-package-json.js → build-scripts/create-commonjs-package-json-file.js} +0 -0
package/README.md CHANGED
@@ -4,7 +4,9 @@
4
4
  [![npm downloads](https://img.shields.io/npm/dm/libphonenumber-js.svg?style=flat-square)](https://www.npmjs.com/package/libphonenumber-js)
5
5
  <!-- [![coverage](https://img.shields.io/coveralls/catamphetamine/libphonenumber-js/master.svg?style=flat-square)](https://coveralls.io/r/catamphetamine/libphonenumber-js?branch=master) -->
6
6
 
7
- A simpler and smaller rewrite of Google Android's [`libphonenumber`](https://github.com/google/libphonenumber/) library in javascript.
7
+ A simpler and smaller rewrite of Google Android's [`libphonenumber`](https://github.com/google/libphonenumber/) library in javascript/TypeScript.
8
+
9
+ Parse and format personal phone numbers.
8
10
 
9
11
  [See Demo](https://catamphetamine.gitlab.io/libphonenumber-js/)
10
12
 
@@ -14,7 +16,7 @@ If you’re trying to build a React component with it, take a look at [`react-ph
14
16
 
15
17
  Google's [`libphonenumber`](https://github.com/googlei18n/libphonenumber) is an ultimate phone number formatting and parsing library developed by Google for [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) phones. It is written in C++ and Java, and, while it has an [official autogenerated javascript port](https://github.com/googlei18n/libphonenumber/tree/master/javascript), that port is tightly coupled to Google's `closure` javascript framework, and, when compiled into a [bundle](https://github.com/ruimarinho/google-libphonenumber), weighs about 550 kB (350 kB code + 200 kB metadata).
16
18
 
17
- With many websites today asking for user's phone number, the internet could benefit from a simpler and smaller library that would just get the parsing and formatting right, and that's what `libphonenumber-js` is.
19
+ With many websites today asking for user's phone number, the internet could benefit from a simpler and smaller library that would just deal with parsing or formatting personal phone numbers, and that's what `libphonenumber-js` is.
18
20
 
19
21
  <!-- That's what `libphonenumber-js` is about: I started off from scratch, decyphering Google's code and figuring out how this whole machinery works, through trial and error, with several years of hacking around, receiving bug reports from other developers — eventually, it has reached a state when it can be assumed production-ready. -->
20
22
 
@@ -22,35 +24,37 @@ With many websites today asking for user's phone number, the internet could bene
22
24
  One part of me was curious about how all this phone number parsing and formatting machinery worked, and another part of me was curious if there was a way to reduce those 530 kilobytes to something more reasonable while also getting rid of all the unnecessary bulk and rewriting it all in pure javascript. The resulting library does everything a modern web application needs while maintaining a much smaller size of about 130 kilobytes.
23
25
  -->
24
26
 
25
- ## Difference from Google's `libphonenumber`
27
+ ## Differences from Google's `libphonenumber`
26
28
 
27
29
  * Smaller footprint: `145 kB` (65 kB code + 80 kB sufficient metadata) vs the original Google's `550 kB` (350 kB code + 200 kB full metadata).
28
30
 
29
- * Can search for phone numbers in text (Google's autogenerated javascript port can't).
31
+ * Can search for phone numbers in text. Google's autogenerated javascript port doesn't provide such feature for some reason.
30
32
 
31
- * Aims at parsing and formatting people's phone numbers while skipping all other "special" cases like:
33
+ * Focuses on parsing and formatting personal phone numbers while skipping any other "special" cases like:
32
34
 
33
35
  * Emergency phone numbers like `911`.
34
36
 
35
- * ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/): short SMS-only numbers like `12345`.
37
+ * ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/) — short SMS-only numbers like `12345`.
38
+
39
+ * Numbers starting with an `*`. For example, `*555` is used in New Zeland to report non-urgent traffic incidents. Or, in Israel, certain advertising numbers start with a `*`.
36
40
 
37
- * Numbers starting with a [`*`](https://github.com/googlei18n/libphonenumber/blob/master/FALSEHOODS.md), like `*555`.
41
+ * Australian [`13`-smart](https://github.com/catamphetamine/libphonenumber-js/issues/400) numbers, which are a "catchy" "short" form of regular "landline" numbers and are mainly used in [advertisement](https://www.youtube.com/watch?v=Y0HchzE-7rM).
38
42
 
39
- * Alphabetic phone numbers like `1-800-GOT-MILK`: people don't input their phone numbers like that, it's only used in advertisement.
43
+ * Alphabetic phone numbers like `1-800-GOT-MILK`. People don't input their phone numbers like that. It was only used in advertisement in the days of push-button telephones.
40
44
 
41
- * "Two-in-one" phone numbers with "combined" extensions like `(530) 583-6985 x302/x2303` that in fact represent two separate phone numbers, because the library can only return a single phone number when parsing a string.
45
+ * "Two-in-one" phone numbers with "combined" extensions like `(530) 583-6985 x302/x2303`. Phone numbers like that actually represent two separate phone numbers, so it's not clear which one to pick or how to return both at the same time.
42
46
 
43
- * Overall, doesn't support formatting non-"conventional" numbers like numbers with the "area code" omitted or "alternative" "short" numbers like [Australian `13`-smart numbers](https://github.com/catamphetamine/libphonenumber-js/issues/400). For example, when dialing phone numbers within the same "area", people sometimes skip the "area code", and dial, say, `456-789` instead of `(123) 456-789`. Google's `libphonenumber` supports formatting such numbers (with "area code" omitted) because it is used for dialing on the Android phone operating system. Because `libphonenumber-js` isn't a phone operating system and is not used for actual dialing — only for inputting internationally-dialable personal phone numbers — it doesn't format such "short" phone numbers because it doesn't need to support those.
47
+ * Local numbers with the "area code" omitted. For example, when dialing phone numbers within the same "area", people sometimes skip the "area code", and dial, say, just `456-789` instead of proper `(123) 456-789`. This all is considered a relic of the past. In the modern world, there're no "local areas" and anyone could call everyone else around the world.
44
48
 
45
- * Any other "miscellaneous" cases that're considered irrelevant for the task.
49
+ * Doesn't provide "geolocation" feature when it can tell a city by a phone number.
46
50
 
47
- * Doesn't provide "geolocation" by a phone number.
51
+ * Doesn't use hyphens or brackets when formatting phone numbers in international format. Instead, whitespace is used. The rationale is that brackets aren't relevant in international context because there're no "local areas", and hyphens aren't used because whitespace just looks cleaner.
48
52
 
49
- * Doesn't use hyphens or brackets when formatting international phone numbers, only whitespace (seems more logical this way).
53
+ * Doesn't set `.country` to `"001"` when parsing ["non-geographic"](#non-geographic) phone numbers, like mobile satellite communications services. Instead, `.country` is set to `undefined` in those cases, and instead a developer can call `.isNonGeographic()` method of the `PhoneNumber` instance to find out whether the parsed phone number is a "non-geographic" one.
50
54
 
51
- * Doesn't set `.country` to `"001"` when parsing ["non-geographic"](#non-geographic) phone numbers (like mobile satellite communications services). Instead, `.country` is `undefined` in those cases, and a developer can call `.isNonGeographic()` method of the `PhoneNumber` instance to find out whether the parsed phone number is a "non-geographic" one.
55
+ * Doesn't provide the equivalent of `libphonenumber`'s `formatNumberForMobileDialing()` function that formats a number for dialing from a mobile phone within the same country. This feature may be required for dialing local numbers from a mobile phone in some countries like Brazil or Colombia where they require adding ["carrier codes"](https://www.bandwidth.com/glossary/carrier-identification-code-cic/) when making such calls. Since `libphonenumber-js` is not a dialing library (we're not Android phone operaing system), it doesn't prepend any "carrier codes" when formatting such phone numbers, though it does parse such "carrier codes" correctly.
52
56
 
53
- * Doesn't provide the equivalent of `libphonenumber`'s `formatNumberForMobileDialing()` function that formats a number for dialing using a mobile phone within the country: dialing local numbers from a mobile phone is a bit more complicated in some countries like Brazil or Colombia where they require adding ["carrier codes"](https://www.bandwidth.com/glossary/carrier-identification-code-cic/) when making a call. Since `libphonenumber-js` is not a dialing library (we're not Android phone operaing system), it doesn't prepend any "carrier codes" when formatting phone numbers, though it does parse such "carrier codes" correctly.
57
+ * Fixed a small [bug](https://issuetracker.google.com/issues/335892662) when Canadian numbers `+1310xxxx` wheren't considered possible.
54
58
 
55
59
  <!-- (Australia, Bolivia, Brazil, China, Colombia, Croatia, Faroe Islands, South Korea, Liechtenstein, Luxembourg, Venezuela) -->
56
60
 
@@ -70,7 +74,9 @@ One part of me was curious about how all this phone number parsing and formattin
70
74
 
71
75
  ## GitHub
72
76
 
73
- On March 9th, 2020, GitHub, Inc. silently [banned](https://medium.com/@catamphetamine/how-github-blocked-me-and-all-my-libraries-c32c61f061d3) my account (erasing all my repos, issues and comments, even in my employer's private repos) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The [GitHub repo](https://github.com/catamphetamine/libphonenumber-js) is now only used as a backup (you can star the repo there too), and the primary repo is now the [GitLab one](https://gitlab.com/catamphetamine/libphonenumber-js). Issues can be reported in any repo.
77
+ On March 9th, 2020, GitHub, Inc. silently [banned](https://medium.com/@catamphetamine/how-github-blocked-me-and-all-my-libraries-c32c61f061d3) my account — erasing all my repos, issues and comments, even in my employer's private repos — without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The [GitHub repo](https://github.com/catamphetamine/libphonenumber-js) is now only used as a backup (although you can still "star" it), and the primary repo is now the [GitLab one](https://gitlab.com/catamphetamine/libphonenumber-js). For users' convenience, issues can be reported in both places.
78
+
79
+ If you're about to report an issue, see [bug reporting](#bug-reporting) instructions.
74
80
 
75
81
  ## Install
76
82
 
@@ -86,7 +92,7 @@ via [yarn](https://yarnpkg.com)
86
92
  $ yarn add libphonenumber-js
87
93
  ```
88
94
 
89
- If you're not using a bundler then use a [standalone version from a CDN](https://gitlab.com/catamphetamine/libphonenumber-js/#cdn).
95
+ Alternatively, one could include it on a web page [directly](#cdn) via a `<script/>` tag.
90
96
 
91
97
  ## Use
92
98
 
@@ -145,10 +151,13 @@ https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/42
145
151
  _(new API)_
146
152
  -->
147
153
 
154
+ Parses a complete phone number.
155
+
148
156
  ```js
149
157
  import parsePhoneNumber from 'libphonenumber-js'
150
158
 
151
159
  const phoneNumber = parsePhoneNumber(' 8 (800) 555-35-35 ', 'RU')
160
+
152
161
  if (phoneNumber) {
153
162
  phoneNumber.country === 'RU'
154
163
  phoneNumber.number === '+78005553535'
@@ -172,28 +181,10 @@ parseNumber('Phone: 8 (800) 555 35 35.', 'RU')
172
181
  </details>
173
182
  -->
174
183
 
175
- ### Validate phone number
176
-
177
- ```js
178
- import {
179
- isPossiblePhoneNumber,
180
- isValidPhoneNumber,
181
- validatePhoneNumberLength
182
- } from 'libphonenumber-js'
183
-
184
- isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
185
- isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
186
-
187
- validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
188
- validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid.
189
- ```
190
-
191
- `isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and the actual phone number digits.
192
-
193
- `validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, it returns the actual reason: `TOO_SHORT`, `TOO_LONG`, etc.
194
-
195
184
  ### Format phone number
196
185
 
186
+ Formats a complete phone number.
187
+
197
188
  <!--
198
189
  _(new API)_
199
190
  -->
@@ -203,9 +194,11 @@ import parsePhoneNumber from 'libphonenumber-js'
203
194
 
204
195
  const phoneNumber = parsePhoneNumber('+12133734253')
205
196
 
206
- phoneNumber.formatInternational() === '+1 213 373 4253'
207
- phoneNumber.formatNational() === '(213) 373-4253'
208
- phoneNumber.getURI() === 'tel:+12133734253'
197
+ if (phoneNumber) {
198
+ phoneNumber.formatInternational() === '+1 213 373 4253'
199
+ phoneNumber.formatNational() === '(213) 373-4253'
200
+ phoneNumber.getURI() === 'tel:+12133734253'
201
+ }
209
202
  ```
210
203
 
211
204
  <!--
@@ -232,18 +225,44 @@ formatNumber({ country: 'US', phone: '2133734253' }, 'NATIONAL')
232
225
 
233
226
  ### "As You Type" formatter
234
227
 
228
+ Parses and formats an incomplete phone number.
229
+
235
230
  ```js
236
231
  import { AsYouType } from 'libphonenumber-js'
237
232
 
238
233
  new AsYouType().input('+12133734')
239
- // Outputs: '+1 213 373 4'
234
+ // Returns: '+1 213 373 4'
240
235
 
241
236
  new AsYouType('US').input('2133734')
242
- // Outputs: '(213) 373-4'
237
+ // Returns: '(213) 373-4'
243
238
  ```
244
239
 
240
+ ### Validate phone number
241
+
242
+ Validates a complete phone number.
243
+
244
+ ```js
245
+ import {
246
+ isPossiblePhoneNumber,
247
+ isValidPhoneNumber,
248
+ validatePhoneNumberLength
249
+ } from 'libphonenumber-js'
250
+
251
+ isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
252
+ isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
253
+
254
+ validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
255
+ validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid.
256
+ ```
257
+
258
+ `isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and phone number digits.
259
+
260
+ `validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, instead of just returning `false`, it returns the actual reason why the phone number length is incorrect: `TOO_SHORT`, `TOO_LONG`, etc.
261
+
245
262
  ### Full-text search
246
263
 
264
+ Finds complete phone numbers in text.
265
+
247
266
  <!--
248
267
  _(new API)_
249
268
  -->
@@ -282,31 +301,45 @@ findPhoneNumbersInText(`
282
301
 
283
302
  ## "min" vs "max" vs "mobile" vs "core"
284
303
 
285
- This library provides different "metadata" sets, "metadata" being a list of phone number parsing and formatting rules for all countries. The complete list of those rules is huge, so this library provides a way to optimize bundle size by choosing between `max`, `min`, `mobile` and "custom" metadata:
304
+ This library provides different "metadata" sets, where a "metadata" set is a complete list of phone number parsing and formatting rules for all possible countries.
286
305
 
287
- * `max` — The complete metadata set, is about `145 kilobytes` in size (`libphonenumber-js/metadata.max.json`). Choose this when you need the most strict version of `isValid()`, or if you need to detect phone number type ("fixed line", "mobile", etc).
306
+ As one may guess, the complete list of those rules is huge, so this library provides a way to optimize bundle size by choosing between `max`, `min`, `mobile` or "custom" metadata:
288
307
 
289
- * `min` — (default) The smallest metadata set, is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json`). Choose this by default: when you don't need to detect phone number type ("fixed line", "mobile", etc), or when a basic version of `isValid()` is enough. The `min` metadata set doesn't contain the regular expressions for phone number digits validation (via [`.isValid()`](#isvalid)) and detecting phone number type (via [`.getType()`](#gettype)) for most countries. In this case, `.isValid()` still performs some basic phone number validation (for example, checks phone number length), but it doesn't validate phone number digits themselves the way `max` metadata validation does.
308
+ * `min` — (default) The smallest metadata set
309
+ * Is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json` file)
310
+ * Choose this when:
311
+ * You don't need to detect phone number type — "fixed line", "mobile", etc — via [`.getType()`](#gettype-string)
312
+ <!-- * Because the `min` metadata doesn't include (for most countries anyway) the regular expressions that're required for phone number type detection. -->
313
+ * You're fine with just validating phone number length via [`.isPossible()`](#ispossible-boolean) and you don't need to strictly validate phone number digits via [`.isValid()`](#isvalid-boolean)
314
+ <!-- * Because the `min` metadata doesn't include (for most countries anyway) the regular expressions that're required for phone number digits validation. -->
290
315
 
291
- * `mobile` — The complete metadata set for dealing with mobile numbers _only_, is about `95 kilobytes` in size (`libphonenumber-js/metadata.mobile.json`). Choose this when you need `max` metadata and when you _only_ accept mobile numbers. Other phone number types will still be parseable, but they won't be recognized as being "valid" (`.isValid()` will return `false`).
316
+ * `max` — The complete metadata set
317
+ * Is about `145 kilobytes` in size (`libphonenumber-js/metadata.max.json` file)
318
+ * Choose this when:
319
+ * The basic [`.isPossible()`](#ispossible-boolean) phone number length check is not enough for you and you need the strict phone number digits validation via [`.isValid()`](#isvalid-boolean)
320
+ * You need to detect phone number type — "fixed line", "mobile", etc — via [`.getType()`](#gettype-string)
292
321
 
293
- To use a particular metadata set, simply import functions from a relevant sub-package:
322
+ * `mobile` — The complete metadata set for dealing with mobile numbers _only_
323
+ * Is about `95 kilobytes` in size (`libphonenumber-js/metadata.mobile.json` file)
324
+ * Choose this when:
325
+ * You need `max` metadata capabilities and you _only_ accept mobile numbers
326
+ * It will still be able to handle non-mobile numbers just fine, with the only difference that `.isValid()` or `.isPossible()` might potentially return `false` for them, or `.getType()` might potentially return `undefined`.
294
327
 
295
- * `libphonenumber-js/max`
296
- * `libphonenumber-js/min`
297
- * `libphonenumber-js/mobile`
328
+ Choose one from the above and then simply `import` the functions from the relevant sub-package:
298
329
 
299
- Importing functions directly from `libphonenumber-js` effectively results in using the `min` metadata.
330
+ * `min` — `libphonenumber-js/min`, or just `libphonenumber-js`, since it's the default.
331
+ * `max` — `libphonenumber-js/max`
332
+ * `mobile` — `libphonenumber-js/mobile`
300
333
 
301
- Sometimes (rarely) not all countries are needed, and in those cases developers may want to [generate](#customizing-metadata) their own "custom" metadata set. For those cases, there's `libphonenumber-js/core` sub-package which doesn't come pre-packaged with any default metadata set and instead accepts metadata as the last argument of each exported function.
334
+ As for "custom" metadata, it could be used in those rare cases when not all countries are needed and a developer would really prefer to reduce the bundle size to a minimum. In that case, one could [generate](#customizing-metadata) their own "custom" metadata set and then `import` the functions from `libphonenumber-js/core` sub-package which doesn't come pre-packaged with any metadata and instead accepts `metadata` as the last argument of each exported function.
302
335
 
303
336
  ## Definitions
304
337
 
305
338
  ### Country code
306
339
 
307
- A "country code" is a [two-letter ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) (like `US`). <!-- or a special `001` country code used for ["non-geographic entities"](#non-geographic) (as per [Google's libphonenumber library](https://github.com/googlei18n/libphonenumber/blob/0068d861a68d3d4612f7bf8646ab844dd3cefce5/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java#L23-L24)). -->
340
+ A "country code" is a [two-letter ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) (like `"US"`). <!-- or a special `001` country code used for ["non-geographic entities"](#non-geographic) (as per [Google's libphonenumber library](https://github.com/googlei18n/libphonenumber/blob/0068d861a68d3d4612f7bf8646ab844dd3cefce5/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java#L23-L24)). -->
308
341
 
309
- This library supports all [officially assigned](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) ISO alpha-2 country codes, plus a few extra ones like: `AC` ([Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)), `TA` ([Tristan da Cunha](https://en.wikipedia.org/wiki/Tristan_da_Cunha)), `XK` ([Kosovo](https://en.wikipedia.org/wiki/Kosovo)).
342
+ This library supports all [officially assigned](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) ISO alpha-2 country codes, plus a few extra ones: `AC` ([Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)), `TA` ([Tristan da Cunha](https://en.wikipedia.org/wiki/Tristan_da_Cunha)), `XK` ([Kosovo](https://en.wikipedia.org/wiki/Kosovo)).
310
343
 
311
344
  <!--
312
345
  var countries = []
@@ -331,27 +364,33 @@ There're several calling codes that don't belong to any country:
331
364
  * `+888` — [United Nations Office for the Coordination of Humanitarian Affairs](https://en.wikipedia.org/wiki/United_Nations_Office_for_the_Coordination_of_Humanitarian_Affairs#International_Dialing_Code)
332
365
  * `+979` — [International Premium Rate Service](https://en.wikipedia.org/wiki/International_Premium_Rate_Service)
333
366
 
334
- Such phone numbering plans are called "non-geographic", and their phone numbers have `country` set to `undefined`.
367
+ Such phone numbering plans are called "non-geographic", and their phone numbers' `country` is `undefined`.
335
368
 
336
369
  ### National (significant) number
337
370
 
338
- "National (significant) number" are the national phone number digits (without "national prefix"). For example, `+1 213 373 4253` (or `(213) 373-4253` in national format) is a US phone number and its national (significant) number is `213 373 4253`. Another example is `+33 1 45 45 32 45` (or `01 45 45 32 45` in national format) which is a [French](https://en.wikipedia.org/wiki/Telephone_numbers_in_France) phone number where they add `0` "national prefix" when writing phone numbers in national format; in this case the national (significant) number is `1 45 45 32 45`.
371
+ "National (significant) number" is all national phone number digits, excluding the "national prefix". Examples:
372
+ * International number: `+1 213 373 4253`. Country: `"US"`. National number: `(213) 373-4253`. National (significant) number: `213 373 4253`
373
+ * Inetrnational number: `+33 1 45 45 32 45`. Country: `"FR"`. National number: `01 45 45 32 45`. Notice the `0` at the start of the national number — it's because in France they [add](https://en.wikipedia.org/wiki/Telephone_numbers_in_France) `0` "national prefix" when writing phone numbers in national format. National (significant) number: `1 45 45 32 45` (doesn't include the "national prefix").
339
374
 
340
375
  ### Country calling code
341
376
 
342
- "Country calling code" are the digits between the `+` and the national (significant) number when the number is written in international format. E.g. for US country calling code is `1` and for France it's `33`. Several countries can share the same "country calling code", e.g. [NANPA](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) countries like USA and Canada sharing the same `1` country calling code.
377
+ "Country calling code" is all digits between the `+` and the [national (significant) number](#national-significant-number) in a number that is written in international format. Examples:
378
+ * International number: `+1 213 373 4253`. Country: `"US"`. National (significant) number: `213 373 4253`. Country calling code: `1`
379
+ * Inetrnational number: `+33 1 45 45 32 45`. Country: `"FR"`. National (significant) number: `1 45 45 32 45`. Country calling code: `33`
380
+
381
+ Several countries could share the same "country calling code". For example, [NANPA](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) countries like USA and Canada share the same `1` country calling code.
343
382
 
344
383
  ## API
345
384
 
346
385
  ### parsePhoneNumber(string, defaultCountry?: string | options?: object): PhoneNumber
347
386
 
348
- Parses a phone number from `string`.
387
+ Parses a complete phone number from `string`.
349
388
 
350
- Can be imported both as a "default" export and as a "named" export `parsePhoneNumberFromString`.
389
+ Can be imported either as a "default" export or as a "named" export called `parsePhoneNumberFromString`.
351
390
 
352
391
  ```js
353
392
  import parsePhoneNumber from 'libphonenumber-js'
354
- // Or: import { parsePhoneNumberFromString as parsePhoneNumber } from 'libphonenumber-js'
393
+ // import { parsePhoneNumberFromString as parsePhoneNumber } from 'libphonenumber-js'
355
394
 
356
395
  const phoneNumber = parsePhoneNumber('(213) 373-42-53 ext. 1234', 'US')
357
396
  if (phoneNumber) {
@@ -359,7 +398,7 @@ if (phoneNumber) {
359
398
  }
360
399
  ```
361
400
 
362
- Returns an instance of [`PhoneNumber`](#phonenumber) class, or `undefined` if no phone number could be parsed: for example, when the string contains no phone number, or the phone number starts with a non-existent [country calling code](#country-calling-code), etc.
401
+ Returns an instance of [`PhoneNumber`](#phonenumber) class, or `undefined` if no phone number could be parsed from the input string. That could be for a number of reasons. For example, the phone number in the input string could be incomplete, or it could contain a phone number that has an invalid [country calling code](#country-calling-code), etc.
363
402
 
364
403
  Available `options`:
365
404
 
@@ -367,9 +406,9 @@ Available `options`:
367
406
 
368
407
  * `defaultCallingCode: string` — Default calling code for parsing numbers written in non-international form (without a `+` sign). Will be ignored when parsing numbers written in international form (with a `+` sign). It could be specified when parsing phone numbers belonging to ["non-geographic numbering plans"](#non-geographic) which by nature don't have a country code, making the `defaultCountry` option unusable.
369
408
 
370
- * `extract: boolean` — Defines the ["strictness"](#strictness) of parsing a phone number. By default, the `extract` flag is `true` meaning that it will attempt to extract the phone number from an input string like `"My phone number is (213) 373-4253 and my hair is blue"`. This could be thought of as "less strict" parsing. To make it "more strict", one could pass `extract: false` flag, in which case the function will attempt to parse the input string as if the whole string was a phone number. Applied to the example above, it would return `undefined` because the entire string is not a phone number, but for input string `"(213) 373-4253"` it would return a parsed `PhoneNumber`.
409
+ * `extract: boolean` — Defines the ["strictness"](#strictness) of parsing a phone number. By default, the `extract` flag is `true` meaning that it will attempt to extract the phone number from an input string like `"My phone number is (213) 373-4253 and my hair is blue"`. This could be thought of as "less strict" parsing. To make it "more strict", one could pass `extract: false` flag, in which case the function will attempt to parse the input string as if the whole string was a phone number. Applied to the example above, it would return `undefined` because the entire string is not a phone number, but for an input string `"(213) 373-4253"` it would return a parsed `PhoneNumber`.
371
410
 
372
- If a developer wants to know the exact reason why the phone number couldn't be parsed then they can use `parsePhoneNumberWithError()` function which throws the exact error:
411
+ If a developer wants to know the exact reason why the phone number couldn't be parsed then they can use `parsePhoneNumberWithError()` function which throws an exact error:
373
412
 
374
413
  ```js
375
414
  import { parsePhoneNumberWithError, ParseError } from 'libphonenumber-js'
@@ -406,12 +445,14 @@ try {
406
445
 
407
446
  #### Strictness
408
447
 
409
- By default, the parsing function will attempt to extract a phone number from an input string even in cases like `"Support: (213) 373-4253 (robot)"`, which mimicks the behavior of the original Google's `libphonenumber` library, and is the default behavior for legacy reasons. However, if "strict" input validation is required, one can pass `extract: false` flag to demand that the whole input string be a viable phone number.
448
+ By default, the parsing function will attempt to extract a phone number from the input string even in cases like `"Support: (213) 373-4253 (robot)"`, which mimicks the behavior of the original Google's `libphonenumber` library, and is the default behavior for legacy reasons. However, if "strict" input validation is required, one can pass `extract: false` flag to demand that the whole input string be a viable phone number.
410
449
 
411
450
  ```js
451
+ // By default it parses without `extract: false` flag.
452
+ // It will "extract" the phone number from the input string.
412
453
  parsePhoneNumber('Call: (213) 373-4253', 'US') === PhoneNumber
413
454
 
414
- // When parsing the same string with `extract: false` flag,
455
+ // When parsing the same input string with `extract: false` flag,
415
456
  // it will return `undefined`, because a phone number can't
416
457
  // contain letters or a colon.
417
458
  parsePhoneNumber('Call: (213) 373-4253', {
@@ -419,6 +460,8 @@ parsePhoneNumber('Call: (213) 373-4253', {
419
460
  extract: false
420
461
  }) === undefined
421
462
 
463
+ // When the whole input string represents a phone number,
464
+ // parsing it with `extract: false` flag will return a `PhoneNumber`.
422
465
  parsePhoneNumber('(213) 373-4253', {
423
466
  defaultCountry: 'US',
424
467
  extract: false
@@ -427,9 +470,9 @@ parsePhoneNumber('(213) 373-4253', {
427
470
 
428
471
  ### `PhoneNumber`
429
472
 
430
- `PhoneNumber` class represents a parsed phone number.
473
+ `PhoneNumber` class represents a complete phone number.
431
474
 
432
- `PhoneNumber` class instance is returned from [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function, or it could also be constructed manually from a parsed phone `number` in [`E.164`](https://en.wikipedia.org/wiki/E.164) format:
475
+ `PhoneNumber` class instance is returned from [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function, or it could also be constructed manually from a number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format:
433
476
 
434
477
  ```js
435
478
  const phoneNumber = new PhoneNumber('+12133734253')
@@ -464,7 +507,7 @@ const phoneNumber = new PhoneNumber('RU', '8005553535', metadata)
464
507
 
465
508
  #### `setExt(ext: string)`
466
509
 
467
- Sets a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)) of a phone number. Could be useful when formatting phone numbers stored as two separate fields: the phone number itself and the extension part.
510
+ Adds a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)) to a phone number.
468
511
 
469
512
  ```js
470
513
  const phone = "+12133734253"
@@ -482,7 +525,7 @@ if (phoneNumber) {
482
525
 
483
526
  #### `format(format: string, [options]): string`
484
527
 
485
- Formats the phone number into a string according to a `format`.
528
+ Formats the phone number into a string according to a specified `format`.
486
529
 
487
530
  Available `format`s:
488
531
 
@@ -494,9 +537,9 @@ Available `format`s:
494
537
 
495
538
  Available `options`:
496
539
 
497
- * `formatExtension(number, extension)` — Formats `number` and `extension` into a string. By default returns `${number} ext. ${extension}` for almost all countries with rare exceptions of some special cases like `${number} x${extension}` for UK.
540
+ * `formatExtension(number, extension)` — Formats `number` and `extension` into a string. By default returns `${number} ext. ${extension}` for almost all countries with rare exceptions of some special cases like `${number} x${extension}` for the United Kingdom.
498
541
 
499
- * `nationalPrefix: Boolean` — Some phone numbers can be formatted both with national prefix and without it. In such cases the library defaults to "with national prefix" (for legacy reasons). Pass `nationalPrefix: false` option to force formatting without national prefix in such cases.
542
+ * `nationalPrefix: Boolean` — Some phone numbers can be formatted both with national prefix and without it. In such cases the library defaults to "with national prefix" (for legacy reasons). Pass `nationalPrefix: false` option to force formatting without a national prefix.
500
543
 
501
544
  Examples:
502
545
 
@@ -509,7 +552,7 @@ phoneNumber.format("NATIONAL") === '(213) 373-4253'
509
552
  phoneNumber.format("INTERNATIONAL") === '+1 213 373 4253'
510
553
  phoneNumber.format("RFC3966") === 'tel:+12133734253'
511
554
 
512
- // Aliases
555
+ // The following are just convenience aliases for the `.format()` function.
513
556
  phoneNumber.formatNational() === phoneNumber.format("NATIONAL")
514
557
  phoneNumber.formatInternational() === phoneNumber.format("INTERNATIONAL")
515
558
  phoneNumber.getURI() === phoneNumber.format("RFC3966")
@@ -517,16 +560,16 @@ phoneNumber.getURI() === phoneNumber.format("RFC3966")
517
560
 
518
561
  #### `isPossible(): boolean`
519
562
 
520
- Checks if the phone number is "possible". Only checks the phone number length, doesn't check the actual phone number digits against any regular expressions.
563
+ Checks if the phone number is "possible". Only checks the phone number length. Doesn't check the actual phone number digits.
521
564
 
522
565
  #### `isValid(): boolean`
523
566
 
524
567
  Checks if the phone number is "valid". First checks the phone number length and then checks the phone number digits against all available regular expressions.
525
568
 
526
- By default the library uses "minimal" metadata which is only 75 kilobytes in size but also doesn't include the precise validation regular expressions resulting in less strict validation rules (some very basic validation like length check is still included for each country). If you don't mind the extra 65 kilobytes of metadata then use ["full" metadata](#min-vs-max-vs-mobile-vs-core) instead (140 kilobytes). Google's library always uses "full" metadata so it will yield different `isValidNumber()` results compared to the "minimal" metadata used by default in this library.
569
+ By default, this library uses `min` ("minimal") metadata which is only `80 kB` in size but also doesn't include the precise validation regular expressions resulting in less strict validation rules (some very basic validation like number length check is still included for each country). If you don't mind the extra `65 kB` of metadata then use [`max`](#min-vs-max-vs-mobile-vs-core) metadata instead of the default (`min`) one. Google's library always uses "full" metadata so it will yield different `isValidNumber()` results compared to the "minimal" metadata used by default in this library.
527
570
 
528
571
  <details>
529
- <summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code></summary>
572
+ <summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
530
573
 
531
574
  ####
532
575
  ```js
@@ -534,19 +577,22 @@ import parseMin from 'libphonenumber-js/min'
534
577
  import parseMax from 'libphonenumber-js/max'
535
578
  import parseMobile from 'libphonenumber-js/mobile'
536
579
 
537
- // Mobile numbers in Singapore starting from `8`
538
- // can only have the second digit in the range of `0..8`.
539
- // Here the second digit is `9` which makes it an invalid mobile number.
540
- // This is a "strict" (advanced) validation rule and is
541
- // not included in the (default) "min" bundle.
580
+ // In Singapore (+65), when a mobile number starts with `8`,
581
+ // it can only have the second digit in the range of `0..8`.
582
+ // In a number "+6589555555", the second digit is `9`
583
+ // which makes it an invalid mobile number.
584
+ // This is a "strict" validation rule and it is
585
+ // not included in the (default) `min` metadata.
542
586
 
543
- // The basic number length check passes (`8..11`) and the
544
- // "loose" national number validation regexp check passes too:
587
+ // When using the (default) `min` metadata,
588
+ // basic number length check passes (from `8` to `11`)
589
+ // and the "loose" national number validation regexp check passes too:
545
590
  // `(?:1\d{3}|[369]|7000|8(?:\d{2})?)\d{7}`.
546
591
  parseMin('+6589555555').isValid() === true
547
592
 
548
- // The "advanced" validation regexp for mobile numbers is
549
- // `(?:8[1-8]|9[0-8])\\d{6}` and possible lengths are `8`.
593
+ // When using `max` or `mobile` metadata,
594
+ // the validation regexp for mobile numbers is more precise —
595
+ // `(?:8[1-8]|9[0-8])\\d{6}` — as well as the possible lengths (only `8`).
550
596
  parseMax('+6589555555').isValid() === false
551
597
  parseMobile('+6589555555').isValid() === false
552
598
  ```
@@ -604,15 +650,15 @@ parsePhoneNumber('444 1 4444444444', 'TR').validateLength() === 'TOO_LONG'
604
650
 
605
651
  #### `getPossibleCountries(): string[]`
606
652
 
607
- Returns a list of countries this phone number could possibly belong to.
653
+ Returns a list of countries that this phone number could possibly belong to.
608
654
 
609
- Can be used when parsing complete international phone numbers containing a "calling code" that is shared between several countries. If parsing such a phone number returns `country: undefined` then `getPossibleCountries()` function could be used to somehow speculate about what country could this phone number possibly belong to.
655
+ Can be used when parsing a complete international phone number which contains a "country calling code" that is shared between several countries. If parsing such a phone number returns `country: undefined` then `getPossibleCountries()` function could be used to somehow speculate about what country could this phone number possibly belong to.
610
656
 
611
657
  #### `getType(): string?`
612
658
 
613
- Returns phone number type (fixed line, mobile, toll free, etc) or `undefined` (if the number is invalid or if there are no phone number type regular expressions for this country in metadata).
659
+ Returns phone number type — fixed line, mobile, toll free, etc — or `undefined` if the number is invalid or if there are no phone number type regular expressions for this country in metadata.
614
660
 
615
- By default the library uses "minimal" metadata which is only 75 kilobytes in size but also doesn't include the regular expressions for determining a specific phone number type (fixed line, mobile, toll free, etc) resulting in `getType()` returning `undefined` for most countries. If you don't mind the extra 65 kilobytes of metadata then use ["full" metadata](#min-vs-max-vs-mobile-vs-core) instead (140 kilobytes). Google's library always uses "full" metadata so it will yield different `getNumberType()` results compared to the "minimal" metadata used by default in this library.
661
+ By default, this library uses `min` ("minimal") metadata which is only `80 kB` in size but also doesn't include the regular expressions for determining a phone number type — fixed line, mobile, toll free, etc — which could result in `getType()` returning `undefined` for most countries. If you don't mind the extra `65 kB` of metadata then use [`max`](#min-vs-max-vs-mobile-vs-core) metadata instead of the default (`min`) one. Google's library always uses "full" metadata so it will yield different `getNumberType()` results compared to the "minimal" metadata used by default in this library.
616
662
 
617
663
  <details>
618
664
  <summary>The list of possible return values</summary>
@@ -635,7 +681,7 @@ By default the library uses "minimal" metadata which is only 75 kilobytes in siz
635
681
  ####
636
682
 
637
683
  <details>
638
- <summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code></summary>
684
+ <summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
639
685
 
640
686
  ####
641
687
  ```js
@@ -643,20 +689,19 @@ import parseMin from 'libphonenumber-js/min'
643
689
  import parseMax from 'libphonenumber-js/max'
644
690
  import parseMobile from 'libphonenumber-js/mobile'
645
691
 
646
- // Singapore valid mobile number.
692
+ // Singapore (+65) mobile number "+6584655555".
647
693
 
648
- // The (default) "min" bundle doesn't contain any regexps for
649
- // getting phone number type based on national number (for Singapore).
694
+ // The (default) `min` bundle doesn't contain any regexps for
695
+ // getting phone number type from phone number digits for Singapore.
650
696
  parseMin('+6584655555').getType() === undefined
651
697
 
652
- // The "max" bundle contains regexps for
653
- // getting phone number type based on national number
654
- // for all possible phone number types.
698
+ // The `max` does contain all the regexps for getting
699
+ // phone number type from phone number digits for any country.
655
700
  parseMax('+6584655555').getType() === 'MOBILE'
656
701
 
657
- // The "mobile" bundle contains regexps for
658
- // getting phone number type based on national number
659
- // for mobile phone numbers only.
702
+
703
+ // The `max` does contain all the regexps for getting
704
+ // mobile phone number type from mobile phone number digits for any country.
660
705
  parseMobile('+6584655555').getType() === 'MOBILE'
661
706
  ```
662
707
  </details>
@@ -701,15 +746,15 @@ See ["Using phone number validation feature"](#using-phone-number-validation-fea
701
746
 
702
747
  ### `isValidPhoneNumberForCountry(input: string, country: string): boolean`
703
748
 
704
- Same as `isValidPhoneNumber()` but with the "default country" argument being an "exact country" instead.
749
+ Same as `isValidPhoneNumber()` but with the "default country" argument being replaced with an "exact country", which is more strict.
705
750
 
706
- This function is not currently exported from this library. The reason is that its result would be too vague when `false` is returned — it could mean any of:
751
+ This function is not currently exported from this library. The reason is that its result would be too vague when it returns `false` — it could mean any of:
707
752
 
708
753
  * The input is not a valid phone number.
709
754
  * The input is a valid phone number but it belongs to another country.
710
755
  * The input is a phone number that belongs to the correct country but is not valid.
711
756
 
712
- At least the second case should be handled separately from a "User Experience" point of view: if the user has input a valid phone number but for another country, they should be notified that "the country is incorrect" rather than that "the phone number is incorrect", otherwise it would be bad UX design.
757
+ At least the second case should be handled separately from a "User Experience" point of view: if the user has input a valid phone number but for another country, they should be notified that "perhaps you meant another country" rather than just throwing "the phone number is incorrect" error in their face.
713
758
 
714
759
  But for those who'd still like to have such function, here's a possible implementation for it:
715
760
 
@@ -770,7 +815,7 @@ validatePhoneNumberLength('444 1 4444444444', 'TR') === 'TOO_LONG'
770
815
 
771
816
  For the description of the `defaultCountry?: string | options?: object` argument, see [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function description.
772
817
 
773
- This function is just a more detailed version of `isPossiblePhoneNumber()` for those who've [asked](https://github.com/catamphetamine/libphonenumber-js/issues/406) for a more specific rejection reason.
818
+ This function is just a more detailed version of `isPossiblePhoneNumber()` for those who [asked](https://github.com/catamphetamine/libphonenumber-js/issues/406) for a more specific rejection reason.
774
819
 
775
820
  <!-- This function is just a shortcut for a two-step process of ["strictly"](#strictness) parsing a phone number and then calling `.validateLength()`. -->
776
821
 
@@ -778,7 +823,7 @@ The phone number is parsed ["strictly"](#strictness) from the input string.
778
823
 
779
824
  ### `class` AsYouType(defaultCountry?: string | options?: object)
780
825
 
781
- Creates a formatter for a partially entered phone number.
826
+ Creates a formatter for a partially-entered phone number.
782
827
 
783
828
  For the description of the `defaultCountry?: string | options?: object` argument, see [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function description.
784
829
 
@@ -795,13 +840,13 @@ new AsYouType('US').input('2133734') === '(213) 373-4'
795
840
 
796
841
  The formatter instance also provides the following getters:
797
842
 
798
- * `getNumber(): PhoneNumber?` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been set and the user enters a phone number not in international format.
843
+ * `getNumber(): PhoneNumber?` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been specified and the user enters a phone number in national format (without a `+`).
799
844
 
800
- * `getNumberValue(): string?` — Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when inputting a phone number in national format and no default country or default "country calling code" have been set.
845
+ * `getNumberValue(): string?` — Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for default country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when the user inputs a phone number in national format (without a `+`) and no default country or default "country calling code" have been specified.
801
846
 
802
- * `getChars(): string` — Returns the phone number characters entered by the user: digits and a `+` sign (if present). Returns an empty string if no phone number characters have been input.
847
+ * `getChars(): string` — Returns the phone number characters: `+` sign (if present) and the digits. Returns an empty string if no phone number characters have been input.
803
848
 
804
- * `getTemplate(): string` — Returns the template used to format the phone number characters (digits and a `+` sign, if present), which are denoted by `x`-es. Returns an empty string if no phone number characters have been input.
849
+ * `getTemplate(): string` — Returns the template that is used to format the phone number characters — `+` sign (if present) and the digits — which are designated by `x`-es. Returns an empty string if no phone number characters have been input.
805
850
 
806
851
  ```js
807
852
  // National phone number input example.
@@ -844,9 +889,9 @@ asYouType.getTemplate() === 'xx xxx xxx xxxx'
844
889
 
845
890
  * `getCountry(): string?` — Returns a two-letter [country code](#country-code) of the phone number. Returns `undefined` for ["non-geographic"](#non-geographic) phone numbering plans. Returns `undefined` if no phone number has been input yet, or if it couldn't tell what country the phone number belongs to — that could happen when several countries have the same "country calling code" and the phone number doesn't look like it belongs to any particular one of them.
846
891
 
847
- * `isPossible(): boolean` — Returns `true` if the phone number is "possible". Is just a shortcut for [`PhoneNumber.isPossible()`](#ispossible-boolean).
892
+ * `isPossible(): boolean` — Returns `true` if the phone number is "possible". Is just a shortcut for [`getNumber()?.isPossible()`](#ispossible-boolean).
848
893
 
849
- * `isValid(): boolean` — Returns `true` if the phone number is "valid". Is just a shortcut for [`PhoneNumber.isValid()`](#isvalid-boolean).
894
+ * `isValid(): boolean` — Returns `true` if the phone number is "valid". Is just a shortcut for [`getNumber()?.isValid()`](#isvalid-boolean).
850
895
 
851
896
  <details>
852
897
  <summary>Legacy API (before version <code>1.6.0</code>)</summary>
@@ -889,13 +934,13 @@ asYouType.getNationalNumber() === '2133734253'
889
934
 
890
935
  ####
891
936
 
892
- "As You Type" formatter was created by Google as part of their Android OS and therefore only works for numerical keyboard input, i.e. it can only accept digits (and a `+` sign in the start of an international number). When used on desktops where a user can input all kinds of punctuation (spaces, dashes, parens, etc) it simply ignores everything except digits (and a `+` sign in the start of an international number).
937
+ "As You Type" formatter was created by Google as part of their Android OS and therefore only works for numerical keyboard input, i.e. it can only accept digits (and a `+` sign at the start of an international number). When used on desktops where a user can input all kinds of punctuation (spaces, dashes, parens, etc) it simply ignores everything except digits (and a `+` sign at the start of an international number).
893
938
 
894
- Google's "As You Type" formatter does not support entering phone number extensions. If your project requires phone number extensions input then use a separate input field for that.
939
+ Google's "As You Type" formatter does not support entering phone number extensions. If your project requires inputting phone number extensions then use a separate input field for that.
895
940
 
896
941
  ### findPhoneNumbersInText(text: string, defaultCountry?: string | options?: object): object[]
897
942
 
898
- Searches for phone numbers in `text`.
943
+ Searches for complete phone numbers in `text`.
899
944
 
900
945
  Available `options`:
901
946
 
@@ -971,7 +1016,7 @@ findNumbers(`
971
1016
 
972
1017
  ####
973
1018
 
974
- By default it processes the whole text and then outputs the phone numbers found. If the text is very big (say, a hundred thousand characters) then it might freeze the user interface for a couple of seconds. To avoid such lags one can employ "iterator" approach using `searchPhoneNumbersInText()` to perform the search asynchronously (e.g. using `requestIdleCallback` or `requestAnimationFrame`).
1019
+ By default it processes the whole text and then outputs the phone numbers found. If the text is very big (say, a hundred thousand characters) then it might freeze the user interface for a couple of seconds. To avoid such lags one could employ "iterator" approach using `searchPhoneNumbersInText()` to perform the search asynchronously (e.g. using `requestIdleCallback` or `requestAnimationFrame`).
975
1020
 
976
1021
  (in previous versions, it was called `searchNumbers()`)
977
1022
 
@@ -1029,7 +1074,7 @@ iteration()
1029
1074
 
1030
1075
  ####
1031
1076
 
1032
- Although Google's javascript port doesn't have the `findPhoneNumbersInText()` functionality the Java and C++ ports do. I guess Google just doesn't need to crawl phone numbers on Node.js because they can afford to hire a Java/C++ developer to do that. Still, javascript nowadays is the most popular programming language given its simplicity and user-friendliness. The `findPhoneNumbersInText()` function provided is a port of Google's `PhoneNumberMatcher.java` into javascript.
1077
+ Although Google's javascript port doesn't provide the `findPhoneNumbersInText()` function, the Java and C++ ports do. I guess that's because Google just doesn't need to crawl phone numbers on Node.js because they do it in Java or C++. Still, javascript nowadays is the most popular programming language given its simplicity and user-friendliness. The `findPhoneNumbersInText()` function provided by this library is a port of Google's `PhoneNumberMatcher.java` into javascript.
1033
1078
 
1034
1079
  ### getExampleNumber(country: string, examples: object): PhoneNumber
1035
1080
 
@@ -1100,11 +1145,11 @@ parseIncompletePhoneNumber('+7 800 555') === '+7800555'
1100
1145
  parseIncompletePhoneNumber('+٤٤٢٣٢٣٢٣٤') === '+442323234'
1101
1146
  ```
1102
1147
 
1103
- ### parsePhoneNumberCharacter(nextCharacter, prevParsedCharacters, emitEvent?)
1148
+ ### parsePhoneNumberCharacter(character, prevParsedCharacters?, eventListener?)
1104
1149
 
1105
1150
  Parses next character of an input string while parsing phone number digits (including a `+`) from that string. Basically, it discards everything except `+` and digits, and `+` is only allowed at the start of a phone number.
1106
1151
 
1107
- This function is a low-level one that is currently only used in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) with [`input-format`](https://gitlab.com/catamphetamine/input-format). Frankly speaking, that's the only reason why this function is exported. Other developers should just ignore it and use `parseIncompletePhoneNumber()` instead because it's much simpler.
1152
+ This function is a low-level one that is currently only used in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) with [`input-format`](https://gitlab.com/catamphetamine/input-format). Frankly speaking, that's the only reason why this function is exported. Other developers, perhaps, should just ignore it and use `parseIncompletePhoneNumber()` instead because it's much simpler.
1108
1153
 
1109
1154
  ```js
1110
1155
  // Suppose a user inputs a "+1 (213) 373-42-53" string
@@ -1132,7 +1177,7 @@ parsePhoneNumberCharacter('3', '+1213373425') === '3'
1132
1177
 
1133
1178
  So basically, it's the same as `parseIncompletePhoneNumber()` with the only difference that it operates at a character-by-character level rather than at a string-as-a-whole level.
1134
1179
 
1135
- The optional `emitEvent` argument is a function of `(eventName: string)` arguments. It will be called in a situation when the application should stop parsing the input string. Currently, the only situation when that could happen is when it encounters an "out-of-place" `+` character. For example, when parsing a `"+1 (234) + 56-78"` string it would emit an `"end"` event at the second `+` character so that the application would return `"+1234"` rather than `"+12345678"`.
1180
+ The optional `eventListener` argument is a function of `eventName: string` argument. It will be called with an `"end"` argument in a situation when the application should stop parsing the input string. Currently, the only situation when that could happen is when it encounters an "out-of-place" `+` character. For example, when parsing a `"+1 (234) + 56-78"` string, it would emit an `"end"` event at the second `+` character so that the application could return `"+1234"` rather than `"+12345678"`.
1136
1181
 
1137
1182
  ### formatIncompletePhoneNumber(value: string, defaultCountry?: string | options?: object): string
1138
1183
 
@@ -1368,7 +1413,7 @@ formatNumber({}) throws Error
1368
1413
 
1369
1414
  _(legacy API)_
1370
1415
 
1371
- See the description for [`PhoneNumber.getType()`](#gettype).
1416
+ See the description for [`PhoneNumber.getType()`](#gettype-string).
1372
1417
 
1373
1418
  The `number` argument can be either a result of the `parseNumber()` function call — `{ country, phone }` — or a string (phone number digits only) possibly accompanied with the second `defaultCountry` argument.
1374
1419
 
@@ -1439,9 +1484,9 @@ isValidNumberForRegion('07624369230', 'IM') === true
1439
1484
 
1440
1485
  ## Using phone number validation feature
1441
1486
 
1442
- I personally don't use strict phone number validation in my projects. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`PhoneNumber.isValid()`](#isvalid) function may one day become outdated on a website that isn't actively maintained anymore. Imagine a "promo-site" or a "personal website" being deployed once and then running for years without any maintenance, where a client may be unable to submit a simple "Contact Us" form just because this newly allocated pool of mobile phone numbers wasn't present in that old version of `libphonenumber-js` bundled in it.
1487
+ I personally don't use strict phone number validation feature. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`PhoneNumber.isValid()`](#isvalid-boolean) function may one day become outdated on a website that isn't actively maintained anymore. Imagine a "promo-site" or a "personal website" being deployed once and then running for years without any maintenance, where a client may be unable to submit a simple "Contact Us" form just because this newly-allocated pool of mobile phone numbers wasn't present in that old version of `libphonenumber-js` that was used when building the website.
1443
1488
 
1444
- Whenever there's a "business requirement" to validate a phone number that's being input by a user, I prefer using [`PhoneNumber.isPossible()`](#ispossible) instead of [`PhoneNumber.isValid()`](#isvalid), so that it just validates the phone number length, and doesn't validate the actual phone number digits. But it doesn't mean that you shouldn't use [`PhoneNumber.isValid()`](#isvalid) — maybe in your case it would make sense.
1489
+ Whenever there's a "business requirement" to validate phone number input, I prefer using [`PhoneNumber.isPossible()`](#ispossible-boolean) instead of [`PhoneNumber.isValid()`](#isvalid-boolean), so that it just validates the phone number length and doesn't validate the actual phone number digits. But it doesn't mean that you shouldn't use [`PhoneNumber.isValid()`](#isvalid-boolean) — maybe in your case it would make sense.
1445
1490
 
1446
1491
  ## React
1447
1492
 
@@ -1455,33 +1500,35 @@ This package currently [doesn't provide](https://gitlab.com/catamphetamine/libph
1455
1500
 
1456
1501
  ## Bug reporting
1457
1502
 
1458
- This library is a simpler rewrite of Google's [`libphonenumber`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/) library. Basically, it mimicks Google's library behavior. Hence, as long as this library's [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) and Google's library [demo page](https://libphonenumber.appspot.com/) exhibit the same behavior, it's not considered a bug.
1503
+ This library is a simpler rewrite of Google's [`libphonenumber`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/) library. Basically, it mimicks Google's library behavior. Hence, as long as this library's [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) and Google's library [demo page](https://libphonenumber.appspot.com/) exhibit the same behavior, it's not considered a bug, regardless of whether that behavior contradicts any new changes in telephone numbering plans, etc. In that case, your only option would be to [report the issue to Google](https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md) directly.
1459
1504
 
1460
- When reporting issues with:
1505
+ That being said, if the two demo pages exhibit different behavior and it's not caused by the intentional [differences](#differences-from-googles-libphonenumber), then it would be considered a valid case for reporting an issue.
1461
1506
 
1462
- * Parsing
1463
- * Validation
1464
- * Formatting
1465
- * "As You Type" formatting
1507
+ When reporting issues related to:
1508
+
1509
+ * Parsing numbers
1510
+ * Validating numbers
1511
+ * Formatting numbers
1512
+ * "As You Type" formatter
1466
1513
 
1467
1514
  one must provide two links:
1468
1515
 
1469
1516
  * A link to the [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) illustrating the supposedly incorrect behavior.
1470
1517
  * A link to [Google's `libphonenumber` demo page](https://libphonenumber.appspot.com/) illustrating the expected behavior.
1471
1518
 
1472
- Google's demo page [outputs four sections](https://libphonenumber.appspot.com/phonenumberparser?number=438331999&country=AU):
1519
+ Google's demo page output is divided into [four sections](https://libphonenumber.appspot.com/phonenumberparser?number=438331999&country=AU):
1473
1520
  * "Parsing Result"
1474
1521
  * "Validation Results"
1475
1522
  * "Formatting Results"
1476
1523
  * "AsYouTypeFormatter Results"
1477
1524
 
1478
- Choose the relevant section of those four corresponding to the type of the bug you're reporting.
1525
+ Depending the type of bug you're reporting, mention the relevant section of the demo page output in your ticket.
1479
1526
 
1480
- In a bug report:
1527
+ When writing a bug report:
1481
1528
 
1482
1529
  * First, describe the observed `libphonenumber-js` demo result (and provide a link to it).
1483
1530
  * Second, describe the observed Google's demo result (and provide a link to it).
1484
- * The described observed result of Google's demo must be different from the described observed result of `libphonenumber-js` demo, otherwise it's not considered a bug. If you don't agree with Google's demo result then create an issue in [Google's repo](https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md) instead. If they fix it in their library, I'll port the fix to this library.
1531
+ * The described observed result of Google's demo must be different from the described observed result of `libphonenumber-js` demo, otherwise it's not considered a bug. If you don't agree with Google's demo result then [report it to Google](https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md) directly instead. If they fix it in their library, I'll port the fix to this library.
1485
1532
 
1486
1533
  <!--
1487
1534
  Phone number validation bugs should **only** be reported if they appear when using [custom metadata functions](#customizing-metadata) fed with `metadata.max.json` because by default all functions in this library use the reduced metadata set which results in looser validation than the original Google `libphonenumber`'s. The [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) also uses the reduced metadata set and therefore its validation is also looser than the original Google `libphonenumber`'s.
@@ -1491,17 +1538,13 @@ There is also a possibility of this library's demo metadata being outdated, or t
1491
1538
 
1492
1539
  <!-- When reporting `findPhoneNumbersInText()` bugs, one should know that `findPhoneNumbersInText()` code was ported from [Google's Java code](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java). I didn't write it myself, I just ported it. Therefore, it is unlikely that anyone other than Google would fix any bugs. -->
1493
1540
 
1494
- ## TypeScript
1495
-
1496
- This library comes with TypeScript "typings". If you happen to find any bugs in those, create an issue.
1497
-
1498
1541
  <!--
1499
1542
  https://codesandbox.io/s/damp-fast-26c7z?file=/src/index.ts
1500
1543
  -->
1501
1544
 
1502
1545
  ## CDN
1503
1546
 
1504
- One can use any npm CDN service, e.g. [unpkg.com](https://unpkg.com) or [jsdelivr.com](https://jsdelivr.com)
1547
+ To include this library directly via a `<script/>` tag on a page, one can use any npm CDN service, e.g. [unpkg.com](https://unpkg.com) or [jsdelivr.com](https://jsdelivr.com)
1505
1548
 
1506
1549
  ```html
1507
1550
  <script src="https://unpkg.com/libphonenumber-js@[version]/bundle/libphonenumber-[type].js"></script>
@@ -1589,7 +1632,7 @@ import min from 'libphonenumber-js/min/metadata'
1589
1632
  const metadata = new Metadata(min)
1590
1633
  ```
1591
1634
 
1592
- As one can see, the [`Metadata` class](https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/source/metadata.js) is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input/-/blob/master/source/helpers/phoneInputHelpers.js) to get "leading digits" for a country, or to get maximum phone number length for a country. Stick to the methods documented above, don't call any other methods. If you think there's a need to call any other methods, create a discussion issue.
1635
+ As one can see, the [`Metadata` class](https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/source/metadata.js) is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input/-/blob/master/source/helpers/phoneInputHelpers.js) to get the "leading digits" for a country, or to get the maximum phone number length for a country. Stick to the methods documented above and don't call any other methods. If you think there's a need to call any other methods not mentioned above, create an issue with a discussion.
1593
1636
 
1594
1637
  <!--
1595
1638
  Currently I have a script set up monitoring changes to `PhoneNumberMetadata.xml` in Google's repo and automatically releasing new versions of this library when metadata in Google's repo gets updated. So this library's metadata is supposed to be up-to-date. Still, in case the automatic metadata update script malfunctions some day, anyone can request metadata update via a Pull Request here on GitHub:
@@ -1612,12 +1655,12 @@ Alternatively, a developer may wish to update metadata urgently, without waiting
1612
1655
 
1613
1656
  This library comes prepackaged with [three types of metadata](#min-vs-max-vs-mobile-vs-core).
1614
1657
 
1615
- Sometimes, if only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 kilobytes (even when including all regular expressions for validating phone number digits and detecting phone number type), then they can generate such custom metadata and pass it as the last argument to this library's "core" functions.
1658
+ Sometimes, if only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 kilobytes, while still including all regular expressions for validating phone number digits and detecting phone number type, then they can generate such custom metadata and pass it as the last argument to this library's "core" functions.
1616
1659
 
1617
- See [generate custom metadata instructions](https://gitlab.com/catamphetamine/libphonenumber-metadata-generator).
1660
+ See [generate custom metadata](https://gitlab.com/catamphetamine/libphonenumber-metadata-generator) instructions.
1618
1661
 
1619
1662
  <details>
1620
- <summary>Then, use the generated <code>metadata.custom.json</code> file with the "core" functions.</summary>
1663
+ <summary>How to use the generated <code>metadata.custom.json</code> file with the "core" functions.</summary>
1621
1664
 
1622
1665
  ####
1623
1666
 
@@ -1753,7 +1796,9 @@ exports.AsYouType.prototype.constructor = exports.AsYouType
1753
1796
  ```
1754
1797
  </details>
1755
1798
 
1756
- Metadata should be re-generated each time the project is being deployed because Google constantly updates their metadata.
1799
+ ######
1800
+
1801
+ Note that if you'll be using your own "custom" metadata then you're responsible for keeping it up-to-date because Google regularly updates their metadata.
1757
1802
 
1758
1803
  <!-- ## To do -->
1759
1804
 
@@ -1814,55 +1859,58 @@ launchctl list | grep 'libphonenumber-js'
1814
1859
 
1815
1860
  ### Maintenance
1816
1861
 
1817
- Google periodically releases new metadata with the changes described in the [release notes](https://github.com/googlei18n/libphonenumber/blob/master/release_notes.txt). Sometimes those are minor non-breaking updates, sometimes those are major-version breaking updates.
1862
+ Google periodically releases new metadata with the changes described in their [release notes](https://github.com/googlei18n/libphonenumber/blob/master/release_notes.txt). Most of the times, those are minor non-breaking updates. Rarely, those could be major-version breaking changes.
1863
+
1864
+ After Google does that, this library pulls the updated metadata from Google's repository and publishes a new version of itself on `npm`.
1865
+
1866
+ The metadata pulling process is automated through an "autoupdate" script: see `autoupdate.cmd` (Windows) or `autoupdate.sh` (Linux/macOS). The script detects changes to `PhoneNumberMetadata.xml` file in Google `libphonenumber`'s repo and, if there are any changes, it pulls the latest metadata, transforms it, commits the changes to the repository, builds a new version of the package and releases it to `npm`.
1818
1867
 
1819
- Metadata update process is automated through an "autoupdate" script: see `autoupdate.cmd` (Windows) or `autoupdate.sh` (Linux/macOS). The script detects changes to `PhoneNumberMetadata.xml` in Google `libphonenumber`'s repo and if there are changes then it pulls the latest metadata, processes it, commits the changes to GitHub, builds a new version of the library and releases it to NPM. So this library's metadata is supposed to be up-to-date. I could set up this script to run automatically but on my Windows machine `ssh-agent` doesn't work properly so I run the "autoupdate" script manually from time to time.
1868
+ I did attempt to set up the autoupdate script to run dialy on my Windows machine in an automatic fashion through "Task Scheduler", and even overcame the issue of `ssh-agent` asking for a password input every time when running `git` command, but then `npm` started requiring "two-factor authentication" in order to publish a package, which requires human intervention, so the autoupdate process can't really be 100% autonomous and automatic and has to be run by a human's hand every now-and-then.
1820
1869
 
1821
- Also Google sometimes (very rarely) updates their code:
1870
+ So I just run the "autoupdate" script manually from time to time. With this workflow, one can see how the metadata could potentially get a bit stale, in which case just ping me to re-run the autoupdate script, assuming I'm still alive and well.
1822
1871
 
1823
- * [`phonenumberutil.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js) — `parseNumber()`, `formatNumber()`, `isValidNumber()`, `getNumberType()`
1824
- * [`AsYouTypeFormatter.java`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java) — `AsYouType`
1825
- * [`PhoneNumberMatcher.java`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) — `findPhoneNumbersInText()`
1872
+ Also Google sometimes (extremely rarely) updates their code:
1826
1873
 
1827
- The latest sync-up was on Aug 14th, 2024.
1874
+ * [`phonenumberutil.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js) — is mirrored as functions: `parseNumber()`, `formatNumber()`, `isValidNumber()`, `getNumberType()`
1875
+ * [`AsYouTypeFormatter.java`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java) — is mirrored as `AsYouType` class
1876
+ * [`PhoneNumberMatcher.java`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) — is mirrored as `findPhoneNumbersInText()` function
1877
+
1878
+ The latest sync-up with Google's code was on Sep 19th, 2025.
1828
1879
 
1829
1880
  ## Contributing
1830
1881
 
1831
- After cloning this repo, ensure dependencies are installed by running:
1882
+ After cloning this repo, install the dependencies:
1832
1883
 
1833
1884
  ```sh
1834
1885
  npm install
1835
1886
  ```
1836
1887
 
1837
- This module is written in ES6 and uses [Babel](http://babeljs.io/) for ES5
1838
- transpilation. Widely consumable JavaScript can be produced by running:
1888
+ This library is written in ES6 and uses [Babel](http://babeljs.io/) for ES5
1889
+ transpilation during the "build" step:
1839
1890
 
1840
1891
  ```sh
1841
1892
  npm run build
1842
1893
  ```
1843
1894
 
1844
- Once `npm run build` has run, you may `import` or `require()` directly from
1845
- node.
1846
-
1847
- After developing, the full test suite can be evaluated by running:
1895
+ After making any code changes, run the tests:
1848
1896
 
1849
1897
  ```sh
1850
1898
  npm test
1851
1899
  ```
1852
1900
 
1853
- Test coverage was meant to remain at 100% but then the coverage analysis tools got buggy and didn't report the coverage correctly:
1901
+ Aside from all tests passing, it should also remain at 100% of code coverage. At some point, the coverage analysis tools got buggy and stopped reporting the coverage correctly. Anyway, a code coverage report could be generated by running the following command:
1854
1902
 
1855
1903
  ```sh
1856
1904
  npm run test-coverage
1857
1905
  ```
1858
1906
 
1859
- When you're ready to test your new functionality on a real project, you can run
1907
+ To test the updated code on a real project before publishing a new release, one could use "pack-and-install" trick to simulate publishing a new release.
1860
1908
 
1861
1909
  ```sh
1862
1910
  npm pack
1863
1911
  ```
1864
1912
 
1865
- It will `build`, `test` and then create a `.tgz` archive which you can then install in your project folder
1913
+ It will `build`, `test` and then create a `.tgz` archive that can be installed from a project folder just like a normal `npm` package.
1866
1914
 
1867
1915
  ```sh
1868
1916
  npm install [module name with version].tar.gz
@@ -1886,16 +1934,19 @@ npm run test-coverage
1886
1934
 
1887
1935
  The code coverage report can be viewed by opening `./coverage/lcov-report/index.html`.
1888
1936
 
1937
+ At some point, the coverage analysis tools got buggy and stopped reporting the coverage correctly.
1938
+
1889
1939
  ## Advertisement
1890
1940
 
1891
1941
  If you like this library then you might also like:
1892
1942
 
1893
1943
  * [`react-phone-number-input`](https://npmjs.com/package/react-phone-number-input) — A `React` component for phone number input.
1894
1944
  * [`javascript-time-ago`](https://npmjs.com/package/javascript-time-ago) — An international human-readable past or future date formatter. Example: `"2 days ago"`.
1895
- * [`react-time-ago`](https://npmjs.com/package/react-time-ago) — A `React` component for international human-readable formatting of past or future dates. Example: `"2 days ago"`.
1945
+ * [`react-time-ago`](https://npmjs.com/package/react-time-ago) — A `React` component for international human-readable past or future date formatter. Example: `"2 days ago"`.
1896
1946
  * [`read-excel-file`](https://www.npmjs.com/package/read-excel-file) — A simple and easy-to-use `*.xlsx` file reader (client-side or server-side).
1897
1947
  * [`write-excel-file`](https://www.npmjs.com/package/write-excel-file) — A simple and easy-to-use `*.xlsx` file writer (client-side or server-side).
1898
1948
  * [`flexible-json-schema`](https://www.npmjs.com/package/flexible-json-schema) — A simple and easy-to-use `*.json` schema data validator / parser.
1949
+ * [`navigation-stack`](https://www.npmjs.com/package/navigation-stack) — Navigation in a Single-Page Application.
1899
1950
  * [`virtual-scroller`](https://www.npmjs.com/package/virtual-scroller) — A universal implementation of a "virtual scroller" infinite list scrolling component: only renders the rows that fit the screen bounds.
1900
1951
 
1901
1952
  ## License