libphonenumber-js 1.12.42 → 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 (332) hide show
  1. package/CHANGELOG.md +131 -94
  2. package/README.md +36 -6
  3. package/bundle/libphonenumber-js.min.js +1 -1
  4. package/bundle/libphonenumber-js.min.js.map +1 -1
  5. package/bundle/libphonenumber-max.js +1 -1
  6. package/bundle/libphonenumber-max.js.map +1 -1
  7. package/bundle/libphonenumber-min.js +1 -1
  8. package/bundle/libphonenumber-min.js.map +1 -1
  9. package/bundle/libphonenumber-mobile.js +1 -1
  10. package/bundle/libphonenumber-mobile.js.map +1 -1
  11. package/core/es6/index.d.cts +2 -0
  12. package/core/es6/index.d.ts +2 -0
  13. package/core/es6/index.js +39 -0
  14. package/core/es6/package.json +12 -0
  15. package/core/index.cjs +5 -0
  16. package/core/index.cjs.js +5 -0
  17. package/core/index.d.cts +3 -1
  18. package/core/index.d.ts +3 -1
  19. package/core/index.js +5 -0
  20. package/custom.d.ts +6 -4
  21. package/custom.js +5 -5
  22. package/es6-modern/AsYouType.js +445 -0
  23. package/es6-modern/AsYouType.js.map +1 -0
  24. package/es6-modern/AsYouType.test.js.map +1 -0
  25. package/es6-modern/AsYouTypeFormatter.PatternMatcher.js +195 -0
  26. package/es6-modern/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
  27. package/es6-modern/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
  28. package/es6-modern/AsYouTypeFormatter.PatternParser.js +184 -0
  29. package/es6-modern/AsYouTypeFormatter.PatternParser.js.map +1 -0
  30. package/es6-modern/AsYouTypeFormatter.PatternParser.test.js.map +1 -0
  31. package/es6-modern/AsYouTypeFormatter.complete.js +120 -0
  32. package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -0
  33. package/es6-modern/AsYouTypeFormatter.js +659 -0
  34. package/es6-modern/AsYouTypeFormatter.js.map +1 -0
  35. package/es6-modern/AsYouTypeFormatter.util.js +96 -0
  36. package/es6-modern/AsYouTypeFormatter.util.js.map +1 -0
  37. package/es6-modern/AsYouTypeFormatter.util.test.js.map +1 -0
  38. package/es6-modern/AsYouTypeParser.js +435 -0
  39. package/es6-modern/AsYouTypeParser.js.map +1 -0
  40. package/es6-modern/AsYouTypeState.js +104 -0
  41. package/es6-modern/AsYouTypeState.js.map +1 -0
  42. package/es6-modern/ParseError.js +15 -0
  43. package/es6-modern/ParseError.js.map +1 -0
  44. package/es6-modern/PhoneNumber.js +166 -0
  45. package/es6-modern/PhoneNumber.js.map +1 -0
  46. package/es6-modern/PhoneNumber.test.js.map +1 -0
  47. package/es6-modern/PhoneNumberMatcher.js +330 -0
  48. package/es6-modern/PhoneNumberMatcher.js.map +1 -0
  49. package/es6-modern/PhoneNumberMatcher.test.js.map +1 -0
  50. package/es6-modern/constants.js +31 -0
  51. package/es6-modern/constants.js.map +1 -0
  52. package/es6-modern/findNumbers/LRUCache.js +98 -0
  53. package/es6-modern/findNumbers/LRUCache.js.map +1 -0
  54. package/es6-modern/findNumbers/Leniency.js +311 -0
  55. package/es6-modern/findNumbers/Leniency.js.map +1 -0
  56. package/es6-modern/findNumbers/RegExpCache.js +20 -0
  57. package/es6-modern/findNumbers/RegExpCache.js.map +1 -0
  58. package/es6-modern/findNumbers/isValidCandidate.js +66 -0
  59. package/es6-modern/findNumbers/isValidCandidate.js.map +1 -0
  60. package/es6-modern/findNumbers/isValidPreCandidate.js +26 -0
  61. package/es6-modern/findNumbers/isValidPreCandidate.js.map +1 -0
  62. package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +63 -0
  63. package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
  64. package/es6-modern/findNumbers/parsePreCandidate.js +18 -0
  65. package/es6-modern/findNumbers/parsePreCandidate.js.map +1 -0
  66. package/es6-modern/findNumbers/utf-8.js +63 -0
  67. package/es6-modern/findNumbers/utf-8.js.map +1 -0
  68. package/es6-modern/findNumbers/util.js +26 -0
  69. package/es6-modern/findNumbers/util.js.map +1 -0
  70. package/es6-modern/findNumbers/util.test.js.map +1 -0
  71. package/es6-modern/findPhoneNumbersInText.js +19 -0
  72. package/es6-modern/findPhoneNumbersInText.js.map +1 -0
  73. package/es6-modern/findPhoneNumbersInText.test.js.map +1 -0
  74. package/es6-modern/format.js +204 -0
  75. package/es6-modern/format.js.map +1 -0
  76. package/es6-modern/format.test.js.map +1 -0
  77. package/es6-modern/formatIncompletePhoneNumber.js +18 -0
  78. package/es6-modern/formatIncompletePhoneNumber.js.map +1 -0
  79. package/es6-modern/formatIncompletePhoneNumber.test.js.map +1 -0
  80. package/es6-modern/formatPhoneNumberForMobileDialing.js +190 -0
  81. package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -0
  82. package/es6-modern/getCountries.js +5 -0
  83. package/es6-modern/getCountries.js.map +1 -0
  84. package/es6-modern/getCountries.test.js.map +1 -0
  85. package/es6-modern/getCountryCallingCode.js +3 -0
  86. package/es6-modern/getCountryCallingCode.js.map +1 -0
  87. package/es6-modern/getCountryCallingCode.test.js.map +1 -0
  88. package/es6-modern/getExampleNumber.js +7 -0
  89. package/es6-modern/getExampleNumber.js.map +1 -0
  90. package/es6-modern/getExampleNumber.test.js.map +1 -0
  91. package/es6-modern/helpers/RFC3966.js +63 -0
  92. package/es6-modern/helpers/RFC3966.js.map +1 -0
  93. package/es6-modern/helpers/RFC3966.test.js.map +1 -0
  94. package/es6-modern/helpers/applyInternationalSeparatorStyle.js +36 -0
  95. package/es6-modern/helpers/applyInternationalSeparatorStyle.js.map +1 -0
  96. package/es6-modern/helpers/applyInternationalSeparatorStyle.test.js.map +1 -0
  97. package/es6-modern/helpers/checkNumberLength.js +112 -0
  98. package/es6-modern/helpers/checkNumberLength.js.map +1 -0
  99. package/es6-modern/helpers/checkNumberLength.test.js.map +1 -0
  100. package/es6-modern/helpers/extension/createExtensionPattern.js +93 -0
  101. package/es6-modern/helpers/extension/createExtensionPattern.js.map +1 -0
  102. package/es6-modern/helpers/extension/extractExtension.js +30 -0
  103. package/es6-modern/helpers/extension/extractExtension.js.map +1 -0
  104. package/es6-modern/helpers/extractCountryCallingCode.js +142 -0
  105. package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -0
  106. package/es6-modern/helpers/extractCountryCallingCode.test.js.map +1 -0
  107. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +49 -0
  108. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -0
  109. package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +66 -0
  110. package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
  111. package/es6-modern/helpers/extractNationalNumber.js +117 -0
  112. package/es6-modern/helpers/extractNationalNumber.js.map +1 -0
  113. package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -0
  114. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +102 -0
  115. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -0
  116. package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js.map +1 -0
  117. package/es6-modern/helpers/extractPhoneContext.js +82 -0
  118. package/es6-modern/helpers/extractPhoneContext.js.map +1 -0
  119. package/es6-modern/helpers/extractPhoneContext.test.js.map +1 -0
  120. package/es6-modern/helpers/formatNationalNumberUsingFormat.js +34 -0
  121. package/es6-modern/helpers/formatNationalNumberUsingFormat.js.map +1 -0
  122. package/es6-modern/helpers/getCountryByCallingCode.js +30 -0
  123. package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -0
  124. package/es6-modern/helpers/getCountryByNationalNumber.js +76 -0
  125. package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -0
  126. package/es6-modern/helpers/getIddPrefix.js +26 -0
  127. package/es6-modern/helpers/getIddPrefix.js.map +1 -0
  128. package/es6-modern/helpers/getNumberType.js +81 -0
  129. package/es6-modern/helpers/getNumberType.js.map +1 -0
  130. package/es6-modern/helpers/getNumberType.test.js.map +1 -0
  131. package/es6-modern/helpers/getPossibleCountriesForNumber.js +28 -0
  132. package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -0
  133. package/es6-modern/helpers/isObject.js +5 -0
  134. package/es6-modern/helpers/isObject.js.map +1 -0
  135. package/es6-modern/helpers/isViablePhoneNumber.js +77 -0
  136. package/es6-modern/helpers/isViablePhoneNumber.js.map +1 -0
  137. package/es6-modern/helpers/matchesEntirely.js +12 -0
  138. package/es6-modern/helpers/matchesEntirely.js.map +1 -0
  139. package/es6-modern/helpers/matchesEntirely.test.js.map +1 -0
  140. package/es6-modern/helpers/mergeArrays.js +23 -0
  141. package/es6-modern/helpers/mergeArrays.js.map +1 -0
  142. package/es6-modern/helpers/mergeArrays.test.js.map +1 -0
  143. package/es6-modern/helpers/parseDigits.js +111 -0
  144. package/es6-modern/helpers/parseDigits.js.map +1 -0
  145. package/es6-modern/helpers/parseDigits.test.js.map +1 -0
  146. package/es6-modern/helpers/stripIddPrefix.js +29 -0
  147. package/es6-modern/helpers/stripIddPrefix.js.map +1 -0
  148. package/es6-modern/helpers/stripIddPrefix.test.js.map +1 -0
  149. package/es6-modern/isPossible.js +77 -0
  150. package/es6-modern/isPossible.js.map +1 -0
  151. package/es6-modern/isPossible.test.js.map +1 -0
  152. package/es6-modern/isPossiblePhoneNumber.js +16 -0
  153. package/es6-modern/isPossiblePhoneNumber.js.map +1 -0
  154. package/es6-modern/isPossiblePhoneNumber.test.js.map +1 -0
  155. package/es6-modern/isValid.js +57 -0
  156. package/es6-modern/isValid.js.map +1 -0
  157. package/es6-modern/isValid.test.js.map +1 -0
  158. package/es6-modern/isValidPhoneNumber.js +16 -0
  159. package/es6-modern/isValidPhoneNumber.js.map +1 -0
  160. package/es6-modern/isValidPhoneNumber.test.js.map +1 -0
  161. package/es6-modern/legacy/PhoneNumberSearch.js +109 -0
  162. package/es6-modern/legacy/PhoneNumberSearch.js.map +1 -0
  163. package/es6-modern/legacy/findNumbers.js +16 -0
  164. package/es6-modern/legacy/findNumbers.js.map +1 -0
  165. package/es6-modern/legacy/findNumbers.test.js.map +1 -0
  166. package/es6-modern/legacy/findPhoneNumbers.js +26 -0
  167. package/es6-modern/legacy/findPhoneNumbers.js.map +1 -0
  168. package/es6-modern/legacy/findPhoneNumbers.test.js.map +1 -0
  169. package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js +79 -0
  170. package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js.map +1 -0
  171. package/es6-modern/legacy/format.js +97 -0
  172. package/es6-modern/legacy/format.js.map +1 -0
  173. package/es6-modern/legacy/format.test.js.map +1 -0
  174. package/es6-modern/legacy/getNumberType.js +104 -0
  175. package/es6-modern/legacy/getNumberType.js.map +1 -0
  176. package/es6-modern/legacy/getNumberType.test.js.map +1 -0
  177. package/es6-modern/legacy/isPossibleNumber.js +30 -0
  178. package/es6-modern/legacy/isPossibleNumber.js.map +1 -0
  179. package/es6-modern/legacy/isPossibleNumber.test.js.map +1 -0
  180. package/es6-modern/legacy/isValidNumber.js +17 -0
  181. package/es6-modern/legacy/isValidNumber.js.map +1 -0
  182. package/es6-modern/legacy/isValidNumber.test.js.map +1 -0
  183. package/es6-modern/legacy/isValidNumberForRegion.js +65 -0
  184. package/es6-modern/legacy/isValidNumberForRegion.js.map +1 -0
  185. package/es6-modern/legacy/isValidNumberForRegion.test.js.map +1 -0
  186. package/es6-modern/legacy/isValidNumberForRegion_.js +14 -0
  187. package/es6-modern/legacy/isValidNumberForRegion_.js.map +1 -0
  188. package/es6-modern/legacy/parse.js +11 -0
  189. package/es6-modern/legacy/parse.js.map +1 -0
  190. package/es6-modern/legacy/parse.test.js.map +1 -0
  191. package/es6-modern/legacy/searchNumbers.js +32 -0
  192. package/es6-modern/legacy/searchNumbers.js.map +1 -0
  193. package/es6-modern/legacy/searchNumbers.test.js.map +1 -0
  194. package/es6-modern/metadata.js +494 -0
  195. package/es6-modern/metadata.js.map +1 -0
  196. package/es6-modern/metadata.test.js.map +1 -0
  197. package/es6-modern/normalizeArguments.js +52 -0
  198. package/es6-modern/normalizeArguments.js.map +1 -0
  199. package/es6-modern/parse.js +341 -0
  200. package/es6-modern/parse.js.map +1 -0
  201. package/es6-modern/parse.test.js.map +1 -0
  202. package/es6-modern/parseIncompletePhoneNumber.js +70 -0
  203. package/es6-modern/parseIncompletePhoneNumber.js.map +1 -0
  204. package/es6-modern/parseIncompletePhoneNumber.test.js.map +1 -0
  205. package/es6-modern/parsePhoneNumber.js +11 -0
  206. package/es6-modern/parsePhoneNumber.js.map +1 -0
  207. package/es6-modern/parsePhoneNumber.test.js.map +1 -0
  208. package/es6-modern/parsePhoneNumberWithError.js +11 -0
  209. package/es6-modern/parsePhoneNumberWithError.js.map +1 -0
  210. package/es6-modern/parsePhoneNumberWithError.test.js.map +1 -0
  211. package/es6-modern/parsePhoneNumberWithError_.js +8 -0
  212. package/es6-modern/parsePhoneNumberWithError_.js.map +1 -0
  213. package/es6-modern/parsePhoneNumber_.js +24 -0
  214. package/es6-modern/parsePhoneNumber_.js.map +1 -0
  215. package/es6-modern/searchPhoneNumbersInText.js +31 -0
  216. package/es6-modern/searchPhoneNumbersInText.js.map +1 -0
  217. package/es6-modern/searchPhoneNumbersInText.test.js.map +1 -0
  218. package/es6-modern/tools/semver-compare.js +27 -0
  219. package/es6-modern/tools/semver-compare.js.map +1 -0
  220. package/es6-modern/tools/semver-compare.test.js.map +1 -0
  221. package/es6-modern/validatePhoneNumberLength.js +35 -0
  222. package/es6-modern/validatePhoneNumberLength.js.map +1 -0
  223. package/es6-modern/validatePhoneNumberLength.test.js.map +1 -0
  224. package/index.cjs +4 -0
  225. package/index.cjs.js +4 -0
  226. package/index.d.cts +5 -3
  227. package/index.d.ts +5 -3
  228. package/index.js +5 -0
  229. package/max/es6/exports/AsYouType.js +11 -0
  230. package/max/es6/exports/Metadata.js +11 -0
  231. package/max/es6/exports/PhoneNumber.js +11 -0
  232. package/max/es6/exports/PhoneNumberMatcher.js +11 -0
  233. package/max/es6/exports/findNumbers.js +6 -0
  234. package/max/es6/exports/findPhoneNumbersInText.js +6 -0
  235. package/max/es6/exports/formatIncompletePhoneNumber.js +6 -0
  236. package/max/es6/exports/getCountries.js +6 -0
  237. package/max/es6/exports/getCountryCallingCode.js +6 -0
  238. package/max/es6/exports/getExampleNumber.js +6 -0
  239. package/max/es6/exports/getExtPrefix.js +6 -0
  240. package/max/es6/exports/isPossiblePhoneNumber.js +6 -0
  241. package/max/es6/exports/isSupportedCountry.js +6 -0
  242. package/max/es6/exports/isValidPhoneNumber.js +6 -0
  243. package/max/es6/exports/parsePhoneNumber.js +7 -0
  244. package/max/es6/exports/parsePhoneNumberWithError.js +7 -0
  245. package/max/es6/exports/searchNumbers.js +6 -0
  246. package/max/es6/exports/searchPhoneNumbersInText.js +6 -0
  247. package/max/es6/exports/validatePhoneNumberLength.js +6 -0
  248. package/max/es6/exports/withMetadataArgument.js +13 -0
  249. package/max/es6/index.d.cts +2 -0
  250. package/max/es6/index.d.ts +2 -0
  251. package/max/es6/index.js +44 -0
  252. package/max/es6/package.json +12 -0
  253. package/max/index.cjs +5 -0
  254. package/max/index.cjs.js +5 -0
  255. package/max/index.d.cts +3 -1
  256. package/max/index.d.ts +3 -1
  257. package/max/index.js +4 -0
  258. package/max/metadata/metadata.max.json +1 -1
  259. package/max/metadata/metadata.max.json.js +1 -1
  260. package/metadata.full.json +1 -1
  261. package/metadata.full.json.js +1 -1
  262. package/metadata.max.json +1 -1
  263. package/metadata.max.json.js +1 -1
  264. package/metadata.min.json +1 -1
  265. package/metadata.min.json.js +1 -1
  266. package/metadata.mobile.json +1 -1
  267. package/metadata.mobile.json.js +1 -1
  268. package/min/es6/exports/AsYouType.js +11 -0
  269. package/min/es6/exports/Metadata.js +11 -0
  270. package/min/es6/exports/PhoneNumber.js +11 -0
  271. package/min/es6/exports/PhoneNumberMatcher.js +11 -0
  272. package/min/es6/exports/findNumbers.js +6 -0
  273. package/min/es6/exports/findPhoneNumbersInText.js +6 -0
  274. package/min/es6/exports/formatIncompletePhoneNumber.js +6 -0
  275. package/min/es6/exports/getCountries.js +6 -0
  276. package/min/es6/exports/getCountryCallingCode.js +6 -0
  277. package/min/es6/exports/getExampleNumber.js +6 -0
  278. package/min/es6/exports/getExtPrefix.js +6 -0
  279. package/min/es6/exports/isPossiblePhoneNumber.js +6 -0
  280. package/min/es6/exports/isSupportedCountry.js +6 -0
  281. package/min/es6/exports/isValidPhoneNumber.js +6 -0
  282. package/min/es6/exports/parsePhoneNumber.js +7 -0
  283. package/min/es6/exports/parsePhoneNumberWithError.js +7 -0
  284. package/min/es6/exports/searchNumbers.js +6 -0
  285. package/min/es6/exports/searchPhoneNumbersInText.js +6 -0
  286. package/min/es6/exports/validatePhoneNumberLength.js +6 -0
  287. package/min/es6/exports/withMetadataArgument.js +13 -0
  288. package/min/es6/index.d.cts +2 -0
  289. package/min/es6/index.d.ts +2 -0
  290. package/min/es6/index.js +44 -0
  291. package/min/es6/package.json +12 -0
  292. package/min/index.cjs +5 -0
  293. package/min/index.cjs.js +5 -0
  294. package/min/index.d.cts +3 -1
  295. package/min/index.d.ts +3 -1
  296. package/min/index.js +3 -0
  297. package/min/metadata/metadata.min.json +1 -1
  298. package/min/metadata/metadata.min.json.js +1 -1
  299. package/mobile/es6/exports/AsYouType.js +11 -0
  300. package/mobile/es6/exports/Metadata.js +11 -0
  301. package/mobile/es6/exports/PhoneNumber.js +11 -0
  302. package/mobile/es6/exports/PhoneNumberMatcher.js +11 -0
  303. package/mobile/es6/exports/findNumbers.js +6 -0
  304. package/mobile/es6/exports/findPhoneNumbersInText.js +6 -0
  305. package/mobile/es6/exports/formatIncompletePhoneNumber.js +6 -0
  306. package/mobile/es6/exports/getCountries.js +6 -0
  307. package/mobile/es6/exports/getCountryCallingCode.js +6 -0
  308. package/mobile/es6/exports/getExampleNumber.js +6 -0
  309. package/mobile/es6/exports/getExtPrefix.js +6 -0
  310. package/mobile/es6/exports/isPossiblePhoneNumber.js +6 -0
  311. package/mobile/es6/exports/isSupportedCountry.js +6 -0
  312. package/mobile/es6/exports/isValidPhoneNumber.js +6 -0
  313. package/mobile/es6/exports/parsePhoneNumber.js +7 -0
  314. package/mobile/es6/exports/parsePhoneNumberWithError.js +7 -0
  315. package/mobile/es6/exports/searchNumbers.js +6 -0
  316. package/mobile/es6/exports/searchPhoneNumbersInText.js +6 -0
  317. package/mobile/es6/exports/validatePhoneNumberLength.js +6 -0
  318. package/mobile/es6/exports/withMetadataArgument.js +13 -0
  319. package/mobile/es6/index.d.cts +2 -0
  320. package/mobile/es6/index.d.ts +2 -0
  321. package/mobile/es6/index.js +44 -0
  322. package/mobile/es6/package.json +12 -0
  323. package/mobile/index.cjs +5 -0
  324. package/mobile/index.cjs.js +5 -0
  325. package/mobile/index.d.cts +3 -1
  326. package/mobile/index.d.ts +3 -1
  327. package/mobile/index.js +4 -0
  328. package/mobile/metadata/metadata.mobile.json +1 -1
  329. package/mobile/metadata/metadata.mobile.json.js +1 -1
  330. package/package.json +28 -9
  331. package/types.d.cts +54 -21
  332. package/types.d.ts +54 -21
@@ -0,0 +1,112 @@
1
+ import Metadata from '../metadata.js';
2
+ import mergeArrays from './mergeArrays.js';
3
+ export default function checkNumberLength(nationalNumber, country, metadata) {
4
+ return checkNumberLengthForType(nationalNumber, country, undefined, metadata);
5
+ }
6
+
7
+ // Checks whether a number is possible for a certain `country` based on the number length.
8
+ //
9
+ // This function is not supported by metadata generated with ancient versions of
10
+ // `libphonenumber-js` (before version `1.0.18`) which didn't include "possible lengths".
11
+ //
12
+ // There was also a known issue with `checkNumberLength()` function:
13
+ // if a number is possible only in a certain `country` among several `countries`
14
+ // that share the same "country calling code", that function would check
15
+ // the possibility of the phone number only in the "main" `country` for the "country calling code"
16
+ // and would not check if it's actually be possible in the speciifc `country`.
17
+ //
18
+ // For example, "+1310xxxx" numbers are valid in Canada.
19
+ // However, they are not possible in the US due to being too short.
20
+ // Since Canada and the US share the same country calling code — "+1" —
21
+ // `checkNumberLength()` function used to return not "IS_POSSIBLE" for "+1310xxxx" numbers.
22
+ //
23
+ // In such cases, when using "/max" metadata, `isValid()` could output `true`
24
+ // but at the same time `isPossible()` could output `false`, which was contradictory.
25
+ //
26
+ // See https://issuetracker.google.com/issues/335892662 for the discusson in Google's issues.
27
+ //
28
+ // The solution suggested by Google was implemented: an optional `country` argument
29
+ // was added to `checkNumberLength()` function. If present, that `country` will be used
30
+ // to check phone number length for that specific `country` rather than the "main" country
31
+ // for the shared "country calling code".
32
+ //
33
+ export function checkNumberLengthForType(nationalNumber, country, type, metadata) {
34
+ // If the exact `country` is specified, it's no necessarily already selected in `metadata`.
35
+ // Most likely, in cases when there're multiple countries corresponding to the same
36
+ // "country calling code", the "main" country for that "country calling code" will be selected.
37
+ if (country) {
38
+ metadata = new Metadata(metadata.metadata);
39
+ metadata.selectNumberingPlan(country);
40
+ }
41
+ const type_info = metadata.type(type);
42
+
43
+ // There should always be "<possiblePengths/>" set for every type element.
44
+ // This is declared in the XML schema.
45
+ // For size efficiency, where a sub-description (e.g. fixed-line)
46
+ // has the same "<possiblePengths/>" as the "general description", this is missing,
47
+ // so we fall back to the "general description". Where no numbers of the type
48
+ // exist at all, there is one possible length (-1) which is guaranteed
49
+ // not to match the length of any real phone number.
50
+ let possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths();
51
+ // let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
52
+
53
+ // Metadata before version `1.0.18` didn't contain `possible_lengths`.
54
+ if (!possible_lengths) {
55
+ return 'IS_POSSIBLE';
56
+ }
57
+ if (type === 'FIXED_LINE_OR_MOBILE') {
58
+ // No such country in metadata.
59
+ /* istanbul ignore next */
60
+ if (!metadata.type('FIXED_LINE')) {
61
+ // The rare case has been encountered where no fixedLine data is available
62
+ // (true for some non-geographic entities), so we just check mobile.
63
+ return checkNumberLengthForType(nationalNumber, country, 'MOBILE', metadata);
64
+ }
65
+ const mobile_type = metadata.type('MOBILE');
66
+ if (mobile_type) {
67
+ // Merge the mobile data in if there was any. "Concat" creates a new
68
+ // array, it doesn't edit possible_lengths in place, so we don't need a copy.
69
+ // Note that when adding the possible lengths from mobile, we have
70
+ // to again check they aren't empty since if they are this indicates
71
+ // they are the same as the general desc and should be obtained from there.
72
+ possible_lengths = mergeArrays(possible_lengths, mobile_type.possibleLengths());
73
+ // The current list is sorted; we need to merge in the new list and
74
+ // re-sort (duplicates are okay). Sorting isn't so expensive because
75
+ // the lists are very small.
76
+
77
+ // if (local_lengths) {
78
+ // local_lengths = mergeArrays(local_lengths, mobile_type.possibleLengthsLocal())
79
+ // } else {
80
+ // local_lengths = mobile_type.possibleLengthsLocal()
81
+ // }
82
+ }
83
+ }
84
+ // If the type doesn't exist then return 'INVALID_LENGTH'.
85
+ else if (type && !type_info) {
86
+ return 'INVALID_LENGTH';
87
+ }
88
+ const actual_length = nationalNumber.length;
89
+
90
+ // In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
91
+ // // This is safe because there is never an overlap beween the possible lengths
92
+ // // and the local-only lengths; this is checked at build time.
93
+ // if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
94
+ // {
95
+ // return 'IS_POSSIBLE_LOCAL_ONLY'
96
+ // }
97
+
98
+ const minimum_length = possible_lengths[0];
99
+ if (minimum_length === actual_length) {
100
+ return 'IS_POSSIBLE';
101
+ }
102
+ if (minimum_length > actual_length) {
103
+ return 'TOO_SHORT';
104
+ }
105
+ if (possible_lengths[possible_lengths.length - 1] < actual_length) {
106
+ return 'TOO_LONG';
107
+ }
108
+
109
+ // We skip the first element since we've already checked it.
110
+ return possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH';
111
+ }
112
+ //# sourceMappingURL=checkNumberLength.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkNumberLength.js","names":["Metadata","mergeArrays","checkNumberLength","nationalNumber","country","metadata","checkNumberLengthForType","undefined","type","selectNumberingPlan","type_info","possible_lengths","possibleLengths","mobile_type","actual_length","length","minimum_length","indexOf"],"sources":["../../source/helpers/checkNumberLength.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport mergeArrays from './mergeArrays.js'\r\n\r\nexport default function checkNumberLength(nationalNumber, country, metadata) {\r\n\treturn checkNumberLengthForType(nationalNumber, country, undefined, metadata)\r\n}\r\n\r\n// Checks whether a number is possible for a certain `country` based on the number length.\r\n//\r\n// This function is not supported by metadata generated with ancient versions of\r\n// `libphonenumber-js` (before version `1.0.18`) which didn't include \"possible lengths\".\r\n//\r\n// There was also a known issue with `checkNumberLength()` function:\r\n// if a number is possible only in a certain `country` among several `countries`\r\n// that share the same \"country calling code\", that function would check\r\n// the possibility of the phone number only in the \"main\" `country` for the \"country calling code\"\r\n// and would not check if it's actually be possible in the speciifc `country`.\r\n//\r\n// For example, \"+1310xxxx\" numbers are valid in Canada.\r\n// However, they are not possible in the US due to being too short.\r\n// Since Canada and the US share the same country calling code — \"+1\" —\r\n// `checkNumberLength()` function used to return not \"IS_POSSIBLE\" for \"+1310xxxx\" numbers.\r\n//\r\n// In such cases, when using \"/max\" metadata, `isValid()` could output `true`\r\n// but at the same time `isPossible()` could output `false`, which was contradictory.\r\n//\r\n// See https://issuetracker.google.com/issues/335892662 for the discusson in Google's issues.\r\n//\r\n// The solution suggested by Google was implemented: an optional `country` argument\r\n// was added to `checkNumberLength()` function. If present, that `country` will be used\r\n// to check phone number length for that specific `country` rather than the \"main\" country\r\n// for the shared \"country calling code\".\r\n//\r\nexport function checkNumberLengthForType(nationalNumber, country, type, metadata) {\r\n\t// If the exact `country` is specified, it's no necessarily already selected in `metadata`.\r\n\t// Most likely, in cases when there're multiple countries corresponding to the same\r\n\t// \"country calling code\", the \"main\" country for that \"country calling code\" will be selected.\r\n\tif (country) {\r\n\t\tmetadata = new Metadata(metadata.metadata)\r\n\t\tmetadata.selectNumberingPlan(country)\r\n\t}\r\n\r\n\tconst type_info = metadata.type(type)\r\n\r\n\t// There should always be \"<possiblePengths/>\" set for every type element.\r\n\t// This is declared in the XML schema.\r\n\t// For size efficiency, where a sub-description (e.g. fixed-line)\r\n\t// has the same \"<possiblePengths/>\" as the \"general description\", this is missing,\r\n\t// so we fall back to the \"general description\". Where no numbers of the type\r\n\t// exist at all, there is one possible length (-1) which is guaranteed\r\n\t// not to match the length of any real phone number.\r\n\tlet possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths()\r\n\t// let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()\r\n\r\n\t// Metadata before version `1.0.18` didn't contain `possible_lengths`.\r\n\tif (!possible_lengths) {\r\n\t\treturn 'IS_POSSIBLE'\r\n\t}\r\n\r\n\tif (type === 'FIXED_LINE_OR_MOBILE') {\r\n\t\t// No such country in metadata.\r\n\t\t/* istanbul ignore next */\r\n\t\tif (!metadata.type('FIXED_LINE')) {\r\n\t\t\t// The rare case has been encountered where no fixedLine data is available\r\n\t\t\t// (true for some non-geographic entities), so we just check mobile.\r\n\t\t\treturn checkNumberLengthForType(nationalNumber, country, 'MOBILE', metadata)\r\n\t\t}\r\n\r\n\t\tconst mobile_type = metadata.type('MOBILE')\r\n\t\tif (mobile_type) {\r\n\t\t\t// Merge the mobile data in if there was any. \"Concat\" creates a new\r\n\t\t\t// array, it doesn't edit possible_lengths in place, so we don't need a copy.\r\n\t\t\t// Note that when adding the possible lengths from mobile, we have\r\n\t\t\t// to again check they aren't empty since if they are this indicates\r\n\t\t\t// they are the same as the general desc and should be obtained from there.\r\n\t\t\tpossible_lengths = mergeArrays(possible_lengths, mobile_type.possibleLengths())\r\n\t\t\t// The current list is sorted; we need to merge in the new list and\r\n\t\t\t// re-sort (duplicates are okay). Sorting isn't so expensive because\r\n\t\t\t// the lists are very small.\r\n\r\n\t\t\t// if (local_lengths) {\r\n\t\t\t// \tlocal_lengths = mergeArrays(local_lengths, mobile_type.possibleLengthsLocal())\r\n\t\t\t// } else {\r\n\t\t\t// \tlocal_lengths = mobile_type.possibleLengthsLocal()\r\n\t\t\t// }\r\n\t\t}\r\n\t}\r\n\t// If the type doesn't exist then return 'INVALID_LENGTH'.\r\n\telse if (type && !type_info) {\r\n\t\treturn 'INVALID_LENGTH'\r\n\t}\r\n\r\n\tconst actual_length = nationalNumber.length\r\n\r\n\t// In `libphonenumber-js` all \"local-only\" formats are dropped for simplicity.\r\n\t// // This is safe because there is never an overlap beween the possible lengths\r\n\t// // and the local-only lengths; this is checked at build time.\r\n\t// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)\r\n\t// {\r\n\t// \treturn 'IS_POSSIBLE_LOCAL_ONLY'\r\n\t// }\r\n\r\n\tconst minimum_length = possible_lengths[0]\r\n\r\n\tif (minimum_length === actual_length) {\r\n\t\treturn 'IS_POSSIBLE'\r\n\t}\r\n\r\n\tif (minimum_length > actual_length) {\r\n\t\treturn 'TOO_SHORT'\r\n\t}\r\n\r\n\tif (possible_lengths[possible_lengths.length - 1] < actual_length) {\r\n\t\treturn 'TOO_LONG'\r\n\t}\r\n\r\n\t// We skip the first element since we've already checked it.\r\n\treturn possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH'\r\n}"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,WAAW,MAAM,kBAAkB;AAE1C,eAAe,SAASC,iBAAiBA,CAACC,cAAc,EAAEC,OAAO,EAAEC,QAAQ,EAAE;EAC5E,OAAOC,wBAAwB,CAACH,cAAc,EAAEC,OAAO,EAAEG,SAAS,EAAEF,QAAQ,CAAC;AAC9E;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,wBAAwBA,CAACH,cAAc,EAAEC,OAAO,EAAEI,IAAI,EAAEH,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AE;IACnC,OAAO,WAAW;EACnB;EAEA,IAAIH,gBAAgB,CAACA,gBAAgB,CAACI,MAAM,GAAG,CAAC,CAAC,GAAGD,aAAa,EAAE;IAClE,OAAO,UAAU;EAClB;;EAEA;EACA,OAAOH,gBAAgB,CAACM,OAAO,CAACH,aAAa,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,aAAa,GAAG,gBAAgB;AAC1F","ignoreList":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkNumberLength.test.js","names":["describe","it","expect","Metadata","metadata","type","oldMetadata","checkNumberLength","checkNumberLengthForType","checkNumberLengthWithCountry","to","equal","undefined","checkNumberLengthWithoutCountry","_oldMetadata","country","_metadata","number"],"sources":["../../source/helpers/checkNumberLength.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport Metadata from '../metadata.js'\r\nimport metadata from '../../metadata.max.json' with { type: 'json' }\r\nimport oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' with { type: 'json' }\r\n\r\nimport checkNumberLength, { checkNumberLengthForType } from './checkNumberLength.js'\r\n\r\ndescribe('checkNumberLength', () => {\r\n\tit('should check phone number length', () => {\r\n\t\t// Too short.\r\n\t\texpect(checkNumberLengthWithCountry('800555353', 'RU', 'FIXED_LINE')).to.equal('TOO_SHORT')\r\n\t\t// Normal.\r\n\t\texpect(checkNumberLengthWithCountry('8005553535', 'RU', 'FIXED_LINE')).to.equal('IS_POSSIBLE')\r\n\t\t// Too long.\r\n\t\texpect(checkNumberLengthWithCountry('80055535355', 'RU', 'FIXED_LINE')).to.equal('TOO_LONG')\r\n\r\n\t\t// No such type.\r\n\t\texpect(checkNumberLengthWithCountry('169454850', 'AC', 'VOIP')).to.equal('INVALID_LENGTH')\r\n\t\t// No such possible length.\r\n\t\texpect(checkNumberLengthWithCountry('1694548', 'AD', undefined)).to.equal('INVALID_LENGTH')\r\n\r\n\t\t// FIXED_LINE_OR_MOBILE\r\n\t\texpect(checkNumberLengthWithCountry('1694548', 'AD', 'FIXED_LINE_OR_MOBILE')).to.equal('INVALID_LENGTH')\r\n\t\t// No mobile phones.\r\n\t\texpect(checkNumberLengthWithCountry('8123', 'TA', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t\t// No \"possible lengths\" for \"mobile\".\r\n\t\texpect(checkNumberLengthWithCountry('81234567', 'SZ', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should check phone number length (no `country` specified)', () => {\r\n\t\t// Too short.\r\n\t\texpect(checkNumberLengthWithoutCountry('800555353', 'RU', 'FIXED_LINE')).to.equal('TOO_SHORT')\r\n\t\t// Normal.\r\n\t\texpect(checkNumberLengthWithoutCountry('8005553535', 'RU', 'FIXED_LINE')).to.equal('IS_POSSIBLE')\r\n\t\t// Too long.\r\n\t\texpect(checkNumberLengthWithoutCountry('80055535355', 'RU', 'FIXED_LINE')).to.equal('TOO_LONG')\r\n\r\n\t\t// No such type.\r\n\t\texpect(checkNumberLengthWithoutCountry('169454850', 'AC', 'VOIP')).to.equal('INVALID_LENGTH')\r\n\t\t// No such possible length.\r\n\t\texpect(checkNumberLengthWithoutCountry('1694548', 'AD', undefined)).to.equal('INVALID_LENGTH')\r\n\r\n\t\t// FIXED_LINE_OR_MOBILE\r\n\t\texpect(checkNumberLengthWithoutCountry('1694548', 'AD', 'FIXED_LINE_OR_MOBILE')).to.equal('INVALID_LENGTH')\r\n\t\t// No mobile phones.\r\n\t\texpect(checkNumberLengthWithoutCountry('8123', 'TA', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t\t// No \"possible lengths\" for \"mobile\".\r\n\t\texpect(checkNumberLengthWithoutCountry('81234567', 'SZ', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should work for old metadata', function() {\r\n\t\tconst _oldMetadata = new Metadata(oldMetadata)\r\n\t\t_oldMetadata.country('RU')\r\n\t\texpect(checkNumberLengthForType('8005553535', 'RU', 'FIXED_LINE', _oldMetadata)).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should work for old metadata (no `country` specified)', function() {\r\n\t\tconst _oldMetadata = new Metadata(oldMetadata)\r\n\t\t_oldMetadata.country('RU')\r\n\t\texpect(checkNumberLengthForType('8005553535', undefined, 'FIXED_LINE', _oldMetadata)).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-\"main\" country and is not possible in the \"main\" country', () => {\r\n\t\tconst _metadata = new Metadata(metadata)\r\n\t\t_metadata.country('US')\r\n\r\n\t\texpect(checkNumberLength('3100000', undefined, _metadata)).to.equal('TOO_SHORT');\r\n\t\texpect(checkNumberLength('3100000', 'US', _metadata)).to.equal('TOO_SHORT');\r\n\t\texpect(checkNumberLength('3100000', 'CA', _metadata)).to.equal('IS_POSSIBLE');\r\n\t})\r\n})\r\n\r\nfunction checkNumberLengthWithCountry(number, country, type) {\r\n\tconst _metadata = new Metadata(metadata)\r\n\t_metadata.country(country)\r\n\treturn checkNumberLengthForType(number, country, type, _metadata)\r\n}\r\n\r\nfunction checkNumberLengthWithoutCountry(number, country, type) {\r\n\tconst _metadata = new Metadata(metadata)\r\n\t_metadata.country(country)\r\n\treturn checkNumberLengthForType(number, undefined, type, _metadata)\r\n}"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAClE,OAAOC,WAAW,MAAM,6CAA6C,QAAQD,IAAI,EAAE,MAAM;AAEzF,OAAOE,iBAAiB,IAAIC,wBAAwB,QAAQ,wBAAwB;AAEpFR,QAAQ,CAAC,mBAAmB,EAAE,MAAM;EACnCC,EAAE,CAAC,kCAAkC,EA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AEL,SAAS,EAAEP,IAAI,EAAEW,SAAS,CAAC;AACpE","ignoreList":[]}
@@ -0,0 +1,93 @@
1
+ import { VALID_DIGITS } from '../../constants.js';
2
+
3
+ // The RFC 3966 format for extensions.
4
+ const RFC3966_EXTN_PREFIX = ';ext=';
5
+
6
+ /**
7
+ * Helper method for constructing regular expressions for parsing. Creates
8
+ * an expression that captures up to max_length digits.
9
+ * @return {string} RegEx pattern to capture extension digits.
10
+ */
11
+ const getExtensionDigitsPattern = maxLength => `([${VALID_DIGITS}]{1,${maxLength}})`;
12
+
13
+ /**
14
+ * Helper initialiser method to create the regular-expression pattern to match
15
+ * extensions.
16
+ * Copy-pasted from Google's `libphonenumber`:
17
+ * https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766
18
+ * @return {string} RegEx pattern to capture extensions.
19
+ */
20
+ export default function createExtensionPattern(purpose) {
21
+ // We cap the maximum length of an extension based on the ambiguity of the way
22
+ // the extension is prefixed. As per ITU, the officially allowed length for
23
+ // extensions is actually 40, but we don't support this since we haven't seen real
24
+ // examples and this introduces many false interpretations as the extension labels
25
+ // are not standardized.
26
+ /** @type {string} */
27
+ var extLimitAfterExplicitLabel = '20';
28
+ /** @type {string} */
29
+ var extLimitAfterLikelyLabel = '15';
30
+ /** @type {string} */
31
+ var extLimitAfterAmbiguousChar = '9';
32
+ /** @type {string} */
33
+ var extLimitWhenNotSure = '6';
34
+
35
+ /** @type {string} */
36
+ var possibleSeparatorsBetweenNumberAndExtLabel = "[ \u00A0\\t,]*";
37
+ // Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.
38
+ /** @type {string} */
39
+ var possibleCharsAfterExtLabel = "[:\\.\uFF0E]?[ \u00A0\\t,-]*";
40
+ /** @type {string} */
41
+ var optionalExtnSuffix = "#?";
42
+
43
+ // Here the extension is called out in more explicit way, i.e mentioning it obvious
44
+ // patterns like "ext.".
45
+ /** @type {string} */
46
+ var explicitExtLabels = "(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|\u0434\u043E\u0431|anexo)";
47
+ // One-character symbols that can be used to indicate an extension, and less
48
+ // commonly used or more ambiguous extension labels.
49
+ /** @type {string} */
50
+ var ambiguousExtLabels = "(?:[x\uFF58#\uFF03~\uFF5E]|int|\uFF49\uFF4E\uFF54)";
51
+ // When extension is not separated clearly.
52
+ /** @type {string} */
53
+ var ambiguousSeparator = "[- ]+";
54
+ // This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching
55
+ // comma as extension label may have it.
56
+ /** @type {string} */
57
+ var possibleSeparatorsNumberExtLabelNoComma = "[ \u00A0\\t]*";
58
+ // ",," is commonly used for auto dialling the extension when connected. First
59
+ // comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do
60
+ // not repeat it here. Semi-colon works in Iphone and Android also to pop up a
61
+ // button with the extension number following.
62
+ /** @type {string} */
63
+ var autoDiallingAndExtLabelsFound = "(?:,{2}|;)";
64
+
65
+ /** @type {string} */
66
+ var rfcExtn = RFC3966_EXTN_PREFIX + getExtensionDigitsPattern(extLimitAfterExplicitLabel);
67
+ /** @type {string} */
68
+ var explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterExplicitLabel) + optionalExtnSuffix;
69
+ /** @type {string} */
70
+ var ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix;
71
+ /** @type {string} */
72
+ var americanStyleExtnWithSuffix = ambiguousSeparator + getExtensionDigitsPattern(extLimitWhenNotSure) + "#";
73
+
74
+ /** @type {string} */
75
+ var autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma + autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterLikelyLabel) + optionalExtnSuffix;
76
+ /** @type {string} */
77
+ var onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma + "(?:,)+" + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix;
78
+
79
+ // The first regular expression covers RFC 3966 format, where the extension is added
80
+ // using ";ext=". The second more generic where extension is mentioned with explicit
81
+ // labels like "ext:". In both the above cases we allow more numbers in extension than
82
+ // any other extension labels. The third one captures when single character extension
83
+ // labels or less commonly used labels are used. In such cases we capture fewer
84
+ // extension digits in order to reduce the chance of falsely interpreting two
85
+ // numbers beside each other as a number + extension. The fourth one covers the
86
+ // special case of American numbers where the extension is written with a hash
87
+ // at the end, such as "- 503#". The fifth one is exclusively for extension
88
+ // autodialling formats which are used when dialling and in this case we accept longer
89
+ // extensions. The last one is more liberal on the number of commas that acts as
90
+ // extension labels, so we have a strict cap on the number of digits in such extensions.
91
+ return rfcExtn + "|" + explicitExtn + "|" + ambiguousExtn + "|" + americanStyleExtnWithSuffix + "|" + autoDiallingExtn + "|" + onlyCommasExtn;
92
+ }
93
+ //# sourceMappingURL=createExtensionPattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createExtensionPattern.js","names":["VALID_DIGITS","RFC3966_EXTN_PREFIX","getExtensionDigitsPattern","maxLength","createExtensionPattern","purpose","extLimitAfterExplicitLabel","extLimitAfterLikelyLabel","extLimitAfterAmbiguousChar","extLimitWhenNotSure","possibleSeparatorsBetweenNumberAndExtLabel","possibleCharsAfterExtLabel","optionalExtnSuffix","explicitExtLabels","ambiguousExtLabels","ambiguousSeparator","possibleSeparatorsNumberExtLabelNoComma","autoDiallingAndExtLabelsFound","rfcExtn","explicitExtn","ambiguousExtn","americanStyleExtnWithSuffix","autoDiallingExtn","onlyCommasExtn"],"sources":["../../../source/helpers/extension/createExtensionPattern.js"],"sourcesContent":["import { VALID_DIGITS } from '../../constants.js'\r\n\r\n// The RFC 3966 format for extensions.\r\nconst RFC3966_EXTN_PREFIX = ';ext='\r\n\r\n/**\r\n * Helper method for constructing regular expressions for parsing. Creates\r\n * an expression that captures up to max_length digits.\r\n * @return {string} RegEx pattern to capture extension digits.\r\n */\r\nconst getExtensionDigitsPattern = (maxLength) => `([${VALID_DIGITS}]{1,${maxLength}})`\r\n\r\n/**\r\n * Helper initialiser method to create the regular-expression pattern to match\r\n * extensions.\r\n * Copy-pasted from Google's `libphonenumber`:\r\n * https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766\r\n * @return {string} RegEx pattern to capture extensions.\r\n */\r\nexport default function createExtensionPattern(purpose) {\r\n\t// We cap the maximum length of an extension based on the ambiguity of the way\r\n\t// the extension is prefixed. As per ITU, the officially allowed length for\r\n\t// extensions is actually 40, but we don't support this since we haven't seen real\r\n\t// examples and this introduces many false interpretations as the extension labels\r\n\t// are not standardized.\r\n\t/** @type {string} */\r\n\tvar extLimitAfterExplicitLabel = '20';\r\n\t/** @type {string} */\r\n\tvar extLimitAfterLikelyLabel = '15';\r\n\t/** @type {string} */\r\n\tvar extLimitAfterAmbiguousChar = '9';\r\n\t/** @type {string} */\r\n\tvar extLimitWhenNotSure = '6';\r\n\r\n\t/** @type {string} */\r\n\tvar possibleSeparatorsBetweenNumberAndExtLabel = \"[ \\u00A0\\\\t,]*\";\r\n\t// Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.\r\n\t/** @type {string} */\r\n\tvar possibleCharsAfterExtLabel = \"[:\\\\.\\uFF0E]?[ \\u00A0\\\\t,-]*\";\r\n\t/** @type {string} */\r\n\tvar optionalExtnSuffix = \"#?\";\r\n\r\n\t// Here the extension is called out in more explicit way, i.e mentioning it obvious\r\n\t// patterns like \"ext.\".\r\n\t/** @type {string} */\r\n\tvar explicitExtLabels =\r\n\t \"(?:e?xt(?:ensi(?:o\\u0301?|\\u00F3))?n?|\\uFF45?\\uFF58\\uFF54\\uFF4E?|\\u0434\\u043E\\u0431|anexo)\";\r\n\t// One-character symbols that can be used to indicate an extension, and less\r\n\t// commonly used or more ambiguous extension labels.\r\n\t/** @type {string} */\r\n\tvar ambiguousExtLabels = \"(?:[x\\uFF58#\\uFF03~\\uFF5E]|int|\\uFF49\\uFF4E\\uFF54)\";\r\n\t// When extension is not separated clearly.\r\n\t/** @type {string} */\r\n\tvar ambiguousSeparator = \"[- ]+\";\r\n\t// This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching\r\n\t// comma as extension label may have it.\r\n\t/** @type {string} */\r\n\tvar possibleSeparatorsNumberExtLabelNoComma = \"[ \\u00A0\\\\t]*\";\r\n\t// \",,\" is commonly used for auto dialling the extension when connected. First\r\n\t// comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do\r\n\t// not repeat it here. Semi-colon works in Iphone and Android also to pop up a\r\n\t// button with the extension number following.\r\n\t/** @type {string} */\r\n\tvar autoDiallingAndExtLabelsFound = \"(?:,{2}|;)\";\r\n\r\n\t/** @type {string} */\r\n\tvar rfcExtn = RFC3966_EXTN_PREFIX\r\n\t + getExtensionDigitsPattern(extLimitAfterExplicitLabel);\r\n\t/** @type {string} */\r\n\tvar explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels\r\n\t + possibleCharsAfterExtLabel\r\n\t + getExtensionDigitsPattern(extLimitAfterExplicitLabel)\r\n\t + optionalExtnSuffix;\r\n\t/** @type {string} */\r\n\tvar ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels\r\n\t + possibleCharsAfterExtLabel\r\n\t+ getExtensionDigitsPattern(extLimitAfterAmbiguousChar)\r\n\t+ optionalExtnSuffix;\r\n\t/** @type {string} */\r\n\tvar americanStyleExtnWithSuffix = ambiguousSeparator\r\n\t+ getExtensionDigitsPattern(extLimitWhenNotSure) + \"#\";\r\n\r\n\t/** @type {string} */\r\n\tvar autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma\r\n\t + autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel\r\n\t + getExtensionDigitsPattern(extLimitAfterLikelyLabel)\r\n\t+ optionalExtnSuffix;\r\n\t/** @type {string} */\r\n\tvar onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma\r\n\t + \"(?:,)+\" + possibleCharsAfterExtLabel\r\n\t + getExtensionDigitsPattern(extLimitAfterAmbiguousChar)\r\n\t + optionalExtnSuffix;\r\n\r\n\t// The first regular expression covers RFC 3966 format, where the extension is added\r\n\t// using \";ext=\". The second more generic where extension is mentioned with explicit\r\n\t// labels like \"ext:\". In both the above cases we allow more numbers in extension than\r\n\t// any other extension labels. The third one captures when single character extension\r\n\t// labels or less commonly used labels are used. In such cases we capture fewer\r\n\t// extension digits in order to reduce the chance of falsely interpreting two\r\n\t// numbers beside each other as a number + extension. The fourth one covers the\r\n\t// special case of American numbers where the extension is written with a hash\r\n\t// at the end, such as \"- 503#\". The fifth one is exclusively for extension\r\n\t// autodialling formats which are used when dialling and in this case we accept longer\r\n\t// extensions. The last one is more liberal on the number of commas that acts as\r\n\t// extension labels, so we have a strict cap on the number of digits in such extensions.\r\n\treturn rfcExtn + \"|\"\r\n\t + explicitExtn + \"|\"\r\n\t + ambiguousExtn + \"|\"\r\n\t + americanStyleExtnWithSuffix + \"|\"\r\n\t + autoDiallingExtn + \"|\"\r\n\t + onlyCommasExtn;\r\n}"],"mappings":"AAAA,SAASA,YAAY,QAAQ,oBAAoB;;AAEjD;AACA,MAAMC,mBAAmB,GAAG,OAAO;;AAEnC;AACA;AACA;AACA;AACA;AACA,MAAMC,yBAAyB,GAAIC,SAAS,IAAK,KAAKH,YAAY,OAAOG,SAAS,IAAI;;AAEtF;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,sBAAsBA,CAACC,OAAO,EAAE;EACvD;EACA;EACA;EACA;EACA;EACA;EACA,IAAIC,0BAA0B,GAAG,IAAI;EACrC;EACA,IAAIC,wBAAwB,GAAG,IAAI;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AAY,GAAG,GAAG,GAClBC,aAAa,GAAG,GAAG,GACnBC,2BAA2B,GAAG,GAAG,GACjCC,gBAAgB,GAAG,GAAG,GACtBC,cAAc;AACxB","ignoreList":[]}
@@ -0,0 +1,30 @@
1
+ import createExtensionPattern from './createExtensionPattern.js';
2
+
3
+ // Regexp of all known extension prefixes used by different regions followed by
4
+ // 1 or more valid digits, for use when parsing.
5
+ const EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i');
6
+
7
+ // Strips any extension (as in, the part of the number dialled after the call is
8
+ // connected, usually indicated with extn, ext, x or similar) from the end of
9
+ // the number, and returns it.
10
+ export default function extractExtension(number) {
11
+ const start = number.search(EXTN_PATTERN);
12
+ if (start < 0) {
13
+ return {};
14
+ }
15
+ // If we find a potential extension, and the number preceding this is a viable
16
+ // number, we assume it is an extension.
17
+ const numberWithoutExtension = number.slice(0, start);
18
+ const matches = number.match(EXTN_PATTERN);
19
+ let i = 1;
20
+ while (i < matches.length) {
21
+ if (matches[i]) {
22
+ return {
23
+ number: numberWithoutExtension,
24
+ ext: matches[i]
25
+ };
26
+ }
27
+ i++;
28
+ }
29
+ }
30
+ //# sourceMappingURL=extractExtension.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractExtension.js","names":["createExtensionPattern","EXTN_PATTERN","RegExp","extractExtension","number","start","search","numberWithoutExtension","slice","matches","match","i","length","ext"],"sources":["../../../source/helpers/extension/extractExtension.js"],"sourcesContent":["import createExtensionPattern from './createExtensionPattern.js'\r\n\r\n// Regexp of all known extension prefixes used by different regions followed by\r\n// 1 or more valid digits, for use when parsing.\r\nconst EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i')\r\n\r\n// Strips any extension (as in, the part of the number dialled after the call is\r\n// connected, usually indicated with extn, ext, x or similar) from the end of\r\n// the number, and returns it.\r\nexport default function extractExtension(number) {\r\n\tconst start = number.search(EXTN_PATTERN)\r\n\tif (start < 0) {\r\n\t\treturn {}\r\n\t}\r\n\t// If we find a potential extension, and the number preceding this is a viable\r\n\t// number, we assume it is an extension.\r\n\tconst numberWithoutExtension = number.slice(0, start)\r\n\tconst matches = number.match(EXTN_PATTERN)\r\n\tlet i = 1\r\n\twhile (i < matches.length) {\r\n\t\tif (matches[i]) {\r\n\t\t\treturn {\r\n\t\t\t\tnumber: numberWithoutExtension,\r\n\t\t\t\text: matches[i]\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n}"],"mappings":"AAAA,OAAOA,sBAAsB,MAAM,6BAA6B;;AAEhE;AACA;AACA,MAAMC,YAAY,GAAG,IAAIC,MAAM,CAAC,KAAK,GAAGF,sBAAsB,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC;;AAE7E;AACA;AACA;AACA,eAAe,SAASG,gBAAgBA,CAACC,MAAM,EAAE;EAChD,MAAMC,KAAK,GAAGD,MAAM,CAACE,MAAM,CAACL,YAAY,CAAC;EACzC,IAAII,KAAK,GAAG,CAAC,EAAE;IACd,OAAO,CAAC,CAAC;EACV;EACA;EACA;EACA,MAAME,sBAAsB,GAAGH,MAAM,CAACI,KAAK,CAAC,CAAC,EAAEH,KAAK,CAAC;EACrD,MAAMI,OAAO,GAAGL,MAAM,CAACM,KAAK,CAACT,YAAY,CAAC;EAC1C,IAAIU,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGF,OAAO,CAACG,MAAM,EAAE;IAC1B,IAAIH,OAAO,CAACE,CAAC,CAAC,EAAE;MACf,OAAO;QACNP,MAAM,EAAEG,sBAAsB;QAC9BM,GAAG,EAAEJ,OAAO,CAACE,CAAC;MACf,CAAC;IACF;IACAA,CAAC,EAAE;EACJ;AACD","ignoreList":[]}
@@ -0,0 +1,142 @@
1
+ import stripIddPrefix from './stripIddPrefix.js';
2
+ import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js';
3
+ import Metadata from '../metadata.js';
4
+ import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js';
5
+
6
+ /**
7
+ * Converts a phone number digits (possibly with a `+`)
8
+ * into a calling code and the rest phone number digits.
9
+ * The "rest phone number digits" could include
10
+ * a national prefix, carrier code, and national
11
+ * (significant) number.
12
+ * @param {string} number — Phone number digits (possibly with a `+`).
13
+ * @param {string} [country] — Country.
14
+ * @param {string} [defaultCountry] — Default country.
15
+ * @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).
16
+ * @param {object} metadata
17
+ * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
18
+ * @example
19
+ * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
20
+ * extractCountryCallingCode('2133734253', null, 'US', null, metadata)
21
+ * extractCountryCallingCode('2133734253', null, null, '1', metadata)
22
+ * extractCountryCallingCode('+12133734253', null, null, null, metadata)
23
+ * extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)
24
+ */
25
+ export default function extractCountryCallingCode(number, country, defaultCountry, defaultCallingCode, metadata) {
26
+ if (!number) {
27
+ return {};
28
+ }
29
+ let isNumberWithIddPrefix;
30
+
31
+ // If this is not an international phone number,
32
+ // then either extract an "IDD" prefix, or extract a
33
+ // country calling code from a number by autocorrecting it
34
+ // by prepending a leading `+` in cases when it starts
35
+ // with the country calling code.
36
+ // https://wikitravel.org/en/International_dialling_prefix
37
+ // https://github.com/catamphetamine/libphonenumber-js/issues/376
38
+ if (number[0] !== '+') {
39
+ // Convert an "out-of-country" dialing phone number
40
+ // to a proper international phone number.
41
+ const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadata);
42
+ // If an IDD prefix was stripped then
43
+ // convert the number to international one
44
+ // for subsequent parsing.
45
+ if (numberWithoutIDD && numberWithoutIDD !== number) {
46
+ isNumberWithIddPrefix = true;
47
+ number = '+' + numberWithoutIDD;
48
+ } else {
49
+ // Check to see if the number starts with the country calling code
50
+ // for the default country. If so, we remove the country calling code,
51
+ // and do some checks on the validity of the number before and after.
52
+ // https://github.com/catamphetamine/libphonenumber-js/issues/376
53
+ if (country || defaultCountry || defaultCallingCode) {
54
+ const {
55
+ countryCallingCode,
56
+ number: shorterNumber
57
+ } = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadata);
58
+ if (countryCallingCode) {
59
+ return {
60
+ countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
61
+ countryCallingCode,
62
+ number: shorterNumber
63
+ };
64
+ }
65
+ }
66
+ return {
67
+ // No need to set it to `UNSPECIFIED`. It can be just `undefined`.
68
+ // countryCallingCodeSource: 'UNSPECIFIED',
69
+ number
70
+ };
71
+ }
72
+ }
73
+
74
+ // `number` can only be international at this point.
75
+
76
+ // Fast abortion: country codes do not begin with a '0'
77
+ if (number[1] === '0') {
78
+ return {};
79
+ }
80
+ metadata = new Metadata(metadata);
81
+
82
+ // The thing with country phone codes
83
+ // is that they are orthogonal to each other
84
+ // i.e. there's no such country phone code A
85
+ // for which country phone code B exists
86
+ // where B starts with A.
87
+ // Therefore, while scanning digits,
88
+ // if a valid country code is found,
89
+ // that means that it is the country code.
90
+ //
91
+ let i = 2;
92
+ while (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
93
+ const countryCallingCode = number.slice(1, i);
94
+ if (metadata.hasCallingCode(countryCallingCode)) {
95
+ metadata.selectNumberingPlan(countryCallingCode);
96
+ return {
97
+ countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
98
+ countryCallingCode,
99
+ number: number.slice(i)
100
+ };
101
+ }
102
+ i++;
103
+ }
104
+ return {};
105
+ }
106
+
107
+ // The possible values for the returned `countryCallingCodeSource` are:
108
+ //
109
+ // Copy-pasted from:
110
+ // https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
111
+ //
112
+ // // The source from which the country_code is derived. This is not set in the
113
+ // // general parsing method, but in the method that parses and keeps raw_input.
114
+ // // New fields could be added upon request.
115
+ // enum CountryCodeSource {
116
+ // // Default value returned if this is not set, because the phone number was
117
+ // // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
118
+ // // return false if this is the case.
119
+ // UNSPECIFIED = 0;
120
+ //
121
+ // // The country_code is derived based on a phone number with a leading "+",
122
+ // // e.g. the French number "+33 1 42 68 53 00".
123
+ // FROM_NUMBER_WITH_PLUS_SIGN = 1;
124
+ //
125
+ // // The country_code is derived based on a phone number with a leading IDD,
126
+ // // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
127
+ // FROM_NUMBER_WITH_IDD = 5;
128
+ //
129
+ // // The country_code is derived based on a phone number without a leading
130
+ // // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
131
+ // // supplied as France.
132
+ // FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
133
+ //
134
+ // // The country_code is derived NOT based on the phone number itself, but
135
+ // // from the defaultCountry parameter provided in the parsing function by the
136
+ // // clients. This happens mostly for numbers written in the national format
137
+ // // (without country code). For example, this would be set when parsing the
138
+ // // French number "01 42 68 53 00", when defaultCountry is supplied as
139
+ // // France.
140
+ // FROM_DEFAULT_COUNTRY = 20;
141
+ // }
142
+ //# sourceMappingURL=extractCountryCallingCode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractCountryCallingCode.js","names":["stripIddPrefix","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","Metadata","MAX_LENGTH_COUNTRY_CODE","extractCountryCallingCode","number","country","defaultCountry","defaultCallingCode","metadata","isNumberWithIddPrefix","numberWithoutIDD","countryCallingCode","shorterNumber","countryCallingCodeSource","i","length","slice","hasCallingCode","selectNumberingPlan"],"sources":["../../source/helpers/extractCountryCallingCode.js"],"sourcesContent":["import stripIddPrefix from './stripIddPrefix.js'\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'\r\nimport Metadata from '../metadata.js'\r\nimport { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'\r\n\r\n/**\r\n * Converts a phone number digits (possibly with a `+`)\r\n * into a calling code and the rest phone number digits.\r\n * The \"rest phone number digits\" could include\r\n * a national prefix, carrier code, and national\r\n * (significant) number.\r\n * @param {string} number — Phone number digits (possibly with a `+`).\r\n * @param {string} [country] — Country.\r\n * @param {string} [defaultCountry] — Default country.\r\n * @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`\r\n * @example\r\n * // Returns `{ countryCallingCode: \"1\", number: \"2133734253\" }`.\r\n * extractCountryCallingCode('2133734253', null, 'US', null, metadata)\r\n * extractCountryCallingCode('2133734253', null, null, '1', metadata)\r\n * extractCountryCallingCode('+12133734253', null, null, null, metadata)\r\n * extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)\r\n */\r\nexport default function extractCountryCallingCode(\r\n\tnumber,\r\n\tcountry,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadata\r\n) {\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tlet isNumberWithIddPrefix\r\n\r\n\t// If this is not an international phone number,\r\n\t// then either extract an \"IDD\" prefix, or extract a\r\n\t// country calling code from a number by autocorrecting it\r\n\t// by prepending a leading `+` in cases when it starts\r\n\t// with the country calling code.\r\n\t// https://wikitravel.org/en/International_dialling_prefix\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\tif (number[0] !== '+') {\r\n\t\t// Convert an \"out-of-country\" dialing phone number\r\n\t\t// to a proper international phone number.\r\n\t\tconst numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadata)\r\n\t\t// If an IDD prefix was stripped then\r\n\t\t// convert the number to international one\r\n\t\t// for subsequent parsing.\r\n\t\tif (numberWithoutIDD && numberWithoutIDD !== number) {\r\n\t\t\tisNumberWithIddPrefix = true\r\n\t\t\tnumber = '+' + numberWithoutIDD\r\n\t\t} else {\r\n\t\t\t// Check to see if the number starts with the country calling code\r\n\t\t\t// for the default country. If so, we remove the country calling code,\r\n\t\t\t// and do some checks on the validity of the number before and after.\r\n\t\t\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\t\t\tif (country || defaultCountry || defaultCallingCode) {\r\n\t\t\t\tconst {\r\n\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t\t\tnumber,\r\n\t\t\t\t\tcountry,\r\n\t\t\t\t\tdefaultCountry,\r\n\t\t\t\t\tdefaultCallingCode,\r\n\t\t\t\t\tmetadata\r\n\t\t\t\t)\r\n\t\t\t\tif (countryCallingCode) {\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',\r\n\t\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\t// No need to set it to `UNSPECIFIED`. It can be just `undefined`.\r\n\t\t\t\t// countryCallingCodeSource: 'UNSPECIFIED',\r\n\t\t\t\tnumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// `number` can only be international at this point.\r\n\r\n\t// Fast abortion: country codes do not begin with a '0'\r\n\tif (number[1] === '0') {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// The thing with country phone codes\r\n\t// is that they are orthogonal to each other\r\n\t// i.e. there's no such country phone code A\r\n\t// for which country phone code B exists\r\n\t// where B starts with A.\r\n\t// Therefore, while scanning digits,\r\n\t// if a valid country code is found,\r\n\t// that means that it is the country code.\r\n\t//\r\n\tlet i = 2\r\n\twhile (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {\r\n\t\tconst countryCallingCode = number.slice(1, i)\r\n\t\tif (metadata.hasCallingCode(countryCallingCode)) {\r\n\t\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: number.slice(i)\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn {}\r\n}\r\n\r\n// The possible values for the returned `countryCallingCodeSource` are:\r\n//\r\n// Copy-pasted from:\r\n// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto\r\n//\r\n// // The source from which the country_code is derived. This is not set in the\r\n// // general parsing method, but in the method that parses and keeps raw_input.\r\n// // New fields could be added upon request.\r\n// enum CountryCodeSource {\r\n// // Default value returned if this is not set, because the phone number was\r\n// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will\r\n// // return false if this is the case.\r\n// UNSPECIFIED = 0;\r\n//\r\n// // The country_code is derived based on a phone number with a leading \"+\",\r\n// // e.g. the French number \"+33 1 42 68 53 00\".\r\n// FROM_NUMBER_WITH_PLUS_SIGN = 1;\r\n//\r\n// // The country_code is derived based on a phone number with a leading IDD,\r\n// // e.g. the French number \"011 33 1 42 68 53 00\", as it is dialled from US.\r\n// FROM_NUMBER_WITH_IDD = 5;\r\n//\r\n// // The country_code is derived based on a phone number without a leading\r\n// // \"+\", e.g. the French number \"33 1 42 68 53 00\" when defaultCountry is\r\n// // supplied as France.\r\n// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;\r\n//\r\n// // The country_code is derived NOT based on the phone number itself, but\r\n// // from the defaultCountry parameter provided in the parsing function by the\r\n// // clients. This happens mostly for numbers written in the national format\r\n// // (without country code). For example, this would be set when parsing the\r\n// // French number \"01 42 68 53 00\", when defaultCountry is supplied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;;EAEA;;EAEA;EACA,IAAIA,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;IACtB,OAAO,CAAC,CAAC;EACV;EAEAI,QAAQ,GAAG,IAAIP,QAAQ,CAACO,QAAQ,CAAC;;EAEjC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAIM,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAG,CAAC,IAAIZ,uBAAuB,IAAIY,CAAC,IAAIV,MAAM,CAACW,MAAM,EAAE;IAC9D,MAAMJ,kBAAkB,GAAGP,MAAM,CAACY,KAAK,CAAC,CAAC,EAAEF,CAAC,CAAC;IAC7C,IAAIN,QAAQ,CAACS,cAAc,CAACN,kBAAkB,CAAC,EAAE;MAChDH,QAAQ,CAACU,mBAAmB,CAACP,kBAAkB,CAAC;MAChD,OAAO;QACNE,wBAAwB,EAAEJ,qBAAqB,GAAG,sBAAsB,GAAG,4BAA4B;QACvGE,kBAAkB;QAClBP,MAAM,EAAEA,MAAM,CAACY,KAAK,CAACF,CAAC;MACvB,CAAC;IACF;IACAA,CAAC,EAAE;EACJ;EAEA,OAAO,CAAC,CAAC;AACV;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractCountryCallingCode.test.js","names":["describe","it","expect","extractCountryCallingCode","metadata","type","to","deep","equal","countryCallingCodeSource","countryCallingCode","number"],"sources":["../../source/helpers/extractCountryCallingCode.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport extractCountryCallingCode from './extractCountryCallingCode.js'\r\nimport metadata from '../../metadata.min.json' with { type: 'json' }\r\n\r\ndescribe('extractCountryCallingCode', () => {\r\n\tit('should extract country calling code from a number', () => {\r\n\t\texpect(extractCountryCallingCode('+78005553535', null, null, null, metadata)).to.deep.equal({\r\n\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\texpect(extractCountryCallingCode('+7800', null, null, null, metadata)).to.deep.equal({\r\n\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '800'\r\n\t\t})\r\n\r\n\t\texpect(extractCountryCallingCode('', null, null, null, metadata)).to.deep.equal({})\r\n\t})\r\n})\r\n"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,yBAAyB,MAAM,gCAAgC;AACtE,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAElEL,QAAQ,CAAC,2BAA2B,EAAE,MAAM;EAC3CC,EAAE,CAAC,mDAAmD,EAAE,MAAM;IAC7DC,MAAM,CAACC,yBAAyB,CAAC,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAEC,QAAQ,CAAC,CAAC,CAACE,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAC3FC,wBAAwB,EAAE,4BAA4B;MACtDC,kBAAkB,EAAE,GAAG;MACvBC,MAAM,EAAE;IACT,CAAC,CAAC;IAEFT,MAAM,CAACC,yBAAyB,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAEC,QAAQ,CAAC,CAAC,CAACE,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MACpFC,wBAAwB,EAAE,4BAA4B;MACtDC,kBAAkB,EAAE,GAAG;MACvBC,MAAM,EAAE;IACT,CAAC,CAAC;IAEFT,MAAM,CAACC,yBAAyB,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAEC,QAAQ,CAAC,CAAC,CAACE,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC,CAAC,CAAC,CAAC;EACpF,CAAC,CAAC;AACH,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,49 @@
1
+ import Metadata from '../metadata.js';
2
+ import matchesEntirely from './matchesEntirely.js';
3
+ import extractNationalNumber from './extractNationalNumber.js';
4
+ import checkNumberLength from './checkNumberLength.js';
5
+ import getCountryCallingCode from '../getCountryCallingCode.js';
6
+
7
+ /**
8
+ * Sometimes some people incorrectly input international phone numbers
9
+ * without the leading `+`. This function corrects such input.
10
+ * @param {string} number — Phone number digits.
11
+ * @param {string} [country] — Exact country of the phone number.
12
+ * @param {string} [defaultCountry]
13
+ * @param {string} [defaultCallingCode]
14
+ * @param {object} metadata
15
+ * @return {object} `{ countryCallingCode: string?, number: string }`.
16
+ */
17
+ export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadata) {
18
+ const countryCallingCode = country || defaultCountry ? getCountryCallingCode(country || defaultCountry, metadata) : defaultCallingCode;
19
+ if (number.indexOf(countryCallingCode) === 0) {
20
+ metadata = new Metadata(metadata);
21
+ metadata.selectNumberingPlan(country || defaultCountry, countryCallingCode);
22
+ const possibleShorterNumber = number.slice(countryCallingCode.length);
23
+ const {
24
+ nationalNumber: possibleShorterNationalNumber
25
+ } = extractNationalNumber(possibleShorterNumber, country, metadata);
26
+ const {
27
+ nationalNumber
28
+ } = extractNationalNumber(number, country, metadata);
29
+
30
+ // If the number was not valid before but is valid now,
31
+ // or if it was too long before, we consider the number
32
+ // with the country calling code stripped to be a better result
33
+ // and keep that instead.
34
+ // For example, in Germany (+49), `49` is a valid area code,
35
+ // so if a number starts with `49`, it could be both a valid
36
+ // national German number or an international number without
37
+ // a leading `+`.
38
+ if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLength(nationalNumber, country, metadata) === 'TOO_LONG') {
39
+ return {
40
+ countryCallingCode,
41
+ number: possibleShorterNumber
42
+ };
43
+ }
44
+ }
45
+ return {
46
+ number
47
+ };
48
+ }
49
+ //# sourceMappingURL=extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js","names":["Metadata","matchesEntirely","extractNationalNumber","checkNumberLength","getCountryCallingCode","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","number","country","defaultCountry","defaultCallingCode","metadata","countryCallingCode","indexOf","selectNumberingPlan","possibleShorterNumber","slice","length","nationalNumber","possibleShorterNationalNumber","nationalNumberPattern"],"sources":["../../source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport matchesEntirely from './matchesEntirely.js'\r\nimport extractNationalNumber from './extractNationalNumber.js'\r\nimport checkNumberLength from './checkNumberLength.js'\r\nimport getCountryCallingCode from '../getCountryCallingCode.js'\r\n\r\n/**\r\n * Sometimes some people incorrectly input international phone numbers\r\n * without the leading `+`. This function corrects such input.\r\n * @param {string} number — Phone number digits.\r\n * @param {string} [country] — Exact country of the phone number.\r\n * @param {string} [defaultCountry]\r\n * @param {string} [defaultCallingCode]\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCode: string?, number: string }`.\r\n */\r\nexport default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\tnumber,\r\n\tcountry,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadata\r\n) {\r\n\tconst countryCallingCode = country || defaultCountry\r\n\t\t? getCountryCallingCode(country || defaultCountry, metadata)\r\n\t\t: defaultCallingCode\r\n\r\n\tif (number.indexOf(countryCallingCode) === 0) {\r\n\t\tmetadata = new Metadata(metadata)\r\n\t\tmetadata.selectNumberingPlan(country || defaultCountry, countryCallingCode)\r\n\r\n\t\tconst possibleShorterNumber = number.slice(countryCallingCode.length)\r\n\r\n\t\tconst {\r\n\t\t\tnationalNumber: possibleShorterNationalNumber,\r\n\t\t} = extractNationalNumber(\r\n\t\t\tpossibleShorterNumber,\r\n\t\t\tcountry,\r\n\t\t\tmetadata\r\n\t\t)\r\n\r\n\t\tconst {\r\n\t\t\tnationalNumber\r\n\t\t} = extractNationalNumber(\r\n\t\t\tnumber,\r\n\t\t\tcountry,\r\n\t\t\tmetadata\r\n\t\t)\r\n\r\n\t\t// If the number was not valid before but is valid now,\r\n\t\t// or if it was too long before, we consider the number\r\n\t\t// with the country calling code stripped to be a better result\r\n\t\t// and keep that instead.\r\n\t\t// For example, in Germany (+49), `49` is a valid area code,\r\n\t\t// so if a number starts with `49`, it could be both a valid\r\n\t\t// national German number or an international number without\r\n\t\t// a leading `+`.\r\n\t\tif (\r\n\t\t\t(\r\n\t\t\t\t!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) &&\r\n\t\t\t\tmatchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())\r\n\t\t\t)\r\n\t\t\t||\r\n\t\t\tcheckNumberLength(nationalNumber, country, metadata) === 'TOO_LONG'\r\n\t\t) {\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: possibleShorterNumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn { number }\r\n}"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,eAAe,MAAM,sBAAsB;AAClD,OAAOC,qBAAqB,MAAM,4BAA4B;AAC9D,OAAOC,iBAAiB,MAAM,wBAAwB;AACtD,OAAOC,qBAAqB,MAAM,6BAA6B;;AAE/D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,+DAA+DA,CACtFC,MAAM,EACNC,OAAO,EACPC,cAAc,EACdC,kBAAkB,EAClBC,QAAQ,EACP;EACD,MAAMC,kBAAkB,GAAGJ,OAAO,IAAIC,cAAc,GACjDJ,qBAAqB,CAACG,OAAO,IAAIC,cAAc,EAAEE,QAAQ,CAAC,GAC1DD,kBAAkB;EAErB,IAAIH,MAAM,CAACM,OAAO,CAACD,kBAAkB,CAAC,KAAK,CAAC,EAAE;IAC7CD,QAAQ,GAAG,IAAIV,QAAQ,CAACU,QAAQ,CAAC;IACjCA,QAAQ,CAACG,mBAAmB,CAACN,OAAO,IAAIC,cAAc,EAAEG,kBAAkB,CAAC;IAE3E,MAAMG,qBAAqB,GAAGR,MAAM,CAACS,KAAK,CAACJ,kBAAkB,CAACK,MAAM,CAAC;IAErE,MAAM;MACLC,cAAc,EAAEC;IACjB,CAAC,GAAGhB,qBAAqB,CACxBY,qBAAqB,EACrBP,OAAO,EACPG,QACD,CAAC;IAED,MAAM;MACLO;IACD,CAAC,GAAGf,qBAAqB,CACxBI,MAAM,EACNC,OAAO,EACPG,QACD,CAAC;;IAED;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA,IAEE,CAACT,eAAe,CAACgB,cAAc,EAAEP,QAAQ,CAACS,qBAAqB,CAAC,CAAC,CAAC,IAClElB,eAAe,CAACiB,6BAA6B,EAAER,QAAQ,CAACS,qBAAqB,CAAC,CAAC,CAAC,IAGjFhB,iBAAiB,CAACc,cAAc,EAAEV,OAAO,EAAEG,QAAQ,CAAC,KAAK,UAAU,EAClE;MACD,OAAO;QACNC,kBAAkB;QAClBL,MAAM,EAAEQ;MACT,CAAC;IACF;EACD;EAEA,OAAO;IAAER;EAAO,CAAC;AAClB","ignoreList":[]}
@@ -0,0 +1,66 @@
1
+ import extractPhoneContext, { isPhoneContextValid, PLUS_SIGN, RFC3966_PREFIX_, RFC3966_PHONE_CONTEXT_, RFC3966_ISDN_SUBADDRESS_ } from './extractPhoneContext.js';
2
+ import ParseError from '../ParseError.js';
3
+
4
+ /**
5
+ * @param {string} numberToParse
6
+ * @param {string} nationalNumber
7
+ * @return {}
8
+ */
9
+ export default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, {
10
+ extractFormattedPhoneNumber
11
+ }) {
12
+ const phoneContext = extractPhoneContext(numberToParse);
13
+ if (!isPhoneContextValid(phoneContext)) {
14
+ throw new ParseError('NOT_A_NUMBER');
15
+ }
16
+ let phoneNumberString;
17
+ if (phoneContext === null) {
18
+ // Extract a possible number from the string passed in.
19
+ // (this strips leading characters that could not be the start of a phone number)
20
+ phoneNumberString = extractFormattedPhoneNumber(numberToParse) || '';
21
+ } else {
22
+ phoneNumberString = '';
23
+
24
+ // If the phone context contains a phone number prefix, we need to capture
25
+ // it, whereas domains will be ignored.
26
+ if (phoneContext.charAt(0) === PLUS_SIGN) {
27
+ phoneNumberString += phoneContext;
28
+ }
29
+
30
+ // Now append everything between the "tel:" prefix and the phone-context.
31
+ // This should include the national number, an optional extension or
32
+ // isdn-subaddress component. Note we also handle the case when "tel:" is
33
+ // missing, as we have seen in some of the phone number inputs.
34
+ // In that case, we append everything from the beginning.
35
+ const indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_);
36
+ let indexOfNationalNumber;
37
+ // RFC 3966 "tel:" prefix is preset at this stage because
38
+ // `isPhoneContextValid()` requires it to be present.
39
+ /* istanbul ignore else */
40
+ if (indexOfRfc3966Prefix >= 0) {
41
+ indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length;
42
+ } else {
43
+ indexOfNationalNumber = 0;
44
+ }
45
+ const indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_);
46
+ phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext);
47
+ }
48
+
49
+ // Delete the isdn-subaddress and everything after it if it is present.
50
+ // Note extension won't appear at the same time with isdn-subaddress
51
+ // according to paragraph 5.3 of the RFC3966 spec.
52
+ const indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_);
53
+ if (indexOfIsdn > 0) {
54
+ phoneNumberString = phoneNumberString.substring(0, indexOfIsdn);
55
+ }
56
+ // If both phone context and isdn-subaddress are absent but other
57
+ // parameters are present, the parameters are left in nationalNumber.
58
+ // This is because we are concerned about deleting content from a potential
59
+ // number string when there is no strong evidence that the number is
60
+ // actually written in RFC3966.
61
+
62
+ if (phoneNumberString !== '') {
63
+ return phoneNumberString;
64
+ }
65
+ }
66
+ //# sourceMappingURL=extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js","names":["extractPhoneContext","isPhoneContextValid","PLUS_SIGN","RFC3966_PREFIX_","RFC3966_PHONE_CONTEXT_","RFC3966_ISDN_SUBADDRESS_","ParseError","extractFormattedPhoneNumberFromPossibleRfc3966NumberUri","numberToParse","extractFormattedPhoneNumber","phoneContext","phoneNumberString","charAt","indexOfRfc3966Prefix","indexOf","indexOfNationalNumber","length","indexOfPhoneContext","substring","indexOfIsdn"],"sources":["../../source/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js"],"sourcesContent":["import extractPhoneContext, {\r\n\tisPhoneContextValid,\r\n\tPLUS_SIGN,\r\n\tRFC3966_PREFIX_,\r\n\tRFC3966_PHONE_CONTEXT_,\r\n\tRFC3966_ISDN_SUBADDRESS_\r\n} from './extractPhoneContext.js'\r\n\r\nimport ParseError from '../ParseError.js'\r\n\r\n/**\r\n * @param {string} numberToParse\r\n * @param {string} nationalNumber\r\n * @return {}\r\n */\r\nexport default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, {\r\n\textractFormattedPhoneNumber\r\n}) {\r\n\tconst phoneContext = extractPhoneContext(numberToParse)\r\n\tif (!isPhoneContextValid(phoneContext)) {\r\n\t\tthrow new ParseError('NOT_A_NUMBER')\r\n\t}\r\n\r\n\tlet phoneNumberString\r\n\r\n\tif (phoneContext === null) {\r\n\t\t// Extract a possible number from the string passed in.\r\n\t\t// (this strips leading characters that could not be the start of a phone number)\r\n\t\tphoneNumberString = extractFormattedPhoneNumber(numberToParse) || ''\r\n\t} else {\r\n\t\tphoneNumberString = ''\r\n\r\n\t\t// If the phone context contains a phone number prefix, we need to capture\r\n\t\t// it, whereas domains will be ignored.\r\n\t\tif (phoneContext.charAt(0) === PLUS_SIGN) {\r\n\t\t\tphoneNumberString += phoneContext\r\n\t\t}\r\n\r\n\t\t// Now append everything between the \"tel:\" prefix and the phone-context.\r\n\t\t// This should include the national number, an optional extension or\r\n\t\t// isdn-subaddress component. Note we also handle the case when \"tel:\" is\r\n\t\t// missing, as we have seen in some of the phone number inputs.\r\n\t\t// In that case, we append everything from the beginning.\r\n\t\tconst indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_)\r\n\t\tlet indexOfNationalNumber\r\n\t\t// RFC 3966 \"tel:\" prefix is preset at this stage because\r\n\t\t// `isPhoneContextValid()` requires it to be present.\r\n\t\t/* istanbul ignore else */\r\n\t\tif (indexOfRfc3966Prefix >= 0) {\r\n\t\t\tindexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length\r\n\t\t} else {\r\n\t\t\tindexOfNationalNumber = 0\r\n\t\t}\r\n\t\tconst indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_)\r\n\t\tphoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext)\r\n\t}\r\n\r\n\t// Delete the isdn-subaddress and everything after it if it is present.\r\n\t// Note extension won't appear at the same time with isdn-subaddress\r\n\t// according to paragraph 5.3 of the RFC3966 spec.\r\n\tconst indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_)\r\n\tif (indexOfIsdn > 0) {\r\n\t\tphoneNumberString = phoneNumberString.substring(0, indexOfIsdn)\r\n\t}\r\n\t// If both phone context and isdn-subaddress are absent but other\r\n\t// parameters are present, the parameters are left in nationalNumber.\r\n\t// This is because we are concerned about deleting content from a potential\r\n\t// number string when there is no strong evidence that the number is\r\n\t// actually written in RFC3966.\r\n\r\n\tif (phoneNumberString !== '') {\r\n\t\treturn phoneNumberString\r\n\t}\r\n}"],"mappings":"AAAA,OAAOA,mBAAmB,IACzBC,mBAAmB,EACnBC,SAAS,EACTC,eAAe,EACfC,sBAAsB,EACtBC,wBAAwB,QAClB,0BAA0B;AAEjC,OAAOC,UAAU,MAAM,kBAAkB;;AAEzC;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,uDAAuDA,CAACC,aAAa,EAAE;EAC9FC;AACD,CAAC,EAAE;EACF,MAAMC,YAAY,GAAGV,mBAAmB,CAACQ,aAAa,CAAC;EACvD,IAAI,CAACP,mBAAmB,CAACS,YAAY,CAAC,EAAE;IACvC,MAAM,IAAIJ,UAAU,CAAC,cAAc,CAAC;EACrC;EAEA,IAAIK,iBAAiB;EAErB,IAAID,YAAY,KAAK,IAAI,EAAE;IAC1B;IACA;IACAC,iBAAiB,GAAGF,2BAA2B,CAACD,aAAa,CAAC,IAAI,EAAE;EACrE,CAAC,MAAM;IACNG,iBAAiB,GAAG,EAAE;;IAEtB;IACA;IACA,IAAID,YAAY,CAACE,MAAM,CAAC,CAAC,CAAC,KAAKV,SAAS,EAAE;MACzCS,iBAAiB,IAAID,YAAY;IAClC;;IAEA;IACA;IACA;IACA;IACA;IACA,MAAMG,oBAAoB,GAAGL,aAAa,CAACM,OAAO,CAACX,eAAe,CAAC;IACnE,IAAIY,qBAAqB;IACzB;IACA;IACA;IACA,IAAIF,oBAAoB,IAAI,CAAC,EAAE;MAC9BE,qBAAqB,GAAGF,oBAAoB,GAAGV,eAAe,CAACa,MAAM;IACtE,CAAC,MAAM;MACND,qBAAqB,GAAG,CAAC;IAC1B;IACA,MAAME,mBAAmB,GAAGT,aAAa,CAACM,OAAO,CAACV,sBAAsB,CAAC;IACzEO,iBAAiB,IAAIH,aAAa,CAACU,SAAS,CAACH,qBAAqB,EAAEE,mBAAmB,CAAC;EACzF;;EAEA;EACA;EACA;EACA,MAAME,WAAW,GAAGR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