Skip to content

Commit

Permalink
Phone Input: Fix toIcannFormat to calculate dial codes for countries …
Browse files Browse the repository at this point in the history
…with +1 dial code and a region code (#10759)
  • Loading branch information
umurkontaci committed Jan 19, 2017
1 parent 64b3f64 commit dbc1189
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
17 changes: 17 additions & 0 deletions bin/build-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ function removeAllNumberKeys( obj ) {
return _.omitBy( obj, function( val, key ) { return /^\d+$/.test( key ); } );
}

function removeRegionCodeAndCountryDialCodeIfSameWithCountryDialCode( countryData ) {
for ( var key in countryData ) {
if ( countryData.hasOwnProperty( key ) ) {
const country = countryData[ key ],
{ countryDialCode, dialCode } = country;
if ( countryDialCode === dialCode ) {
delete country.regionCode;
delete country.countryDialCode;
}
}
}
return countryData;
}

/**
* Processes Google's libphonenumber data and generates a proper JS object
* @param {{}} libPhoneNumberData
Expand All @@ -228,6 +242,8 @@ function processLibPhoneNumberMetadata( libPhoneNumberData ) {
data[ countryCodeUpper ] = {
isoCode: countryCodeUpper,
dialCode: String( country[ libPhoneNumberIndexes.COUNTRY_DIAL_CODE ] + ( country[ libPhoneNumberIndexes.REGION_AREA_CODE ] || '' ) ),
countryDialCode: String( country[ libPhoneNumberIndexes.COUNTRY_DIAL_CODE ] ),
regionCode: country[ libPhoneNumberIndexes.REGION_AREA_CODE ] || '',
areaCodes: areaCodes[ countryCode ],
nationalPrefix: country[ libPhoneNumberIndexes.NATIONAL_PREFIX ],
patterns: ( country[ libPhoneNumberIndexes.NUMBER_FORMAT ] || [] ).map( processNumberFormat ),
Expand Down Expand Up @@ -313,6 +329,7 @@ getLibPhoneNumberData()
.then( generateDeepRemoveEmptyArraysFromObject( [ 'patterns', 'internationalPatterns' ] ) )
.then( insertCountryAliases )
.then( removeAllNumberKeys )
.then( removeRegionCodeAndCountryDialCodeIfSameWithCountryDialCode )
.then( generateFullDataset )
.then( deepRemoveUndefinedKeysFromObject )
.then( convertToJSStringAndVerify )
Expand Down
52 changes: 52 additions & 0 deletions client/components/phone-input/data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions client/components/phone-input/phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ export function toE164( inputNumber, country ) {
}

export function toIcannFormat( inputNumber, country ) {
const { nationalNumber } = processNumber( inputNumber, country );
return '+' + country.dialCode + '.' + nationalNumber;
const { nationalNumber } = processNumber( inputNumber, country ),
countryCode = country.countryDialCode || country.dialCode,
dialCode = country.countryDialCode && country.regionCode ? country.regionCode : '';
return '+' + countryCode + '.' + dialCode + nationalNumber;
}
17 changes: 17 additions & 0 deletions client/components/phone-input/test/test-phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
formatNumber,
makeTemplate,
findPattern,
toIcannFormat,
DIGIT_PLACEHOLDER,
applyTemplate,
toE164
Expand Down Expand Up @@ -230,5 +231,21 @@ describe( 'metadata:', () => {
} );
} );
} );


describe( 'toIcannFormat', () => {
it( 'should be able to handle NANPA', () => {
equal( toIcannFormat( '14256559999', countries.US ), '+1.4256559999' );
equal( toIcannFormat( '4256559999', countries.US ), '+1.4256559999' );
} );
it( 'should be able to handle Europe', () => {
equal( toIcannFormat( '05325556677', countries.TR ), '+90.5325556677' );
equal( toIcannFormat( '01234567890', countries.GB ), '+44.1234567890' );
equal( toIcannFormat( '012345678', countries.IT ), '+39.012345678' );
} );
it( 'should separate country codes properly for countries with +1 and a separate leading digit', () => {
equal( toIcannFormat( '+18686559999', countries.TT ), '+1.8686559999' );
} );
} );
} );

0 comments on commit dbc1189

Please sign in to comment.