stripIddPrefix.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import Metadata from '../metadata.js';
  2. import { VALID_DIGITS } from '../constants.js';
  3. var CAPTURING_DIGIT_PATTERN = new RegExp('([' + VALID_DIGITS + '])');
  4. export default function stripIddPrefix(number, country, callingCode, metadata) {
  5. if (!country) {
  6. return;
  7. } // Check if the number is IDD-prefixed.
  8. var countryMetadata = new Metadata(metadata);
  9. countryMetadata.selectNumberingPlan(country, callingCode);
  10. var IDDPrefixPattern = new RegExp(countryMetadata.IDDPrefix());
  11. if (number.search(IDDPrefixPattern) !== 0) {
  12. return;
  13. } // Strip IDD prefix.
  14. number = number.slice(number.match(IDDPrefixPattern)[0].length); // If there're any digits after an IDD prefix,
  15. // then those digits are a country calling code.
  16. // Since no country code starts with a `0`,
  17. // the code below validates that the next digit (if present) is not `0`.
  18. var matchedGroups = number.match(CAPTURING_DIGIT_PATTERN);
  19. if (matchedGroups && matchedGroups[1] != null && matchedGroups[1].length > 0) {
  20. if (matchedGroups[1] === '0') {
  21. return;
  22. }
  23. }
  24. return number;
  25. }
  26. //# sourceMappingURL=stripIddPrefix.js.map