libphonenumber-js 1.12.43 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/CHANGELOG.md +131 -104
  2. package/README.md +36 -6
  3. package/core/es6/index.d.cts +2 -0
  4. package/core/es6/index.d.ts +2 -0
  5. package/core/es6/index.js +39 -0
  6. package/core/es6/package.json +12 -0
  7. package/core/index.cjs +5 -0
  8. package/core/index.cjs.js +5 -0
  9. package/core/index.d.cts +3 -1
  10. package/core/index.d.ts +3 -1
  11. package/core/index.js +5 -0
  12. package/custom.d.ts +6 -4
  13. package/custom.js +5 -5
  14. package/es6-modern/AsYouType.js +445 -0
  15. package/es6-modern/AsYouType.js.map +1 -0
  16. package/es6-modern/AsYouType.test.js.map +1 -0
  17. package/es6-modern/AsYouTypeFormatter.PatternMatcher.js +195 -0
  18. package/es6-modern/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
  19. package/es6-modern/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
  20. package/es6-modern/AsYouTypeFormatter.PatternParser.js +184 -0
  21. package/es6-modern/AsYouTypeFormatter.PatternParser.js.map +1 -0
  22. package/es6-modern/AsYouTypeFormatter.PatternParser.test.js.map +1 -0
  23. package/es6-modern/AsYouTypeFormatter.complete.js +120 -0
  24. package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -0
  25. package/es6-modern/AsYouTypeFormatter.js +659 -0
  26. package/es6-modern/AsYouTypeFormatter.js.map +1 -0
  27. package/es6-modern/AsYouTypeFormatter.util.js +96 -0
  28. package/es6-modern/AsYouTypeFormatter.util.js.map +1 -0
  29. package/es6-modern/AsYouTypeFormatter.util.test.js.map +1 -0
  30. package/es6-modern/AsYouTypeParser.js +435 -0
  31. package/es6-modern/AsYouTypeParser.js.map +1 -0
  32. package/es6-modern/AsYouTypeState.js +104 -0
  33. package/es6-modern/AsYouTypeState.js.map +1 -0
  34. package/es6-modern/ParseError.js +15 -0
  35. package/es6-modern/ParseError.js.map +1 -0
  36. package/es6-modern/PhoneNumber.js +166 -0
  37. package/es6-modern/PhoneNumber.js.map +1 -0
  38. package/es6-modern/PhoneNumber.test.js.map +1 -0
  39. package/es6-modern/PhoneNumberMatcher.js +330 -0
  40. package/es6-modern/PhoneNumberMatcher.js.map +1 -0
  41. package/es6-modern/PhoneNumberMatcher.test.js.map +1 -0
  42. package/es6-modern/constants.js +31 -0
  43. package/es6-modern/constants.js.map +1 -0
  44. package/es6-modern/findNumbers/LRUCache.js +98 -0
  45. package/es6-modern/findNumbers/LRUCache.js.map +1 -0
  46. package/es6-modern/findNumbers/Leniency.js +311 -0
  47. package/es6-modern/findNumbers/Leniency.js.map +1 -0
  48. package/es6-modern/findNumbers/RegExpCache.js +20 -0
  49. package/es6-modern/findNumbers/RegExpCache.js.map +1 -0
  50. package/es6-modern/findNumbers/isValidCandidate.js +66 -0
  51. package/es6-modern/findNumbers/isValidCandidate.js.map +1 -0
  52. package/es6-modern/findNumbers/isValidPreCandidate.js +26 -0
  53. package/es6-modern/findNumbers/isValidPreCandidate.js.map +1 -0
  54. package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +63 -0
  55. package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
  56. package/es6-modern/findNumbers/parsePreCandidate.js +18 -0
  57. package/es6-modern/findNumbers/parsePreCandidate.js.map +1 -0
  58. package/es6-modern/findNumbers/utf-8.js +63 -0
  59. package/es6-modern/findNumbers/utf-8.js.map +1 -0
  60. package/es6-modern/findNumbers/util.js +26 -0
  61. package/es6-modern/findNumbers/util.js.map +1 -0
  62. package/es6-modern/findNumbers/util.test.js.map +1 -0
  63. package/es6-modern/findPhoneNumbersInText.js +19 -0
  64. package/es6-modern/findPhoneNumbersInText.js.map +1 -0
  65. package/es6-modern/findPhoneNumbersInText.test.js.map +1 -0
  66. package/es6-modern/format.js +204 -0
  67. package/es6-modern/format.js.map +1 -0
  68. package/es6-modern/format.test.js.map +1 -0
  69. package/es6-modern/formatIncompletePhoneNumber.js +18 -0
  70. package/es6-modern/formatIncompletePhoneNumber.js.map +1 -0
  71. package/es6-modern/formatIncompletePhoneNumber.test.js.map +1 -0
  72. package/es6-modern/formatPhoneNumberForMobileDialing.js +190 -0
  73. package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -0
  74. package/es6-modern/getCountries.js +5 -0
  75. package/es6-modern/getCountries.js.map +1 -0
  76. package/es6-modern/getCountries.test.js.map +1 -0
  77. package/es6-modern/getCountryCallingCode.js +3 -0
  78. package/es6-modern/getCountryCallingCode.js.map +1 -0
  79. package/es6-modern/getCountryCallingCode.test.js.map +1 -0
  80. package/es6-modern/getExampleNumber.js +7 -0
  81. package/es6-modern/getExampleNumber.js.map +1 -0
  82. package/es6-modern/getExampleNumber.test.js.map +1 -0
  83. package/es6-modern/helpers/RFC3966.js +63 -0
  84. package/es6-modern/helpers/RFC3966.js.map +1 -0
  85. package/es6-modern/helpers/RFC3966.test.js.map +1 -0
  86. package/es6-modern/helpers/applyInternationalSeparatorStyle.js +36 -0
  87. package/es6-modern/helpers/applyInternationalSeparatorStyle.js.map +1 -0
  88. package/es6-modern/helpers/applyInternationalSeparatorStyle.test.js.map +1 -0
  89. package/es6-modern/helpers/checkNumberLength.js +112 -0
  90. package/es6-modern/helpers/checkNumberLength.js.map +1 -0
  91. package/es6-modern/helpers/checkNumberLength.test.js.map +1 -0
  92. package/es6-modern/helpers/extension/createExtensionPattern.js +93 -0
  93. package/es6-modern/helpers/extension/createExtensionPattern.js.map +1 -0
  94. package/es6-modern/helpers/extension/extractExtension.js +30 -0
  95. package/es6-modern/helpers/extension/extractExtension.js.map +1 -0
  96. package/es6-modern/helpers/extractCountryCallingCode.js +142 -0
  97. package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -0
  98. package/es6-modern/helpers/extractCountryCallingCode.test.js.map +1 -0
  99. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +49 -0
  100. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -0
  101. package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +66 -0
  102. package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
  103. package/es6-modern/helpers/extractNationalNumber.js +117 -0
  104. package/es6-modern/helpers/extractNationalNumber.js.map +1 -0
  105. package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -0
  106. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +102 -0
  107. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -0
  108. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js.map +1 -0
  109. package/es6-modern/helpers/extractPhoneContext.js +82 -0
  110. package/es6-modern/helpers/extractPhoneContext.js.map +1 -0
  111. package/es6-modern/helpers/extractPhoneContext.test.js.map +1 -0
  112. package/es6-modern/helpers/formatNationalNumberUsingFormat.js +34 -0
  113. package/es6-modern/helpers/formatNationalNumberUsingFormat.js.map +1 -0
  114. package/es6-modern/helpers/getCountryByCallingCode.js +30 -0
  115. package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -0
  116. package/es6-modern/helpers/getCountryByNationalNumber.js +76 -0
  117. package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -0
  118. package/es6-modern/helpers/getIddPrefix.js +26 -0
  119. package/es6-modern/helpers/getIddPrefix.js.map +1 -0
  120. package/es6-modern/helpers/getNumberType.js +81 -0
  121. package/es6-modern/helpers/getNumberType.js.map +1 -0
  122. package/es6-modern/helpers/getNumberType.test.js.map +1 -0
  123. package/es6-modern/helpers/getPossibleCountriesForNumber.js +28 -0
  124. package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -0
  125. package/es6-modern/helpers/isObject.js +5 -0
  126. package/es6-modern/helpers/isObject.js.map +1 -0
  127. package/es6-modern/helpers/isViablePhoneNumber.js +77 -0
  128. package/es6-modern/helpers/isViablePhoneNumber.js.map +1 -0
  129. package/es6-modern/helpers/matchesEntirely.js +12 -0
  130. package/es6-modern/helpers/matchesEntirely.js.map +1 -0
  131. package/es6-modern/helpers/matchesEntirely.test.js.map +1 -0
  132. package/es6-modern/helpers/mergeArrays.js +23 -0
  133. package/es6-modern/helpers/mergeArrays.js.map +1 -0
  134. package/es6-modern/helpers/mergeArrays.test.js.map +1 -0
  135. package/es6-modern/helpers/parseDigits.js +111 -0
  136. package/es6-modern/helpers/parseDigits.js.map +1 -0
  137. package/es6-modern/helpers/parseDigits.test.js.map +1 -0
  138. package/es6-modern/helpers/stripIddPrefix.js +29 -0
  139. package/es6-modern/helpers/stripIddPrefix.js.map +1 -0
  140. package/es6-modern/helpers/stripIddPrefix.test.js.map +1 -0
  141. package/es6-modern/isPossible.js +77 -0
  142. package/es6-modern/isPossible.js.map +1 -0
  143. package/es6-modern/isPossible.test.js.map +1 -0
  144. package/es6-modern/isPossiblePhoneNumber.js +16 -0
  145. package/es6-modern/isPossiblePhoneNumber.js.map +1 -0
  146. package/es6-modern/isPossiblePhoneNumber.test.js.map +1 -0
  147. package/es6-modern/isValid.js +57 -0
  148. package/es6-modern/isValid.js.map +1 -0
  149. package/es6-modern/isValid.test.js.map +1 -0
  150. package/es6-modern/isValidPhoneNumber.js +16 -0
  151. package/es6-modern/isValidPhoneNumber.js.map +1 -0
  152. package/es6-modern/isValidPhoneNumber.test.js.map +1 -0
  153. package/es6-modern/legacy/PhoneNumberSearch.js +109 -0
  154. package/es6-modern/legacy/PhoneNumberSearch.js.map +1 -0
  155. package/es6-modern/legacy/findNumbers.js +16 -0
  156. package/es6-modern/legacy/findNumbers.js.map +1 -0
  157. package/es6-modern/legacy/findNumbers.test.js.map +1 -0
  158. package/es6-modern/legacy/findPhoneNumbers.js +26 -0
  159. package/es6-modern/legacy/findPhoneNumbers.js.map +1 -0
  160. package/es6-modern/legacy/findPhoneNumbers.test.js.map +1 -0
  161. package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js +79 -0
  162. package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js.map +1 -0
  163. package/es6-modern/legacy/format.js +97 -0
  164. package/es6-modern/legacy/format.js.map +1 -0
  165. package/es6-modern/legacy/format.test.js.map +1 -0
  166. package/es6-modern/legacy/getNumberType.js +104 -0
  167. package/es6-modern/legacy/getNumberType.js.map +1 -0
  168. package/es6-modern/legacy/getNumberType.test.js.map +1 -0
  169. package/es6-modern/legacy/isPossibleNumber.js +30 -0
  170. package/es6-modern/legacy/isPossibleNumber.js.map +1 -0
  171. package/es6-modern/legacy/isPossibleNumber.test.js.map +1 -0
  172. package/es6-modern/legacy/isValidNumber.js +17 -0
  173. package/es6-modern/legacy/isValidNumber.js.map +1 -0
  174. package/es6-modern/legacy/isValidNumber.test.js.map +1 -0
  175. package/es6-modern/legacy/isValidNumberForRegion.js +65 -0
  176. package/es6-modern/legacy/isValidNumberForRegion.js.map +1 -0
  177. package/es6-modern/legacy/isValidNumberForRegion.test.js.map +1 -0
  178. package/es6-modern/legacy/isValidNumberForRegion_.js +14 -0
  179. package/es6-modern/legacy/isValidNumberForRegion_.js.map +1 -0
  180. package/es6-modern/legacy/parse.js +11 -0
  181. package/es6-modern/legacy/parse.js.map +1 -0
  182. package/es6-modern/legacy/parse.test.js.map +1 -0
  183. package/es6-modern/legacy/searchNumbers.js +32 -0
  184. package/es6-modern/legacy/searchNumbers.js.map +1 -0
  185. package/es6-modern/legacy/searchNumbers.test.js.map +1 -0
  186. package/es6-modern/metadata.js +494 -0
  187. package/es6-modern/metadata.js.map +1 -0
  188. package/es6-modern/metadata.test.js.map +1 -0
  189. package/es6-modern/normalizeArguments.js +52 -0
  190. package/es6-modern/normalizeArguments.js.map +1 -0
  191. package/es6-modern/parse.js +341 -0
  192. package/es6-modern/parse.js.map +1 -0
  193. package/es6-modern/parse.test.js.map +1 -0
  194. package/es6-modern/parseIncompletePhoneNumber.js +70 -0
  195. package/es6-modern/parseIncompletePhoneNumber.js.map +1 -0
  196. package/es6-modern/parseIncompletePhoneNumber.test.js.map +1 -0
  197. package/es6-modern/parsePhoneNumber.js +11 -0
  198. package/es6-modern/parsePhoneNumber.js.map +1 -0
  199. package/es6-modern/parsePhoneNumber.test.js.map +1 -0
  200. package/es6-modern/parsePhoneNumberWithError.js +11 -0
  201. package/es6-modern/parsePhoneNumberWithError.js.map +1 -0
  202. package/es6-modern/parsePhoneNumberWithError.test.js.map +1 -0
  203. package/es6-modern/parsePhoneNumberWithError_.js +8 -0
  204. package/es6-modern/parsePhoneNumberWithError_.js.map +1 -0
  205. package/es6-modern/parsePhoneNumber_.js +24 -0
  206. package/es6-modern/parsePhoneNumber_.js.map +1 -0
  207. package/es6-modern/searchPhoneNumbersInText.js +31 -0
  208. package/es6-modern/searchPhoneNumbersInText.js.map +1 -0
  209. package/es6-modern/searchPhoneNumbersInText.test.js.map +1 -0
  210. package/es6-modern/tools/semver-compare.js +27 -0
  211. package/es6-modern/tools/semver-compare.js.map +1 -0
  212. package/es6-modern/tools/semver-compare.test.js.map +1 -0
  213. package/es6-modern/validatePhoneNumberLength.js +35 -0
  214. package/es6-modern/validatePhoneNumberLength.js.map +1 -0
  215. package/es6-modern/validatePhoneNumberLength.test.js.map +1 -0
  216. package/index.cjs +4 -0
  217. package/index.cjs.js +4 -0
  218. package/index.d.cts +5 -3
  219. package/index.d.ts +5 -3
  220. package/index.js +5 -0
  221. package/max/es6/exports/AsYouType.js +11 -0
  222. package/max/es6/exports/Metadata.js +11 -0
  223. package/max/es6/exports/PhoneNumber.js +11 -0
  224. package/max/es6/exports/PhoneNumberMatcher.js +11 -0
  225. package/max/es6/exports/findNumbers.js +6 -0
  226. package/max/es6/exports/findPhoneNumbersInText.js +6 -0
  227. package/max/es6/exports/formatIncompletePhoneNumber.js +6 -0
  228. package/max/es6/exports/getCountries.js +6 -0
  229. package/max/es6/exports/getCountryCallingCode.js +6 -0
  230. package/max/es6/exports/getExampleNumber.js +6 -0
  231. package/max/es6/exports/getExtPrefix.js +6 -0
  232. package/max/es6/exports/isPossiblePhoneNumber.js +6 -0
  233. package/max/es6/exports/isSupportedCountry.js +6 -0
  234. package/max/es6/exports/isValidPhoneNumber.js +6 -0
  235. package/max/es6/exports/parsePhoneNumber.js +7 -0
  236. package/max/es6/exports/parsePhoneNumberWithError.js +7 -0
  237. package/max/es6/exports/searchNumbers.js +6 -0
  238. package/max/es6/exports/searchPhoneNumbersInText.js +6 -0
  239. package/max/es6/exports/validatePhoneNumberLength.js +6 -0
  240. package/max/es6/exports/withMetadataArgument.js +13 -0
  241. package/max/es6/index.d.cts +2 -0
  242. package/max/es6/index.d.ts +2 -0
  243. package/max/es6/index.js +44 -0
  244. package/max/es6/package.json +12 -0
  245. package/max/index.cjs +5 -0
  246. package/max/index.cjs.js +5 -0
  247. package/max/index.d.cts +3 -1
  248. package/max/index.d.ts +3 -1
  249. package/max/index.js +4 -0
  250. package/min/es6/exports/AsYouType.js +11 -0
  251. package/min/es6/exports/Metadata.js +11 -0
  252. package/min/es6/exports/PhoneNumber.js +11 -0
  253. package/min/es6/exports/PhoneNumberMatcher.js +11 -0
  254. package/min/es6/exports/findNumbers.js +6 -0
  255. package/min/es6/exports/findPhoneNumbersInText.js +6 -0
  256. package/min/es6/exports/formatIncompletePhoneNumber.js +6 -0
  257. package/min/es6/exports/getCountries.js +6 -0
  258. package/min/es6/exports/getCountryCallingCode.js +6 -0
  259. package/min/es6/exports/getExampleNumber.js +6 -0
  260. package/min/es6/exports/getExtPrefix.js +6 -0
  261. package/min/es6/exports/isPossiblePhoneNumber.js +6 -0
  262. package/min/es6/exports/isSupportedCountry.js +6 -0
  263. package/min/es6/exports/isValidPhoneNumber.js +6 -0
  264. package/min/es6/exports/parsePhoneNumber.js +7 -0
  265. package/min/es6/exports/parsePhoneNumberWithError.js +7 -0
  266. package/min/es6/exports/searchNumbers.js +6 -0
  267. package/min/es6/exports/searchPhoneNumbersInText.js +6 -0
  268. package/min/es6/exports/validatePhoneNumberLength.js +6 -0
  269. package/min/es6/exports/withMetadataArgument.js +13 -0
  270. package/min/es6/index.d.cts +2 -0
  271. package/min/es6/index.d.ts +2 -0
  272. package/min/es6/index.js +44 -0
  273. package/min/es6/package.json +12 -0
  274. package/min/index.cjs +5 -0
  275. package/min/index.cjs.js +5 -0
  276. package/min/index.d.cts +3 -1
  277. package/min/index.d.ts +3 -1
  278. package/min/index.js +3 -0
  279. package/mobile/es6/exports/AsYouType.js +11 -0
  280. package/mobile/es6/exports/Metadata.js +11 -0
  281. package/mobile/es6/exports/PhoneNumber.js +11 -0
  282. package/mobile/es6/exports/PhoneNumberMatcher.js +11 -0
  283. package/mobile/es6/exports/findNumbers.js +6 -0
  284. package/mobile/es6/exports/findPhoneNumbersInText.js +6 -0
  285. package/mobile/es6/exports/formatIncompletePhoneNumber.js +6 -0
  286. package/mobile/es6/exports/getCountries.js +6 -0
  287. package/mobile/es6/exports/getCountryCallingCode.js +6 -0
  288. package/mobile/es6/exports/getExampleNumber.js +6 -0
  289. package/mobile/es6/exports/getExtPrefix.js +6 -0
  290. package/mobile/es6/exports/isPossiblePhoneNumber.js +6 -0
  291. package/mobile/es6/exports/isSupportedCountry.js +6 -0
  292. package/mobile/es6/exports/isValidPhoneNumber.js +6 -0
  293. package/mobile/es6/exports/parsePhoneNumber.js +7 -0
  294. package/mobile/es6/exports/parsePhoneNumberWithError.js +7 -0
  295. package/mobile/es6/exports/searchNumbers.js +6 -0
  296. package/mobile/es6/exports/searchPhoneNumbersInText.js +6 -0
  297. package/mobile/es6/exports/validatePhoneNumberLength.js +6 -0
  298. package/mobile/es6/exports/withMetadataArgument.js +13 -0
  299. package/mobile/es6/index.d.cts +2 -0
  300. package/mobile/es6/index.d.ts +2 -0
  301. package/mobile/es6/index.js +44 -0
  302. package/mobile/es6/package.json +12 -0
  303. package/mobile/index.cjs +5 -0
  304. package/mobile/index.cjs.js +5 -0
  305. package/mobile/index.d.cts +3 -1
  306. package/mobile/index.d.ts +3 -1
  307. package/mobile/index.js +4 -0
  308. package/package.json +28 -9
  309. package/types.d.cts +54 -21
  310. package/types.d.ts +54 -21
@@ -0,0 +1,659 @@
1
+ import { DIGIT_PLACEHOLDER, countOccurences, repeat, cutAndStripNonPairedParens, closeNonPairedParens, stripNonPairedParens, populateTemplateWithDigits } from './AsYouTypeFormatter.util.js';
2
+ import formatCompleteNumber, { canFormatCompleteNumber } from './AsYouTypeFormatter.complete.js';
3
+ import PatternMatcher from './AsYouTypeFormatter.PatternMatcher.js';
4
+ import parseDigits from './helpers/parseDigits.js';
5
+ export { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.util.js';
6
+ import { FIRST_GROUP_PATTERN } from './helpers/formatNationalNumberUsingFormat.js';
7
+ import { VALID_PUNCTUATION } from './constants.js';
8
+ import applyInternationalSeparatorStyle from './helpers/applyInternationalSeparatorStyle.js';
9
+
10
+ // Used in phone number format template creation.
11
+ // Could be any digit, I guess.
12
+ const DUMMY_DIGIT = '9';
13
+ // I don't know why is it exactly `15`
14
+ const LONGEST_NATIONAL_PHONE_NUMBER_LENGTH = 15;
15
+ // Create a phone number consisting only of the digit 9 that matches the
16
+ // `number_pattern` by applying the pattern to the "longest phone number" string.
17
+ const LONGEST_DUMMY_PHONE_NUMBER = repeat(DUMMY_DIGIT, LONGEST_NATIONAL_PHONE_NUMBER_LENGTH);
18
+
19
+ // A set of characters that, if found in a national prefix formatting rules, are an indicator to
20
+ // us that we should separate the national prefix from the number when formatting.
21
+ const NATIONAL_PREFIX_SEPARATORS_PATTERN = /[- ]/;
22
+
23
+ // Deprecated: Google has removed some formatting pattern related code from their repo.
24
+ // https://github.com/googlei18n/libphonenumber/commit/a395b4fef3caf57c4bc5f082e1152a4d2bd0ba4c
25
+ // "We no longer have numbers in formatting matching patterns, only \d."
26
+ // Because this library supports generating custom metadata
27
+ // some users may still be using old metadata so the relevant
28
+ // code seems to stay until some next major version update.
29
+ const SUPPORT_LEGACY_FORMATTING_PATTERNS = true;
30
+
31
+ // A pattern that is used to match character classes in regular expressions.
32
+ // An example of a character class is "[1-4]".
33
+ const CREATE_CHARACTER_CLASS_PATTERN = SUPPORT_LEGACY_FORMATTING_PATTERNS && (() => /\[([^\[\]])*\]/g);
34
+
35
+ // Any digit in a regular expression that actually denotes a digit. For
36
+ // example, in the regular expression "80[0-2]\d{6,10}", the first 2 digits
37
+ // (8 and 0) are standalone digits, but the rest are not.
38
+ // Two look-aheads are needed because the number following \\d could be a
39
+ // two-digit number, since the phone number can be as long as 15 digits.
40
+ const CREATE_STANDALONE_DIGIT_PATTERN = SUPPORT_LEGACY_FORMATTING_PATTERNS && (() => /\d(?=[^,}][^,}])/g);
41
+
42
+ // A regular expression that is used to determine if a `format` is
43
+ // suitable to be used in the "as you type formatter".
44
+ // A `format` is suitable when the resulting formatted number has
45
+ // the same digits as the user has entered.
46
+ //
47
+ // In the simplest case, that would mean that the format
48
+ // doesn't add any additional digits when formatting a number.
49
+ // Google says that it also shouldn't add "star" (`*`) characters,
50
+ // like it does in some Israeli formats.
51
+ // Such basic format would only contain "valid punctuation"
52
+ // and "captured group" identifiers ($1, $2, etc).
53
+ //
54
+ // An example of a format that adds additional digits:
55
+ //
56
+ // Country: `AR` (Argentina).
57
+ // Format:
58
+ // {
59
+ // "pattern": "(\\d)(\\d{2})(\\d{4})(\\d{4})",
60
+ // "leading_digits_patterns": ["91"],
61
+ // "national_prefix_formatting_rule": "0$1",
62
+ // "format": "$2 15-$3-$4",
63
+ // "international_format": "$1 $2 $3-$4"
64
+ // }
65
+ //
66
+ // In the format above, the `format` adds `15` to the digits when formatting a number.
67
+ // A sidenote: this format actually is suitable because `national_prefix_for_parsing`
68
+ // has previously removed `15` from a national number, so re-adding `15` in `format`
69
+ // doesn't actually result in any extra digits added to user's input.
70
+ // But verifying that would be a complex procedure, so the code chooses a simpler path:
71
+ // it simply filters out all `format`s that contain anything but "captured group" ids.
72
+ //
73
+ // This regular expression is called `ELIGIBLE_FORMAT_PATTERN` in Google's
74
+ // `libphonenumber` code.
75
+ //
76
+ const NON_ALTERING_FORMAT_REG_EXP = new RegExp('[' + VALID_PUNCTUATION + ']*' +
77
+ // Google developers say:
78
+ // "We require that the first matching group is present in the
79
+ // output pattern to ensure no data is lost while formatting."
80
+ '\\$1' + '[' + VALID_PUNCTUATION + ']*' + '(\\$\\d[' + VALID_PUNCTUATION + ']*)*' + '$');
81
+
82
+ // This is the minimum length of the leading digits of a phone number
83
+ // to guarantee the first "leading digits pattern" for a phone number format
84
+ // to be preemptive.
85
+ const MIN_LEADING_DIGITS_LENGTH = 3;
86
+ export default class AsYouTypeFormatter {
87
+ constructor({
88
+ state,
89
+ metadata
90
+ }) {
91
+ this.metadata = metadata;
92
+ this.resetFormat();
93
+ }
94
+ resetFormat() {
95
+ this.chosenFormat = undefined;
96
+ this.template = undefined;
97
+ this.nationalNumberTemplate = undefined;
98
+ this.populatedNationalNumberTemplate = undefined;
99
+ this.populatedNationalNumberTemplatePosition = -1;
100
+ }
101
+ reset(numberingPlan, state) {
102
+ this.resetFormat();
103
+ if (numberingPlan) {
104
+ this.isNANP = numberingPlan.callingCode() === '1';
105
+ this.matchingFormats = numberingPlan.formats();
106
+ if (state.nationalSignificantNumber) {
107
+ this.narrowDownMatchingFormats(state);
108
+ }
109
+ } else {
110
+ this.isNANP = undefined;
111
+ this.matchingFormats = [];
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Formats an updated phone number.
117
+ * @param {string} nextDigits — Additional phone number digits.
118
+ * @param {object} state — `AsYouType` state.
119
+ * @return {[string]} Returns undefined if the updated phone number can't be formatted using any of the available formats.
120
+ */
121
+ format(nextDigits, state) {
122
+ // See if the phone number digits can be formatted as a complete phone number.
123
+ // If not, use the results from `formatNationalNumberWithNextDigits()`,
124
+ // which formats based on the chosen formatting pattern.
125
+ //
126
+ // Attempting to format complete phone number first is how it's done
127
+ // in Google's `libphonenumber`, so this library just follows it.
128
+ // Google's `libphonenumber` code doesn't explain in detail why does it
129
+ // attempt to format digits as a complete phone number
130
+ // instead of just going with a previoulsy (or newly) chosen `format`:
131
+ //
132
+ // "Checks to see if there is an exact pattern match for these digits.
133
+ // If so, we should use this instead of any other formatting template
134
+ // whose leadingDigitsPattern also matches the input."
135
+ //
136
+ if (canFormatCompleteNumber(state.nationalSignificantNumber, state.country, this.metadata)) {
137
+ for (const format of this.matchingFormats) {
138
+ const formattedCompleteNumber = formatCompleteNumber(state, format, {
139
+ metadata: this.metadata,
140
+ shouldTryNationalPrefixFormattingRule: format => this.shouldTryNationalPrefixFormattingRule(format, {
141
+ international: state.international,
142
+ nationalPrefix: state.nationalPrefix
143
+ }),
144
+ getSeparatorAfterNationalPrefix: format => this.getSeparatorAfterNationalPrefix(format)
145
+ });
146
+ if (formattedCompleteNumber) {
147
+ this.resetFormat();
148
+ this.chosenFormat = format;
149
+ this.setNationalNumberTemplate(formattedCompleteNumber.replace(/\d/g, DIGIT_PLACEHOLDER), state);
150
+ this.populatedNationalNumberTemplate = formattedCompleteNumber;
151
+ // With a new formatting template, the matched position
152
+ // using the old template needs to be reset.
153
+ this.populatedNationalNumberTemplatePosition = this.template.lastIndexOf(DIGIT_PLACEHOLDER);
154
+ return formattedCompleteNumber;
155
+ }
156
+ }
157
+ }
158
+ // Format the digits as a partial (incomplete) phone number
159
+ // using the previously chosen formatting pattern (or a newly chosen one).
160
+ return this.formatNationalNumberWithNextDigits(nextDigits, state);
161
+ }
162
+
163
+ // Formats the next phone number digits.
164
+ formatNationalNumberWithNextDigits(nextDigits, state) {
165
+ const previouslyChosenFormat = this.chosenFormat;
166
+
167
+ // Choose a format from the list of matching ones.
168
+ const newlyChosenFormat = this.chooseFormat(state);
169
+ if (newlyChosenFormat) {
170
+ if (newlyChosenFormat === previouslyChosenFormat) {
171
+ // If it can format the next (current) digits
172
+ // using the previously chosen phone number format
173
+ // then return the updated formatted number.
174
+ return this.formatNextNationalNumberDigits(nextDigits);
175
+ } else {
176
+ // If a more appropriate phone number format
177
+ // has been chosen for these "leading digits",
178
+ // then re-format the national phone number part
179
+ // using the newly selected format.
180
+ return this.formatNextNationalNumberDigits(state.getNationalDigits());
181
+ }
182
+ }
183
+ }
184
+ narrowDownMatchingFormats({
185
+ nationalSignificantNumber,
186
+ nationalPrefix,
187
+ international
188
+ }) {
189
+ const leadingDigits = nationalSignificantNumber;
190
+
191
+ // "leading digits" pattern list starts with a
192
+ // "leading digits" pattern fitting a maximum of 3 leading digits.
193
+ // So, after a user inputs 3 digits of a national (significant) phone number
194
+ // this national (significant) number can already be formatted.
195
+ // The next "leading digits" pattern is for 4 leading digits max,
196
+ // and the "leading digits" pattern after it is for 5 leading digits max, etc.
197
+
198
+ // This implementation is different from Google's
199
+ // in that it searches for a fitting format
200
+ // even if the user has entered less than
201
+ // `MIN_LEADING_DIGITS_LENGTH` digits of a national number.
202
+ // Because some leading digit patterns already match for a single first digit.
203
+ let leadingDigitsPatternIndex = leadingDigits.length - MIN_LEADING_DIGITS_LENGTH;
204
+ if (leadingDigitsPatternIndex < 0) {
205
+ leadingDigitsPatternIndex = 0;
206
+ }
207
+ this.matchingFormats = this.matchingFormats.filter(format => this.formatSuits(format, international, nationalPrefix) && this.formatMatches(format, leadingDigits, leadingDigitsPatternIndex));
208
+
209
+ // If there was a phone number format chosen
210
+ // and it no longer holds given the new leading digits then reset it.
211
+ // The test for this `if` condition is marked as:
212
+ // "Reset a chosen format when it no longer holds given the new leading digits".
213
+ // To construct a valid test case for this one can find a country
214
+ // in `PhoneNumberMetadata.xml` yielding one format for 3 `<leadingDigits>`
215
+ // and yielding another format for 4 `<leadingDigits>` (Australia in this case).
216
+ if (this.chosenFormat && this.matchingFormats.indexOf(this.chosenFormat) === -1) {
217
+ this.resetFormat();
218
+ }
219
+ }
220
+ formatSuits(format, international, nationalPrefix) {
221
+ // When a prefix before a national (significant) number is
222
+ // simply a national prefix, then it's parsed as `this.nationalPrefix`.
223
+ // In more complex cases, a prefix before national (significant) number
224
+ // could include a national prefix as well as some "capturing groups",
225
+ // and in that case there's no info whether a national prefix has been parsed.
226
+ // If national prefix is not used when formatting a phone number
227
+ // using this format, but a national prefix has been entered by the user,
228
+ // and was extracted, then discard such phone number format.
229
+ // In Google's "AsYouType" formatter code, the equivalent would be this part:
230
+ // https://github.com/google/libphonenumber/blob/0a45cfd96e71cad8edb0e162a70fcc8bd9728933/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java#L175-L184
231
+ if (nationalPrefix && !format.usesNationalPrefix() &&
232
+ // !format.domesticCarrierCodeFormattingRule() &&
233
+ !format.nationalPrefixIsOptionalWhenFormattingInNationalFormat()) {
234
+ return false;
235
+ }
236
+ // If national prefix is mandatory for this phone number format
237
+ // and there're no guarantees that a national prefix is present in user input
238
+ // then discard this phone number format as not suitable.
239
+ // In Google's "AsYouType" formatter code, the equivalent would be this part:
240
+ // https://github.com/google/libphonenumber/blob/0a45cfd96e71cad8edb0e162a70fcc8bd9728933/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java#L185-L193
241
+ if (!international && !nationalPrefix && format.nationalPrefixIsMandatoryWhenFormattingInNationalFormat()) {
242
+ return false;
243
+ }
244
+ return true;
245
+ }
246
+ formatMatches(format, leadingDigits, leadingDigitsPatternIndex) {
247
+ const leadingDigitsPatternsCount = format.leadingDigitsPatterns().length;
248
+
249
+ // If this format is not restricted to a certain
250
+ // leading digits pattern then it fits.
251
+ // The test case could be found by searching for "leadingDigitsPatternsCount === 0".
252
+ if (leadingDigitsPatternsCount === 0) {
253
+ return true;
254
+ }
255
+
256
+ // Start narrowing down the list of possible formats based on the leading digits.
257
+ // (only previously matched formats take part in the narrowing down process)
258
+
259
+ // `leading_digits_patterns` start with 3 digits min
260
+ // and then go up from there one digit at a time.
261
+ leadingDigitsPatternIndex = Math.min(leadingDigitsPatternIndex, leadingDigitsPatternsCount - 1);
262
+ const leadingDigitsPattern = format.leadingDigitsPatterns()[leadingDigitsPatternIndex];
263
+
264
+ // Google imposes a requirement on the leading digits
265
+ // to be minimum 3 digits long in order to be eligible
266
+ // for checking those with a leading digits pattern.
267
+ //
268
+ // Since `leading_digits_patterns` start with 3 digits min,
269
+ // Google's original `libphonenumber` library only starts
270
+ // excluding any non-matching formats only when the
271
+ // national number entered so far is at least 3 digits long,
272
+ // otherwise format matching would give false negatives.
273
+ //
274
+ // For example, when the digits entered so far are `2`
275
+ // and the leading digits pattern is `21` –
276
+ // it's quite obvious in this case that the format could be the one
277
+ // but due to the absence of further digits it would give false negative.
278
+ //
279
+ // Also, `leading_digits_patterns` doesn't always correspond to a single
280
+ // digits count. For example, `60|8` pattern would already match `8`
281
+ // but the `60` part would require having at least two leading digits,
282
+ // so the whole pattern would require inputting two digits first in order to
283
+ // decide on whether it matches the input, even when the input is "80".
284
+ //
285
+ // This library — `libphonenumber-js` — allows filtering by `leading_digits_patterns`
286
+ // even when there's only 1 or 2 digits of the national (significant) number.
287
+ // To do that, it uses a non-strict pattern matcher written specifically for that.
288
+ //
289
+ if (leadingDigits.length < MIN_LEADING_DIGITS_LENGTH) {
290
+ // Before leading digits < 3 matching was implemented:
291
+ // return true
292
+ //
293
+ // After leading digits < 3 matching was implemented:
294
+ try {
295
+ return new PatternMatcher(leadingDigitsPattern).match(leadingDigits, {
296
+ allowOverflow: true
297
+ }) !== undefined;
298
+ } catch (error) /* istanbul ignore next */{
299
+ // There's a slight possibility that there could be some undiscovered bug
300
+ // in the pattern matcher code. Since the "leading digits < 3 matching"
301
+ // feature is not "essential" for operation, it can fall back to the old way
302
+ // in case of any issues rather than halting the application's execution.
303
+ console.error(error);
304
+ return true;
305
+ }
306
+ }
307
+
308
+ // If at least `MIN_LEADING_DIGITS_LENGTH` digits of a national number are
309
+ // available then use the usual regular expression matching.
310
+ //
311
+ // The whole pattern is wrapped in round brackets (`()`) because
312
+ // the pattern can use "or" operator (`|`) at the top level of the pattern.
313
+ //
314
+ return new RegExp(`^(${leadingDigitsPattern})`).test(leadingDigits);
315
+ }
316
+ getFormatFormat(format, international) {
317
+ return international ? format.internationalFormat() : format.format();
318
+ }
319
+ chooseFormat(state) {
320
+ // When there are multiple available formats, the formatter uses the first
321
+ // format where a formatting template could be created.
322
+ //
323
+ // For some weird reason, `istanbul` says "else path not taken"
324
+ // for the `for of` line below. Supposedly that means that
325
+ // the loop doesn't ever go over the last element in the list.
326
+ // That's true because there always is `this.chosenFormat`
327
+ // when `this.matchingFormats` is non-empty.
328
+ // And, for some weird reason, it doesn't think that the case
329
+ // with empty `this.matchingFormats` qualifies for a valid "else" path.
330
+ // So simply muting this `istanbul` warning.
331
+ // It doesn't skip the contents of the `for of` loop,
332
+ // it just skips the `for of` line.
333
+ //
334
+ /* istanbul ignore next */
335
+ for (const format of this.matchingFormats.slice()) {
336
+ // If this format is currently being used
337
+ // and is still suitable, then stick to it.
338
+ if (this.chosenFormat === format) {
339
+ break;
340
+ }
341
+ // Sometimes, a formatting rule inserts additional digits in a phone number,
342
+ // and "as you type" formatter can't do that: it should only use the digits
343
+ // that the user has input.
344
+ //
345
+ // For example, in Argentina, there's a format for mobile phone numbers:
346
+ //
347
+ // {
348
+ // "pattern": "(\\d)(\\d{2})(\\d{4})(\\d{4})",
349
+ // "leading_digits_patterns": ["91"],
350
+ // "national_prefix_formatting_rule": "0$1",
351
+ // "format": "$2 15-$3-$4",
352
+ // "international_format": "$1 $2 $3-$4"
353
+ // }
354
+ //
355
+ // In that format, `international_format` is used instead of `format`
356
+ // because `format` inserts `15` in the formatted number,
357
+ // and `AsYouType` formatter should only use the digits
358
+ // the user has actually input, without adding any extra digits.
359
+ // In this case, it wouldn't make a difference, because the `15`
360
+ // is first stripped when applying `national_prefix_for_parsing`
361
+ // and then re-added when using `format`, so in reality it doesn't
362
+ // add any new digits to the number, but to detect that, the code
363
+ // would have to be more complex: it would have to try formatting
364
+ // the digits using the format and then see if any digits have
365
+ // actually been added or removed, and then, every time a new digit
366
+ // is input, it should re-check whether the chosen format doesn't
367
+ // alter the digits.
368
+ //
369
+ // Google's code doesn't go that far, and so does this library:
370
+ // it simply requires that a `format` doesn't add any additonal
371
+ // digits to user's input.
372
+ //
373
+ // Also, people in general should move from inputting phone numbers
374
+ // in national format (possibly with national prefixes)
375
+ // and use international phone number format instead:
376
+ // it's a logical thing in the modern age of mobile phones,
377
+ // globalization and the internet.
378
+ //
379
+ /* istanbul ignore if */
380
+ if (!NON_ALTERING_FORMAT_REG_EXP.test(this.getFormatFormat(format, state.international))) {
381
+ continue;
382
+ }
383
+ if (!this.createTemplateForFormat(format, state)) {
384
+ // Remove the format if it can't generate a template.
385
+ this.matchingFormats = this.matchingFormats.filter(_ => _ !== format);
386
+ continue;
387
+ }
388
+ this.chosenFormat = format;
389
+ break;
390
+ }
391
+ if (!this.chosenFormat) {
392
+ // No format matches the national (significant) phone number.
393
+ this.resetFormat();
394
+ }
395
+ return this.chosenFormat;
396
+ }
397
+ createTemplateForFormat(format, state) {
398
+ // The formatter doesn't format numbers when numberPattern contains '|', e.g.
399
+ // (20|3)\d{4}. In those cases we quickly return.
400
+ // (Though there's no such format in current metadata)
401
+ /* istanbul ignore if */
402
+ if (SUPPORT_LEGACY_FORMATTING_PATTERNS && format.pattern().indexOf('|') >= 0) {
403
+ return;
404
+ }
405
+ // Get formatting template for this phone number format
406
+ const template = this.getTemplateForFormat(format, state);
407
+ // If the national number entered is too long
408
+ // for any phone number format, then abort.
409
+ if (template) {
410
+ this.setNationalNumberTemplate(template, state);
411
+ return true;
412
+ }
413
+ }
414
+ getSeparatorAfterNationalPrefix(format) {
415
+ // `US` metadata doesn't have a `national_prefix_formatting_rule`,
416
+ // so the `if` condition below doesn't apply to `US`,
417
+ // but in reality there shoudl be a separator
418
+ // between a national prefix and a national (significant) number.
419
+ // So `US` national prefix separator is a "special" "hardcoded" case.
420
+ if (this.isNANP) {
421
+ return ' ';
422
+ }
423
+ // If a `format` has a `national_prefix_formatting_rule`
424
+ // and that rule has a separator after a national prefix,
425
+ // then it means that there should be a separator
426
+ // between a national prefix and a national (significant) number.
427
+ if (format && format.nationalPrefixFormattingRule() && NATIONAL_PREFIX_SEPARATORS_PATTERN.test(format.nationalPrefixFormattingRule())) {
428
+ return ' ';
429
+ }
430
+ // At this point, there seems to be no clear evidence that
431
+ // there should be a separator between a national prefix
432
+ // and a national (significant) number. So don't insert one.
433
+ return '';
434
+ }
435
+ getInternationalPrefixBeforeCountryCallingCode({
436
+ IDDPrefix,
437
+ missingPlus
438
+ }, options) {
439
+ if (IDDPrefix) {
440
+ return options && options.spacing === false ? IDDPrefix : IDDPrefix + ' ';
441
+ }
442
+ if (missingPlus) {
443
+ return '';
444
+ }
445
+ return '+';
446
+ }
447
+ getTemplate(state) {
448
+ if (!this.template) {
449
+ return;
450
+ }
451
+ // `this.template` holds the template for a "complete" phone number.
452
+ // The currently entered phone number is most likely not "complete",
453
+ // so trim all non-populated digits.
454
+ let index = -1;
455
+ let i = 0;
456
+ const internationalPrefix = state.international ? this.getInternationalPrefixBeforeCountryCallingCode(state, {
457
+ spacing: false
458
+ }) : '';
459
+ while (i < internationalPrefix.length + state.getDigitsWithoutInternationalPrefix().length) {
460
+ index = this.template.indexOf(DIGIT_PLACEHOLDER, index + 1);
461
+ i++;
462
+ }
463
+ return cutAndStripNonPairedParens(this.template, index + 1);
464
+ }
465
+ setNationalNumberTemplate(template, state) {
466
+ this.nationalNumberTemplate = template;
467
+ this.populatedNationalNumberTemplate = template;
468
+ // With a new formatting template, the matched position
469
+ // using the old template needs to be reset.
470
+ this.populatedNationalNumberTemplatePosition = -1;
471
+ // For convenience, the public `.template` property
472
+ // contains the whole international number
473
+ // if the phone number being input is international:
474
+ // 'x' for the '+' sign, 'x'es for the country phone code,
475
+ // a spacebar and then the template for the formatted national number.
476
+ if (state.international) {
477
+ this.template = this.getInternationalPrefixBeforeCountryCallingCode(state).replace(/[\d\+]/g, DIGIT_PLACEHOLDER) + repeat(DIGIT_PLACEHOLDER, state.callingCode.length) + ' ' + template;
478
+ } else {
479
+ this.template = template;
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Generates formatting template for a national phone number,
485
+ * optionally containing a national prefix, for a format.
486
+ * @param {Format} format
487
+ * @param {string} nationalPrefix
488
+ * @return {string}
489
+ */
490
+ getTemplateForFormat(format, {
491
+ nationalSignificantNumber,
492
+ international,
493
+ nationalPrefix,
494
+ prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix
495
+ }) {
496
+ let pattern = format.pattern();
497
+
498
+ /* istanbul ignore else */
499
+ if (SUPPORT_LEGACY_FORMATTING_PATTERNS) {
500
+ pattern = pattern
501
+ // Replace anything in the form of [..] with \d
502
+ .replace(CREATE_CHARACTER_CLASS_PATTERN(), '\\d')
503
+ // Replace any standalone digit (not the one in `{}`) with \d
504
+ .replace(CREATE_STANDALONE_DIGIT_PATTERN(), '\\d');
505
+ }
506
+
507
+ // Generate a dummy national number (consisting of `9`s)
508
+ // that fits this format's `pattern`.
509
+ //
510
+ // This match will always succeed,
511
+ // because the "longest dummy phone number"
512
+ // has enough length to accomodate any possible
513
+ // national phone number format pattern.
514
+ //
515
+ let digits = LONGEST_DUMMY_PHONE_NUMBER.match(pattern)[0];
516
+
517
+ // If the national number entered is too long
518
+ // for any phone number format, then abort.
519
+ if (nationalSignificantNumber.length > digits.length) {
520
+ return;
521
+ }
522
+
523
+ // Get a formatting template which can be used to efficiently format
524
+ // a partial number where digits are added one by one.
525
+
526
+ // Below `strictPattern` is used for the
527
+ // regular expression (with `^` and `$`).
528
+ // This wasn't originally in Google's `libphonenumber`
529
+ // and I guess they don't really need it
530
+ // because they're not using "templates" to format phone numbers
531
+ // but I added `strictPattern` after encountering
532
+ // South Korean phone number formatting bug.
533
+ //
534
+ // Non-strict regular expression bug demonstration:
535
+ //
536
+ // this.nationalSignificantNumber : `111111111` (9 digits)
537
+ //
538
+ // pattern : (\d{2})(\d{3,4})(\d{4})
539
+ // format : `$1 $2 $3`
540
+ // digits : `9999999999` (10 digits)
541
+ //
542
+ // '9999999999'.replace(new RegExp(/(\d{2})(\d{3,4})(\d{4})/g), '$1 $2 $3') = "99 9999 9999"
543
+ //
544
+ // template : xx xxxx xxxx
545
+ //
546
+ // But the correct template in this case is `xx xxx xxxx`.
547
+ // The template was generated incorrectly because of the
548
+ // `{3,4}` variability in the `pattern`.
549
+ //
550
+ // The fix is, if `this.nationalSignificantNumber` has already sufficient length
551
+ // to satisfy the `pattern` completely then `this.nationalSignificantNumber`
552
+ // is used instead of `digits`.
553
+
554
+ const strictPattern = new RegExp('^' + pattern + '$');
555
+ const nationalNumberDummyDigits = nationalSignificantNumber.replace(/\d/g, DUMMY_DIGIT);
556
+
557
+ // If `this.nationalSignificantNumber` has already sufficient length
558
+ // to satisfy the `pattern` completely then use it
559
+ // instead of `digits`.
560
+ if (strictPattern.test(nationalNumberDummyDigits)) {
561
+ digits = nationalNumberDummyDigits;
562
+ }
563
+ let numberFormat = this.getFormatFormat(format, international);
564
+ let nationalPrefixIncludedInTemplate;
565
+
566
+ // If a user did input a national prefix (and that's guaranteed),
567
+ // and if a `format` does have a national prefix formatting rule,
568
+ // then see if that national prefix formatting rule
569
+ // prepends exactly the same national prefix the user has input.
570
+ // If that's the case, then use the `format` with the national prefix formatting rule.
571
+ // Otherwise, use the `format` without the national prefix formatting rule,
572
+ // and prepend a national prefix manually to it.
573
+ if (this.shouldTryNationalPrefixFormattingRule(format, {
574
+ international,
575
+ nationalPrefix
576
+ })) {
577
+ const numberFormatWithNationalPrefix = numberFormat.replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule());
578
+ // If `national_prefix_formatting_rule` of a `format` simply prepends
579
+ // national prefix at the start of a national (significant) number,
580
+ // then such formatting can be used with `AsYouType` formatter.
581
+ // There seems to be no `else` case: everywhere in metadata,
582
+ // national prefix formatting rule is national prefix + $1,
583
+ // or `($1)`, in which case such format isn't even considered
584
+ // when the user has input a national prefix.
585
+ /* istanbul ignore else */
586
+ if (parseDigits(format.nationalPrefixFormattingRule()) === (nationalPrefix || '') + parseDigits('$1')) {
587
+ numberFormat = numberFormatWithNationalPrefix;
588
+ nationalPrefixIncludedInTemplate = true;
589
+ // Replace all digits of the national prefix in the formatting template
590
+ // with `DIGIT_PLACEHOLDER`s.
591
+ if (nationalPrefix) {
592
+ let i = nationalPrefix.length;
593
+ while (i > 0) {
594
+ numberFormat = numberFormat.replace(/\d/, DIGIT_PLACEHOLDER);
595
+ i--;
596
+ }
597
+ }
598
+ }
599
+ }
600
+
601
+ // Generate formatting template for this phone number format.
602
+ let template = digits
603
+ // Format the dummy phone number according to the format.
604
+ .replace(new RegExp(pattern), numberFormat)
605
+ // Replace each dummy digit with a DIGIT_PLACEHOLDER.
606
+ .replace(new RegExp(DUMMY_DIGIT, 'g'), DIGIT_PLACEHOLDER);
607
+
608
+ // If a prefix of a national (significant) number is not as simple
609
+ // as just a basic national prefix, then just prepend such prefix
610
+ // before the national (significant) number, optionally spacing
611
+ // the two with a whitespace.
612
+ if (!nationalPrefixIncludedInTemplate) {
613
+ if (prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix) {
614
+ // Prepend the prefix to the template manually.
615
+ template = repeat(DIGIT_PLACEHOLDER, prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix.length) + ' ' + template;
616
+ } else if (nationalPrefix) {
617
+ // Prepend national prefix to the template manually.
618
+ template = repeat(DIGIT_PLACEHOLDER, nationalPrefix.length) + this.getSeparatorAfterNationalPrefix(format) + template;
619
+ }
620
+ }
621
+ if (international) {
622
+ template = applyInternationalSeparatorStyle(template);
623
+ }
624
+ return template;
625
+ }
626
+ formatNextNationalNumberDigits(digits) {
627
+ const result = populateTemplateWithDigits(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition, digits);
628
+ if (!result) {
629
+ // Reset the format.
630
+ this.resetFormat();
631
+ return;
632
+ }
633
+ this.populatedNationalNumberTemplate = result[0];
634
+ this.populatedNationalNumberTemplatePosition = result[1];
635
+
636
+ // Return the formatted phone number so far.
637
+ return cutAndStripNonPairedParens(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition + 1);
638
+
639
+ // The old way which was good for `input-format` but is not so good
640
+ // for `react-phone-number-input`'s default input (`InputBasic`).
641
+ // return closeNonPairedParens(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition + 1)
642
+ // .replace(new RegExp(DIGIT_PLACEHOLDER, 'g'), ' ')
643
+ }
644
+ shouldTryNationalPrefixFormattingRule(format, {
645
+ international,
646
+ nationalPrefix
647
+ }) {
648
+ if (format.nationalPrefixFormattingRule()) {
649
+ // In some countries, `national_prefix_formatting_rule` is `($1)`,
650
+ // so it applies even if the user hasn't input a national prefix.
651
+ // `format.usesNationalPrefix()` detects such cases.
652
+ const usesNationalPrefix = format.usesNationalPrefix();
653
+ if (usesNationalPrefix && nationalPrefix || !usesNationalPrefix && !international) {
654
+ return true;
655
+ }
656
+ }
657
+ }
658
+ }
659
+ //# sourceMappingURL=AsYouTypeFormatter.js.map