checkNumberLength.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import mergeArrays from './mergeArrays.js';
  2. export default function checkNumberLength(nationalNumber, metadata) {
  3. return checkNumberLengthForType(nationalNumber, undefined, metadata);
  4. } // Checks whether a number is possible for the country based on its length.
  5. // Should only be called for the "new" metadata which has "possible lengths".
  6. export function checkNumberLengthForType(nationalNumber, type, metadata) {
  7. var type_info = metadata.type(type); // There should always be "<possiblePengths/>" set for every type element.
  8. // This is declared in the XML schema.
  9. // For size efficiency, where a sub-description (e.g. fixed-line)
  10. // has the same "<possiblePengths/>" as the "general description", this is missing,
  11. // so we fall back to the "general description". Where no numbers of the type
  12. // exist at all, there is one possible length (-1) which is guaranteed
  13. // not to match the length of any real phone number.
  14. var possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths(); // let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
  15. // Metadata before version `1.0.18` didn't contain `possible_lengths`.
  16. if (!possible_lengths) {
  17. return 'IS_POSSIBLE';
  18. }
  19. if (type === 'FIXED_LINE_OR_MOBILE') {
  20. // No such country in metadata.
  21. /* istanbul ignore next */
  22. if (!metadata.type('FIXED_LINE')) {
  23. // The rare case has been encountered where no fixedLine data is available
  24. // (true for some non-geographic entities), so we just check mobile.
  25. return checkNumberLengthForType(nationalNumber, 'MOBILE', metadata);
  26. }
  27. var mobile_type = metadata.type('MOBILE');
  28. if (mobile_type) {
  29. // Merge the mobile data in if there was any. "Concat" creates a new
  30. // array, it doesn't edit possible_lengths in place, so we don't need a copy.
  31. // Note that when adding the possible lengths from mobile, we have
  32. // to again check they aren't empty since if they are this indicates
  33. // they are the same as the general desc and should be obtained from there.
  34. possible_lengths = mergeArrays(possible_lengths, mobile_type.possibleLengths()); // The current list is sorted; we need to merge in the new list and
  35. // re-sort (duplicates are okay). Sorting isn't so expensive because
  36. // the lists are very small.
  37. // if (local_lengths) {
  38. // local_lengths = mergeArrays(local_lengths, mobile_type.possibleLengthsLocal())
  39. // } else {
  40. // local_lengths = mobile_type.possibleLengthsLocal()
  41. // }
  42. }
  43. } // If the type doesn't exist then return 'INVALID_LENGTH'.
  44. else if (type && !type_info) {
  45. return 'INVALID_LENGTH';
  46. }
  47. var actual_length = nationalNumber.length; // In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
  48. // // This is safe because there is never an overlap beween the possible lengths
  49. // // and the local-only lengths; this is checked at build time.
  50. // if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
  51. // {
  52. // return 'IS_POSSIBLE_LOCAL_ONLY'
  53. // }
  54. var minimum_length = possible_lengths[0];
  55. if (minimum_length === actual_length) {
  56. return 'IS_POSSIBLE';
  57. }
  58. if (minimum_length > actual_length) {
  59. return 'TOO_SHORT';
  60. }
  61. if (possible_lengths[possible_lengths.length - 1] < actual_length) {
  62. return 'TOO_LONG';
  63. } // We skip the first element since we've already checked it.
  64. return possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH';
  65. }
  66. //# sourceMappingURL=checkNumberLength.js.map