libphonenumber-js 1.12.39 → 1.12.41

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 (183) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/bundle/libphonenumber-js.min.js +1 -1
  3. package/bundle/libphonenumber-js.min.js.map +1 -1
  4. package/bundle/libphonenumber-max.js +1 -1
  5. package/bundle/libphonenumber-max.js.map +1 -1
  6. package/bundle/libphonenumber-min.js +1 -1
  7. package/bundle/libphonenumber-min.js.map +1 -1
  8. package/bundle/libphonenumber-mobile.js +1 -1
  9. package/bundle/libphonenumber-mobile.js.map +1 -1
  10. package/max/metadata/metadata.max.json +1 -1
  11. package/max/metadata/metadata.max.json.js +1 -1
  12. package/metadata.full.json +1 -1
  13. package/metadata.full.json.js +1 -1
  14. package/metadata.max.json +1 -1
  15. package/metadata.max.json.js +1 -1
  16. package/metadata.min.json +1 -1
  17. package/metadata.min.json.js +1 -1
  18. package/metadata.mobile.json +1 -1
  19. package/metadata.mobile.json.js +1 -1
  20. package/min/metadata/metadata.min.json +1 -1
  21. package/min/metadata/metadata.min.json.js +1 -1
  22. package/mobile/metadata/metadata.mobile.json +1 -1
  23. package/mobile/metadata/metadata.mobile.json.js +1 -1
  24. package/package.json +1 -1
  25. package/.babelrc +0 -21
  26. package/.nycrc +0 -27
  27. package/METADATA.md +0 -155
  28. package/autoupdate.cmd +0 -6
  29. package/autoupdate.sh +0 -16
  30. package/build/AsYouType.test.js +0 -1339
  31. package/build/AsYouTypeFormatter.PatternMatcher.test.js +0 -502
  32. package/build/AsYouTypeFormatter.PatternParser.test.js +0 -49
  33. package/build/AsYouTypeFormatter.util.test.js +0 -24
  34. package/build/PhoneNumber.test.js +0 -141
  35. package/build/PhoneNumberMatcher.test.js +0 -122
  36. package/build/findNumbers/util.test.js +0 -34
  37. package/build/findPhoneNumbersInText.test.js +0 -300
  38. package/build/format.test.js +0 -339
  39. package/build/formatIncompletePhoneNumber.test.js +0 -33
  40. package/build/getCountries.test.js +0 -13
  41. package/build/getCountryCallingCode.test.js +0 -18
  42. package/build/getExampleNumber.test.js +0 -21
  43. package/build/helpers/RFC3966.test.js +0 -47
  44. package/build/helpers/applyInternationalSeparatorStyle.test.js +0 -13
  45. package/build/helpers/checkNumberLength.test.js +0 -81
  46. package/build/helpers/extractCountryCallingCode.test.js +0 -23
  47. package/build/helpers/extractNationalNumber.test.js +0 -27
  48. package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +0 -20
  49. package/build/helpers/extractPhoneContext.test.js +0 -71
  50. package/build/helpers/getNumberType.test.js +0 -23
  51. package/build/helpers/matchesEntirely.test.js +0 -16
  52. package/build/helpers/mergeArrays.test.js +0 -12
  53. package/build/helpers/parseDigits.test.js +0 -12
  54. package/build/helpers/stripIddPrefix.test.js +0 -22
  55. package/build/isPossible.test.js +0 -121
  56. package/build/isPossiblePhoneNumber.test.js +0 -42
  57. package/build/isValid.test.js +0 -144
  58. package/build/isValidPhoneNumber.test.js +0 -29
  59. package/build/legacy/findNumbers.test.js +0 -210
  60. package/build/legacy/findPhoneNumbers.test.js +0 -245
  61. package/build/legacy/format.test.js +0 -299
  62. package/build/legacy/getNumberType.test.js +0 -80
  63. package/build/legacy/isPossibleNumber.test.js +0 -82
  64. package/build/legacy/isValidNumber.test.js +0 -102
  65. package/build/legacy/isValidNumberForRegion.test.js +0 -40
  66. package/build/legacy/parse.test.js +0 -617
  67. package/build/legacy/searchNumbers.test.js +0 -33
  68. package/build/metadata.test.js +0 -170
  69. package/build/parse.test.js +0 -653
  70. package/build/parseIncompletePhoneNumber.test.js +0 -109
  71. package/build/parsePhoneNumber.test.js +0 -98
  72. package/build/parsePhoneNumberWithError.test.js +0 -140
  73. package/build/searchPhoneNumbersInText.test.js +0 -51
  74. package/build/tools/semver-compare.test.js +0 -13
  75. package/build/validatePhoneNumberLength.test.js +0 -37
  76. package/build-scripts/check-for-metadata-changes-and-create-pull-request.js +0 -88
  77. package/build-scripts/check-for-metadata-changes-and-release.js +0 -67
  78. package/build-scripts/check-for-uncommitted-files.js +0 -13
  79. package/build-scripts/create-commonjs-package-json-file.js +0 -11
  80. package/build-scripts/create-js-file-for-json-file.js +0 -14
  81. package/build-scripts/create-metadata-update-branch.js +0 -31
  82. package/build-scripts/generate-country-codes-list-in-typescript.js +0 -14
  83. package/build-scripts/generate-metadata-from-google-metadata.js +0 -75
  84. package/build-scripts/helpers/commit.js +0 -14
  85. package/build-scripts/helpers/exec.js +0 -6
  86. package/build-scripts/helpers/getUncommittedChanges.js +0 -33
  87. package/build-scripts/helpers/sendEmail.js +0 -62
  88. package/build-scripts/helpers/updateMetadataFromGoogleMetadata.js +0 -130
  89. package/build-scripts/pull-google-metadata.js +0 -29
  90. package/es6/AsYouType.test.js +0 -1336
  91. package/es6/AsYouTypeFormatter.PatternMatcher.test.js +0 -499
  92. package/es6/AsYouTypeFormatter.PatternParser.test.js +0 -46
  93. package/es6/AsYouTypeFormatter.util.test.js +0 -22
  94. package/es6/PhoneNumber.test.js +0 -138
  95. package/es6/PhoneNumberMatcher.test.js +0 -119
  96. package/es6/findNumbers/util.test.js +0 -32
  97. package/es6/findPhoneNumbersInText.test.js +0 -297
  98. package/es6/format.test.js +0 -336
  99. package/es6/formatIncompletePhoneNumber.test.js +0 -30
  100. package/es6/getCountries.test.js +0 -10
  101. package/es6/getCountryCallingCode.test.js +0 -15
  102. package/es6/getExampleNumber.test.js +0 -18
  103. package/es6/helpers/RFC3966.test.js +0 -45
  104. package/es6/helpers/applyInternationalSeparatorStyle.test.js +0 -10
  105. package/es6/helpers/checkNumberLength.test.js +0 -76
  106. package/es6/helpers/extractCountryCallingCode.test.js +0 -20
  107. package/es6/helpers/extractNationalNumber.test.js +0 -24
  108. package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +0 -17
  109. package/es6/helpers/extractPhoneContext.test.js +0 -68
  110. package/es6/helpers/getNumberType.test.js +0 -20
  111. package/es6/helpers/matchesEntirely.test.js +0 -13
  112. package/es6/helpers/mergeArrays.test.js +0 -9
  113. package/es6/helpers/parseDigits.test.js +0 -9
  114. package/es6/helpers/stripIddPrefix.test.js +0 -19
  115. package/es6/isPossible.test.js +0 -118
  116. package/es6/isPossiblePhoneNumber.test.js +0 -39
  117. package/es6/isValid.test.js +0 -141
  118. package/es6/isValidPhoneNumber.test.js +0 -26
  119. package/es6/legacy/findNumbers.test.js +0 -207
  120. package/es6/legacy/findPhoneNumbers.test.js +0 -243
  121. package/es6/legacy/format.test.js +0 -296
  122. package/es6/legacy/getNumberType.test.js +0 -77
  123. package/es6/legacy/isPossibleNumber.test.js +0 -79
  124. package/es6/legacy/isValidNumber.test.js +0 -99
  125. package/es6/legacy/isValidNumberForRegion.test.js +0 -37
  126. package/es6/legacy/parse.test.js +0 -614
  127. package/es6/legacy/searchNumbers.test.js +0 -30
  128. package/es6/metadata.test.js +0 -166
  129. package/es6/parse.test.js +0 -650
  130. package/es6/parseIncompletePhoneNumber.test.js +0 -105
  131. package/es6/parsePhoneNumber.test.js +0 -95
  132. package/es6/parsePhoneNumberWithError.test.js +0 -137
  133. package/es6/searchPhoneNumbersInText.test.js +0 -48
  134. package/es6/tools/semver-compare.test.js +0 -10
  135. package/es6/validatePhoneNumberLength.test.js +0 -34
  136. package/jest.config.json +0 -26
  137. package/rollup.config.mjs +0 -57
  138. package/source/AsYouType.test.js +0 -1409
  139. package/source/AsYouTypeFormatter.PatternMatcher.test.js +0 -644
  140. package/source/AsYouTypeFormatter.PatternParser.test.js +0 -70
  141. package/source/AsYouTypeFormatter.util.test.js +0 -25
  142. package/source/PhoneNumber.test.js +0 -138
  143. package/source/PhoneNumberMatcher.test.js +0 -112
  144. package/source/findNumbers/util.test.js +0 -37
  145. package/source/findPhoneNumbersInText.test.js +0 -316
  146. package/source/format.test.js +0 -273
  147. package/source/formatIncompletePhoneNumber.test.js +0 -33
  148. package/source/getCountries.test.js +0 -12
  149. package/source/getCountryCallingCode.test.js +0 -16
  150. package/source/getExampleNumber.test.js +0 -20
  151. package/source/helpers/RFC3966.test.js +0 -39
  152. package/source/helpers/applyInternationalSeparatorStyle.test.js +0 -11
  153. package/source/helpers/checkNumberLength.test.js +0 -85
  154. package/source/helpers/extractCountryCallingCode.test.js +0 -23
  155. package/source/helpers/extractNationalNumber.test.js +0 -27
  156. package/source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +0 -18
  157. package/source/helpers/extractPhoneContext.test.js +0 -107
  158. package/source/helpers/getNumberType.test.js +0 -27
  159. package/source/helpers/matchesEntirely.test.js +0 -14
  160. package/source/helpers/mergeArrays.test.js +0 -10
  161. package/source/helpers/parseDigits.test.js +0 -10
  162. package/source/helpers/stripIddPrefix.test.js +0 -24
  163. package/source/isPossible.test.js +0 -86
  164. package/source/isPossiblePhoneNumber.test.js +0 -36
  165. package/source/isValid.test.js +0 -123
  166. package/source/isValidPhoneNumber.test.js +0 -23
  167. package/source/legacy/findNumbers.test.js +0 -220
  168. package/source/legacy/findPhoneNumbers.test.js +0 -252
  169. package/source/legacy/format.test.js +0 -246
  170. package/source/legacy/getNumberType.test.js +0 -70
  171. package/source/legacy/isPossibleNumber.test.js +0 -49
  172. package/source/legacy/isValidNumber.test.js +0 -95
  173. package/source/legacy/isValidNumberForRegion.test.js +0 -31
  174. package/source/legacy/parse.test.js +0 -533
  175. package/source/legacy/searchNumbers.test.js +0 -29
  176. package/source/metadata.test.js +0 -178
  177. package/source/parse.test.js +0 -561
  178. package/source/parseIncompletePhoneNumber.test.js +0 -118
  179. package/source/parsePhoneNumber.test.js +0 -99
  180. package/source/parsePhoneNumberWithError.test.js +0 -115
  181. package/source/searchPhoneNumbersInText.test.js +0 -47
  182. package/source/tools/semver-compare.test.js +0 -43
  183. package/source/validatePhoneNumberLength.test.js +0 -33
@@ -1,1409 +0,0 @@
1
- import { describe, it } from 'mocha'
2
- import { expect } from 'chai'
3
-
4
- import metadata from '../metadata.min.json' with { type: 'json' }
5
- import AsYouType_ from './AsYouType.js'
6
-
7
- class AsYouType extends AsYouType_ {
8
- constructor(country_code) {
9
- super(country_code, metadata)
10
- }
11
- }
12
-
13
- const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
14
-
15
- describe('AsYouType', () => {
16
- it('should use "national_prefix_formatting_rule"', () => {
17
- // With national prefix (full).
18
- expect(new AsYouType('RU').input('88005553535')).to.equal('8 (800) 555-35-35')
19
- // With national prefix (partial).
20
- expect(new AsYouType('RU').input('880055535')).to.equal('8 (800) 555-35')
21
- })
22
-
23
- it('should populate national number template (digit by digit)', () => {
24
- const formatter = new AsYouType('US')
25
- formatter.input('1')
26
- // formatter.formatter.template.should.equal('x (xxx) xxx-xxxx')
27
- expect(formatter.formatter.template).to.equal('x xxx-xxxx')
28
- // formatter.formatter.populatedNationalNumberTemplate.should.equal('1 (xxx) xxx-xxxx')
29
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('1 xxx-xxxx')
30
- formatter.input('213')
31
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('1 (213) xxx-xxxx')
32
- formatter.input('3734253')
33
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('1 (213) 373-4253')
34
- })
35
-
36
- it('should populate international number template (digit by digit) (default country)', () => {
37
- const formatter = new AsYouType('US')
38
- expect(formatter.formatter.template).to.be.undefined
39
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
40
- expect(formatter.input('')).to.equal('')
41
- expect(formatter.formatter.template).to.be.undefined
42
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
43
- expect(formatter.input('+')).to.equal('+')
44
- expect(formatter.getTemplate()).to.equal('x')
45
- expect(formatter.formatter.template).to.be.undefined
46
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
47
- expect(formatter.input('1')).to.equal('+1')
48
- expect(formatter.getTemplate()).to.equal('xx')
49
- // Hasn't started formatting the phone number using the template yet.
50
- // formatter.formatter.template.should.equal('xx xxx xxx xxxx')
51
- expect(formatter.formatter.template).to.equal('xx xxx xxxx')
52
- // formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx')
53
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('xxx xxxx')
54
- // Has some national number digits, starts formatting the phone number using the template.
55
- formatter.input('213')
56
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('213 xxx xxxx')
57
- formatter.input('3734253')
58
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('213 373 4253')
59
- })
60
-
61
- it('should populate international number template (digit by digit)', () => {
62
- const formatter = new AsYouType()
63
- expect(formatter.formatter.template).to.be.undefined
64
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
65
- expect(formatter.input('')).to.equal('')
66
- expect(formatter.formatter.template).to.be.undefined
67
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
68
- expect(formatter.input('+')).to.equal('+')
69
- expect(formatter.formatter.template).to.be.undefined
70
- expect(formatter.formatter.populatedNationalNumberTemplate).to.be.undefined
71
- expect(formatter.input('1')).to.equal('+1')
72
- // formatter.formatter.template.should.equal('xx xxx xxx xxxx')
73
- expect(formatter.formatter.template).to.equal('xx xxx xxxx')
74
- // Hasn't yet started formatting the phone number using the template.
75
- // formatter.formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx')
76
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('xxx xxxx')
77
- // Has some national number digits, starts formatting the phone number using the template.
78
- formatter.input('213')
79
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('213 xxx xxxx')
80
- formatter.input('3734253')
81
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('213 373 4253')
82
- })
83
-
84
- it('should populate national number template (attempt to format complete number)', () => {
85
- const formatter = new AsYouType('US')
86
- expect(formatter.input('12133734253')).to.equal('1 (213) 373-4253')
87
- expect(formatter.formatter.template).to.equal('x (xxx) xxx-xxxx')
88
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('1 (213) 373-4253')
89
- })
90
-
91
- it('should parse and format phone numbers as you type', () => {
92
- // International number test
93
- expect(new AsYouType().input('+12133734')).to.equal('+1 213 373 4')
94
- // Local number test
95
- expect(new AsYouType('US').input('2133734')).to.equal('(213) 373-4')
96
-
97
- // US national number retains national prefix.
98
- expect(new AsYouType('US').input('12133734')).to.equal('1 (213) 373-4')
99
-
100
- // US national number retains national prefix (full number).
101
- expect(new AsYouType('US').input('12133734253')).to.equal('1 (213) 373-4253')
102
-
103
- let formatter
104
-
105
- // // Should discard national prefix from a "complete" phone number.
106
- // new AsYouType('RU').input('8800555353').should.equal('880 055-53-53')
107
-
108
- // Shouldn't extract national prefix when inputting in international format.
109
- expect(new AsYouType('RU').input('+7800555353')).to.equal('+7 800 555 35 3')
110
-
111
- expect(new AsYouType('CH').input('044-668-1')).to.equal('044 668 1')
112
-
113
- // Test International phone number (international)
114
-
115
- formatter = new AsYouType()
116
-
117
- // formatter.valid.should.be.false
118
- expect(type(formatter.getCountry())).to.equal('undefined')
119
- expect(type(formatter.getCountryCallingCode())).to.equal('undefined')
120
- expect(formatter.getTemplate()).to.equal('')
121
-
122
- expect(formatter.input('+')).to.equal('+')
123
-
124
- // formatter.valid.should.be.false
125
- expect(type(formatter.getCountry())).to.equal('undefined')
126
- expect(type(formatter.getCountryCallingCode())).to.equal('undefined')
127
- expect(formatter.getTemplate()).to.equal('x')
128
-
129
- expect(formatter.input('1')).to.equal('+1')
130
-
131
- // formatter.valid.should.be.false
132
- expect(type(formatter.getCountry())).to.equal('undefined')
133
- expect(formatter.getCountryCallingCode()).to.equal('1')
134
- expect(formatter.getTemplate()).to.equal('xx')
135
-
136
- expect(formatter.input('2')).to.equal('+1 2')
137
- expect(formatter.getTemplate()).to.equal('xx x')
138
-
139
- // formatter.valid.should.be.false
140
- expect(type(formatter.getCountry())).to.equal('undefined')
141
-
142
- expect(formatter.input('1')).to.equal('+1 21')
143
- expect(formatter.input('3')).to.equal('+1 213')
144
- expect(formatter.input(' ')).to.equal('+1 213')
145
- expect(formatter.input('3')).to.equal('+1 213 3')
146
- expect(formatter.input('3')).to.equal('+1 213 33')
147
- expect(formatter.input('3')).to.equal('+1 213 333')
148
- expect(formatter.input('4')).to.equal('+1 213 333 4')
149
- expect(formatter.input('4')).to.equal('+1 213 333 44')
150
- expect(formatter.input('4')).to.equal('+1 213 333 444')
151
-
152
- // formatter.valid.should.be.false
153
- expect(type(formatter.getCountry())).to.equal('undefined')
154
-
155
- expect(formatter.input('4')).to.equal('+1 213 333 4444')
156
-
157
- // formatter.valid.should.be.true
158
- expect(formatter.getCountry()).to.equal('US')
159
- // This one below contains "punctuation spaces"
160
- // along with the regular spaces
161
- expect(formatter.getTemplate()).to.equal('xx xxx xxx xxxx')
162
-
163
- expect(formatter.input('5')).to.equal('+1 21333344445')
164
-
165
- // formatter.valid.should.be.false
166
- expect(formatter.getCountry()).to.be.undefined
167
- expect(formatter.getCountryCallingCode()).to.equal('1')
168
- expect(formatter.formatter.template).to.be.undefined
169
-
170
- // Check that clearing an international formatter
171
- // also clears country metadata.
172
-
173
- formatter.reset()
174
-
175
- expect(formatter.input('+')).to.equal('+')
176
- expect(formatter.input('7')).to.equal('+7')
177
- expect(formatter.input('9')).to.equal('+7 9')
178
- expect(formatter.input('99 111 22 33')).to.equal('+7 999 111 22 33')
179
-
180
- // Test Switzerland phone numbers
181
-
182
- formatter = new AsYouType('CH')
183
-
184
- expect(formatter.input(' ')).to.equal('')
185
- expect(formatter.input('0')).to.equal('0')
186
- expect(formatter.input('4')).to.equal('04')
187
- expect(formatter.input(' ')).to.equal('04')
188
- expect(formatter.input('-')).to.equal('04')
189
- expect(formatter.input('4')).to.equal('044')
190
- expect(formatter.input('-')).to.equal('044')
191
- expect(formatter.input('6')).to.equal('044 6')
192
- expect(formatter.input('6')).to.equal('044 66')
193
- expect(formatter.input('8')).to.equal('044 668')
194
- expect(formatter.input('-')).to.equal('044 668')
195
- expect(formatter.input('1')).to.equal('044 668 1')
196
- expect(formatter.input('8')).to.equal('044 668 18')
197
-
198
- // formatter.valid.should.be.false
199
- expect(formatter.getCountry()).to.equal('CH')
200
- expect(formatter.formatter.template).to.equal('xxx xxx xx xx')
201
- expect(formatter.getTemplate()).to.equal('xxx xxx xx')
202
-
203
- expect(formatter.input(' 00')).to.equal('044 668 18 00')
204
-
205
- // formatter.valid.should.be.true
206
- expect(formatter.getCountry()).to.equal('CH')
207
- expect(formatter.getTemplate()).to.equal('xxx xxx xx xx')
208
-
209
- expect(formatter.input('9')).to.equal('04466818009')
210
-
211
- // formatter.valid.should.be.false
212
- expect(formatter.getCountry()).to.equal('CH')
213
- expect(formatter.formatter.template).to.be.undefined
214
-
215
- // Kazakhstan (non-main country for +7 country phone code)
216
-
217
- formatter = new AsYouType()
218
-
219
- formatter.input('+77172580659')
220
- expect(formatter.getCountry()).to.equal('KZ')
221
-
222
- // Brazil
223
-
224
- formatter = new AsYouType('BR')
225
- expect(formatter.input('11987654321')).to.equal('(11) 98765-4321')
226
-
227
- // UK (Jersey) (non-main country for +44 country phone code)
228
-
229
- formatter = new AsYouType()
230
- expect(formatter.input('+447700300000')).to.equal('+44 7700 300000')
231
- expect(formatter.getTemplate()).to.equal('xxx xxxx xxxxxx')
232
- expect(formatter.getCountry()).to.equal('JE')
233
-
234
- // Braces must be part of the template.
235
- formatter = new AsYouType('RU')
236
- expect(formatter.input('88005553535')).to.equal('8 (800) 555-35-35')
237
- expect(formatter.getTemplate()).to.equal('x (xxx) xxx-xx-xx')
238
-
239
- // Test Russian phone numbers
240
- // (with optional national prefix `8`)
241
-
242
- formatter = new AsYouType('RU')
243
-
244
- expect(formatter.input('8')).to.equal('8')
245
- expect(formatter.input('9')).to.equal('8 9')
246
- expect(formatter.input('9')).to.equal('8 99')
247
- expect(formatter.input('9')).to.equal('8 (999)')
248
- expect(formatter.input('-')).to.equal('8 (999)')
249
- expect(formatter.input('1234')).to.equal('8 (999) 123-4')
250
- expect(formatter.input('567')).to.equal('8 (999) 123-45-67')
251
- expect(formatter.input('8')).to.equal('899912345678')
252
-
253
- // Shouldn't strip national prefix if it is optional
254
- // and if it's a valid phone number (international).
255
- formatter = new AsYouType('RU')
256
- // formatter.input('8005553535').should.equal('(800) 555-35-35')
257
- expect(formatter.input('+78005553535')).to.equal('+7 800 555 35 35')
258
- expect(formatter.getNationalNumber()).to.equal('8005553535')
259
-
260
- // Check that clearing an national formatter:
261
- // * doesn't clear country metadata
262
- // * clears all other things
263
-
264
- formatter.reset()
265
-
266
- expect(formatter.input('8')).to.equal('8')
267
- expect(formatter.input('9')).to.equal('8 9')
268
- expect(formatter.input('9')).to.equal('8 99')
269
- expect(formatter.input('9')).to.equal('8 (999)')
270
- expect(formatter.input('-')).to.equal('8 (999)')
271
- expect(formatter.input('1234')).to.equal('8 (999) 123-4')
272
- expect(formatter.input('567')).to.equal('8 (999) 123-45-67')
273
- expect(formatter.input('8')).to.equal('899912345678')
274
-
275
- // National prefix should not be prepended
276
- // when formatting local NANPA phone numbers.
277
- expect(new AsYouType('US').input('1')).to.equal('1')
278
- expect(new AsYouType('US').input('12')).to.equal('1 2')
279
- expect(new AsYouType('US').input('123')).to.equal('1 23')
280
-
281
- // Bulgaria
282
- // (should not prepend national prefix `0`)
283
- expect(new AsYouType('BG').input('111 222 3')).to.equal('1112223')
284
-
285
- // Deutchland
286
- expect(new AsYouType().input('+4915539898001')).to.equal('+49 15539 898001')
287
-
288
- // KZ detection
289
- formatter = new AsYouType()
290
- formatter.input('+7 702 211 1111')
291
- expect(formatter.getCountry()).to.equal('KZ')
292
- // formatter.valid.should.equal(true)
293
-
294
- // New Zealand formatting fix (issue #89)
295
- expect(new AsYouType('NZ').input('0212')).to.equal('021 2')
296
-
297
- // South Korea
298
- formatter = new AsYouType()
299
- expect(formatter.input('+82111111111')).to.equal('+82 11 111 1111')
300
- expect(formatter.getTemplate()).to.equal('xxx xx xxx xxxx')
301
- })
302
-
303
- it('should filter out formats that require a national prefix and no national prefix has been input', () => {
304
- // Afghanistan.
305
- const formatter = new AsYouType('AF')
306
-
307
- // No national prefix, and national prefix is required in the format.
308
- // (not `"national_prefix_is_optional_when_formatting": true`)
309
- expect(formatter.input('44444444')).to.equal('44444444')
310
- expect(formatter.formatter.template).to.be.undefined
311
-
312
- // With national prefix
313
- expect(formatter.reset().input('044444444')).to.equal('044 444 444')
314
- expect(formatter.formatter.template).to.equal('xxx xxx xxxx')
315
- })
316
-
317
- it('should work when a digit is not a national prefix but a part of a valid national number', () => {
318
- // In Russia, `8` could be both a valid national prefix
319
- // and a part of a valid national number.
320
- const formatter = new AsYouType('RU')
321
- // The formatter could try both variants:
322
- // with extracting national prefix
323
- // and without extracting it,
324
- // and then choose whichever way has `this.matchingFormats`.
325
- // Or there could be two instances of the formatter:
326
- // one that extracts national prefix and one that doesn't,
327
- // and then the one that has `this.matchingFormats` would be
328
- // used to format the phone number.
329
- // Something like an option `extractNationalPrefix: false`
330
- // and creating `this.withNationalPrefixFormatter = new AsYouType(this.defaultCountry || this.defaultCallingCode, { metadata, extractNationalPrefix: false })`
331
- // and something like `this.withNationalPrefixFormatter.input(nextDigits)` in `input(nextDigits)`.
332
- // But, for this specific case, it's not required:
333
- // in Russia, people are used to inputting `800` numbers with national prefix `8`:
334
- // `8 800 555 35 35`.
335
- // formatter.input('8005553535').should.equal('(800) 555-35-35')
336
- expect(formatter.input('8005553535')).to.equal('8005553535')
337
- formatter.reset()
338
- expect(formatter.input('+78005553535')).to.equal('+7 800 555 35 35')
339
- })
340
-
341
- it('should match formats that require a national prefix and no national prefix has been input (national prefix is mandatory for a format)', () => {
342
- const formatter = new AsYouType('FR')
343
- expect(formatter.input('612345678')).to.equal('612345678')
344
- formatter.reset()
345
- expect(formatter.input('0612345678')).to.equal('06 12 34 56 78')
346
- })
347
-
348
- it('should match formats that require a national prefix and no national prefix has been input (national prefix is not mandatory for a format)', () => {
349
- const formatter = new AsYouType('RU')
350
- // Without national prefix.
351
- expect(formatter.input('9991234567')).to.equal('999 123-45-67')
352
- formatter.reset()
353
- // With national prefix.
354
- expect(formatter.input('89991234567')).to.equal('8 (999) 123-45-67')
355
- })
356
-
357
- it('should not use `national_prefix_formatting_rule` when formatting international phone numbers', () => {
358
- // Brazil.
359
- // `national_prefix_formatting_rule` is `($1)`.
360
- // Should not add braces around `12` when being input in international format.
361
- expect(new AsYouType().input('+55123456789')).to.equal('+55 12 3456 789')
362
- expect(new AsYouType('BR').input('+55123456789')).to.equal('+55 12 3456 789')
363
- expect(new AsYouType('BR').input('123456789')).to.equal('(12) 3456-789')
364
- })
365
-
366
- it('should support incorrectly entered international phone numbers (with a national prefix)', () => {
367
- let formatter
368
-
369
- formatter = new AsYouType()
370
- expect(formatter.input('+1 1 877 215 5230')).to.equal('+1 1 877 215 5230')
371
- // formatter.input('+1 1 877 215 5230').should.equal('+1 1 8772155230')
372
- expect(formatter.getNationalNumber()).to.equal('8772155230')
373
-
374
- // They've added another number format that has `8` leading digit
375
- // and 14 digits. Maybe it's something related to Kazakhstan.
376
- // formatter = new AsYouType()
377
- // formatter.input('+78800555353').should.equal('+7 880 055 53 53')
378
- // formatter.input('5').should.equal('+7 8 800 555 35 35')
379
- // formatter.getNationalNumber().should.equal('8005553535')
380
- })
381
-
382
- it('should return a partial template for current value', () => {
383
- const asYouType = new AsYouType('US')
384
-
385
- expect(asYouType.input('')).to.equal('')
386
- expect(asYouType.getTemplate()).to.equal('')
387
-
388
- expect(asYouType.input('2')).to.equal('2')
389
- // asYouType.getTemplate().should.equal('x')
390
- // Doesn't format for a single digit.
391
- expect(asYouType.getTemplate()).to.equal('x')
392
-
393
- expect(asYouType.input('1')).to.equal('21')
394
- expect(asYouType.getTemplate()).to.equal('xx')
395
-
396
- expect(asYouType.input('3')).to.equal('(213)')
397
- expect(asYouType.getTemplate()).to.equal('(xxx)')
398
- })
399
-
400
- it(`should fall back to the default country`, () => {
401
- const formatter = new AsYouType('RU')
402
-
403
- expect(formatter.input('8')).to.equal('8')
404
- expect(formatter.input('9')).to.equal('8 9')
405
- expect(formatter.input('9')).to.equal('8 99')
406
- expect(formatter.input('9')).to.equal('8 (999)')
407
-
408
- // formatter.valid.should.be.false
409
- expect(formatter.formatter.template).to.equal('x (xxx) xxx-xx-xx')
410
- expect(formatter.getCountry()).to.equal('RU')
411
- // formatter.getCountryCallingCode().should.equal('7')
412
-
413
- expect(formatter.input('000000000000')).to.equal('8999000000000000')
414
-
415
- // formatter.valid.should.be.false
416
- expect(formatter.formatter.template).to.be.undefined
417
- expect(formatter.getCountry()).to.equal('RU')
418
- // formatter.getCountryCallingCode().should.equal('7')
419
-
420
- formatter.reset()
421
-
422
- // formatter.valid.should.be.false
423
- expect(formatter.formatter.template).to.be.undefined
424
- expect(formatter.getCountry()).to.be.undefined
425
- // formatter.getCountryCallingCode().should.equal('7')
426
-
427
- expect(formatter.input('+1-213-373-4253')).to.equal('+1 213 373 4253')
428
-
429
- // formatter.valid.should.be.true
430
- expect(formatter.getTemplate()).to.equal('xx xxx xxx xxxx')
431
- expect(formatter.getCountry()).to.equal('US')
432
- expect(formatter.getCountryCallingCode()).to.equal('1')
433
- })
434
-
435
- it('should work in edge cases', () => {
436
- let formatter
437
- let thrower
438
-
439
- // No metadata
440
- thrower = () => new AsYouType_('RU')
441
- expect(thrower).to.throw('`metadata` argument not passed')
442
-
443
- // Second '+' sign
444
-
445
- formatter = new AsYouType('RU')
446
-
447
- expect(formatter.input('+')).to.equal('+')
448
- expect(formatter.input('7')).to.equal('+7')
449
- expect(formatter.input('+')).to.equal('+7')
450
-
451
- // Out-of-position '+' sign
452
-
453
- formatter = new AsYouType('RU')
454
-
455
- expect(formatter.input('8')).to.equal('8')
456
- expect(formatter.input('+')).to.equal('8')
457
-
458
- // No format matched
459
-
460
- formatter = new AsYouType('RU')
461
-
462
- expect(formatter.input('88005553535')).to.equal('8 (800) 555-35-35')
463
- expect(formatter.input('0')).to.equal('880055535350')
464
-
465
- // Invalid country phone code
466
-
467
- formatter = new AsYouType()
468
-
469
- expect(formatter.input('+0123')).to.equal('+0123')
470
-
471
- // No country specified and not an international number
472
-
473
- formatter = new AsYouType()
474
-
475
- expect(formatter.input('88005553535')).to.equal('88005553535')
476
-
477
- // Extract national prefix when no `national_prefix` is set
478
-
479
- formatter = new AsYouType('AD')
480
-
481
- expect(formatter.input('155555')).to.equal('155 555')
482
-
483
- // Typing nonsense
484
-
485
- formatter = new AsYouType('RU')
486
-
487
- expect(formatter.input('+1abc2')).to.equal('+1')
488
-
489
- // Should reset default country when explicitly
490
- // typing in an international phone number
491
-
492
- formatter = new AsYouType('RU')
493
-
494
- formatter.input('+')
495
- expect(type(formatter.getCountry())).to.equal('undefined')
496
- expect(type(formatter.getCountryCallingCode())).to.equal('undefined')
497
-
498
- // Country not inferrable from the phone number,
499
- // while the phone number itself can already be formatted "completely".
500
-
501
- formatter = new AsYouType()
502
-
503
- formatter.input('+12223333333')
504
- expect(type(formatter.getCountry())).to.equal('undefined')
505
- expect(formatter.getCountryCallingCode()).to.equal('1')
506
-
507
- // Reset a chosen format when it no longer applies given the new leading digits.
508
- // If Google changes metadata for England then this test might not cover the case.
509
- formatter = new AsYouType('GB')
510
- expect(formatter.input('0845')).to.equal('0845')
511
- // New leading digits don't match the format previously chosen.
512
- // Reset the format.
513
- expect(formatter.input('0')).to.equal('0845 0')
514
- })
515
-
516
- it('should choose between matching formats based on the absence or presence of a national prefix', () => {
517
- // The first matching format:
518
- // {
519
- // "pattern": "(\\d{2})(\\d{5,6})",
520
- // "leading_digits_patterns": [
521
- // "(?:10|2[0-57-9])[19]",
522
- // "(?:10|2[0-57-9])(?:10|9[56])",
523
- // "(?:10|2[0-57-9])(?:100|9[56])"
524
- // ],
525
- // "national_prefix_formatting_rule": "0$1",
526
- // "format": "$1 $2",
527
- // "domestic_carrier_code_formatting_rule": "$CC $FG"
528
- // }
529
- //
530
- // The second matching format:
531
- // {
532
- // "pattern": "(\\d{2})(\\d{4})(\\d{4})",
533
- // "leading_digits_patterns": [
534
- // "10|2(?:[02-57-9]|1[1-9])",
535
- // "10|2(?:[02-57-9]|1[1-9])",
536
- // "10[0-79]|2(?:[02-57-9]|1[1-79])|(?:10|21)8(?:0[1-9]|[1-9])"
537
- // ],
538
- // "national_prefix_formatting_rule": "0$1",
539
- // "national_prefix_is_optional_when_formatting": true,
540
- // "format": "$1 $2 $3",
541
- // "domestic_carrier_code_formatting_rule": "$CC $FG"
542
- // }
543
- //
544
- const formatter = new AsYouType('CN')
545
- // National prefix has been input.
546
- // Chooses the first format.
547
- expect(formatter.input('01010000')).to.equal('010 10000')
548
- formatter.reset()
549
- // No national prefix has been input,
550
- // and `national_prefix_for_parsing` not matched.
551
- // The first format won't match, because it doesn't have
552
- // `"national_prefix_is_optional_when_formatting": true`.
553
- // The second format will match, because it does have
554
- // `"national_prefix_is_optional_when_formatting": true`.
555
- expect(formatter.input('1010000')).to.equal('10 1000 0')
556
- })
557
-
558
- it('should not accept phone number extensions', () => {
559
- expect(new AsYouType().input('+1-213-373-4253 ext. 123')).to.equal('+1 213 373 4253')
560
- })
561
-
562
- it('should parse non-European digits', () => {
563
- expect(new AsYouType().input('+١٢١٢٢٣٢٣٢٣٢')).to.equal('+1 212 232 3232')
564
- })
565
-
566
- it('should return a PhoneNumber instance', () => {
567
- const formatter = new AsYouType('BR')
568
-
569
- // No country calling code.
570
- expect(formatter.getNumber()).to.be.undefined
571
-
572
- formatter.input('+1')
573
- // No national number digits.
574
- expect(formatter.getNumber()).to.be.undefined
575
-
576
- formatter.input('213-373-4253')
577
-
578
- let phoneNumber = formatter.getNumber()
579
- expect(phoneNumber.country).to.equal('US')
580
- expect(phoneNumber.countryCallingCode).to.equal('1')
581
- expect(phoneNumber.number).to.equal('+12133734253')
582
- expect(phoneNumber.nationalNumber).to.equal('2133734253')
583
-
584
- formatter.reset()
585
- formatter.input('+1-113-373-4253')
586
-
587
- phoneNumber = formatter.getNumber()
588
- expect(phoneNumber.country).to.be.undefined
589
- expect(phoneNumber.countryCallingCode).to.equal('1')
590
-
591
- // An incorrect NANPA international phone number.
592
- // (contains national prefix in an international phone number)
593
-
594
- formatter.reset()
595
- formatter.input('+1-1')
596
-
597
- // Before leading digits < 3 matching was implemented:
598
- //
599
- // phoneNumber = formatter.getNumber()
600
- // expect(phoneNumber).to.not.be.undefined
601
- //
602
- // formatter.input('1')
603
- // phoneNumber = formatter.getNumber()
604
- // expect(phoneNumber.country).to.be.undefined
605
- // phoneNumber.countryCallingCode.should.equal('1')
606
- // phoneNumber.number.should.equal('+111')
607
-
608
- // After leading digits < 3 matching was implemented:
609
- //
610
- phoneNumber = formatter.getNumber()
611
- expect(phoneNumber).to.be.undefined
612
- //
613
- formatter.input('1')
614
- phoneNumber = formatter.getNumber()
615
- expect(phoneNumber.country).to.be.undefined
616
- expect(phoneNumber.countryCallingCode).to.equal('1')
617
- expect(phoneNumber.number).to.equal('+11')
618
- })
619
-
620
- it('should work with countries that add digits to national (significant) number', () => {
621
- // When formatting Argentinian mobile numbers in international format,
622
- // a `9` is prepended, when compared to national format.
623
- const asYouType = new AsYouType('AR')
624
- expect(asYouType.input('+5493435551212')).to.equal('+54 9 3435 55 1212')
625
- asYouType.reset()
626
- // Digits shouldn't be changed when formatting in national format.
627
- // (no `9` is prepended).
628
- // First parses national (significant) number by prepending `9` to it
629
- // and stripping `15` from it.
630
- // Then uses `$2 15-$3-$4` format that strips the leading `9`
631
- // and adds `15`.
632
- expect(asYouType.input('0343515551212')).to.equal('03435 15-55-1212')
633
- })
634
-
635
- it('should return non-formatted phone number when no format matches and national (significant) number has digits added', () => {
636
- // When inputting "0343515551212999" Argentinian mobile number,
637
- // the parsed national (significant) number is "93435551212999".
638
- // There, one can see how it stripped "0" national prefix and prepended a "9",
639
- // because that's how it is instructed to do in Argentina's metadata.
640
- //
641
- // So in this case, national (significant) number doesn't match the input
642
- // and, therefore, `state.nationalSignificantNumberIsModified` flag is `true`.
643
- //
644
- const asYouType = new AsYouType('AR')
645
- // This line checks that the formatter didn't alter the output digits compared to the input digits.
646
- expect(asYouType.input('0343515551212999')).to.equal('0343515551212999')
647
- })
648
-
649
- it('should format Argentina numbers (starting with 011) (digit by digit)', () => {
650
- // Inputting a number digit-by-digit and as a whole a two different cases
651
- // in case of this library compared to Google's `libphonenumber`
652
- // that always inputs a number digit-by-digit.
653
- // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/23
654
- // nextDigits 0111523456789
655
- // nationalNumber 91123456789
656
- const formatter = new AsYouType('AR')
657
- expect(formatter.input('0')).to.equal('0')
658
- expect(formatter.getTemplate()).to.equal('x')
659
- expect(formatter.input('1')).to.equal('01')
660
- expect(formatter.getTemplate()).to.equal('xx')
661
- expect(formatter.input('1')).to.equal('011')
662
- expect(formatter.getTemplate()).to.equal('xxx')
663
- expect(formatter.input('1')).to.equal('011 1')
664
- expect(formatter.getTemplate()).to.equal('xxx x')
665
- expect(formatter.input('5')).to.equal('011 15')
666
- expect(formatter.getTemplate()).to.equal('xxx xx')
667
- expect(formatter.input('2')).to.equal('011 152')
668
- expect(formatter.getTemplate()).to.equal('xxx xxx')
669
- expect(formatter.input('3')).to.equal('011 1523')
670
- expect(formatter.getTemplate()).to.equal('xxx xxxx')
671
- expect(formatter.input('4')).to.equal('011 1523-4')
672
- expect(formatter.getTemplate()).to.equal('xxx xxxx-x')
673
- expect(formatter.input('5')).to.equal('011 1523-45')
674
- expect(formatter.getTemplate()).to.equal('xxx xxxx-xx')
675
- expect(formatter.input('6')).to.equal('011 1523-456')
676
- expect(formatter.getTemplate()).to.equal('xxx xxxx-xxx')
677
- expect(formatter.input('7')).to.equal('011 1523-4567')
678
- expect(formatter.getTemplate()).to.equal('xxx xxxx-xxxx')
679
- expect(formatter.input('8')).to.equal('011152345678')
680
- expect(formatter.getTemplate()).to.equal('xxxxxxxxxxxx')
681
- expect(formatter.input('9')).to.equal('011 15-2345-6789')
682
- expect(formatter.getTemplate()).to.equal('xxx xx-xxxx-xxxx')
683
- // Private property (not public API).
684
- expect(formatter.state.nationalSignificantNumber).to.equal('91123456789')
685
- // Private property (not public API).
686
- // `formatter.digits` is not always `formatter.nationalPrefix`
687
- // plus `formatter.nationalNumberDigits`.
688
- expect(formatter.state.nationalPrefix).to.equal('0')
689
- expect(formatter.isPossible()).to.equal(true)
690
- expect(formatter.isValid()).to.equal(true)
691
- })
692
-
693
- it('should format Argentina numbers (starting with 011)', () => {
694
- // Inputting a number digit-by-digit and as a whole a two different cases
695
- // in case of this library compared to Google's `libphonenumber`
696
- // that always inputs a number digit-by-digit.
697
- // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/23
698
- // nextDigits 0111523456789
699
- // nationalNumber 91123456789
700
- const formatter = new AsYouType('AR')
701
- expect(formatter.input('0111523456789')).to.equal('011 15-2345-6789')
702
- // Private property (not public API).
703
- expect(formatter.state.nationalSignificantNumber).to.equal('91123456789')
704
- // Private property (not public API).
705
- // `formatter.digits` is not always `formatter.nationalPrefix`
706
- // plus `formatter.nationalNumberDigits`.
707
- expect(formatter.state.nationalPrefix).to.equal('0')
708
- // expect(formatter.nationalPrefix).to.be.undefined
709
- expect(formatter.isPossible()).to.equal(true)
710
- expect(formatter.isValid()).to.equal(true)
711
- })
712
-
713
- // https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/93
714
- it('should format Indonesian numbers', () => {
715
- const formatter = new AsYouType('ID')
716
- expect(formatter.getChars()).to.equal('')
717
- // Before leading digits < 3 matching was implemented:
718
- // formatter.input('081').should.equal('(081)')
719
- // After leading digits < 3 matching was implemented:
720
- expect(formatter.input('081')).to.equal('081')
721
- })
722
-
723
- it('should prepend `prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix` (not a complete number)', () => {
724
- // A country having `national_prefix_for_parsing` with a "capturing group".
725
- // National prefix is either not used in a format or is optional.
726
- // Input phone number without a national prefix.
727
- const formatter = new AsYouType('AU')
728
- expect(formatter.input('1831130345678')).to.equal('1831 1303 456 78')
729
- // Private property (not public API).
730
- expect(formatter.state.nationalSignificantNumber).to.equal('130345678')
731
- // Private property (not public API).
732
- // `formatter.digits` is not always `formatter.nationalPrefix`
733
- // plus `formatter.nationalNumberDigits`.
734
- expect(formatter.state.nationalPrefix).to.be.undefined
735
- expect(formatter.state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix).to.equal('1831')
736
- })
737
-
738
- it('should prepend `prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix` (complete number)', () => {
739
- // A country having `national_prefix_for_parsing` with a "capturing group".
740
- // National prefix is either not used in a format or is optional.
741
- // Input phone number without a national prefix.
742
- const formatter = new AsYouType('AU')
743
- expect(formatter.input('18311303456789')).to.equal('1831 1303 456 789')
744
- // Private property (not public API).
745
- expect(formatter.state.nationalSignificantNumber).to.equal('1303456789')
746
- // Private property (not public API).
747
- // `formatter.digits` is not always `formatter.nationalPrefix`
748
- // plus `formatter.nationalNumberDigits`.
749
- expect(formatter.state.nationalPrefix).to.be.undefined
750
- expect(formatter.state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix).to.equal('1831')
751
- })
752
-
753
- it('should work with Mexico numbers', () => {
754
- const asYouType = new AsYouType('MX')
755
-
756
- // Fixed line. International.
757
- expect(asYouType.input('+52(449)978-000')).to.equal('+52 449 978 000')
758
- expect(asYouType.input('1')).to.equal('+52 449 978 0001')
759
- asYouType.reset()
760
-
761
- // "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
762
- // no longer valid since August 2019."
763
- // // Fixed line. National. With national prefix "01".
764
- // asYouType.input('01449978000').should.equal('01449 978 000')
765
- // asYouType.getTemplate().should.equal('xxxxx xxx xxx')
766
- // asYouType.input('1').should.equal('01449 978 0001')
767
- // asYouType.getTemplate().should.equal('xxxxx xxx xxxx')
768
- // asYouType.reset()
769
-
770
- // Fixed line. National. Without national prefix.
771
- expect(asYouType.input('(449)978-000')).to.equal('449 978 000')
772
- expect(asYouType.getTemplate()).to.equal('xxx xxx xxx')
773
- expect(asYouType.input('1')).to.equal('449 978 0001')
774
- expect(asYouType.getTemplate()).to.equal('xxx xxx xxxx')
775
- asYouType.reset()
776
-
777
- // Mobile.
778
- expect(asYouType.input('+52331234567')).to.equal('+52 33 1234 567')
779
- expect(asYouType.input('8')).to.equal('+52 33 1234 5678')
780
- asYouType.reset()
781
-
782
- // "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
783
- // no longer valid since August 2019."
784
- // // Mobile.
785
- // // With `1` prepended before area code to mobile numbers in international format.
786
- // asYouType.input('+521331234567').should.equal('+52 133 1234 567')
787
- // asYouType.getTemplate().should.equal('xxx xxx xxxx xxx')
788
- // // Google's `libphonenumber` seems to not able to format this type of number.
789
- // // https://issuetracker.google.com/issues/147938979
790
- // asYouType.input('8').should.equal('+52 133 1234 5678')
791
- // asYouType.getTemplate().should.equal('xxx xxx xxxx xxxx')
792
- // asYouType.reset()
793
- //
794
- // // Mobile. National. With "044" prefix.
795
- // asYouType.input('044331234567').should.equal('04433 1234 567')
796
- // asYouType.input('8').should.equal('04433 1234 5678')
797
- // asYouType.reset()
798
- //
799
- // // Mobile. National. With "045" prefix.
800
- // asYouType.input('045331234567').should.equal('04533 1234 567')
801
- // asYouType.input('8').should.equal('04533 1234 5678')
802
- })
803
-
804
- it('should just prepend national prefix if national_prefix_formatting_rule does not produce a suitable number', () => {
805
- // "national_prefix": "8"
806
- // "national_prefix_for_parsing": "0|80?"
807
- const formatter = new AsYouType('BY')
808
- // "national_prefix_formatting_rule": "8 $1"
809
- // That `national_prefix_formatting_rule` isn't used
810
- // because the user didn't input national prefix `8`.
811
- expect(formatter.input('0800123')).to.equal('0 800 123')
812
- expect(formatter.getTemplate()).to.equal('x xxx xxx')
813
- })
814
-
815
- it('should not duplicate area code for certain countries', () => {
816
- // https://github.com/catamphetamine/libphonenumber-js/issues/318
817
- const asYouType = new AsYouType('VI')
818
- // Even though `parse("3406934")` would return a
819
- // "(340) 340-6934" national number, still
820
- // "As You Type" formatter should leave it as "(340) 6934".
821
- expect(asYouType.input('340693')).to.equal('(340) 693')
822
- expect(asYouType.input('4')).to.equal('(340) 693-4')
823
- expect(asYouType.input('123')).to.equal('(340) 693-4123')
824
- })
825
-
826
- it('shouldn\'t throw when passed a non-existent default country', () => {
827
- expect(new AsYouType('XX').input('+78005553535')).to.equal('+7 800 555 35 35')
828
- expect(new AsYouType('XX').input('88005553535')).to.equal('88005553535')
829
- })
830
-
831
- it('should parse carrier codes', () => {
832
- const formatter = new AsYouType('BR')
833
-
834
- formatter.input('0 15 21 5555-5555')
835
- let phoneNumber = formatter.getNumber()
836
- expect(phoneNumber.carrierCode).to.equal('15')
837
-
838
- formatter.reset()
839
- formatter.input('+1-213-373-4253')
840
- phoneNumber = formatter.getNumber()
841
- expect(phoneNumber.carrierCode).to.be.undefined
842
- })
843
-
844
- it('should format when default country calling code is configured', () => {
845
- const formatter = new AsYouType({ defaultCallingCode: '7' })
846
- expect(formatter.input('88005553535')).to.equal('8 (800) 555-35-35')
847
- expect(formatter.getNumber().countryCallingCode).to.equal('7')
848
- expect(formatter.getNumber().country).to.equal('RU')
849
- })
850
-
851
- it('shouldn\'t return PhoneNumber if country calling code hasn\'t been input yet', () => {
852
- const formatter = new AsYouType()
853
- formatter.input('+80')
854
- expect(formatter.getNumber()).to.be.undefined
855
- })
856
-
857
- it('should format non-geographic numbering plan phone numbers', () => {
858
- const formatter = new AsYouType()
859
- expect(formatter.input('+')).to.equal('+')
860
- expect(formatter.input('8')).to.equal('+8')
861
- expect(formatter.input('7')).to.equal('+87')
862
- expect(formatter.getCountry()).to.be.undefined
863
- expect(formatter.input('0')).to.equal('+870')
864
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
865
- expect(formatter.getCountry()).to.equal('001')
866
- } else {
867
- expect(formatter.getCountry()).to.be.undefined
868
- }
869
- expect(formatter.input('7')).to.equal('+870 7')
870
- expect(formatter.input('7')).to.equal('+870 77')
871
- expect(formatter.input('3')).to.equal('+870 773')
872
- expect(formatter.input('1')).to.equal('+870 773 1')
873
- expect(formatter.input('1')).to.equal('+870 773 11')
874
- expect(formatter.input('1')).to.equal('+870 773 111')
875
- expect(formatter.input('6')).to.equal('+870 773 111 6')
876
- expect(formatter.input('3')).to.equal('+870 773 111 63')
877
- expect(formatter.input('2')).to.equal('+870 773 111 632')
878
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
879
- expect(formatter.getNumber().country).to.equal('001')
880
- } else {
881
- expect(formatter.getCountry()).to.be.undefined
882
- }
883
- expect(formatter.getNumber().countryCallingCode).to.equal('870')
884
- })
885
-
886
- it('should format non-geographic numbering plan phone numbers (default country calling code)', () => {
887
- const formatter = new AsYouType({ defaultCallingCode: '870' })
888
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
889
- expect(formatter.getNumber().country).to.equal('001')
890
- } else {
891
- expect(formatter.getCountry()).to.be.undefined
892
- }
893
- expect(formatter.input('7')).to.equal('7')
894
- expect(formatter.input('7')).to.equal('77')
895
- expect(formatter.input('3')).to.equal('773')
896
- expect(formatter.input('1')).to.equal('773 1')
897
- expect(formatter.input('1')).to.equal('773 11')
898
- expect(formatter.input('1')).to.equal('773 111')
899
- expect(formatter.input('6')).to.equal('773 111 6')
900
- expect(formatter.input('3')).to.equal('773 111 63')
901
- expect(formatter.input('2')).to.equal('773 111 632')
902
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
903
- expect(formatter.getNumber().country).to.equal('001')
904
- } else {
905
- expect(formatter.getCountry()).to.be.undefined
906
- }
907
- expect(formatter.getNumber().countryCallingCode).to.equal('870')
908
- })
909
-
910
- it('should not format non-geographic numbering plan phone numbers (default country 001)', () => {
911
- const formatter = new AsYouType('001')
912
- expect(formatter.defaultCountry).to.be.undefined
913
- expect(formatter.defaultCallingCode).to.be.undefined
914
- expect(formatter.input('7')).to.equal('7')
915
- expect(formatter.input('7')).to.equal('77')
916
- expect(formatter.input('3')).to.equal('773')
917
- expect(formatter.input('1')).to.equal('7731')
918
- expect(formatter.input('1')).to.equal('77311')
919
- expect(formatter.input('1')).to.equal('773111')
920
- expect(formatter.input('6')).to.equal('7731116')
921
- expect(formatter.input('3')).to.equal('77311163')
922
- expect(formatter.input('2')).to.equal('773111632')
923
- expect(formatter.getCountry()).to.be.undefined
924
- expect(formatter.getNumber()).to.be.undefined
925
- })
926
-
927
- it('should return PhoneNumber (should strip national prefix `1` in E.164 value)', () => {
928
- const formatter = new AsYouType('RU')
929
- formatter.input('+1111')
930
- expect(formatter.getNumber().number).to.equal('+111')
931
- })
932
-
933
- it('should return PhoneNumber with autocorrected international numbers without leading +', () => {
934
- // https://github.com/catamphetamine/libphonenumber-js/issues/316
935
- const formatter = new AsYouType('FR')
936
- expect(formatter.input('33612902554')).to.equal('33 6 12 90 25 54')
937
- expect(formatter.getNumber().country).to.equal('FR')
938
- expect(formatter.getNumber().nationalNumber).to.equal('612902554')
939
- expect(formatter.getNumber().number).to.equal('+33612902554')
940
- // Should also strip national prefix.
941
- formatter.reset()
942
- expect(formatter.input('330612902554')).to.equal('33 06 12 90 25 54')
943
- expect(formatter.getNumber().country).to.equal('FR')
944
- expect(formatter.getNumber().nationalNumber).to.equal('612902554')
945
- expect(formatter.getNumber().number).to.equal('+33612902554')
946
- // On second thought, this "prepend default area code" feature won't be added,
947
- // because when a user selects "British Virgin Islands" and inputs
948
- // "2291234", then they see "(229) 123-4" which clearly indicates that
949
- // they should input the complete phone number (with area code).
950
- // So, unless a user completely doesn't understand what they're doing,
951
- // they'd input the complete phone number (with area code).
952
- // // Should prepend the default area code in British Virgin Islands.
953
- // // https://github.com/catamphetamine/react-phone-number-input/issues/335
954
- // const formatter2 = new AsYouType('VG')
955
- // formatter2.input('2291234').should.equal('(229) 123-4')
956
- // formatter2.getNumber().country.should.equal('VG')
957
- // formatter2.getNumber().nationalNumber.should.equal('2842291234')
958
- // formatter2.getNumber().number.should.equal('+12842291234')
959
- })
960
-
961
- it('should work with out-of-country dialing prefix (like 00)', () => {
962
- const formatter = new AsYouType('DE')
963
- expect(formatter.input('00498911196611')).to.equal('00 49 89 11196611')
964
- expect(formatter.getCountry()).to.equal('DE')
965
- expect(formatter.formatter.template).to.equal('xx xx xx xxxxxxxx')
966
- expect(formatter.formatter.populatedNationalNumberTemplate).to.equal('89 11196611')
967
- expect(formatter.getTemplate()).to.equal('xx xx xx xxxxxxxx')
968
- expect(formatter.getNumber().country).to.equal('DE')
969
- expect(formatter.getNumber().nationalNumber).to.equal('8911196611')
970
- expect(formatter.getNumber().number).to.equal('+498911196611')
971
- })
972
-
973
- it('shouldn\'t choose a format when there\'re too many digits for any of them', () => {
974
- const formatter = new AsYouType('RU')
975
- formatter.input('89991112233')
976
- expect(formatter.formatter.chosenFormat.format()).to.equal('$1 $2-$3-$4')
977
- formatter.reset()
978
- formatter.input('899911122334')
979
- expect(formatter.formatter.chosenFormat).to.be.undefined
980
- })
981
-
982
- it('should get separator after national prefix', () => {
983
- // Russia.
984
- // Has separator after national prefix.
985
- const formatter = new AsYouType('RU')
986
- const format = formatter.metadata.formats()[0]
987
- expect(format.nationalPrefixFormattingRule()).to.equal('8 ($1)')
988
- expect(formatter.formatter.getSeparatorAfterNationalPrefix(format)).to.equal(' ')
989
- // Britain.
990
- // Has no separator after national prefix.
991
- const formatter2 = new AsYouType('GB')
992
- const format2 = formatter2.metadata.formats()[0]
993
- expect(format2.nationalPrefixFormattingRule()).to.equal('0$1')
994
- expect(formatter2.formatter.getSeparatorAfterNationalPrefix(format2)).to.equal('')
995
- })
996
-
997
- it('should return if the number is possible', () => {
998
- // National. Russia.
999
- const formatter = new AsYouType('RU')
1000
- expect(formatter.isPossible()).to.equal(false)
1001
- formatter.input('8')
1002
- expect(formatter.isPossible()).to.equal(false)
1003
- formatter.input('8005553535')
1004
- expect(formatter.isPossible()).to.equal(true)
1005
- formatter.input('5')
1006
- expect(formatter.isPossible()).to.equal(false)
1007
- })
1008
-
1009
- it('should return if the number is valid', () => {
1010
- // National. Russia.
1011
- const formatter = new AsYouType('RU')
1012
- expect(formatter.isValid()).to.equal(false)
1013
- formatter.input('88005553535')
1014
- expect(formatter.isValid()).to.equal(true)
1015
- formatter.input('5')
1016
- expect(formatter.isValid()).to.equal(false)
1017
- })
1018
-
1019
- it('should return if the number is international', () => {
1020
- // National. Russia.
1021
- const formatter = new AsYouType('RU')
1022
- expect(formatter.isInternational()).to.equal(false)
1023
- formatter.input('88005553535')
1024
- expect(formatter.isInternational()).to.equal(false)
1025
- // International. Russia.
1026
- const formatterInt = new AsYouType()
1027
- expect(formatterInt.isInternational()).to.equal(false)
1028
- formatterInt.input('+')
1029
- expect(formatterInt.isInternational()).to.equal(true)
1030
- formatterInt.input('78005553535')
1031
- expect(formatterInt.isInternational()).to.equal(true)
1032
- })
1033
-
1034
- it('should return country calling code part of the number', () => {
1035
- // National. Russia.
1036
- const formatter = new AsYouType('RU')
1037
- expect(formatter.getCountryCallingCode()).to.be.undefined
1038
- formatter.input('88005553535')
1039
- expect(formatter.getCountryCallingCode()).to.be.undefined
1040
- // International. Russia.
1041
- const formatterInt = new AsYouType()
1042
- expect(formatterInt.getCountryCallingCode()).to.be.undefined
1043
- formatterInt.input('+')
1044
- expect(formatterInt.getCountryCallingCode()).to.be.undefined
1045
- formatterInt.input('7')
1046
- expect(formatterInt.getCountryCallingCode()).to.equal('7')
1047
- formatterInt.input('8005553535')
1048
- expect(formatterInt.getCountryCallingCode()).to.equal('7')
1049
- })
1050
-
1051
- it('should return the country of the number', () => {
1052
- // National. Russia.
1053
- const formatter = new AsYouType('RU')
1054
- expect(formatter.getCountry()).to.be.undefined
1055
- formatter.input('8')
1056
- expect(formatter.getCountry()).to.equal('RU')
1057
- formatter.input('8005553535')
1058
- expect(formatter.getCountry()).to.equal('RU')
1059
- // International. Austria.
1060
- const formatterInt = new AsYouType()
1061
- expect(formatterInt.getCountry()).to.be.undefined
1062
- formatterInt.input('+')
1063
- expect(formatterInt.getCountry()).to.be.undefined
1064
- formatterInt.input('43')
1065
- expect(formatterInt.getCountry()).to.equal('AT')
1066
- // International. USA.
1067
- const formatterIntRu = new AsYouType()
1068
- expect(formatterIntRu.getCountry()).to.be.undefined
1069
- formatterIntRu.input('+')
1070
- expect(formatterIntRu.getCountry()).to.be.undefined
1071
- formatterIntRu.input('1')
1072
- expect(formatterIntRu.getCountry()).to.be.undefined
1073
- formatterIntRu.input('2133734253')
1074
- expect(formatterIntRu.getCountry()).to.equal('US')
1075
- formatterIntRu.input('1')
1076
- expect(formatterIntRu.getCountry()).to.be.undefined
1077
- })
1078
-
1079
- it('should parse a long IDD prefix', () => {
1080
- const formatter = new AsYouType('AU')
1081
- // `14880011` is a long IDD prefix in Australia.
1082
- expect(formatter.input('1')).to.equal('1')
1083
- expect(formatter.input('4')).to.equal('14')
1084
- expect(formatter.input('8')).to.equal('148')
1085
- expect(formatter.input('8')).to.equal('1488')
1086
- expect(formatter.input('0')).to.equal('14880')
1087
- expect(formatter.input('0')).to.equal('148800')
1088
- expect(formatter.input('1')).to.equal('1488001')
1089
- expect(formatter.input('1')).to.equal('14880011')
1090
- // As if were calling US using `14880011` IDD prefix,
1091
- // though that prefix could mean something else.
1092
- expect(formatter.input('1')).to.equal('14880011 1')
1093
- expect(formatter.input('2')).to.equal('14880011 1 2')
1094
- expect(formatter.input('1')).to.equal('14880011 1 21')
1095
- expect(formatter.input('3')).to.equal('14880011 1 213')
1096
- })
1097
-
1098
- it('should return the phone number characters entered by the user', () => {
1099
- const formatter = new AsYouType('RU')
1100
- expect(formatter.getChars()).to.equal('')
1101
- formatter.input('+123')
1102
- expect(formatter.getChars()).to.equal('+123')
1103
- formatter.reset()
1104
- formatter.input('123')
1105
- expect(formatter.getChars()).to.equal('123')
1106
- })
1107
-
1108
- // A test confirming the case when input `"11"` for country `"US"`
1109
- // produces `value` `"+11"`.
1110
- // https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/113
1111
- it('should determine the national (significant) part correctly when input with national prefix in US', () => {
1112
- const formatter = new AsYouType('US')
1113
- // As soon as the user has input `"11"`, no `format` matches
1114
- // those "national number" digits in the `"US"` country metadata.
1115
- // Since no `format` matches, the number doesn't seem like a valid one,
1116
- // so it attempts to see if the user "forgot" to input a `"+"` at the start.
1117
- // And it looks like they might've to.
1118
- // So it acts as if the leading `"+"` is there,
1119
- // as if the user's input is `"+11"`.
1120
- // See `AsYouType.fixMissingPlus()` function.
1121
- expect(formatter.input('1 122 222 2222 3')).to.equal('1 1 222 222 2223')
1122
- expect(formatter.getNumber().nationalNumber).to.equal('2222222223')
1123
- })
1124
- })
1125
-
1126
- describe('AsYouType.getNumberValue()', () => {
1127
- it('should return E.164 number value (national number, with national prefix, default country: US)', () => {
1128
- const formatter = new AsYouType('US')
1129
- expect(formatter.getNumberValue()).to.be.undefined
1130
- formatter.input('')
1131
- expect(formatter.getNumberValue()).to.be.undefined
1132
- formatter.input('1')
1133
- expect(formatter.getNumberValue()).to.equal('+1')
1134
- formatter.input('2')
1135
- expect(formatter.getNumberValue()).to.equal('+12')
1136
- formatter.input('1')
1137
- expect(formatter.getNumberValue()).to.equal('+121')
1138
- formatter.input('3')
1139
- expect(formatter.getNumberValue()).to.equal('+1213')
1140
- formatter.input('373-4253')
1141
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1142
- formatter.input('4')
1143
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1144
- })
1145
-
1146
- it('should return E.164 number value (national number, with national prefix, default calling code: 1)', () => {
1147
- const formatter = new AsYouType({ defaultCallingCode: '1' })
1148
- expect(formatter.getNumberValue()).to.be.undefined
1149
- formatter.input('')
1150
- expect(formatter.getNumberValue()).to.be.undefined
1151
- formatter.input('1')
1152
- expect(formatter.getNumberValue()).to.equal('+1')
1153
- formatter.input('2')
1154
- expect(formatter.getNumberValue()).to.equal('+12')
1155
- formatter.input('1')
1156
- expect(formatter.getNumberValue()).to.equal('+121')
1157
- formatter.input('3')
1158
- expect(formatter.getNumberValue()).to.equal('+1213')
1159
- formatter.input('373-4253')
1160
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1161
- formatter.input('4')
1162
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1163
- })
1164
-
1165
- it('should return E.164 number value (national number, default country: US)', () => {
1166
- const formatter = new AsYouType('US')
1167
- expect(formatter.getNumberValue()).to.be.undefined
1168
- formatter.input('')
1169
- expect(formatter.getNumberValue()).to.be.undefined
1170
- formatter.input('2')
1171
- expect(formatter.getNumberValue()).to.equal('+12')
1172
- formatter.input('1')
1173
- expect(formatter.getNumberValue()).to.equal('+121')
1174
- formatter.input('3')
1175
- expect(formatter.getNumberValue()).to.equal('+1213')
1176
- formatter.input('373-4253')
1177
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1178
- formatter.input('4')
1179
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1180
- })
1181
-
1182
- it('should return E.164 number value (national number, default calling code: 1)', () => {
1183
- const formatter = new AsYouType({ defaultCallingCode: '1' })
1184
- expect(formatter.getNumberValue()).to.be.undefined
1185
- formatter.input('')
1186
- expect(formatter.getNumberValue()).to.be.undefined
1187
- formatter.input('2')
1188
- expect(formatter.getNumberValue()).to.equal('+12')
1189
- formatter.input('1')
1190
- expect(formatter.getNumberValue()).to.equal('+121')
1191
- formatter.input('3')
1192
- expect(formatter.getNumberValue()).to.equal('+1213')
1193
- formatter.input('373-4253')
1194
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1195
- formatter.input('4')
1196
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1197
- })
1198
-
1199
- it('should return E.164 number value (international number, not a valid calling code)', () => {
1200
- const formatter = new AsYouType()
1201
- expect(formatter.getNumberValue()).to.be.undefined
1202
- formatter.input('')
1203
- expect(formatter.getNumberValue()).to.be.undefined
1204
- formatter.input('+')
1205
- expect(formatter.getNumberValue()).to.be.undefined
1206
- formatter.input('2150')
1207
- expect(formatter.getNumberValue()).to.equal('+2150')
1208
- })
1209
-
1210
- it('should return E.164 number value (international number, default country: US)', () => {
1211
- const formatter = new AsYouType('US')
1212
- expect(formatter.getNumberValue()).to.be.undefined
1213
- formatter.input('')
1214
- expect(formatter.getNumberValue()).to.be.undefined
1215
- formatter.input('+')
1216
- expect(formatter.getNumberValue()).to.be.undefined
1217
- formatter.input('1')
1218
- expect(formatter.getNumberValue()).to.equal('+1')
1219
- formatter.input('1')
1220
- expect(formatter.getNumberValue()).to.equal('+1')
1221
- formatter.input('2')
1222
- expect(formatter.getNumberValue()).to.equal('+12')
1223
- formatter.input('1')
1224
- expect(formatter.getNumberValue()).to.equal('+121')
1225
- formatter.input('3')
1226
- expect(formatter.getNumberValue()).to.equal('+1213')
1227
- formatter.input('373-4253')
1228
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1229
- formatter.input('4')
1230
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1231
- })
1232
-
1233
- it('should return E.164 number value (international number, other default country: RU)', () => {
1234
- const formatter = new AsYouType('RU')
1235
- expect(formatter.getNumberValue()).to.be.undefined
1236
- formatter.input('')
1237
- expect(formatter.getNumberValue()).to.be.undefined
1238
- formatter.input('+')
1239
- expect(formatter.getNumberValue()).to.be.undefined
1240
- formatter.input('1')
1241
- expect(formatter.getNumberValue()).to.equal('+1')
1242
- formatter.input('1')
1243
- expect(formatter.getNumberValue()).to.equal('+1')
1244
- formatter.input('2')
1245
- expect(formatter.getNumberValue()).to.equal('+12')
1246
- formatter.input('1')
1247
- expect(formatter.getNumberValue()).to.equal('+121')
1248
- formatter.input('3')
1249
- expect(formatter.getNumberValue()).to.equal('+1213')
1250
- formatter.input('373-4253')
1251
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1252
- formatter.input('4')
1253
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1254
- })
1255
-
1256
- it('should return E.164 number value (international number, default calling code: 1)', () => {
1257
- const formatter = new AsYouType('US', { defaultCallingCode: '1' })
1258
- expect(formatter.getNumberValue()).to.be.undefined
1259
- formatter.input('')
1260
- expect(formatter.getNumberValue()).to.be.undefined
1261
- formatter.input('+')
1262
- expect(formatter.getNumberValue()).to.be.undefined
1263
- formatter.input('1')
1264
- expect(formatter.getNumberValue()).to.equal('+1')
1265
- formatter.input('1')
1266
- expect(formatter.getNumberValue()).to.equal('+1')
1267
- formatter.input('2')
1268
- expect(formatter.getNumberValue()).to.equal('+12')
1269
- formatter.input('1')
1270
- expect(formatter.getNumberValue()).to.equal('+121')
1271
- formatter.input('3')
1272
- expect(formatter.getNumberValue()).to.equal('+1213')
1273
- formatter.input('373-4253')
1274
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1275
- formatter.input('4')
1276
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1277
- })
1278
-
1279
- it('should return E.164 number value (international number, other default calling code: 7)', () => {
1280
- const formatter = new AsYouType('US', { defaultCallingCode: '7' })
1281
- expect(formatter.getNumberValue()).to.be.undefined
1282
- formatter.input('')
1283
- expect(formatter.getNumberValue()).to.be.undefined
1284
- formatter.input('+')
1285
- expect(formatter.getNumberValue()).to.be.undefined
1286
- formatter.input('1')
1287
- expect(formatter.getNumberValue()).to.equal('+1')
1288
- formatter.input('1')
1289
- expect(formatter.getNumberValue()).to.equal('+1')
1290
- formatter.input('2')
1291
- expect(formatter.getNumberValue()).to.equal('+12')
1292
- formatter.input('1')
1293
- expect(formatter.getNumberValue()).to.equal('+121')
1294
- formatter.input('3')
1295
- expect(formatter.getNumberValue()).to.equal('+1213')
1296
- formatter.input('373-4253')
1297
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1298
- formatter.input('4')
1299
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1300
- })
1301
-
1302
- it('should return E.164 number value (international number)', () => {
1303
- const formatter = new AsYouType()
1304
- expect(formatter.getNumberValue()).to.be.undefined
1305
- formatter.input('')
1306
- expect(formatter.getNumberValue()).to.be.undefined
1307
- formatter.input('+')
1308
- expect(formatter.getNumberValue()).to.be.undefined
1309
- formatter.input('1')
1310
- expect(formatter.getNumberValue()).to.equal('+1')
1311
- formatter.input('1')
1312
- expect(formatter.getNumberValue()).to.equal('+1')
1313
- formatter.input('2')
1314
- expect(formatter.getNumberValue()).to.equal('+12')
1315
- formatter.input('1')
1316
- expect(formatter.getNumberValue()).to.equal('+121')
1317
- formatter.input('3')
1318
- expect(formatter.getNumberValue()).to.equal('+1213')
1319
- formatter.input('373-4253')
1320
- expect(formatter.getNumberValue()).to.equal('+12133734253')
1321
- formatter.input('4')
1322
- expect(formatter.getNumberValue()).to.equal('+121337342534')
1323
- })
1324
-
1325
- it('should return E.164 number value (national number) (no default country or calling code)', () => {
1326
- const formatter = new AsYouType()
1327
- expect(formatter.getNumberValue()).to.be.undefined
1328
- formatter.input('')
1329
- expect(formatter.getNumberValue()).to.be.undefined
1330
- formatter.input('1')
1331
- expect(formatter.getNumberValue()).to.be.undefined
1332
- formatter.input('12')
1333
- expect(formatter.getNumberValue()).to.be.undefined
1334
- formatter.input('3')
1335
- expect(formatter.getNumberValue()).to.be.undefined
1336
- formatter.input('373-4253')
1337
- expect(formatter.getNumberValue()).to.be.undefined
1338
- formatter.input('4')
1339
- expect(formatter.getNumberValue()).to.be.undefined
1340
- })
1341
-
1342
- it('should not drop any input digits', () => {
1343
- // Test "+529011234567" number, proactively ensuring that no formatting is applied,
1344
- // where a format is chosen that would otherwise have led to some digits being dropped.
1345
- const formatter = new AsYouType('MX')
1346
- expect(formatter.input('9')).to.equal('9')
1347
- expect(formatter.input('0')).to.equal('90')
1348
- expect(formatter.input('1')).to.equal('901')
1349
- expect(formatter.input('1')).to.equal('901 1')
1350
- expect(formatter.input('2')).to.equal('901 12')
1351
- expect(formatter.input('3')).to.equal('901 123')
1352
- expect(formatter.input('4')).to.equal('901 123 4')
1353
- expect(formatter.input('5')).to.equal('901 123 45')
1354
- expect(formatter.input('6')).to.equal('901 123 456')
1355
- expect(formatter.input('7')).to.equal('901 123 4567')
1356
- })
1357
-
1358
- it('should work for formats with no leading digits (`leadingDigitsPatternsCount === 0`)', function() {
1359
- const formatter = new AsYouType({
1360
- defaultCallingCode: 888
1361
- })
1362
- expect(formatter.input('1')).to.equal('1')
1363
- })
1364
-
1365
- it('should work for SK phone numbers', function() {
1366
- // There was a bug: "leading digits" `"2"` matched "leading digits pattern" `"90"`.
1367
- // The incorrect `.match()` function result was `{ oveflow: true }`
1368
- // while it should've been `undefined`.
1369
- // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/66
1370
- const formatter = new AsYouType('SK')
1371
- expect(formatter.input('090')).to.equal('090')
1372
- formatter.reset()
1373
- expect(formatter.input('080')).to.equal('080')
1374
- formatter.reset()
1375
- expect(formatter.input('059')).to.equal('059')
1376
- })
1377
-
1378
- it('should work for SK phone numbers (2)', function() {
1379
- // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/69
1380
- const formatter = new AsYouType('SK')
1381
- expect(formatter.input('421901222333')).to.equal('421 901 222 333')
1382
- expect(formatter.getTemplate()).to.equal('xxx xxx xxx xxx')
1383
- })
1384
-
1385
- it('should not choose `defaultCountry` over the "main" one when both the `defaultCountry` and the "main" one match the phone number', function() {
1386
- // This phone number matches both US and CA because they have the same
1387
- // regular expression for some weird reason.
1388
- // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103
1389
- const formatter = new AsYouType('CA')
1390
- formatter.input('8004001000')
1391
- expect(formatter.getNumber().country).not.to.equal('CA')
1392
- expect(formatter.getNumber().country).to.equal('US')
1393
-
1394
- // This phone number is specific to CA.
1395
- const formatter2 = new AsYouType('US')
1396
- formatter2.input('4389999999')
1397
- expect(formatter2.getNumber().country).to.equal('CA')
1398
-
1399
- // This phone number doesn't belong neither to CA nor to US.
1400
- // In fact, it doesn't belong to any country from the "NANPA" zone.
1401
- const formatter3 = new AsYouType('US')
1402
- formatter3.input('1111111111')
1403
- expect(formatter3.getNumber().country).to.equal('US')
1404
- })
1405
- })
1406
-
1407
- function type(something) {
1408
- return typeof something
1409
- }