1.1.2 • Published 3 months ago

angular-phone-helper v1.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

188 countries + International format (Angular 17)

User's phone's format for pipe

{{ '31234567890' | phoneFormat }} <!-- +3 123 456 7890 -->
{{ '1234567890' | phoneFormat: { iso: 'USA', prefix: true } }} // <!-- +1 (123) 456-7890 -->
{{ '41234567890' | phoneFormat: { prefix: true } }} // <!-- +4 123 456 7890 -->
{{ '1234567890' | phoneFormat: { prefix: true, code: '+33', format: '(XXX) XXX-XXXX' } }} // <!-- +33 (123) 456-7890 -->
{{ '1234567890' | phoneFormat: { prefix: false, code: '+33', format: '(XXX) XXX-XXXX' } }} // <!-- (123) 456-7890 -->
{{ '1234567890' | phoneFormat: { prefix: false, format: '(XXX) XXX-XXXX' } }} // <!-- (123) 456-7890 -->

Setting up in module's imports

import {AngularPhoneHelperModule} from "angular-phone-helper";

// default country config
AngularPhoneHelperModule.forRoot({
  defaultCountryIsoCode: 'USA', // Country ISO code
  defaultWithCountryCode: false, // Add prefix by default
})

// custom country config
AngularPhoneHelperModule.forRoot({
  defaultWithCountryCode: true,
  customCountry: {
    isoCode: 'YOUR_CODE_3_LEN',
    isoCodeAlpha2: 'YOUR_CODE_2_LEN',
    name: 'YOUR_COUNTRY_NAME',
    phone: {
      codeDigit: '10',
      code: '+10',
      format: 'XXX XXX-XX-XX',
      formatInternational: 'XXX XXX-XX-XX',
    }
  }
}),
NOTE: Config is optional

NOTE: For custom country
Some countries have different format 
For example: France
codeDigit: "33", code: "+33", formatInternational: "X XX XX XX XX", format: "0X XX XX XX XX"
X - digits
0 - const

ApiConfig

KeyTypeOptionalDefault value
defaultCountryIsoCodestringyes
defaultWithCountryCodebooleanyestrue
customCountryCountryModelyesnull

CountryModel

KeyTypeOptionalDefault value
namestringno
isoCodestringno
isoCodeAlpha2stringno
phonePhoneModelno

PhoneModel

KeyTypeOptionalDefault value
codestringno
codeDigitstringno
formatstringno
formatInternationalstringyes

Usage PhoneFormatService in ts

import {PhoneFormatService} from "angular-phone-helper";

constructor(private readonly phoneFormatService: PhoneFormatService) {
  this.phoneFormatService.isCustomFormatted('123 456-78-90'); // true
  this.phoneFormatService.isInternationalFormatted('+3 123 456 7890'); // true
  this.phoneFormatService.isInternationalFormatted('+4811231234567890'); // false
  this.phoneFormatService.isInternationalFormatted('+481 2312 67890'); // false
  this.phoneFormatService.isUnitedStatesFormatted('(123) 456-7890'); // true
  this.phoneFormatService.isUnitedStatesFormatted('+1 (123) 456-7890'); // true
  this.phoneFormatService.isUnitedStatesFormatted('+2 (123) 456-7890'); // false
}

Usage PhoneFormatPipe in ts

import {countriesDictionary, PhoneFormatPipe} from "angular-phone-helper";

constructor(private readonly phoneFormatPipe: PhoneFormatPipe) {
  this.phoneFormatPipe.transform('31234567890'); // +3 123 456 7890
  this.phoneFormatPipe.transform('1234567890', { iso: 'USA', prefix: true }); // +1 (123) 456-7890
  this.phoneFormatPipe.transform('1234567890', { prefix: true }); // 1234567890 (wrong length)
  this.phoneFormatPipe.transform('1234567890', { iso: countriesDictionary.usa.isoCode }); // +1 (123) 456-7890
  this.phoneFormatPipe.transform('1234567890', { iso: countriesDictionary.usa.isoCode, prefix: false }) // (123) 456-7890
}

Usage PhoneFormatPipe in html

{{ '31234567890' | phoneFormat }} <!-- +3 123 456 7890 -->
{{ '1234567890' | phoneFormat: { iso: 'USA', prefix: true } }} // <!-- +1 (123) 456-7890 -->
{{ '41234567890' | phoneFormat: { prefix: true } }} // <!-- +4 123 456 7890 -->
{{ '1234567890' | phoneFormat: { prefix: true, code: '+33', format: '(XXX) XXX-XXXX' } }} // <!-- +33 (123) 456-7890 -->
{{ '1234567890' | phoneFormat: { prefix: false, code: '+33', format: '(XXX) XXX-XXXX' } }} // <!-- (123) 456-7890 -->
{{ '1234567890' | phoneFormat: { prefix: false, format: '(XXX) XXX-XXXX' } }} // <!-- (123) 456-7890 -->

Supported countries (in progress...)

NOTE: Methods in table and `custom country method`
Input >>> phone: string
Output >>> result: boolean

NOTE: isFormatted method
Input >>> phone: string, countryIsoCode: string
Output >>> result: boolean
IDCountry ISO codePhone prefixPhone formatMethod*
1International+X XXX XXX XXXXisInternationalFormatted
2USA+1(XXX) XXX-XXXXisUnitedStatesFormatted
3CAN+1(XXX) XXX-XXXXisCanadaFormatted
4GBR+440XX XXXX XXXXisUnitedKingdomFormatted
5FRA+330X XX XX XX XXisFranceFormatted
6DEU+490XXX XXXXXXXisGermanyFormatted
7ITA+39XXX XXX XXXXisItalyFormatted
8POL+48XXX XXX XXXisPolandFormatted
9UKR+380XXX XXX XX XXisUkraineFormatted
10MDA+373XX XXX XXXisMoldovaFormatted
11GRC+30XXX XXX XXXXisGreeceFormatted
12TUR+90XXX XXX XXXXisTurkeyFormatted
13IND+91XXXXX XXXXXisIndiaFormatted
14CHN+86XXX XXXX XXXXisChinaFormatted
15IDN+62XXXX-XXXX-XXXXisIndonesiaFormatted
16PHL+63XXX XXX XXXXisPhilippinesFormatted
17AUS+61XXXX XXX XXXisAustraliaFormatted
18BRA+55(XX) XXXX-XXXXisBrazilFormatted
19ARG+54XX XXXX-XXXXisArgentinaFormatted
20MEX+52XX XXXX XXXXisMexicoFormatted
21ESP+34XXX XX XX XXisSpainFormatted
22CZE+420XXX XXX XXXisCzechRepublicFormatted
23PER+51XXX XXX XXXisPeruFormatted
24JPN+81XXX-XXXX-XXXXisJapanFormatted
25KOR+82XXX-XXX-XXXXisSouthKoreaFormatted
26AUT+43XX XXXXXXXXXisAustriaFormatted
27AZE+994XX XXXXX XXXXisAzerbaijanFormatted
28ALB+355XXX XX XX XXXisAlbaniaFormatted
29DZA+213XXX XX XX XX XXisAlgeriaFormatted
30AGO+244XXX XXX XXXisAngolaFormatted
31AND+376XXXXXXisAndorraFormatted
32ATG+1268XXX XXXXisAntiguaAndBarbudaFormatted
33ARM+374XX XXXXXXisArmeniaFormatted
34AFG+93X XX XXX XXXXisAfghanistanFormatted
35UZB+998XX XXX XX XXisUzbekistanFormatted
36YEM+967X XXX XXXisYemenFormatted
37KNA+1869XXX-XXX-XXXXisSaintKittsAndNevisFormatted
38BGD+88001XX XXX XXXXisBangladeshFormatted
39BHS+1-242(XXX) XXX-XXXXisBahamasFormatted
40BRB+1-246(XXX) XXX-XXXXisBarbadosFormatted
41BHR+973XXXX XXXXisBahrainFormatted
43BLZ+501XXX-XXXXisBelizeFormatted
44BEL+320X XX XX XX XXisBelgiumFormatted
45BEN+229XX XX XX XXisBeninFormatted
46BGR+359X XXX XXXXisBulgariaFormatted
47BOL+591XXXX-XXXXisBoliviaFormatted
48BIH+387XXX XXX XXXisBosniaAndHerzegovinaFormatted
49BWA+267XX XXX XXXisBotswanaFormatted
50BRN+673XXXX XXXisBruneiFormatted
51BFA+226XX XX XXXXisBurkinaFasoFormatted
52BDI+257XX XX XXXXisBurundiFormatted
53BTN+975XX XX XXXisBhutanFormatted
54VUT+678XXX-XXXXisVanuatuFormatted
55HUN+36(XX) XXX-XXXXisHungaryFormatted
56VEN+58(XXX) XXX-XXXXisVenezuelaFormatted
57TLS+670XXX-XXXXisEastTimorFormatted
58VNM+84XXX XXX XXXXisVietnamFormatted
59GAB+241X XX XX XXisGabonFormatted
60HTI+509XXX XX-XXXXisHaitiFormatted
61GUY+592XXX XXXXisGuyanaFormatted
62GMB+220XXX XXXXisGambiaFormatted
63GHA+233XXX XXX XXXXisGhanaFormatted
64GTM+502XXXX XXXXisGuatemalaFormatted
65GIN+224XX XXX XXXisGuineaFormatted
66GNB+245X XXX XXXXisGuineaBissauFormatted
67HND+504XXXX-XXXXisHondurasFormatted
68GRD+473XXX XXXXisGrenadaFormatted
69GEO+995XXX XX XX XXisGeorgiaFormatted
70DNK+45XX XX XX XXisDenmarkFormatted
71DJI+253XX XX XX XXisDjiboutiFormatted
72DMA+1(XXX) XXX-XXXXisDominicaFormatted
73DOM+1(XXX) XXX-XXXXisDominicanRepublicFormatted
74EGY+2001X XXX XXXXisEgyptFormatted
75ZMB+260XXX XXX XXXisZambiaFormatted
76ZWE+263X XXX XXXXisZimbabweFormatted
77ISR+972XX-XXX-XXXXisIsraelFormatted
78CPV+238XXX-XX-XXisCapeVerdeFormatted
79KHM+855XX-XXX-XXXisCambodiaFormatted
80CMR+237XX-XX-XX-XXisCameroonFormatted
81CAF+236XX-XX-XX-XXisCentralAfricanRepublicFormatted
82TCD+235XX-XX-XX-XXisChadFormatted
83CHL+56XX-XXXX-XXXXisChileFormatted
84COL+57XXX-XXX-XXXXisColombiaFormatted
85COM+269XX-XX-XXisComorosFormatted
86COG+242XXX-XX-XX-XXXisCongoFormatted
87CRI+506XXX-XXXXisCostaRicaFormatted
88CIV+225XX-XX-XX-XXisIvoryCoastFormatted
89HRV+385XXX-XXX-XXXisCroatiaFormatted
90CUB+53X-XXXXXXisCubaFormatted
91CYP+357XX-XXXXXXisCyprusFormatted
92ECU+593X-XXX-XXXXisEcuadorFormatted
93SLV+503XXXX-XXXXisElSalvadorFormatted
94GNQ+240XXX-XXX-XXXisEquatorialGuineaFormatted
95EST+372XXX XXXXisEstoniaFormatted
96ETH+251XX XXX XXXXisEthiopiaFormatted
97FJI+679X XXXXXXisFijiFormatted
98FIN+358XXX XXX XXXXisFinlandFormatted
99ISL+354XXX XXXXisIcelandFormatted
100IRQ+964XXX XXX XXXXisIraqFormatted
101IRL+353XXX XXX XXXXisIrelandFormatted
102JAM+1(XXX) XXX-XXXXisJamaicaFormatted
103JOR+962X XXX XXXXisJordanFormatted
104KAZ+7XXX-XXX-XXXXisKazakhstanFormatted
105KEN+254XXX-XXX-XXXisKenyaFormatted
106KIR+686XXX-XXXisKiribatiFormatted
107PRK+850XXX-XXX-XXXXisNorthKoreaFormatted
108KWT+965XXXX-XXXXisKuwaitFormatted
109KGZ+996XXX-XXX-XXXisKyrgyzstanFormatted
110LAO+856XX-XXX-XXXisLaosFormatted
111LVA+371XXXX-XXXXisLatviaFormatted
112LBN+961XX-XXX-XXXisLebanonFormatted
113LSO+266XXX-XXXXXisLesothoFormatted
114LBR+231XXX-XXX-XXXXisLiberiaFormatted
115LBY+218XX-XXX-XXXXisLibyaFormatted
116LIE+423XXXXXXXisLiechtensteinFormatted
117LTU+370(X-XXXXX)isLithuaniaFormatted
118LUX+352XXXXXXXisLuxembourgFormatted
119MKD+389XXX-XXX-XXisNorthMacedoniaFormatted
120MDG+261XX-XX-XXX-XXisMadagascarFormatted
121MWI+265XXXXXXXXXisMalawiFormatted
122MYS+60XX-XXXX-XXXXisMalaysiaFormatted
123MDV+960XXXXXXXisMaldivesFormatted
124MLT+356XX-XX-XX-XXisMaltaFormatted
125MHL+692XXX-XXXXisMarshallIslandsFormatted
126MRT+222XX-XX-XX-XXisMauritaniaFormatted
127MUS+230XXXX-XXXXisMauritiusFormatted
128FSM+691XXX-XXXXisMicronesiaFederatedStatesOfFormatted
129MCO+377XX-XX-XX-XX-XXisMonacoFormatted
130MNG+976XX-XX-XXXXisMongoliaFormatted
131MNE+382XX-XXX-XXXisMontenegroFormatted
132MAR+212XX-XX-XX-XX-XXisMoroccoFormatted
133MOZ+258XX-XXX-XXXXisMozambiqueFormatted
134MMR+95XX-XXX-XXXisMyanmarFormatted
135NAM+264XXX-XXXXXXisNamibiaFormatted
136NRU+674XXX-XXXXisNauruFormatted
137NPL+977XXX-XXXXXXXisNepalFormatted
138NLD+31X-XXXXXXisNetherlandsFormatted
139NZL+64XX-XXXX-XXXXisNewZealandFormatted
140NER+227XX-XX-XXXXisNigerFormatted
141NGA+234XXX-XXX-XXXXisNigeriaFormatted
142NIU+683XXXXisNiueFormatted
143NOR+47XXXX-XXXXisNorwayFormatted
144OMN+968XXX-XXXX-XXXXisOmanFormatted
145PAK+92XXX XXXXXXXisPakistanFormatted
146PLW+680XXX-XXXXisPalauFormatted
147PAN+507XXX-XXXX-XXXXisPanamaFormatted
148PNG+675XXXX-XXXisPapuaNewGuineaFormatted
149PRY+595XXX-XXXXXXisParaguayFormatted
150PRT+351XXX XXX XXXisPortugalFormatted
151QAT+974XXX-XXXX-XXXXisQatarFormatted
152ROU+400XX XXX XXXisRomaniaFormatted
153RWA+250XXX XXX XXXXisRwandaFormatted
154WSM+685XX XXXXisSamoaFormatted
155SMR+378XXX XXXXXXisSanMarinoFormatted
156STP+2399XXX-XXXXisSaoTomeAndPrincipeFormatted
157SAU+966XXX XXX XXXXisSaudiArabiaFormatted
158SEN+221XX XXX XX XXisSenegalFormatted
159SRB+381XX XXXXXXXisSerbiaFormatted
160SYC+248X XX XX XXisSeychellesFormatted
161SLE+232(XXX) XXX XXXXisSierraLeoneFormatted
162SGP+65XXXX XXXXisSingaporeFormatted
163SVK+421XXX/XXX XXXisSlovakiaFormatted
164SVN+3860X XXX XX XXisSloveniaFormatted
165SLB+677XXX XXXXisSolomonIslandsFormatted
166SOM+252XX-XXX-XXXXisSomaliaFormatted
167ZAF+27XXX XXX XXXXisSouthAfricaFormatted
168SSD+211XX XXX XXXXisSouthSudanFormatted
169LKA+94XX XXX XXXXisSriLankaFormatted
170SDN+249XXX XXX XXXXisSudanFormatted
171SUR+597XXX-XXXXisSurinameFormatted
172SWZ+268XXX XXXXisEswatiniFormatted
173SWE+46XXX-XXX XX XXisSwedenFormatted
174CHE+410XX XXX XX XXisSwitzerlandFormatted
175TWN+886XXX-XXXXXXisTaiwanFormatted
176TJK+992XXX-XX-XX-XXisTajikistanFormatted
177TZA+255XXX-XXX-XXXXisTanzania,UnitedRepublicOfFormatted
178THA+66XXX-XXXX-XXXXisThailandFormatted
179TGO+228XX-XX-XXXXisTogoFormatted
180TON+676XXX-XXXXisTongaFormatted
181TTO+1868XXX-XXXXisTrinidadAndTobagoFormatted
182TUN+216XX-XXX-XXXisTunisiaFormatted
183TKM+993XX-XX-XX-XX-XXisTurkmenistanFormatted
184TUV+688XXX-XXXXisTuvaluFormatted
185UGA+256XX XXX XXXXisUgandaFormatted
186ARE+971XXXX XXX XXXXisUnitedArabEmiratesFormatted
187URY+598X XXX XXXXisUruguayFormatted
188VCT+1784XXX-XXXXisSaintVincentAndtheGrenadinesFormatted
189LCA+1758XXX-XXXXisSaintLuciaFormatted

Examples

PhoneFormatted PhoneDescription
+1234567890+1 (123) 456-7890USA with prefix
123456789123456789USA with prefix (wrong length)
(123) 456-7890+1 (123) 456-7890USA with prefix
+1 (123) 456-7890+1 (123) 456-7890USA with prefix
1234567890+1 (123) 456-7890USA with prefix
1234567890+1 (123) 456-7890USA with prefix
1234567890(123) 456-7890USA without prefix