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