extractNationalNumber.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'
  2. import matchesEntirely from './matchesEntirely.js'
  3. import checkNumberLength from './checkNumberLength.js'
  4. /**
  5. * Strips national prefix and carrier code from a complete phone number.
  6. * The difference from the non-"FromCompleteNumber" function is that
  7. * it won't extract national prefix if the resultant number is too short
  8. * to be a complete number for the selected phone numbering plan.
  9. * @param {string} number — Complete phone number digits.
  10. * @param {Metadata} metadata — Metadata with a phone numbering plan selected.
  11. * @return {object} `{ nationalNumber: string, carrierCode: string? }`.
  12. */
  13. export default function extractNationalNumber(number, metadata) {
  14. // Parsing national prefixes and carrier codes
  15. // is only required for local phone numbers
  16. // but some people don't understand that
  17. // and sometimes write international phone numbers
  18. // with national prefixes (or maybe even carrier codes).
  19. // http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
  20. // Google's original library forgives such mistakes
  21. // and so does this library, because it has been requested:
  22. // https://github.com/catamphetamine/libphonenumber-js/issues/127
  23. const {
  24. carrierCode,
  25. nationalNumber
  26. } = extractNationalNumberFromPossiblyIncompleteNumber(
  27. number,
  28. metadata
  29. )
  30. if (nationalNumber !== number) {
  31. if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
  32. // Don't strip the national prefix.
  33. return { nationalNumber: number }
  34. }
  35. // Check the national (significant) number length after extracting national prefix and carrier code.
  36. // Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
  37. if (metadata.possibleLengths()) {
  38. // The number remaining after stripping the national prefix and carrier code
  39. // should be long enough to have a possible length for the country.
  40. // Otherwise, don't strip the national prefix and carrier code,
  41. // since the original number could be a valid number.
  42. // This check has been copy-pasted "as is" from Google's original library:
  43. // https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
  44. // It doesn't check for the "possibility" of the original `number`.
  45. // I guess it's fine not checking that one. It works as is anyway.
  46. if (!isPossibleIncompleteNationalNumber(nationalNumber, metadata)) {
  47. // Don't strip the national prefix.
  48. return { nationalNumber: number }
  49. }
  50. }
  51. }
  52. return { nationalNumber, carrierCode }
  53. }
  54. // In some countries, the same digit could be a national prefix
  55. // or a leading digit of a valid phone number.
  56. // For example, in Russia, national prefix is `8`,
  57. // and also `800 555 35 35` is a valid number
  58. // in which `8` is not a national prefix, but the first digit
  59. // of a national (significant) number.
  60. // Same's with Belarus:
  61. // `82004910060` is a valid national (significant) number,
  62. // but `2004910060` is not.
  63. // To support such cases (to prevent the code from always stripping
  64. // national prefix), a condition is imposed: a national prefix
  65. // is not extracted when the original number is "viable" and the
  66. // resultant number is not, a "viable" national number being the one
  67. // that matches `national_number_pattern`.
  68. function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
  69. // The equivalent in Google's code is:
  70. // https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
  71. if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) &&
  72. !matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
  73. return false
  74. }
  75. // This "is possible" national number (length) check has been commented out
  76. // because it's superceded by the (effectively) same check done in the
  77. // `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
  78. // In other words, why run the same check twice if it could only be run once.
  79. // // Check the national (significant) number length after extracting national prefix and carrier code.
  80. // // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
  81. // // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
  82. // if (metadata.possibleLengths()) {
  83. // if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
  84. // !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
  85. // return false
  86. // }
  87. // }
  88. return true
  89. }
  90. function isPossibleIncompleteNationalNumber(nationalNumber, metadata) {
  91. switch (checkNumberLength(nationalNumber, metadata)) {
  92. case 'TOO_SHORT':
  93. case 'INVALID_LENGTH':
  94. // This library ignores "local-only" phone numbers (for simplicity).
  95. // See the readme for more info on what are "local-only" phone numbers.
  96. // case 'IS_POSSIBLE_LOCAL_ONLY':
  97. return false
  98. default:
  99. return true
  100. }
  101. }