Skip to content

Commit

Permalink
fix(as-you-type): fixed removeChar not handling e.g. spaces properly
Browse files Browse the repository at this point in the history
Fixes #49
  • Loading branch information
grantila committed Jun 22, 2020
1 parent e34f488 commit 80bd761
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/index.js

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

10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ function AsYouType( regionCode )
{
this._regionCode = regionCode;
this._aytf = new i18n.phonenumbers.AsYouTypeFormatter( regionCode );
this._rawInput = '';
this._number = '';
}

AsYouType.prototype.addChar = function( nextChar )
{
this._rawInput += nextChar;
this._number = this._aytf.inputDigit( nextChar );
return this._number;
}
Expand All @@ -351,16 +353,16 @@ AsYouType.prototype.number = function( )

AsYouType.prototype.removeChar = function( )
{
var number = this._number;
if ( number.length > 0 )
this.reset( number.substr( 0, number.length - 1 ) );
if ( this._rawInput === '' )
return this._number;

return this._number;
return this.reset( this._rawInput.slice( 0, this._rawInput.length - 1 ) );
}

AsYouType.prototype.reset = function( number /* = '' */ )
{
this._aytf.clear( );
this._rawInput = '';
if ( number )
for ( var i = 0, n = number.length; i < n; ++i )
this.addChar( number.charAt( i ) );
Expand Down
24 changes: 24 additions & 0 deletions test.in/awesome-phonenumber/should-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ describe( 'general', function( ) {
expect( pn2.isPossible( ) ).to.be.true;
} );

it( 'should be able to format as-you-type with removeChar', function( ) {
var ayt = PhoneNumber.getAsYouType( 'SE' );
expect( ayt.addChar( '0' ) ).to.equal( '0' );
expect( ayt.addChar( '7' ) ).to.equal( '07' );
expect( ayt.addChar( '0' ) ).to.equal( '070' );
expect( ayt.addChar( '7' ) ).to.equal( '070-7' );
expect( ayt.addChar( '1' ) ).to.equal( '070-71' );
expect( ayt.addChar( '2' ) ).to.equal( '070-712' );
expect( ayt.addChar( '3' ) ).to.equal( '070-712 3' );
expect( ayt.removeChar( ) ).to.equal( '070-712' );
expect( ayt.addChar( '3' ) ).to.equal( '070-712 3' );

var pn1 = ayt.getPhoneNumber( );
expect( pn1.isValid( ) ).to.be.false;

expect( ayt.addChar( '4' ) ).to.equal( '070-712 34' );
expect( ayt.addChar( '5' ) ).to.equal( '070-712 34 5' );
expect( ayt.addChar( '6' ) ).to.equal( '070-712 34 56' );

var pn2 = ayt.getPhoneNumber( );
expect( pn2.isValid( ) ).to.be.true;
expect( pn2.isPossible( ) ).to.be.true;
} );

it( 'should be able to convert country code <-> region code', function( ) {
expect( PhoneNumber.getCountryCodeForRegionCode( 'SE' ) ).to.equal( 46 );
expect( PhoneNumber.getRegionCodeForCountryCode( 46 ) ).to.equal( 'SE' );
Expand Down

0 comments on commit 80bd761

Please sign in to comment.