checkNumberLength.js 3.5 KB

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