checkNumberLength.js 3.8 KB

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