matchPhoneNumberStringAgainstPhoneNumber.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import parsePhoneNumber from '../parsePhoneNumber.js';
  2. /**
  3. * Matches a phone number object against a phone number string.
  4. * @param {string} phoneNumberString
  5. * @param {PhoneNumber} phoneNumber
  6. * @param {object} metadata — Metadata JSON
  7. * @return {'INVALID_NUMBER'|'NO_MATCH'|'SHORT_NSN_MATCH'|'NSN_MATCH'|'EXACT_MATCH'}
  8. */
  9. export default function matchPhoneNumberStringAgainstPhoneNumber(phoneNumberString, phoneNumber, metadata) {
  10. // Parse `phoneNumberString`.
  11. var phoneNumberStringContainsCallingCode = true;
  12. var parsedPhoneNumber = parsePhoneNumber(phoneNumberString, metadata);
  13. if (!parsedPhoneNumber) {
  14. // If `phoneNumberString` didn't contain a country calling code
  15. // then substitute it with the `phoneNumber`'s country calling code.
  16. phoneNumberStringContainsCallingCode = false;
  17. parsedPhoneNumber = parsePhoneNumber(phoneNumberString, {
  18. defaultCallingCode: phoneNumber.countryCallingCode
  19. }, metadata);
  20. }
  21. if (!parsedPhoneNumber) {
  22. return 'INVALID_NUMBER';
  23. } // Check that the extensions match.
  24. if (phoneNumber.ext) {
  25. if (parsedPhoneNumber.ext !== phoneNumber.ext) {
  26. return 'NO_MATCH';
  27. }
  28. } else {
  29. if (parsedPhoneNumber.ext) {
  30. return 'NO_MATCH';
  31. }
  32. } // Check that country calling codes match.
  33. if (phoneNumberStringContainsCallingCode) {
  34. if (phoneNumber.countryCallingCode !== parsedPhoneNumber.countryCallingCode) {
  35. return 'NO_MATCH';
  36. }
  37. } // Check if the whole numbers match.
  38. if (phoneNumber.number === parsedPhoneNumber.number) {
  39. if (phoneNumberStringContainsCallingCode) {
  40. return 'EXACT_MATCH';
  41. } else {
  42. return 'NSN_MATCH';
  43. }
  44. } // Check if one national number is a "suffix" of the other.
  45. if (phoneNumber.nationalNumber.indexOf(parsedPhoneNumber.nationalNumber) === 0 || parsedPhoneNumber.nationalNumber.indexOf(phoneNumber.nationalNumber) === 0) {
  46. // "A SHORT_NSN_MATCH occurs if there is a difference because of the
  47. // presence or absence of an 'Italian leading zero', the presence or
  48. // absence of an extension, or one NSN being a shorter variant of the
  49. // other."
  50. return 'SHORT_NSN_MATCH';
  51. }
  52. return 'NO_MATCH';
  53. }
  54. //# sourceMappingURL=matchPhoneNumberStringAgainstPhoneNumber.js.map