getPossibleCountriesForNumber.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import Metadata from '../metadata.js';
  2. /**
  3. * Returns a list of countries that the phone number could potentially belong to.
  4. * @param {string} callingCode — Calling code.
  5. * @param {string} nationalNumber — National (significant) number.
  6. * @param {object} metadata — Metadata.
  7. * @return {string[]} A list of possible countries.
  8. */
  9. export default function getPossibleCountriesForNumber(callingCode, nationalNumber, metadata) {
  10. var _metadata = new Metadata(metadata);
  11. var possibleCountries = _metadata.getCountryCodesForCallingCode(callingCode);
  12. if (!possibleCountries) {
  13. return [];
  14. }
  15. return possibleCountries.filter(function (country) {
  16. return couldNationalNumberBelongToCountry(nationalNumber, country, metadata);
  17. });
  18. }
  19. function couldNationalNumberBelongToCountry(nationalNumber, country, metadata) {
  20. var _metadata = new Metadata(metadata);
  21. _metadata.selectNumberingPlan(country);
  22. if (_metadata.numberingPlan.possibleLengths().indexOf(nationalNumber.length) >= 0) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. //# sourceMappingURL=getPossibleCountriesForNumber.js.map