Created
June 22, 2024 07:52
-
-
Save wmakeev/d088a4bacd525b421559c68223c93491 to your computer and use it in GitHub Desktop.
[phone format] #phone #format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatPhone = (phone) => { | |
if (typeof phone !== "string") return ""; | |
const phoneNums = phone.replaceAll(/\D/g, ""); | |
if (phoneNums.length !== 11) return "+" + phoneNums; | |
const code = phoneNums.substring(0, 1); | |
const region = phoneNums.substring(1, 4); | |
const phone1 = phoneNums.substring(4, 7); | |
const phone2 = phoneNums.substring(7, 9); | |
const phone3 = phoneNums.substring(9, 11); | |
return `+${ | |
code === "8" ? "7" : code | |
} (${region}) ${phone1}-${phone2}-${phone3}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment