libphonenumber-js 1.12.43 → 1.13.1

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 +141 -81
  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,15 @@
1
+ // https://stackoverflow.com/a/46971044/970769
2
+ // "Breaking changes in Typescript 2.1"
3
+ // "Extending built-ins like Error, Array, and Map may no longer work."
4
+ // "As a recommendation, you can manually adjust the prototype immediately after any super(...) calls."
5
+ // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
6
+ export default class ParseError extends Error {
7
+ constructor(code) {
8
+ super(code);
9
+ // Set the prototype explicitly.
10
+ // Any subclass of FooError will have to manually set the prototype as well.
11
+ Object.setPrototypeOf(this, ParseError.prototype);
12
+ this.name = this.constructor.name;
13
+ }
14
+ }
15
+ //# sourceMappingURL=ParseError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ParseError.js","names":["ParseError","Error","constructor","code","Object","setPrototypeOf","prototype","name"],"sources":["../source/ParseError.js"],"sourcesContent":["// https://stackoverflow.com/a/46971044/970769\r\n// \"Breaking changes in Typescript 2.1\"\r\n// \"Extending built-ins like Error, Array, and Map may no longer work.\"\r\n// \"As a recommendation, you can manually adjust the prototype immediately after any super(...) calls.\"\r\n// https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work\r\nexport default class ParseError extends Error {\r\n constructor(code) {\r\n super(code)\r\n // Set the prototype explicitly.\r\n // Any subclass of FooError will have to manually set the prototype as well.\r\n Object.setPrototypeOf(this, ParseError.prototype)\r\n this.name = this.constructor.name\r\n }\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,eAAe,MAAMA,UAAU,SAASC,KAAK,CAAC;EAC5CC,WAAWA,CAACC,IAAI,EAAE;IAChB,KAAK,CAACA,IAAI,CAAC;IACX;IACA;IACAC,MAAM,CAACC,cAAc,CAAC,IAAI,EAAEL,UAAU,CAACM,SAAS,CAAC;IACjD,IAAI,CAACC,IAAI,GAAG,IAAI,CAACL,WAAW,CAACK,IAAI;EACnC;AACF","ignoreList":[]}
@@ -0,0 +1,166 @@
1
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
2
+ import Metadata, { validateMetadata } from './metadata.js';
3
+ import isPossibleNumber from './isPossible.js';
4
+ import isValidNumber from './isValid.js';
5
+ import getNumberType from './helpers/getNumberType.js';
6
+ import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js';
7
+ import extractCountryCallingCode from './helpers/extractCountryCallingCode.js';
8
+ import isObject from './helpers/isObject.js';
9
+ import formatNumber from './format.js';
10
+ const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
11
+ export default class PhoneNumber {
12
+ /**
13
+ * @param {string} countryOrCountryCallingCode
14
+ * @param {string} nationalNumber
15
+ * @param {object} metadata — Metadata JSON
16
+ * @return {PhoneNumber}
17
+ */
18
+ constructor(countryOrCountryCallingCode, nationalNumber, metadata) {
19
+ // Validate `countryOrCountryCallingCode` argument.
20
+ if (!countryOrCountryCallingCode) {
21
+ throw new TypeError('First argument is required');
22
+ }
23
+ if (typeof countryOrCountryCallingCode !== 'string') {
24
+ throw new TypeError('First argument must be a string');
25
+ }
26
+
27
+ // In case of public API use: `constructor(number, metadata)`.
28
+ // Transform the arguments from `constructor(number, metadata)` to
29
+ // `constructor(countryOrCountryCallingCode, nationalNumber, metadata)`.
30
+ if (countryOrCountryCallingCode[0] === '+' && !nationalNumber) {
31
+ throw new TypeError('`metadata` argument not passed');
32
+ }
33
+ if (isObject(nationalNumber) && isObject(nationalNumber.countries)) {
34
+ metadata = nationalNumber;
35
+ const e164Number = countryOrCountryCallingCode;
36
+ if (!E164_NUMBER_REGEXP.test(e164Number)) {
37
+ throw new Error('Invalid `number` argument passed: must consist of a "+" followed by digits');
38
+ }
39
+ const {
40
+ countryCallingCode: _countryCallingCode,
41
+ number
42
+ } = extractCountryCallingCode(e164Number, undefined, undefined, undefined, metadata);
43
+ nationalNumber = number;
44
+ countryOrCountryCallingCode = _countryCallingCode;
45
+ if (!nationalNumber) {
46
+ throw new Error('Invalid `number` argument passed: too short');
47
+ }
48
+ }
49
+
50
+ // Validate `nationalNumber` argument.
51
+ if (!nationalNumber) {
52
+ throw new TypeError('`nationalNumber` argument is required');
53
+ }
54
+ if (typeof nationalNumber !== 'string') {
55
+ throw new TypeError('`nationalNumber` argument must be a string');
56
+ }
57
+
58
+ // Validate `metadata` argument.
59
+ validateMetadata(metadata);
60
+
61
+ // Initialize properties.
62
+ const {
63
+ country,
64
+ countryCallingCode
65
+ } = getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadata);
66
+ this.country = country;
67
+ this.countryCallingCode = countryCallingCode;
68
+ this.nationalNumber = nationalNumber;
69
+ this.number = '+' + this.countryCallingCode + this.nationalNumber;
70
+ // Exclude `metadata` property output from `PhoneNumber.toString()`
71
+ // so that it doesn't clutter the console output of Node.js.
72
+ // Previously, when Node.js did `console.log(new PhoneNumber(...))`,
73
+ // it would output the whole internal structure of the `metadata` object.
74
+ this.getMetadata = () => metadata;
75
+ }
76
+ setExt(ext) {
77
+ this.ext = ext;
78
+ }
79
+ getPossibleCountries() {
80
+ if (this.country) {
81
+ return [this.country];
82
+ }
83
+ return getPossibleCountriesForNumber(this.countryCallingCode, this.nationalNumber, this.getMetadata());
84
+ }
85
+ isPossible() {
86
+ return isPossibleNumber(this, {
87
+ v2: true
88
+ }, this.getMetadata());
89
+ }
90
+ isValid() {
91
+ return isValidNumber(this, {
92
+ v2: true
93
+ }, this.getMetadata());
94
+ }
95
+ isNonGeographic() {
96
+ const metadata = new Metadata(this.getMetadata());
97
+ return metadata.isNonGeographicCallingCode(this.countryCallingCode);
98
+ }
99
+ isEqual(phoneNumber) {
100
+ return this.number === phoneNumber.number && this.ext === phoneNumber.ext;
101
+ }
102
+
103
+ // This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,
104
+ // but later it was found out that it doesn't include the possible `TOO_SHORT` result
105
+ // returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,
106
+ // so eventually I simply commented out this method from the `PhoneNumber` class
107
+ // and just left the `validatePhoneNumberLength()` function, even though that one would require
108
+ // and additional step to also validate the actual country / calling code of the phone number.
109
+ // validateLength() {
110
+ // const metadata = new Metadata(this.getMetadata())
111
+ // metadata.selectNumberingPlan(this.countryCallingCode)
112
+ // const result = checkNumberLength(this.nationalNumber, metadata)
113
+ // if (result !== 'IS_POSSIBLE') {
114
+ // return result
115
+ // }
116
+ // }
117
+
118
+ getType() {
119
+ return getNumberType(this, {
120
+ v2: true
121
+ }, this.getMetadata());
122
+ }
123
+ format(format, options) {
124
+ return formatNumber(this, format, options ? _extends({}, options, {
125
+ v2: true
126
+ }) : {
127
+ v2: true
128
+ }, this.getMetadata());
129
+ }
130
+ formatNational(options) {
131
+ return this.format('NATIONAL', options);
132
+ }
133
+ formatInternational(options) {
134
+ return this.format('INTERNATIONAL', options);
135
+ }
136
+ getURI(options) {
137
+ return this.format('RFC3966', options);
138
+ }
139
+ }
140
+ const isCountryCode = value => /^[A-Z]{2}$/.test(value);
141
+ function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
142
+ let country;
143
+ let countryCallingCode;
144
+ const metadata = new Metadata(metadataJson);
145
+ // If country code is passed then derive `countryCallingCode` from it.
146
+ // Also store the country code as `.country`.
147
+ if (isCountryCode(countryOrCountryCallingCode)) {
148
+ country = countryOrCountryCallingCode;
149
+ metadata.selectNumberingPlan(country);
150
+ countryCallingCode = metadata.countryCallingCode();
151
+ } else {
152
+ countryCallingCode = countryOrCountryCallingCode;
153
+ /* istanbul ignore if */
154
+ if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
155
+ if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
156
+ country = '001';
157
+ }
158
+ }
159
+ }
160
+ return {
161
+ country,
162
+ countryCallingCode
163
+ };
164
+ }
165
+ const E164_NUMBER_REGEXP = /^\+\d+$/;
166
+ //# sourceMappingURL=PhoneNumber.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhoneNumber.js","names":["Metadata","validateMetadata","isPossibleNumber","isValidNumber","getNumberType","getPossibleCountriesForNumber","extractCountryCallingCode","isObject","formatNumber","USE_NON_GEOGRAPHIC_COUNTRY_CODE","PhoneNumber","constructor","countryOrCountryCallingCode","nationalNumber","metadata","TypeError","countries","e164Number","E164_NUMBER_REGEXP","test","Error","countryCallingCode","number","undefined","country","getCountryAndCountryCallingCode","getMetadata","setExt","ext","getPossibleCountries","isPossible","v2","isValid","isNonGeographic","isNonGeographicCallingCode","isEqual","phoneNumber","getType","format","options","_extends","formatNational","formatInternational","getURI","isCountryCode","value","metadataJson","selectNumberingPlan"],"sources":["../source/PhoneNumber.js"],"sourcesContent":["import Metadata, { validateMetadata } from './metadata.js'\r\nimport isPossibleNumber from './isPossible.js'\r\nimport isValidNumber from './isValid.js'\r\nimport getNumberType from './helpers/getNumberType.js'\r\nimport getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'\r\nimport extractCountryCallingCode from './helpers/extractCountryCallingCode.js'\r\nimport isObject from './helpers/isObject.js'\r\nimport formatNumber from './format.js'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nexport default class PhoneNumber {\r\n\t/**\r\n\t * @param {string} countryOrCountryCallingCode\r\n\t * @param {string} nationalNumber\r\n\t * @param {object} metadata — Metadata JSON\r\n\t * @return {PhoneNumber}\r\n\t */\r\n\tconstructor(countryOrCountryCallingCode, nationalNumber, metadata) {\r\n\t\t// Validate `countryOrCountryCallingCode` argument.\r\n\t\tif (!countryOrCountryCallingCode) {\r\n\t\t\tthrow new TypeError('First argument is required')\r\n\t\t}\r\n\t\tif (typeof countryOrCountryCallingCode !== 'string') {\r\n\t\t\tthrow new TypeError('First argument must be a string')\r\n\t\t}\r\n\r\n\t\t// In case of public API use: `constructor(number, metadata)`.\r\n\t\t// Transform the arguments from `constructor(number, metadata)` to\r\n\t\t// `constructor(countryOrCountryCallingCode, nationalNumber, metadata)`.\r\n\t\tif (countryOrCountryCallingCode[0] === '+' && !nationalNumber) {\r\n\t\t\tthrow new TypeError('`metadata` argument not passed')\r\n\t\t}\r\n\t\tif (isObject(nationalNumber) && isObject(nationalNumber.countries)) {\r\n\t\t\tmetadata = nationalNumber\r\n\t\t\tconst e164Number = countryOrCountryCallingCode\r\n\t\t\tif (!E164_NUMBER_REGEXP.test(e164Number)) {\r\n\t\t\t\tthrow new Error('Invalid `number` argument passed: must consist of a \"+\" followed by digits')\r\n\t\t\t}\r\n\t\t\tconst { countryCallingCode, number } = extractCountryCallingCode(e164Number, undefined, undefined, undefined, metadata)\r\n\t\t\tnationalNumber = number\r\n\t\t\tcountryOrCountryCallingCode = countryCallingCode\r\n\t\t\tif (!nationalNumber) {\r\n\t\t\t\tthrow new Error('Invalid `number` argument passed: too short')\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t// Validate `nationalNumber` argument.\r\n\t\tif (!nationalNumber) {\r\n\t\t\tthrow new TypeError('`nationalNumber` argument is required')\r\n\t\t}\r\n\t\tif (typeof nationalNumber !== 'string') {\r\n\t\t\tthrow new TypeError('`nationalNumber` argument must be a string')\r\n\t\t}\r\n\r\n\t\t// Validate `metadata` argument.\r\n\t\tvalidateMetadata(metadata)\r\n\r\n\t\t// Initialize properties.\r\n\t\tconst { country, countryCallingCode } = getCountryAndCountryCallingCode(\r\n\t\t\tcountryOrCountryCallingCode,\r\n\t\t\tmetadata\r\n\t\t)\r\n\t\tthis.country = country\r\n\t\tthis.countryCallingCode = countryCallingCode\r\n\t\tthis.nationalNumber = nationalNumber\r\n\t\tthis.number = '+' + this.countryCallingCode + this.nationalNumber\r\n\t\t// Exclude `metadata` property output from `PhoneNumber.toString()`\r\n\t\t// so that it doesn't clutter the console output of Node.js.\r\n\t\t// Previously, when Node.js did `console.log(new PhoneNumber(...))`,\r\n\t\t// it would output the whole internal structure of the `metadata` object.\r\n\t\tthis.getMetadata = () => metadata\r\n\t}\r\n\r\n\tsetExt(ext) {\r\n\t\tthis.ext = ext\r\n\t}\r\n\r\n\tgetPossibleCountries() {\r\n\t\tif (this.country) {\r\n\t\t\treturn [this.country]\r\n\t\t}\r\n\t\treturn getPossibleCountriesForNumber(\r\n\t\t\tthis.countryCallingCode,\r\n\t\t\tthis.nationalNumber,\r\n\t\t\tthis.getMetadata()\r\n\t\t)\r\n\t}\r\n\r\n\tisPossible() {\r\n\t\treturn isPossibleNumber(this, { v2: true }, this.getMetadata())\r\n\t}\r\n\r\n\tisValid() {\r\n\t\treturn isValidNumber(this, { v2: true }, this.getMetadata())\r\n\t}\r\n\r\n\tisNonGeographic() {\r\n\t\tconst metadata = new Metadata(this.getMetadata())\r\n\t\treturn metadata.isNonGeographicCallingCode(this.countryCallingCode)\r\n\t}\r\n\r\n\tisEqual(phoneNumber) {\r\n\t\treturn this.number === phoneNumber.number && this.ext === phoneNumber.ext\r\n\t}\r\n\r\n\t// This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,\r\n\t// but later it was found out that it doesn't include the possible `TOO_SHORT` result\r\n\t// returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,\r\n\t// so eventually I simply commented out this method from the `PhoneNumber` class\r\n\t// and just left the `validatePhoneNumberLength()` function, even though that one would require\r\n\t// and additional step to also validate the actual country / calling code of the phone number.\r\n\t// validateLength() {\r\n\t// \tconst metadata = new Metadata(this.getMetadata())\r\n\t// \tmetadata.selectNumberingPlan(this.countryCallingCode)\r\n\t// \tconst result = checkNumberLength(this.nationalNumber, metadata)\r\n\t// \tif (result !== 'IS_POSSIBLE') {\r\n\t// \t\treturn result\r\n\t// \t}\r\n\t// }\r\n\r\n\tgetType() {\r\n\t\treturn getNumberType(this, { v2: true }, this.getMetadata())\r\n\t}\r\n\r\n\tformat(format, options) {\r\n\t\treturn formatNumber(\r\n\t\t\tthis,\r\n\t\t\tformat,\r\n\t\t\toptions ? { ...options, v2: true } : { v2: true },\r\n\t\t\tthis.getMetadata()\r\n\t\t)\r\n\t}\r\n\r\n\tformatNational(options) {\r\n\t\treturn this.format('NATIONAL', options)\r\n\t}\r\n\r\n\tformatInternational(options) {\r\n\t\treturn this.format('INTERNATIONAL', options)\r\n\t}\r\n\r\n\tgetURI(options) {\r\n\t\treturn this.format('RFC3966', options)\r\n\t}\r\n}\r\n\r\nconst isCountryCode = (value) => /^[A-Z]{2}$/.test(value)\r\n\r\nfunction getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {\r\n\tlet country\r\n\tlet countryCallingCode\r\n\r\n\tconst metadata = new Metadata(metadataJson)\r\n\t// If country code is passed then derive `countryCallingCode` from it.\r\n\t// Also store the country code as `.country`.\r\n\tif (isCountryCode(countryOrCountryCallingCode)) {\r\n\t\tcountry = countryOrCountryCallingCode\r\n\t\tmetadata.selectNumberingPlan(country)\r\n\t\tcountryCallingCode = metadata.countryCallingCode()\r\n\t} else {\r\n\t\tcountryCallingCode = countryOrCountryCallingCode\r\n\t\t/* istanbul ignore if */\r\n\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\tif (metadata.isNonGeographicCallingCode(countryCallingCode)) {\r\n\t\t\t\tcountry = '001'\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn {\r\n\t\tcountry,\r\n\t\tcountryCallingCode\r\n\t}\r\n}\r\n\r\nconst E164_NUMBER_REGEXP = /^\\+\\d+$/"],"mappings":";AAAA,OAAOA,QAAQ,IAAIC,gBAAgB,QAAQ,eAAe;AAC1D,OAAOC,gBAAgB,MAAM,iBAAiB;AAC9C,OAAOC,aAAa,MAAM,cAAc;AACxC,OAAOC,aAAa,MAAM,4BAA4B;AACtD,OAAOC,6BAA6B,MAAM,4CAA4C;AACtF,OAAOC,yBAAyB,MAAM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aAAa,CAAChC,2BAA2B,CAAC,EAAE;IAC/CY,OAAO,GAAGZ,2BAA2B;IACrCE,QAAQ,CAACiC,mBAAmB,CAACvB,OAAO,CAAC;IACrCH,kBAAkB,GAAGP,QAAQ,CAACO,kBAAkB,CAAC,CAAC;EACnD,CAAC,MAAM;IACNA,kBAAkB,GAAGT,2BAA2B;IAChD;IACA,IAAIH,+BAA+B,EAAE;MACpC,IAAIK,QAAQ,CAACoB,0BAA0B,CAACb,kBAAkB,CAAC,EAAE;QAC5DG,OAAO,GAAG,KAAK;MAChB;IACD;EACD;EAEA,OAAO;IACNA,OAAO;IACPH;EACD,CAAC;AACF;AAEA,MAAMH,kBAAkB,GAAG,SAAS","ignoreList":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhoneNumber.test.js","names":["describe","it","expect","metadata","type","PhoneNumber","phoneNumber","setExt","country","to","be","undefined","countryCallingCode","equal","nationalNumber","formatNational","throw","number","ext","format","formatExtension","extension","isEqual","isNonGeographic","getPossibleCountries","deep","indexOf","length","getType"],"sources":["../source/PhoneNumber.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport metadata from '../metadata.min.json' with { type: 'json' }\r\nimport PhoneNumber from './PhoneNumber.js'\r\n\r\ndescribe('PhoneNumber', () => {\r\n\tit('should create a phone number via a public constructor', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('+78005553535', metadata)\r\n\t\tphoneNumber.setExt('1234')\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.countryCallingCode).to.equal('7')\r\n\t\texpect(phoneNumber.nationalNumber).to.equal('8005553535')\r\n\t\texpect(phoneNumber.formatNational()).to.equal('8 (800) 555-35-35 ext. 1234')\r\n\t})\r\n\r\n\tit('should validate constructor arguments (public constructor)', () => {\r\n\t\texpect(() => new PhoneNumber()).to.throw('argument is required')\r\n\t\texpect(() => new PhoneNumber(undefined, metadata)).to.throw('argument is required')\r\n\t\texpect(() => new PhoneNumber('7', metadata)).to.throw('must consist of a \"+\"')\r\n\t\texpect(() => new PhoneNumber('+7', metadata)).to.throw('too short')\r\n\t\texpect(() => new PhoneNumber('+7800')).to.throw('`metadata` argument not passed')\r\n\t\texpect(() => new PhoneNumber(1234567890)).to.throw('must be a string')\r\n\t\texpect(() => new PhoneNumber('+1', 1234567890)).to.throw('must be a string')\r\n\t})\r\n\r\n\tit('should validate constructor arguments (private constructor)', () => {\r\n\t\texpect(() => new PhoneNumber(undefined, '800', metadata)).to.throw('First argument is required')\r\n\t\texpect(() => new PhoneNumber('7', undefined, metadata)).to.throw('`nationalNumber` argument is required')\r\n\t\texpect(() => new PhoneNumber('7', '8005553535')).to.throw('`metadata` argument not passed')\r\n\t})\r\n\r\n\tit('should accept country code argument', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('RU', '8005553535', metadata)\r\n\t\texpect(phoneNumber.countryCallingCode).to.equal('7')\r\n\t\texpect(phoneNumber.country).to.equal('RU')\r\n\t\texpect(phoneNumber.number).to.equal('+78005553535')\r\n\t})\r\n\r\n\tit('should format number with options', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('7', '8005553535', metadata)\r\n\t\tphoneNumber.ext = '123'\r\n\t\texpect(phoneNumber.format('NATIONAL', {\r\n\t\t\tformatExtension: (number, extension) => `${number} доб. ${extension}`\r\n\t\t})).to.equal('8 (800) 555-35-35 доб. 123')\r\n\t})\r\n\r\n\tit('should compare phone numbers', () => {\r\n\t\texpect(\r\n new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553535', metadata))\r\n ).to.equal(true)\r\n\t\texpect(\r\n new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('7', '8005553535', metadata))\r\n ).to.equal(true)\r\n\t\texpect(\r\n new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553536', metadata))\r\n ).to.equal(false)\r\n\t})\r\n\r\n\tit('should tell if a number is non-geographic', () => {\r\n\t\texpect(new PhoneNumber('7', '8005553535', metadata).isNonGeographic()).to.equal(false)\r\n\t\texpect(new PhoneNumber('870', '773111632', metadata).isNonGeographic()).to.equal(true)\r\n\t})\r\n\r\n\tit('should allow setting extension', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('1', '2133734253', metadata)\r\n\t\tphoneNumber.setExt('1234')\r\n\t\texpect(phoneNumber.ext).to.equal('1234')\r\n\t\texpect(phoneNumber.formatNational()).to.equal('(213) 373-4253 ext. 1234')\r\n\t})\r\n\r\n\tit('should return possible countries', () => {\r\n // \"599\": [\r\n // \"CW\", // \"possible_lengths\": [7, 8]\r\n // \"BQ\" // \"possible_lengths\": [7]\r\n // ]\r\n\r\n\t\tlet phoneNumber = new PhoneNumber('599', '123456', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal([])\r\n\r\n\t\tphoneNumber = new PhoneNumber('599', '1234567', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal(['CW', 'BQ'])\r\n\r\n\t\tphoneNumber = new PhoneNumber('599', '12345678', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal(['CW'])\r\n\r\n\t\tphoneNumber = new PhoneNumber('599', '123456789', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal([])\r\n\t})\r\n\r\n\tit('should return possible countries in case of ambiguity', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('1', '2223334444', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries().indexOf('US')).to.equal(0)\r\n\t\texpect(phoneNumber.getPossibleCountries().length).to.equal(25)\r\n\t})\r\n\r\n\t// it('should return empty possible countries when no national number has been input', () => {\r\n\t// \tconst phoneNumber = new PhoneNumber('1', '', metadata)\r\n\t// \texpect(phoneNumber.country).toBe.undefined\r\n\t// \tphoneNumber.getPossibleCountries().should.deep.equal([])\r\n\t// })\r\n\r\n\tit('should return empty possible countries when not enough national number digits have been input', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('1', '222', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal([])\r\n\t})\r\n\r\n\tit('should return possible countries in case of no ambiguity', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('US', '2133734253', metadata)\r\n\t\texpect(phoneNumber.country).to.equal('US')\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal(['US'])\r\n\t})\r\n\r\n\tit('should return empty possible countries in case of an unknown calling code', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('777', '123', metadata)\r\n\t\texpect(phoneNumber.country).to.be.undefined\r\n\t\texpect(phoneNumber.getPossibleCountries()).to.deep.equal([])\r\n\t})\r\n\r\n\tit('should determine phone number type', () => {\r\n\t\tconst phoneNumber = new PhoneNumber('US', '2015550000', metadata)\r\n\t\texpect(phoneNumber.getType()).to.equal('FIXED_LINE_OR_MOBILE')\r\n\t})\r\n\r\n\t// it('should validate phone number length', () => {\r\n\t// \tconst phoneNumber = new PhoneNumber('RU', '800', metadata)\r\n\t// \texpect(phoneNumber.validateLength()).to.equal('TOO_SHORT')\r\n\t//\r\n\t// \tconst phoneNumberValid = new PhoneNumber('RU', '8005553535', metadata)\r\n\t// \texpect(phoneNumberValid.validateLength()).toBe.undefined\r\n\t// })\r\n})"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,QAAQ,MAAM,sBAAsB,QAAQC,IAAI,EAAE,MAAM;AAC/D,OAAOC,WAAW,MAAM,kBAAkB;AAE1CL,QAAQ,CAAC,aAAa,EAAE,MAAM;EAC7BC,EAAE,CAAC,uDAAuD,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,EAAEF,QAAQ,CAAC;IACjED,MAAM,CAACI,WAAW,CAACE,OAAO,CAAC,CAACC,EAAE,CAACI,KAAK,CAAC,IAAI,CAAC;IAC1CX,MAAM,CAACI,WAAW,CAACkB,oBAAoB,CAAC,CAAC,CAAC,CAACf,EAAE,CAACgB,IAAI,CAACZ,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;EACjE,CAAC,CAAC;EAEFZ,EAAE,CAAC,2EAA2E,EAAE,MAAM;IACrF,MAAMK,WAAW,GAAG,IAAID,WAAW,CAAC,KAAK,EAAE,KAAK,EAAEF,QAAQ,CAAC;IAC3DD,MAAM,CAACI,WAAW,CAACE,OAAO,CAAC,CAACC,EAAE,CAACC,EAAE,CAACC,SAAS;IAC3CT,MAAM,CAACI,WAAW,CAACkB,oBAAoB,CAAC,CAAC,CAAC,CAACf,EAAE,CAACgB,IAAI,CAACZ,KAAK,CAAC,EAAE,CAAC;EAC7D,CAAC,CAAC;EAEFZ,EAAE,CAAC,oCAAoC,EAAE,MAAM;IAC9C,MAAMK,WAAW,GAAG,IAAID,WAAW,CAAC,IAAI,EAAE,YAAY,EAAEF,QAAQ,CAAC;IACjED,MAAM,CAACI,WAAW,CAACsB,OAAO,CAAC,CAAC,CAAC,CAACnB,EAAE,CAACI,KAAK,CAAC,sBAAsB,CAAC;EAC/D,CAAC,CAAC;;EAEF;EACA;EACA;EACA;EACA;EACA;EACA;AACD,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,330 @@
1
+ /**
2
+ * A port of Google's `PhoneNumberMatcher.java`.
3
+ * https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
4
+ * Date: 08.03.2018.
5
+ */
6
+
7
+ import { MAX_LENGTH_FOR_NSN, MAX_LENGTH_COUNTRY_CODE, VALID_PUNCTUATION } from './constants.js';
8
+ import createExtensionPattern from './helpers/extension/createExtensionPattern.js';
9
+ import RegExpCache from './findNumbers/RegExpCache.js';
10
+ import { limit, trimAfterFirstMatch } from './findNumbers/util.js';
11
+ import { _pL, _pN, pZ, PZ, pNd } from './findNumbers/utf-8.js';
12
+ import Leniency from './findNumbers/Leniency.js';
13
+ import parsePreCandidate from './findNumbers/parsePreCandidate.js';
14
+ import isValidPreCandidate from './findNumbers/isValidPreCandidate.js';
15
+ import isValidCandidate, { LEAD_CLASS } from './findNumbers/isValidCandidate.js';
16
+ import { isSupportedCountry } from './metadata.js';
17
+ import parsePhoneNumber from './parsePhoneNumber.js';
18
+ const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
19
+ const EXTN_PATTERNS_FOR_MATCHING = createExtensionPattern('matching');
20
+
21
+ /**
22
+ * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
23
+ * ordered according to specificity. For example, white-space is last since that is frequently
24
+ * used in numbers, not just to separate two numbers. We have separate patterns since we don't
25
+ * want to break up the phone-number-like text on more than one different kind of symbol at one
26
+ * time, although symbols of the same type (e.g. space) can be safely grouped together.
27
+ *
28
+ * Note that if there is a match, we will always check any text found up to the first match as
29
+ * well.
30
+ */
31
+ const INNER_MATCHES = [
32
+ // Breaks on the slash - e.g. "651-234-2345/332-445-1234"
33
+ '/+(.*)',
34
+ // Note that the bracket here is inside the capturing group, since we consider it part of the
35
+ // phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321".
36
+ '(\\([^(]*)',
37
+ // Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number."
38
+ // We require a space on either side of the hyphen for it to be considered a separator.
39
+ `(?:${pZ}-|-${pZ})${pZ}*(.+)`,
40
+ // Various types of wide hyphens. Note we have decided not to enforce a space here, since it's
41
+ // possible that it's supposed to be used to break two numbers without spaces, and we haven't
42
+ // seen many instances of it used within a number.
43
+ `[\u2012-\u2015\uFF0D]${pZ}*(.+)`,
44
+ // Breaks on a full stop - e.g. "12345. 332-445-1234 is my number."
45
+ `\\.+${pZ}*([^.]+)`,
46
+ // Breaks on space - e.g. "3324451234 8002341234"
47
+ `${pZ}+(${PZ}+)`];
48
+
49
+ // Limit on the number of leading (plus) characters.
50
+ const leadLimit = limit(0, 2);
51
+
52
+ // Limit on the number of consecutive punctuation characters.
53
+ const punctuationLimit = limit(0, 4);
54
+
55
+ /* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
56
+ * single block, set high enough to accommodate the entire national number and the international
57
+ * country code. */
58
+ const digitBlockLimit = MAX_LENGTH_FOR_NSN + MAX_LENGTH_COUNTRY_CODE;
59
+
60
+ // Limit on the number of blocks separated by punctuation.
61
+ // Uses digitBlockLimit since some formats use spaces to separate each digit.
62
+ const blockLimit = limit(0, digitBlockLimit);
63
+
64
+ /* A punctuation sequence allowing white space. */
65
+ const punctuation = `[${VALID_PUNCTUATION}]` + punctuationLimit;
66
+
67
+ // A digits block without punctuation.
68
+ const digitSequence = pNd + limit(1, digitBlockLimit);
69
+
70
+ /**
71
+ * Phone number pattern allowing optional punctuation.
72
+ * The phone number pattern used by `find()`, similar to
73
+ * VALID_PHONE_NUMBER, but with the following differences:
74
+ * <ul>
75
+ * <li>All captures are limited in order to place an upper bound to the text matched by the
76
+ * pattern.
77
+ * <ul>
78
+ * <li>Leading punctuation / plus signs are limited.
79
+ * <li>Consecutive occurrences of punctuation are limited.
80
+ * <li>Number of digits is limited.
81
+ * </ul>
82
+ * <li>No whitespace is allowed at the start or end.
83
+ * <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.
84
+ * </ul>
85
+ */
86
+ const PATTERN = '(?:' + LEAD_CLASS + punctuation + ')' + leadLimit + digitSequence + '(?:' + punctuation + digitSequence + ')' + blockLimit + '(?:' + EXTN_PATTERNS_FOR_MATCHING + ')?';
87
+
88
+ // Regular expression of trailing characters that we want to remove.
89
+ // We remove all characters that are not alpha or numerical characters.
90
+ // The hash character is retained here, as it may signify
91
+ // the previous block was an extension.
92
+ //
93
+ // // Don't know what does '&&' mean here.
94
+ // const UNWANTED_END_CHAR_PATTERN = new RegExp(`[[\\P{N}&&\\P{L}]&&[^#]]+$`)
95
+ //
96
+ const UNWANTED_END_CHAR_PATTERN = new RegExp(`[^${_pN}${_pL}#]+$`);
97
+
98
+ // const NON_DIGITS_PATTERN = /(\D+)/
99
+
100
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
101
+
102
+ /**
103
+ * A stateful class that finds and extracts telephone numbers from {@linkplain CharSequence text}.
104
+ * Instances can be created using the {@linkplain PhoneNumberUtil#findNumbers factory methods} in
105
+ * {@link PhoneNumberUtil}.
106
+ *
107
+ * <p>Vanity numbers (phone numbers using alphabetic digits such as <tt>1-800-SIX-FLAGS</tt> are
108
+ * not found.
109
+ *
110
+ * <p>This class is not thread-safe.
111
+ */
112
+ export default class PhoneNumberMatcher {
113
+ /**
114
+ * @param {string} text — the character sequence that we will search, null for no text.
115
+ * @param {'POSSIBLE'|'VALID'|'STRICT_GROUPING'|'EXACT_GROUPING'} [options.leniency] — The leniency to use when evaluating candidate phone numbers. See `source/findNumbers/Leniency.js` for more details.
116
+ * @param {number} [options.maxTries] — The maximum number of invalid numbers to try before giving up on the text. This is to cover degenerate cases where the text has a lot of false positives in it. Must be >= 0.
117
+ */
118
+ constructor(text = '', options = {}, metadata) {
119
+ options = {
120
+ v2: options.v2,
121
+ defaultCallingCode: options.defaultCallingCode,
122
+ defaultCountry: options.defaultCountry && isSupportedCountry(options.defaultCountry, metadata) ? options.defaultCountry : undefined,
123
+ // Here it should've assigned a default value only if `options.leniency === undefined`.
124
+ leniency: options.leniency || (options.extended ? 'POSSIBLE' : 'VALID'),
125
+ // Here it should've assigned a default value only if `options.maxTries === undefined`.
126
+ maxTries: options.maxTries || MAX_SAFE_INTEGER
127
+ };
128
+
129
+ // Validate `leniency`.
130
+ // if (!options.leniency) {
131
+ // throw new TypeError('`leniency` is required')
132
+ // }
133
+ if (!Leniency[options.leniency]) {
134
+ throw new TypeError(`Unknown leniency: "${options.leniency}"`);
135
+ }
136
+ if (options.leniency !== 'POSSIBLE' && options.leniency !== 'VALID') {
137
+ throw new TypeError(`Invalid \`leniency\`: "${options.leniency}". Supported values: "POSSIBLE", "VALID".`);
138
+ }
139
+
140
+ // Validate `maxTries`.
141
+ if (options.maxTries < 0) {
142
+ throw new TypeError('`maxTries` must be `>= 0`');
143
+ }
144
+ this.text = text;
145
+ this.options = options;
146
+ this.metadata = metadata;
147
+
148
+ // The degree of phone number validation.
149
+ this.leniency = Leniency[options.leniency];
150
+
151
+ /** The maximum number of retries after matching an invalid number. */
152
+ this.maxTries = options.maxTries;
153
+
154
+ // Compile regular expressions.
155
+ this.PATTERN = new RegExp(PATTERN, 'ig');
156
+ this.INNER_MATCHES = INNER_MATCHES.map(pattern => new RegExp(pattern, 'g'));
157
+
158
+ /** The iteration tristate. */
159
+ this.state = 'NOT_READY';
160
+
161
+ /** The next index to start searching at. Undefined in {@link State#DONE}. */
162
+ this.searchIndex = 0;
163
+
164
+ // A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3
165
+ // countries being used for the same doc with ~10 patterns for each country. Some pages will have
166
+ // a lot more countries in use, but typically fewer numbers for each so expanding the cache for
167
+ // that use-case won't have a lot of benefit.
168
+ this.regExpCache = new RegExpCache(32);
169
+ }
170
+
171
+ /**
172
+ * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}
173
+ * that represents a phone number. Returns the next match, null if none was found.
174
+ *
175
+ * @param index the search index to start searching at
176
+ * @return the phone number match found, null if none can be found
177
+ */
178
+ find(index) {
179
+ // Reset the regular expression.
180
+ this.PATTERN.lastIndex = index;
181
+ let matches;
182
+ while (this.maxTries > 0 && (matches = this.PATTERN.exec(this.text)) !== null) {
183
+ let candidate = matches[0];
184
+ const offset = matches.index;
185
+ candidate = parsePreCandidate(candidate);
186
+ if (isValidPreCandidate(candidate, offset, this.text)) {
187
+ const match =
188
+ // Try to come up with a valid match given the entire candidate.
189
+ this.parseAndVerify(candidate, offset, this.text)
190
+ // If that failed, try to find an "inner match" -
191
+ // there might be a phone number within this candidate.
192
+ || this.extractInnerMatch(candidate, offset, this.text);
193
+ if (match) {
194
+ if (this.options.v2) {
195
+ return {
196
+ startsAt: match.startsAt,
197
+ endsAt: match.endsAt,
198
+ number: match.phoneNumber
199
+ };
200
+ } else {
201
+ const {
202
+ phoneNumber
203
+ } = match;
204
+ const result = {
205
+ startsAt: match.startsAt,
206
+ endsAt: match.endsAt,
207
+ phone: phoneNumber.nationalNumber
208
+ };
209
+ if (phoneNumber.country) {
210
+ /* istanbul ignore if */
211
+ if (USE_NON_GEOGRAPHIC_COUNTRY_CODE && country === '001') {
212
+ result.countryCallingCode = phoneNumber.countryCallingCode;
213
+ } else {
214
+ result.country = phoneNumber.country;
215
+ }
216
+ } else {
217
+ result.countryCallingCode = phoneNumber.countryCallingCode;
218
+ }
219
+ if (phoneNumber.ext) {
220
+ result.ext = phoneNumber.ext;
221
+ }
222
+ return result;
223
+ }
224
+ }
225
+ }
226
+ this.maxTries--;
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Attempts to extract a match from `substring`
232
+ * if the substring itself does not qualify as a match.
233
+ */
234
+ extractInnerMatch(substring, offset, text) {
235
+ for (const innerMatchRegExp of this.INNER_MATCHES) {
236
+ // Reset regular expression.
237
+ innerMatchRegExp.lastIndex = 0;
238
+ let isFirstMatch = true;
239
+ let candidateMatch;
240
+ while (this.maxTries > 0 && (candidateMatch = innerMatchRegExp.exec(substring)) !== null) {
241
+ if (isFirstMatch) {
242
+ // We should handle any group before this one too.
243
+ const _candidate = trimAfterFirstMatch(UNWANTED_END_CHAR_PATTERN, substring.slice(0, candidateMatch.index));
244
+ const _match = this.parseAndVerify(_candidate, offset, text);
245
+ if (_match) {
246
+ return _match;
247
+ }
248
+ this.maxTries--;
249
+ isFirstMatch = false;
250
+ }
251
+ const candidate = trimAfterFirstMatch(UNWANTED_END_CHAR_PATTERN, candidateMatch[1]);
252
+
253
+ // Java code does `groupMatcher.start(1)` here,
254
+ // but there's no way in javascript to get a `candidate` start index,
255
+ // therefore resort to using this kind of an approximation.
256
+ // (`groupMatcher` is called `candidateInSubstringMatch` in this javascript port)
257
+ // https://stackoverflow.com/questions/15934353/get-index-of-each-capture-in-a-javascript-regex
258
+ const candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index);
259
+ const match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text);
260
+ if (match) {
261
+ return match;
262
+ }
263
+ this.maxTries--;
264
+ }
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Parses a phone number from the `candidate` using `parse` and
270
+ * verifies it matches the requested `leniency`. If parsing and verification succeed,
271
+ * a corresponding `PhoneNumberMatch` is returned, otherwise this method returns `null`.
272
+ *
273
+ * @param candidate the candidate match
274
+ * @param offset the offset of {@code candidate} within {@link #text}
275
+ * @return the parsed and validated phone number match, or null
276
+ */
277
+ parseAndVerify(candidate, offset, text) {
278
+ if (!isValidCandidate(candidate, offset, text, this.options.leniency)) {
279
+ return;
280
+ }
281
+ const phoneNumber = parsePhoneNumber(candidate, {
282
+ extended: true,
283
+ defaultCountry: this.options.defaultCountry,
284
+ defaultCallingCode: this.options.defaultCallingCode
285
+ }, this.metadata);
286
+ if (!phoneNumber) {
287
+ return;
288
+ }
289
+ if (!phoneNumber.isPossible()) {
290
+ return;
291
+ }
292
+ if (this.leniency(phoneNumber, {
293
+ candidate,
294
+ defaultCountry: this.options.defaultCountry,
295
+ metadata: this.metadata,
296
+ regExpCache: this.regExpCache
297
+ })) {
298
+ return {
299
+ startsAt: offset,
300
+ endsAt: offset + candidate.length,
301
+ phoneNumber
302
+ };
303
+ }
304
+ }
305
+ hasNext() {
306
+ if (this.state === 'NOT_READY') {
307
+ this.lastMatch = this.find(this.searchIndex);
308
+ if (this.lastMatch) {
309
+ this.searchIndex = this.lastMatch.endsAt;
310
+ this.state = 'READY';
311
+ } else {
312
+ this.state = 'DONE';
313
+ }
314
+ }
315
+ return this.state === 'READY';
316
+ }
317
+ next() {
318
+ // Check the state and find the next match as a side-effect if necessary.
319
+ if (!this.hasNext()) {
320
+ throw new Error('No next element');
321
+ }
322
+
323
+ // Don't retain that memory any longer than necessary.
324
+ const result = this.lastMatch;
325
+ this.lastMatch = null;
326
+ this.state = 'NOT_READY';
327
+ return result;
328
+ }
329
+ }
330
+ //# sourceMappingURL=PhoneNumberMatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhoneNumberMatcher.js","names":["MAX_LENGTH_FOR_NSN","MAX_LENGTH_COUNTRY_CODE","VALID_PUNCTUATION","createExtensionPattern","RegExpCache","limit","trimAfterFirstMatch","_pL","_pN","pZ","PZ","pNd","Leniency","parsePreCandidate","isValidPreCandidate","isValidCandidate","LEAD_CLASS","isSupportedCountry","parsePhoneNumber","USE_NON_GEOGRAPHIC_COUNTRY_CODE","EXTN_PATTERNS_FOR_MATCHING","INNER_MATCHES","leadLimit","punctuationLimit","digitBlockLimit","blockLimit","punctuation","digitSequence","PATTERN","UNWANTED_END_CHAR_PATTERN","RegExp","MAX_SAFE_INTEGER","Number","Math","pow","PhoneNumberMatcher","constructor","text","options","metadata","v2","defaultCallingCode","defaultCountry","undefined","leniency","extended","maxTries","TypeError","map","pattern","state","searchIndex","regExpCache","find","index","lastIndex","matches","exec","candidate","offset","match","parseAndVerify","extractInnerMatch","startsAt","endsAt","number","phoneNumber","result","phone","nationalNumber","country","countryCallingCode","ext","substring","innerMatchRegExp","isFirstMatch","candidateMatch","slice","candidateIndexGuess","indexOf","isPossible","length","hasNext","lastMatch","next","Error"],"sources":["../source/PhoneNumberMatcher.js"],"sourcesContent":["/**\r\n * A port of Google's `PhoneNumberMatcher.java`.\r\n * https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java\r\n * Date: 08.03.2018.\r\n */\r\n\r\nimport {\r\n MAX_LENGTH_FOR_NSN,\r\n MAX_LENGTH_COUNTRY_CODE,\r\n VALID_PUNCTUATION\r\n} from './constants.js'\r\n\r\nimport createExtensionPattern from './helpers/extension/createExtensionPattern.js'\r\n\r\nimport RegExpCache from './findNumbers/RegExpCache.js'\r\n\r\nimport {\r\n\tlimit,\r\n\ttrimAfterFirstMatch\r\n} from './findNumbers/util.js'\r\n\r\nimport {\r\n\t_pL,\r\n\t_pN,\r\n\tpZ,\r\n\tPZ,\r\n\tpNd\r\n} from './findNumbers/utf-8.js'\r\n\r\nimport Leniency from './findNumbers/Leniency.js'\r\nimport parsePreCandidate from './findNumbers/parsePreCandidate.js'\r\nimport isValidPreCandidate from './findNumbers/isValidPreCandidate.js'\r\nimport isValidCandidate, { LEAD_CLASS } from './findNumbers/isValidCandidate.js'\r\n\r\nimport { isSupportedCountry } from './metadata.js'\r\n\r\nimport parsePhoneNumber from './parsePhoneNumber.js'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nconst EXTN_PATTERNS_FOR_MATCHING = createExtensionPattern('matching')\r\n\r\n/**\r\n * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are\r\n * ordered according to specificity. For example, white-space is last since that is frequently\r\n * used in numbers, not just to separate two numbers. We have separate patterns since we don't\r\n * want to break up the phone-number-like text on more than one different kind of symbol at one\r\n * time, although symbols of the same type (e.g. space) can be safely grouped together.\r\n *\r\n * Note that if there is a match, we will always check any text found up to the first match as\r\n * well.\r\n */\r\nconst INNER_MATCHES =\r\n[\r\n\t// Breaks on the slash - e.g. \"651-234-2345/332-445-1234\"\r\n\t'/+(.*)',\r\n\r\n\t// Note that the bracket here is inside the capturing group, since we consider it part of the\r\n\t// phone number. Will match a pattern like \"(650) 223 3345 (754) 223 3321\".\r\n\t'(\\\\([^(]*)',\r\n\r\n\t// Breaks on a hyphen - e.g. \"12345 - 332-445-1234 is my number.\"\r\n\t// We require a space on either side of the hyphen for it to be considered a separator.\r\n\t`(?:${pZ}-|-${pZ})${pZ}*(.+)`,\r\n\r\n\t// Various types of wide hyphens. Note we have decided not to enforce a space here, since it's\r\n\t// possible that it's supposed to be used to break two numbers without spaces, and we haven't\r\n\t// seen many instances of it used within a number.\r\n\t`[\\u2012-\\u2015\\uFF0D]${pZ}*(.+)`,\r\n\r\n\t// Breaks on a full stop - e.g. \"12345. 332-445-1234 is my number.\"\r\n\t`\\\\.+${pZ}*([^.]+)`,\r\n\r\n\t// Breaks on space - e.g. \"3324451234 8002341234\"\r\n\t`${pZ}+(${PZ}+)`\r\n]\r\n\r\n// Limit on the number of leading (plus) characters.\r\nconst leadLimit = limit(0, 2)\r\n\r\n// Limit on the number of consecutive punctuation characters.\r\nconst punctuationLimit = limit(0, 4)\r\n\r\n/* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a\r\n * single block, set high enough to accommodate the entire national number and the international\r\n * country code. */\r\nconst digitBlockLimit = MAX_LENGTH_FOR_NSN + MAX_LENGTH_COUNTRY_CODE\r\n\r\n// Limit on the number of blocks separated by punctuation.\r\n// Uses digitBlockLimit since some formats use spaces to separate each digit.\r\nconst blockLimit = limit(0, digitBlockLimit)\r\n\r\n/* A punctuation sequence allowing white space. */\r\nconst punctuation = `[${VALID_PUNCTUATION}]` + punctuationLimit\r\n\r\n// A digits block without punctuation.\r\nconst digitSequence = pNd + limit(1, digitBlockLimit)\r\n\r\n/**\r\n * Phone number pattern allowing optional punctuation.\r\n * The phone number pattern used by `find()`, similar to\r\n * VALID_PHONE_NUMBER, but with the following differences:\r\n * <ul>\r\n * <li>All captures are limited in order to place an upper bound to the text matched by the\r\n * pattern.\r\n * <ul>\r\n * <li>Leading punctuation / plus signs are limited.\r\n * <li>Consecutive occurrences of punctuation are limited.\r\n * <li>Number of digits is limited.\r\n * </ul>\r\n * <li>No whitespace is allowed at the start or end.\r\n * <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.\r\n * </ul>\r\n */\r\nconst PATTERN = '(?:' + LEAD_CLASS + punctuation + ')' + leadLimit\r\n\t+ digitSequence + '(?:' + punctuation + digitSequence + ')' + blockLimit\r\n\t+ '(?:' + EXTN_PATTERNS_FOR_MATCHING + ')?'\r\n\r\n// Regular expression of trailing characters that we want to remove.\r\n// We remove all characters that are not alpha or numerical characters.\r\n// The hash character is retained here, as it may signify\r\n// the previous block was an extension.\r\n//\r\n// // Don't know what does '&&' mean here.\r\n// const UNWANTED_END_CHAR_PATTERN = new RegExp(`[[\\\\P{N}&&\\\\P{L}]&&[^#]]+$`)\r\n//\r\nconst UNWANTED_END_CHAR_PATTERN = new RegExp(`[^${_pN}${_pL}#]+$`)\r\n\r\n// const NON_DIGITS_PATTERN = /(\\D+)/\r\n\r\nconst MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1\r\n\r\n/**\r\n * A stateful class that finds and extracts telephone numbers from {@linkplain CharSequence text}.\r\n * Instances can be created using the {@linkplain PhoneNumberUtil#findNumbers factory methods} in\r\n * {@link PhoneNumberUtil}.\r\n *\r\n * <p>Vanity numbers (phone numbers using alphabetic digits such as <tt>1-800-SIX-FLAGS</tt> are\r\n * not found.\r\n *\r\n * <p>This class is not thread-safe.\r\n */\r\nexport default class PhoneNumberMatcher\r\n{\r\n /**\r\n * @param {string} text — the character sequence that we will search, null for no text.\r\n * @param {'POSSIBLE'|'VALID'|'STRICT_GROUPING'|'EXACT_GROUPING'} [options.leniency] — The leniency to use when evaluating candidate phone numbers. See `source/findNumbers/Leniency.js` for more details.\r\n * @param {number} [options.maxTries] — The maximum number of invalid numbers to try before giving up on the text. This is to cover degenerate cases where the text has a lot of false positives in it. Must be >= 0.\r\n */\r\n constructor(text = '', options = {}, metadata)\r\n {\r\n options = {\r\n v2: options.v2,\r\n defaultCallingCode: options.defaultCallingCode,\r\n defaultCountry: options.defaultCountry && isSupportedCountry(options.defaultCountry, metadata) ? options.defaultCountry : undefined,\r\n // Here it should've assigned a default value only if `options.leniency === undefined`.\r\n leniency: options.leniency || (options.extended ? 'POSSIBLE' : 'VALID'),\r\n // Here it should've assigned a default value only if `options.maxTries === undefined`.\r\n maxTries: options.maxTries || MAX_SAFE_INTEGER\r\n }\r\n\r\n // Validate `leniency`.\r\n\t\t// if (!options.leniency) {\r\n\t\t// \tthrow new TypeError('`leniency` is required')\r\n\t\t// }\r\n\t\tif (!Leniency[options.leniency]) {\r\n\t\t\tthrow new TypeError(`Unknown leniency: \"${options.leniency}\"`)\r\n\t\t}\r\n if (options.leniency !== 'POSSIBLE' && options.leniency !== 'VALID') {\r\n throw new TypeError(`Invalid \\`leniency\\`: \"${options.leniency}\". Supported values: \"POSSIBLE\", \"VALID\".`)\r\n }\r\n\r\n // Validate `maxTries`.\r\n\t\tif (options.maxTries < 0) {\r\n\t\t\tthrow new TypeError('`maxTries` must be `>= 0`')\r\n\t\t}\r\n\r\n\t\tthis.text = text\r\n\t\tthis.options = options\r\n this.metadata = metadata\r\n\r\n\t\t// The degree of phone number validation.\r\n\t\tthis.leniency = Leniency[options.leniency]\r\n\r\n\t\t/** The maximum number of retries after matching an invalid number. */\r\n\t\tthis.maxTries = options.maxTries\r\n\r\n // Compile regular expressions.\r\n\t\tthis.PATTERN = new RegExp(PATTERN, 'ig')\r\n this.INNER_MATCHES = INNER_MATCHES.map(pattern => new RegExp(pattern, 'g'))\r\n\r\n /** The iteration tristate. */\r\n this.state = 'NOT_READY'\r\n\r\n /** The next index to start searching at. Undefined in {@link State#DONE}. */\r\n this.searchIndex = 0\r\n\r\n // A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3\r\n // countries being used for the same doc with ~10 patterns for each country. Some pages will have\r\n // a lot more countries in use, but typically fewer numbers for each so expanding the cache for\r\n // that use-case won't have a lot of benefit.\r\n this.regExpCache = new RegExpCache(32)\r\n }\r\n\r\n /**\r\n * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}\r\n * that represents a phone number. Returns the next match, null if none was found.\r\n *\r\n * @param index the search index to start searching at\r\n * @return the phone number match found, null if none can be found\r\n */\r\n\tfind(index) {\r\n\t\t// Reset the regular expression.\r\n\t\tthis.PATTERN.lastIndex = index\r\n\r\n\t\tlet matches\r\n\t\twhile ((this.maxTries > 0) && (matches = this.PATTERN.exec(this.text)) !== null) {\r\n\t\t\tlet candidate = matches[0]\r\n\t\t\tconst offset = matches.index\r\n\r\n\t\t\tcandidate = parsePreCandidate(candidate)\r\n\r\n\t\t\tif (isValidPreCandidate(candidate, offset, this.text)) {\r\n\t\t\t\tconst match =\r\n\t\t\t\t\t// Try to come up with a valid match given the entire candidate.\r\n\t\t\t\t\tthis.parseAndVerify(candidate, offset, this.text)\r\n\t\t\t\t\t// If that failed, try to find an \"inner match\" -\r\n\t\t\t\t\t// there might be a phone number within this candidate.\r\n\t\t\t\t\t|| this.extractInnerMatch(candidate, offset, this.text)\r\n\r\n\t\t\t\tif (match) {\r\n\t\t\t\t\tif (this.options.v2) {\r\n\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\tstartsAt: match.startsAt,\r\n\t\t\t\t\t\t\tendsAt: match.endsAt,\r\n\t\t\t\t\t\t\tnumber: match.phoneNumber\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else {\r\n const { phoneNumber } = match\r\n\r\n const result = {\r\n startsAt: match.startsAt,\r\n endsAt: match.endsAt,\r\n phone: phoneNumber.nationalNumber\r\n }\r\n\r\n if (phoneNumber.country) {\r\n /* istanbul ignore if */\r\n if (USE_NON_GEOGRAPHIC_COUNTRY_CODE && country === '001') {\r\n result.countryCallingCode = phoneNumber.countryCallingCode\r\n } else {\r\n result.country = phoneNumber.country\r\n }\r\n } else {\r\n result.countryCallingCode = phoneNumber.countryCallingCode\r\n }\r\n\r\n if (phoneNumber.ext) {\r\n result.ext = phoneNumber.ext\r\n }\r\n\r\n return result\r\n }\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tthis.maxTries--\r\n\t\t}\r\n\t}\r\n\r\n /**\r\n * Attempts to extract a match from `substring`\r\n * if the substring itself does not qualify as a match.\r\n */\r\n extractInnerMatch(substring, offset, text) {\r\n for (const innerMatchRegExp of this.INNER_MATCHES) {\r\n // Reset regular expression.\r\n innerMatchRegExp.lastIndex = 0\r\n\r\n let isFirstMatch = true\r\n let candidateMatch\r\n while (this.maxTries > 0 && (candidateMatch = innerMatchRegExp.exec(substring)) !== null) {\r\n if (isFirstMatch) {\r\n // We should handle any group before this one too.\r\n const candidate = trimAfterFirstMatch(\r\n UNWANTED_END_CHAR_PATTERN,\r\n substring.slice(0, candidateMatch.index)\r\n )\r\n\r\n const match = this.parseAndVerify(candidate, offset, text)\r\n\r\n if (match) {\r\n return match\r\n }\r\n\r\n this.maxTries--\r\n isFirstMatch = false\r\n }\r\n\r\n const candidate = trimAfterFirstMatch(UNWANTED_END_CHAR_PATTERN, candidateMatch[1])\r\n\r\n // Java code does `groupMatcher.start(1)` here,\r\n // but there's no way in javascript to get a `candidate` start index,\r\n // therefore resort to using this kind of an approximation.\r\n // (`groupMatcher` is called `candidateInSubstringMatch` in this javascript port)\r\n // https://stackoverflow.com/questions/15934353/get-index-of-each-capture-in-a-javascript-regex\r\n const candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index)\r\n\r\n const match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text)\r\n if (match) {\r\n return match\r\n }\r\n\r\n this.maxTries--\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Parses a phone number from the `candidate` using `parse` and\r\n * verifies it matches the requested `leniency`. If parsing and verification succeed,\r\n * a corresponding `PhoneNumberMatch` is returned, otherwise this method returns `null`.\r\n *\r\n * @param candidate the candidate match\r\n * @param offset the offset of {@code candidate} within {@link #text}\r\n * @return the parsed and validated phone number match, or null\r\n */\r\n parseAndVerify(candidate, offset, text) {\r\n if (!isValidCandidate(candidate, offset, text, this.options.leniency)) {\r\n return\r\n \t}\r\n\r\n const phoneNumber = parsePhoneNumber(\r\n candidate,\r\n {\r\n extended: true,\r\n defaultCountry: this.options.defaultCountry,\r\n defaultCallingCode: this.options.defaultCallingCode\r\n },\r\n this.metadata\r\n )\r\n\r\n if (!phoneNumber) {\r\n return\r\n }\r\n\r\n if (!phoneNumber.isPossible()) {\r\n return\r\n }\r\n\r\n if (this.leniency(phoneNumber, {\r\n candidate,\r\n defaultCountry: this.options.defaultCountry,\r\n metadata: this.metadata,\r\n regExpCache: this.regExpCache\r\n })) {\r\n return {\r\n startsAt: offset,\r\n endsAt: offset + candidate.length,\r\n phoneNumber\r\n }\r\n }\r\n }\r\n\r\n hasNext()\r\n {\r\n if (this.state === 'NOT_READY')\r\n {\r\n this.lastMatch = this.find(this.searchIndex)\r\n\r\n if (this.lastMatch)\r\n {\r\n this.searchIndex = this.lastMatch.endsAt\r\n this.state = 'READY'\r\n }\r\n else\r\n {\r\n this.state = 'DONE'\r\n }\r\n }\r\n\r\n return this.state === 'READY'\r\n }\r\n\r\n next()\r\n {\r\n // Check the state and find the next match as a side-effect if necessary.\r\n if (!this.hasNext())\r\n {\r\n throw new Error('No next element')\r\n }\r\n\r\n // Don't retain that memory any longer than necessary.\r\n const result = this.lastMatch\r\n this.lastMatch = null\r\n this.state = 'NOT_READY'\r\n return result\r\n }\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,SACEA,kBAAkB,EAClBC,uBAAuB,EACvBC,iBAAiB,QACZ,gBAAgB;AAEvB,OAAOC,sBAAsB,MAAM,+CAA+C;AAElF,OAAOC,WAAW,MAAM,8BAA8B;AAEtD,SACCC,KAAK,EACLC,mBAAmB,QACb,uBAAuB;AAE9B,SACCC,GAAG,EACHC,GAAG,EACHC,EAAE,EACFC,EAAE,EACFC,GAAG,QACG,wBAAwB;AAE/B,OAAOC,QAAQ,MAAM,2BAA2B;AAChD,OAAOC,iBAAiB,MAAM,oCAAoC;AAClE,OAAOC,mBAAmB,MAAM,sCAAsC;AACtE,OAAOC,gBAAgB,IAAIC,UAAU,QAAQ,mCAAmC;AAEhF,SAASC,kBAAkB,QAAQ,eAAe;AAElD,OAAOC,gBAAgB,MAAM,uBAAuB;AAEpD,MAAMC,+BAA+B,GAAG,KAAK;AAE7C,MAAMC,0BAA0B,GAAGjB,sBAAsB,CAAC,UAAU,CAAC;;AAErE;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