@@ -50,7 +50,30 @@ export class MaskApplierService {
5050 this . customPattern = customPattern ;
5151 return this . applyMask ( inputValue , mask ) ;
5252 }
53- public applyMask ( inputValue : string , maskExpression : string , position : number = 0 , cb : Function = ( ) => { } ) : string {
53+
54+ private checkAndRemoveSuffix = ( inputValue : string ) : string => {
55+ if ( ! this . suffix ) {
56+ return inputValue ;
57+ }
58+ for ( let i = this . suffix . length - 1 ; i >= 0 ; i -- ) {
59+ const substr = this . suffix . substr ( i , this . suffix . length ) ;
60+ if (
61+ inputValue . includes ( substr ) &&
62+ ( i - 1 < 0 || ! inputValue . includes ( this . suffix . substr ( i - 1 , this . suffix . length ) ) )
63+ ) {
64+ return inputValue . replace ( substr , '' ) ;
65+ }
66+ }
67+ return inputValue ;
68+ } ;
69+
70+ public applyMask (
71+ inputValue : string ,
72+ maskExpression : string ,
73+ position : number = 0 ,
74+ backspaced : boolean = false ,
75+ cb : Function = ( ) => { } ,
76+ ) : string {
5477 if ( inputValue === undefined || inputValue === null || maskExpression === undefined ) {
5578 return '' ;
5679 }
@@ -63,8 +86,8 @@ export class MaskApplierService {
6386 if ( inputValue . slice ( 0 , this . prefix . length ) === this . prefix ) {
6487 inputValue = inputValue . slice ( this . prefix . length , inputValue . length ) ;
6588 }
66- if ( ! ! this . suffix && inputValue . endsWith ( this . suffix ) ) {
67- inputValue = inputValue . slice ( 0 , inputValue . length - this . suffix . length ) ;
89+ if ( ! ! this . suffix && inputValue && inputValue . length > 0 ) {
90+ inputValue = this . checkAndRemoveSuffix ( inputValue ) ;
6891 }
6992 const inputArray : string [ ] = inputValue . toString ( ) . split ( '' ) ;
7093 if ( maskExpression === 'IP' ) {
@@ -330,7 +353,11 @@ export class MaskApplierService {
330353 if ( shift < 0 ) {
331354 this . _shift . clear ( ) ;
332355 }
333- let res = `${ this . prefix } ${ result } ${ this . suffix } ` ;
356+ let onlySpecial = false ;
357+ if ( backspaced ) {
358+ onlySpecial = inputArray . every ( ( char ) => this . maskSpecialCharacters . includes ( char ) ) ;
359+ }
360+ let res = `${ this . prefix } ${ onlySpecial ? '' : result } ${ this . suffix } ` ;
334361 if ( result . length === 0 ) {
335362 res = `${ this . prefix } ${ result } ` ;
336363 }
@@ -401,8 +428,10 @@ export class MaskApplierService {
401428
402429 const precisionMatch : RegExpMatchArray | null = inputValue . match ( precisionRegEx ) ;
403430 if ( precisionMatch && precisionMatch [ 0 ] . length - 1 > precision ) {
404- inputValue = inputValue . substring ( 0 , inputValue . length - 1 ) ;
405- } else if ( precision === 0 && inputValue . endsWith ( decimalMarker ) ) {
431+ const diff = precisionMatch [ 0 ] . length - 1 - precision ;
432+ inputValue = inputValue . substring ( 0 , inputValue . length - diff ) ;
433+ }
434+ if ( precision === 0 && inputValue . endsWith ( decimalMarker ) ) {
406435 inputValue = inputValue . substring ( 0 , inputValue . length - 1 ) ;
407436 }
408437 }
@@ -413,14 +442,17 @@ export class MaskApplierService {
413442 return str
414443 . split ( '' )
415444 . filter ( ( i : string , idx : number ) => {
416- return i . match ( '^-?\\d' ) || i === '.' || i === ',' || ( i === '-' && idx === 0 ) ;
445+ return i . match ( '^-?\\d' ) || i . match ( '\\s' ) || i === '.' || i === ',' || ( i === '-' && idx === 0 ) ;
417446 } )
418447 . join ( '' ) ;
419448 }
420449
421450 private _charToRegExpExpression ( char : string ) : string {
422- const charsToEscape = '[\\^$.|?*+()' ;
423- return char === ' ' ? '\\s' : charsToEscape . indexOf ( char ) >= 0 ? '\\' + char : char ;
451+ if ( char ) {
452+ const charsToEscape = '[\\^$.|?*+()' ;
453+ return char === ' ' ? '\\s' : charsToEscape . indexOf ( char ) >= 0 ? '\\' + char : char ;
454+ }
455+ return char ;
424456 }
425457 // tslint:disable-next-line:max-file-line-count
426458}
0 commit comments