Skip to content

Commit

Permalink
fix(as-you-type): reset() with args now clearing number() properly
Browse files Browse the repository at this point in the history
fix #64
  • Loading branch information
grantila committed Sep 25, 2021
1 parent ec7c628 commit aa9861c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/index.js

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

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ AsYouType.prototype.reset = function( number /* = '' */ )
{
this._aytf.clear( );
this._rawInput = '';
this._number = '';
if ( number )
for ( var i = 0, n = number.length; i < n; ++i )
this.addChar( number.charAt( i ) );
Expand Down
121 changes: 75 additions & 46 deletions test.in/awesome-phonenumber/should-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,6 @@ describe( 'general', function( ) {
expect( pn3.isFixedLine( ) ).to.be.true;
} );

it( 'should be able to format as-you-type', 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' );

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

expect( ayt.addChar( '3' ) ).to.equal( '070-712 3' );
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 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 Expand Up @@ -152,6 +106,81 @@ describe( 'general', function( ) {
} );


describe( 'as-you-type', function( ) {
it( 'should be able to format as-you-type', 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' );

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

expect( ayt.addChar( '3' ) ).to.equal( '070-712 3' );
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 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 reset properly with new number', 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' );

ayt.reset( '070' );
expect( ayt.number( ) ).to.equal( '070' );
} );

it( 'should reset properly without new number', 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' );

ayt.reset( );
expect( ayt.number( ) ).to.equal( '' );
} );
} );


describe( 'instance', function( ) {
it( 'constructed should be instanceof PhoneNumber', function( ) {
var pn = new PhoneNumber( '+12' );
Expand Down

0 comments on commit aa9861c

Please sign in to comment.