// Angular imports // import { PipeTransform, Pipe } from '@angular/core'; /** * phoneNumber gör om normaliserade nummer till läsbara nummer * 46737262962 => 0737-262962 * * @example * this.phoneNumberPipe.transform(this.model); */ @Pipe({ name: 'phoneNumber' }) export class PhoneNumberPipe implements PipeTransform { private readonly countryCodes: string[] = [ '46', // Sweden '1868', // Trinidad and Tobago '1787', // Puerto Rico '1670', // North Mariana Islands (Saipan) '1340', // Virgin Islands '996', // Kyrgyz Republic '995', // Georgia '994', // Azerbaijan '993', // Turkmenistan '977', // Nepal '976', // Mongolia '975', // Bhutan '974', // Qatar '973', // Bahrain '972', // Israel '971', // United Arab Emirates '968', // Oman '967', // Yemen Arab Republic (North Yemen) '966', // Saudi Arabia '965', // Kuwait '964', // Iraq '963', // Syrian Arab Republic (Syria) '962', // Jordan '961', // Lebanon '960', // Maldives '886', // China-Taiwan, Taiwan '880', // Bangladesh '876', // Jamaica '869', // Nevis, St. Kitts/Nevis '856', // Laos '855', // Cambodia, Khmer Republic (Cambodia/Kampuchea) '853', // Macao '852', // Hong Kong '850', // Korea, People's Republic of (North Korea), North Korea '809', // Anguilla, Bermuda '809', // Dominican Republic '767', // Dominca '692', // Marshall Islands '691', // Micronesia (F.S. of Polynesia) '690', // Tokelau '689', // Tahiti (French Polynesia) '688', // Tuvalu (Ellice Islands) '687', // New Caledonia '686', // Kiribati Republic (Gilbert Islands) '685', // Western Samoa '684', // American Samoa '683', // Niue '682', // Cook Islands '681', // Wallis and Futuna '680', // Palau '679', // Fiji '678', // Vanuatu (New Hebrides) '677', // Solomon Islands '676', // Tonga '675', // Papua New Guinea '674', // Nauru '673', // Brunei Darussalm '672', // Australian External Territories '671', // Guam '670', // Saipan '599', // Netherlands Antilles '598', // Uruguay '597', // Suriname '596', // French Antilles, Martinique (French Antilles) '595', // Paraguay '594', // French Guiana '593', // Ecuador '592', // Guyana '591', // Bolivia '509', // Haiti '508', // St. Pierre &(et) Miquelon (France) '507', // Panama '506', // Costa Rica '505', // Nicaragua '504', // Honduras '503', // El Salvador '502', // Guatemala '501', // Belize '500', // Falkland Islands '473', // Grenada/Carricou, Montserrat '423', // Liechtenstein '421', // Slovakia '420', // Czech Republic '389', // Macedonia '387', // Bosnia and Hercegovina '386', // Slovenia '385', // Croatia '381', // Serbia and Montenegro, Yemen (People's Democratic Republic of), Yugoslavia (discontinued) '380', // Ukraine '378', // San Marino '376', // Andorra '375', // Belarus '374', // Armenia '373', // Moldova '372', // Estonia '371', // Latvia '370', // Lithuania '359', // Bulgaria '358', // Finland '357', // Cyprus '356', // Malta '355', // Albania '354', // Iceland '353', // Ireland (Irish Republic; Eire) '352', // Luxembourg '351', // Portugal (includes Azores) '350', // Gibraltar '345', // Cayman Islands '299', // Greenland '298', // Faroe (Faeroe) Islands (Denmark) '297', // Aruba '291', // Eritrea '290', // St. Helena '284', // British Virgin Islands, British V.I. '269', // Comoros and Mayotte, Mayolte '268', // Antigua '268', // Swaziland '267', // Botswana '266', // Lesotho '265', // Malawi '264', // Namibia (former South-West Africa) '263', // Zimbabwe '262', // Reunion (France) '261', // Madagascar '260', // Zambia '258', // Mozambique '257', // Burundi '256', // Uganda '255', // Tanzania (includes Zanzibar) '254', // Kenya '253', // Djibouti '252', // Somalia '251', // Ethiopia '250', // Rwanda (Rwandese Republic) '249', // Sudan '248', // Seychelles '247', // Ascension Island '246', // Barbados, Diego Garcia '245', // Guinea-Bissau '244', // Angola '243', // Zaire '242', // Bahamas, Congo '241', // Gabon (Gabonese Republic) '240', // Equatorial Guinea '239', // Sao Tome and Principe '238', // Cape Verdi, CapeVerde Islands '237', // Cameroon '236', // Central African Republic '235', // Chad '234', // Nigeria '233', // Ghana '232', // Sierra Leone '231', // Liberia '230', // Mauritius '229', // Benin '228', // Togo (Togolese Republic) '227', // Niger '226', // Burkina Faso '225', // Ivory Coast (La Cote d'Ivoire) '224', // Guinea '223', // Mali '222', // Mauritania '221', // Senegal '220', // Gambia '218', // Libya '216', // Tunisia '213', // Algeria '212', // Morocco '98', // Iran '95', // Myanmar (former Burma) '94', // Sri Lanka '93', // Afghanistan '92', // Pakistan '91', // India '90', // Turkey '86', // China (People's Republic) '84', // Viet Nam '82', // Korea, Republic of (South Korea) '81', // Japan '66', // Thailand '65', // Singapore '64', // New Zealand '63', // Philippines '62', // Indonesia '61', // Australia '60', // Malaysia '58', // Venezuela '57', // Colombia '56', // Chile '55', // Brazil '54', // Argentina '53', // Cuba '52', // Mexico '51', // Peru '49', // Germany '48', // Poland '47', // Norway '45', // Denmark '44', // United Kingdom '43', // Austria '41', // Switzerland '40', // Romania '39', // Italy '39', // Vatican City '36', // Hungary '34', // Spain '33', // France '33', // Monaco '32', // Belgium '31', // Netherlands '30', // Greece '27', // South Africa '20', // Egypt '7', // Kazakhstan, Russia, Tajikistan, Uzbekistan '1', // Canada, Caribbean Nations, USA ]; /** * @param input * @returns phonenumber */ transform(input: string): string { const minimumChars: number = 2; if (input && input.length > minimumChars) { if (input.charAt(0) !== '+') { for (const code of this.countryCodes) { if (code.match(input.substring(0, code.length))) { const re: RegExp = new RegExp('^' + code); return input.replace(re, '+' + code); } } } return input; } return ''; } }