extractExtension.js 988 B

1234567891011121314151617181920212223242526272829
  1. import createExtensionPattern from './createExtensionPattern.js'
  2. // Regexp of all known extension prefixes used by different regions followed by
  3. // 1 or more valid digits, for use when parsing.
  4. const EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i')
  5. // Strips any extension (as in, the part of the number dialled after the call is
  6. // connected, usually indicated with extn, ext, x or similar) from the end of
  7. // the number, and returns it.
  8. export default function extractExtension(number) {
  9. const start = number.search(EXTN_PATTERN)
  10. if (start < 0) {
  11. return {}
  12. }
  13. // If we find a potential extension, and the number preceding this is a viable
  14. // number, we assume it is an extension.
  15. const numberWithoutExtension = number.slice(0, start)
  16. const matches = number.match(EXTN_PATTERN)
  17. let i = 1
  18. while (i < matches.length) {
  19. if (matches[i]) {
  20. return {
  21. number: numberWithoutExtension,
  22. ext: matches[i]
  23. }
  24. }
  25. i++
  26. }
  27. }