Leniency.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. import isValidNumber from '../isValid.js'
  2. import parseDigits from '../helpers/parseDigits.js'
  3. import matchPhoneNumberStringAgainstPhoneNumber from './matchPhoneNumberStringAgainstPhoneNumber.js'
  4. import Metadata from '../metadata.js'
  5. import getCountryByCallingCode from '../helpers/getCountryByCallingCode.js'
  6. import { chooseFormatForNumber } from '../format.js'
  7. import {
  8. startsWith,
  9. endsWith
  10. } from './util.js'
  11. /**
  12. * Leniency when finding potential phone numbers in text segments
  13. * The levels here are ordered in increasing strictness.
  14. */
  15. export default
  16. {
  17. /**
  18. * Phone numbers accepted are "possible", but not necessarily "valid".
  19. */
  20. POSSIBLE(phoneNumber, { candidate, metadata })
  21. {
  22. return true
  23. },
  24. /**
  25. * Phone numbers accepted are "possible" and "valid".
  26. * Numbers written in national format must have their national-prefix
  27. * present if it is usually written for a number of this type.
  28. */
  29. VALID(phoneNumber, { candidate, defaultCountry, metadata })
  30. {
  31. if (
  32. !phoneNumber.isValid() ||
  33. !containsOnlyValidXChars(phoneNumber, candidate, metadata)
  34. )
  35. {
  36. return false
  37. }
  38. // Skipped for simplicity.
  39. // return isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
  40. return true
  41. },
  42. /**
  43. * Phone numbers accepted are "valid" and
  44. * are grouped in a possible way for this locale. For example, a US number written as
  45. * "65 02 53 00 00" and "650253 0000" are not accepted at this leniency level, whereas
  46. * "650 253 0000", "650 2530000" or "6502530000" are.
  47. * Numbers with more than one '/' symbol in the national significant number
  48. * are also dropped at this level.
  49. *
  50. * Warning: This level might result in lower coverage especially for regions outside of
  51. * country code "+1". If you are not sure about which level to use,
  52. * email the discussion group libphonenumber-discuss@googlegroups.com.
  53. */
  54. STRICT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
  55. {
  56. if (
  57. !phoneNumber.isValid() ||
  58. !containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
  59. containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
  60. !isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
  61. )
  62. {
  63. return false
  64. }
  65. return checkNumberGroupingIsValid
  66. (
  67. phoneNumber,
  68. candidate,
  69. metadata,
  70. allNumberGroupsRemainGrouped,
  71. regExpCache
  72. )
  73. },
  74. /**
  75. * Phone numbers accepted are "valid" and are grouped in the same way
  76. * that we would have formatted it, or as a single block.
  77. * For example, a US number written as "650 2530000" is not accepted
  78. * at this leniency level, whereas "650 253 0000" or "6502530000" are.
  79. * Numbers with more than one '/' symbol are also dropped at this level.
  80. *
  81. * Warning: This level might result in lower coverage especially for regions outside of
  82. * country code "+1". If you are not sure about which level to use, email the discussion group
  83. * libphonenumber-discuss@googlegroups.com.
  84. */
  85. EXACT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
  86. {
  87. if (
  88. !phoneNumber.isValid() ||
  89. !containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
  90. containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
  91. !isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
  92. )
  93. {
  94. return false
  95. }
  96. return checkNumberGroupingIsValid
  97. (
  98. phoneNumber,
  99. candidate,
  100. metadata,
  101. allNumberGroupsAreExactlyPresent,
  102. regExpCache
  103. )
  104. }
  105. }
  106. function containsOnlyValidXChars(phoneNumber, candidate, metadata)
  107. {
  108. // The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
  109. // national significant number or (2) an extension sign, in which case they always precede the
  110. // extension number. We assume a carrier code is more than 1 digit, so the first case has to
  111. // have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
  112. // or 'X'. We ignore the character if it appears as the last character of the string.
  113. for (let index = 0; index < candidate.length - 1; index++)
  114. {
  115. const charAtIndex = candidate.charAt(index)
  116. if (charAtIndex === 'x' || charAtIndex === 'X')
  117. {
  118. const charAtNextIndex = candidate.charAt(index + 1)
  119. if (charAtNextIndex === 'x' || charAtNextIndex === 'X')
  120. {
  121. // This is the carrier code case, in which the 'X's always precede the national
  122. // significant number.
  123. index++
  124. if (matchPhoneNumberStringAgainstPhoneNumber(candidate.substring(index), phoneNumber, metadata) !== 'NSN_MATCH')
  125. {
  126. return false
  127. }
  128. // This is the extension sign case, in which the 'x' or 'X' should always precede the
  129. // extension number.
  130. }
  131. else {
  132. const ext = parseDigits(candidate.substring(index))
  133. if (ext) {
  134. if (phoneNumber.ext !== ext) {
  135. return false
  136. }
  137. } else {
  138. if (phoneNumber.ext) {
  139. return false
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return true
  146. }
  147. function isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata: _metadata })
  148. {
  149. // First, check how we deduced the country code. If it was written in international format, then
  150. // the national prefix is not required.
  151. if (phoneNumber.__countryCallingCodeSource !== 'FROM_DEFAULT_COUNTRY')
  152. {
  153. return true
  154. }
  155. const metadata = new Metadata(_metadata)
  156. metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
  157. const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, {
  158. nationalNumber: phoneNumber.nationalNumber,
  159. defaultCountry,
  160. metadata
  161. })
  162. // Check if a national prefix should be present when formatting this number.
  163. const nationalNumber = phoneNumber.nationalNumber
  164. const format = chooseFormatForNumber(metadata.numberingPlan.formats(), nationalNumber)
  165. // To do this, we check that a national prefix formatting rule was present
  166. // and that it wasn't just the first-group symbol ($1) with punctuation.
  167. if (format.nationalPrefixFormattingRule())
  168. {
  169. if (metadata.numberingPlan.nationalPrefixIsOptionalWhenFormattingInNationalFormat())
  170. {
  171. // The national-prefix is optional in these cases, so we don't need to check if it was present.
  172. return true
  173. }
  174. if (!format.usesNationalPrefix())
  175. {
  176. // National Prefix not needed for this number.
  177. return true
  178. }
  179. return Boolean(phoneNumber.nationalPrefix)
  180. }
  181. return true
  182. }
  183. export function containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate)
  184. {
  185. const firstSlashInBodyIndex = candidate.indexOf('/')
  186. if (firstSlashInBodyIndex < 0)
  187. {
  188. // No slashes, this is okay.
  189. return false
  190. }
  191. // Now look for a second one.
  192. const secondSlashInBodyIndex = candidate.indexOf('/', firstSlashInBodyIndex + 1)
  193. if (secondSlashInBodyIndex < 0)
  194. {
  195. // Only one slash, this is okay.
  196. return false
  197. }
  198. // If the first slash is after the country calling code, this is permitted.
  199. const candidateHasCountryCode =
  200. phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITH_PLUS_SIGN' ||
  201. phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITHOUT_PLUS_SIGN'
  202. if (candidateHasCountryCode && parseDigits(candidate.substring(0, firstSlashInBodyIndex)) === phoneNumber.countryCallingCode)
  203. {
  204. // Any more slashes and this is illegal.
  205. return candidate.slice(secondSlashInBodyIndex + 1).indexOf('/') >= 0
  206. }
  207. return true
  208. }
  209. function checkNumberGroupingIsValid(
  210. number,
  211. candidate,
  212. metadata,
  213. checkGroups,
  214. regExpCache
  215. ) {
  216. throw new Error('This part of code hasn\'t been ported')
  217. const normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */)
  218. let formattedNumberGroups = getNationalNumberGroups(metadata, number, null)
  219. if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
  220. return true
  221. }
  222. // If this didn't pass, see if there are any alternate formats that match, and try them instead.
  223. const alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode())
  224. const nationalSignificantNumber = util.getNationalSignificantNumber(number)
  225. if (alternateFormats) {
  226. for (const alternateFormat of alternateFormats.numberFormats()) {
  227. if (alternateFormat.leadingDigitsPatterns().length > 0) {
  228. // There is only one leading digits pattern for alternate formats.
  229. const leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0])
  230. if (!leadingDigitsRegExp.test(nationalSignificantNumber)) {
  231. // Leading digits don't match; try another one.
  232. continue
  233. }
  234. }
  235. formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat)
  236. if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
  237. return true
  238. }
  239. }
  240. }
  241. return false
  242. }
  243. /**
  244. * Helper method to get the national-number part of a number, formatted without any national
  245. * prefix, and return it as a set of digit blocks that would be formatted together following
  246. * standard formatting rules.
  247. */
  248. function getNationalNumberGroups(
  249. metadata,
  250. number,
  251. formattingPattern
  252. ) {
  253. throw new Error('This part of code hasn\'t been ported')
  254. if (formattingPattern) {
  255. // We format the NSN only, and split that according to the separator.
  256. const nationalSignificantNumber = util.getNationalSignificantNumber(number)
  257. return util.formatNsnUsingPattern(nationalSignificantNumber,
  258. formattingPattern, 'RFC3966', metadata).split('-')
  259. }
  260. // This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits.
  261. const rfc3966Format = formatNumber(number, 'RFC3966', metadata)
  262. // We remove the extension part from the formatted string before splitting it into different
  263. // groups.
  264. let endIndex = rfc3966Format.indexOf(';')
  265. if (endIndex < 0) {
  266. endIndex = rfc3966Format.length
  267. }
  268. // The country-code will have a '-' following it.
  269. const startIndex = rfc3966Format.indexOf('-') + 1
  270. return rfc3966Format.slice(startIndex, endIndex).split('-')
  271. }
  272. function allNumberGroupsAreExactlyPresent
  273. (
  274. metadata,
  275. number,
  276. normalizedCandidate,
  277. formattedNumberGroups
  278. )
  279. {
  280. throw new Error('This part of code hasn\'t been ported')
  281. const candidateGroups = normalizedCandidate.split(NON_DIGITS_PATTERN)
  282. // Set this to the last group, skipping it if the number has an extension.
  283. let candidateNumberGroupIndex =
  284. number.hasExtension() ? candidateGroups.length - 2 : candidateGroups.length - 1
  285. // First we check if the national significant number is formatted as a block.
  286. // We use contains and not equals, since the national significant number may be present with
  287. // a prefix such as a national number prefix, or the country code itself.
  288. if (candidateGroups.length == 1
  289. || candidateGroups[candidateNumberGroupIndex].contains(
  290. util.getNationalSignificantNumber(number)))
  291. {
  292. return true
  293. }
  294. // Starting from the end, go through in reverse, excluding the first group, and check the
  295. // candidate and number groups are the same.
  296. let formattedNumberGroupIndex = (formattedNumberGroups.length - 1)
  297. while (formattedNumberGroupIndex > 0 && candidateNumberGroupIndex >= 0)
  298. {
  299. if (candidateGroups[candidateNumberGroupIndex] !== formattedNumberGroups[formattedNumberGroupIndex])
  300. {
  301. return false
  302. }
  303. formattedNumberGroupIndex--
  304. candidateNumberGroupIndex--
  305. }
  306. // Now check the first group. There may be a national prefix at the start, so we only check
  307. // that the candidate group ends with the formatted number group.
  308. return (candidateNumberGroupIndex >= 0
  309. && endsWith(candidateGroups[candidateNumberGroupIndex], formattedNumberGroups[0]))
  310. }
  311. function allNumberGroupsRemainGrouped
  312. (
  313. metadata,
  314. number,
  315. normalizedCandidate,
  316. formattedNumberGroups
  317. )
  318. {
  319. throw new Error('This part of code hasn\'t been ported')
  320. let fromIndex = 0
  321. if (number.getCountryCodeSource() !== CountryCodeSource.FROM_DEFAULT_COUNTRY)
  322. {
  323. // First skip the country code if the normalized candidate contained it.
  324. const countryCode = String(number.getCountryCode())
  325. fromIndex = normalizedCandidate.indexOf(countryCode) + countryCode.length()
  326. }
  327. // Check each group of consecutive digits are not broken into separate groupings in the
  328. // {@code normalizedCandidate} string.
  329. for (let i = 0; i < formattedNumberGroups.length; i++)
  330. {
  331. // Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex}
  332. // doesn't contain the consecutive digits in formattedNumberGroups[i].
  333. fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex)
  334. if (fromIndex < 0) {
  335. return false
  336. }
  337. // Moves {@code fromIndex} forward.
  338. fromIndex += formattedNumberGroups[i].length()
  339. if (i == 0 && fromIndex < normalizedCandidate.length())
  340. {
  341. // We are at the position right after the NDC. We get the region used for formatting
  342. // information based on the country code in the phone number, rather than the number itself,
  343. // as we do not need to distinguish between different countries with the same country
  344. // calling code and this is faster.
  345. const region = util.getRegionCodeForCountryCode(number.getCountryCode())
  346. if (util.getNddPrefixForRegion(region, true) != null
  347. && Character.isDigit(normalizedCandidate.charAt(fromIndex))) {
  348. // This means there is no formatting symbol after the NDC. In this case, we only
  349. // accept the number if there is no formatting symbol at all in the number, except
  350. // for extensions. This is only important for countries with national prefixes.
  351. const nationalSignificantNumber = util.getNationalSignificantNumber(number)
  352. return startsWith
  353. (
  354. normalizedCandidate.slice(fromIndex - formattedNumberGroups[i].length),
  355. nationalSignificantNumber
  356. )
  357. }
  358. }
  359. }
  360. // The check here makes sure that we haven't mistakenly already used the extension to
  361. // match the last group of the subscriber number. Note the extension cannot have
  362. // formatting in-between digits.
  363. return normalizedCandidate.slice(fromIndex).contains(number.getExtension())
  364. }