feat(console): phone number validation with flags (#5139)

Formats the phonenumber according to the preselected country
This commit is contained in:
Miguel Cabrerizo
2023-02-02 09:36:43 +01:00
committed by GitHub
parent e9d5d1dcaf
commit 5704c44117
284 changed files with 306 additions and 65 deletions
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CountryCode, getCountries, getCountryCallingCode } from 'libphonenumber-js';
import * as i18nIsoCountries from 'i18n-iso-countries';
export interface CountryPhoneCode {
countryCode: string;
countryName: string;
countryCallingCode: string;
}
@Injectable()
export class CountryCallingCodesService {
constructor(private translateService: TranslateService) {}
public getCountryCallingCodes(): CountryPhoneCode[] {
const currentLang = this.translateService.currentLang ?? 'en';
const countryPhoneCodes = getCountries()
.filter((code: CountryCode) => i18nIsoCountries.getName(code.toString(), currentLang))
.map((code: CountryCode) => {
return <CountryPhoneCode>{
countryCode: code,
countryName: i18nIsoCountries.getName(code.toString(), currentLang),
countryCallingCode: getCountryCallingCode(code),
};
})
.sort((a, b) => a.countryName.localeCompare(b.countryName));
return countryPhoneCodes;
}
}