applyInternationalSeparatorStyle.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { VALID_PUNCTUATION } from '../constants.js'
  2. // Removes brackets and replaces dashes with spaces.
  3. //
  4. // E.g. "(999) 111-22-33" -> "999 111 22 33"
  5. //
  6. // For some reason Google's metadata contains `<intlFormat/>`s with brackets and dashes.
  7. // Meanwhile, there's no single opinion about using punctuation in international phone numbers.
  8. //
  9. // For example, Google's `<intlFormat/>` for USA is `+1 213-373-4253`.
  10. // And here's a quote from WikiPedia's "North American Numbering Plan" page:
  11. // https://en.wikipedia.org/wiki/North_American_Numbering_Plan
  12. //
  13. // "The country calling code for all countries participating in the NANP is 1.
  14. // In international format, an NANP number should be listed as +1 301 555 01 00,
  15. // where 301 is an area code (Maryland)."
  16. //
  17. // I personally prefer the international format without any punctuation.
  18. // For example, brackets are remnants of the old age, meaning that the
  19. // phone number part in brackets (so called "area code") can be omitted
  20. // if dialing within the same "area".
  21. // And hyphens were clearly introduced for splitting local numbers into memorizable groups.
  22. // For example, remembering "5553535" is difficult but "555-35-35" is much simpler.
  23. // Imagine a man taking a bus from home to work and seeing an ad with a phone number.
  24. // He has a couple of seconds to memorize that number until it passes by.
  25. // If it were spaces instead of hyphens the man wouldn't necessarily get it,
  26. // but with hyphens instead of spaces the grouping is more explicit.
  27. // I personally think that hyphens introduce visual clutter,
  28. // so I prefer replacing them with spaces in international numbers.
  29. // In the modern age all output is done on displays where spaces are clearly distinguishable
  30. // so hyphens can be safely replaced with spaces without losing any legibility.
  31. //
  32. export default function applyInternationalSeparatorStyle(formattedNumber) {
  33. return formattedNumber.replace(new RegExp(`[${VALID_PUNCTUATION}]+`, 'g'), ' ').trim()
  34. }