parse-phone
Version:
phone string parser
250 lines (247 loc) • 2.66 kB
JavaScript
const dialingCodes = [
"+1939",
"+1876",
"+1869",
"+1868",
"+1849",
"+1829",
"+1809",
"+1787",
"+1784",
"+1767",
"+1758",
"+1721",
"+1684",
"+1671",
"+1670",
"+1664",
"+1649",
"+1441",
"+1345",
"+1340",
"+1284",
"+1268",
"+1264",
"+1246",
"+1242",
"+998",
"+996",
"+995",
"+994",
"+993",
"+992",
"+977",
"+976",
"+975",
"+974",
"+973",
"+972",
"+971",
"+970",
"+968",
"+967",
"+966",
"+965",
"+964",
"+963",
"+962",
"+961",
"+960",
"+886",
"+880",
"+872",
"+856",
"+855",
"+853",
"+852",
"+850",
"+692",
"+691",
"+690",
"+689",
"+688",
"+687",
"+686",
"+685",
"+683",
"+682",
"+681",
"+680",
"+679",
"+678",
"+677",
"+676",
"+675",
"+674",
"+673",
"+672",
"+670",
"+599",
"+598",
"+597",
"+596",
"+595",
"+594",
"+593",
"+592",
"+591",
"+590",
"+509",
"+508",
"+507",
"+506",
"+505",
"+504",
"+503",
"+502",
"+501",
"+500",
"+473",
"+423",
"+421",
"+420",
"+389",
"+388",
"+387",
"+386",
"+385",
"+383",
"+382",
"+381",
"+380",
"+379",
"+378",
"+377",
"+376",
"+375",
"+374",
"+373",
"+372",
"+371",
"+370",
"+359",
"+358",
"+357",
"+356",
"+355",
"+354",
"+353",
"+352",
"+351",
"+350",
"+299",
"+298",
"+297",
"+291",
"+290",
"+269",
"+268",
"+267",
"+266",
"+265",
"+264",
"+263",
"+262",
"+261",
"+260",
"+258",
"+257",
"+256",
"+255",
"+254",
"+253",
"+252",
"+251",
"+250",
"+249",
"+248",
"+247",
"+246",
"+245",
"+244",
"+243",
"+242",
"+241",
"+240",
"+239",
"+238",
"+237",
"+236",
"+235",
"+234",
"+233",
"+232",
"+231",
"+230",
"+229",
"+228",
"+227",
"+226",
"+225",
"+224",
"+223",
"+222",
"+221",
"+220",
"+218",
"+216",
"+213",
"+212",
"+211",
"+98",
"+95",
"+94",
"+93",
"+92",
"+91",
"+90",
"+86",
"+84",
"+82",
"+81",
"+78",
"+77",
"+76",
"+74",
"+73",
"+66",
"+65",
"+64",
"+63",
"+62",
"+61",
"+60",
"+58",
"+57",
"+56",
"+55",
"+54",
"+53",
"+52",
"+51",
"+49",
"+48",
"+47",
"+46",
"+45",
"+44",
"+43",
"+41",
"+40",
"+39",
"+36",
"+34",
"+33",
"+32",
"+31",
"+30",
"+27",
"+20",
"+7",
"+1"
]
module.exports = (phoneStr) => {
const matches = dialingCodes.filter(code=>phoneStr.startsWith(code))
if (matches.length == 0) return {success: false}
return { success: true, dialCode: matches[0], full: phoneStr, phone: phoneStr.replace(matches[0], '') }
}