@@ -57,6 +57,36 @@ function getCurrencyDividerAndDecimal(divider: CurrencyDivider | undefined) {
5757 } ;
5858}
5959
60+ /** removes decimal dot and comma from a string */
61+ function removeDividerAndDecimal ( value : string ) : string {
62+ return value . replace ( / [ , . ] / g, '' ) ;
63+ }
64+
65+ function iDontYetKnowWhatsGoingOnHere (
66+ value : string ,
67+ divider : CurrencyDivider ,
68+ ) : string {
69+ if ( value . length > 3 ) {
70+ let arr : string [ ] = [ ] ;
71+ for ( let i = 0 ; i < value . length ; i += 3 ) {
72+ arr . push (
73+ value
74+ . split ( '' )
75+ . splice ( value . length - i , 3 )
76+ . join ( '' ) ,
77+ ) ;
78+ }
79+
80+ arr = arr . reverse ( ) ;
81+ arr . pop ( ) ;
82+ let initial = arr . join ( '' ) ;
83+ if ( value . includes ( initial ) ) {
84+ value = value . replace ( initial , '' ) ;
85+ }
86+ return ( value = value + divider + arr . join ( divider ) ) ;
87+ } else return value ;
88+ }
89+
6090export function getValueFromCurrencyMask ( {
6191 value,
6292 newValue,
@@ -68,29 +98,12 @@ export function getValueFromCurrencyMask({
6898 if ( value . length >= newValue . length ) return undefined ;
6999
70100 if ( newValue . includes ( decimal ) ) {
71- let intVal = newValue . split ( decimal ) [ 0 ] . replace ( / [ , . ] / g, '' ) ;
72- let decimalValue = newValue . split ( decimal ) [ 1 ] ;
73- if ( intVal . length > 3 ) {
74- let arr : string [ ] = [ ] ;
75- for ( let i = 0 ; i < intVal . length ; i += 3 ) {
76- arr . push (
77- intVal
78- . split ( '' )
79- . splice ( intVal . length - i , 3 )
80- . join ( '' ) ,
81- ) ;
82- }
83-
84- arr = arr . reverse ( ) ;
85- arr . pop ( ) ;
86- let initial = arr . join ( '' ) ;
87- if ( intVal . includes ( initial ) ) {
88- intVal = intVal . replace ( initial , '' ) ;
89- }
90- intVal = intVal + divider + arr . join ( divider ) ;
91- }
101+ let intVal = removeDividerAndDecimal ( newValue . split ( decimal ) [ 0 ] ) ;
102+ let fractionalVal = newValue . split ( decimal ) [ 1 ] ;
103+
104+ intVal = iDontYetKnowWhatsGoingOnHere ( intVal , divider ) ;
92105
93- newValue = intVal + decimal + decimalValue ;
106+ newValue = intVal + decimal + fractionalVal ;
94107
95108 let decimalPlaces : number =
96109 maxDecimalPlaces !== undefined ? maxDecimalPlaces : 2 ;
@@ -101,30 +114,16 @@ export function getValueFromCurrencyMask({
101114 newValue . split ( decimal ) [ 1 ] . length > value . split ( decimal ) [ 1 ] . length &&
102115 value . split ( decimal ) [ 1 ] . length === decimalPlaces
103116 ) {
104- return '' ;
117+ return undefined ;
105118 }
106119 if ( newValue . split ( decimal ) [ 1 ] . length > decimalPlaces ) {
107120 newValue = newValue . slice ( 0 , newValue . length - 1 ) ;
108121 }
109122 } else if ( newValue . length > 3 ) {
110123 let arr : string [ ] = [ ] ;
111- let unmasked = newValue . replace ( / [ , . ] / g, '' ) ;
112- for ( let i = 0 ; i < unmasked . length ; i += 3 ) {
113- arr . push (
114- unmasked
115- . split ( '' )
116- . splice ( unmasked . length - i , 3 )
117- . join ( '' ) ,
118- ) ;
119- }
124+ let unmasked = removeDividerAndDecimal ( newValue ) ;
120125
121- arr = arr . reverse ( ) ;
122- arr . pop ( ) ;
123- let initial = arr . join ( '' ) ;
124- if ( unmasked . includes ( initial ) ) {
125- unmasked = unmasked . replace ( initial , '' ) ;
126- }
127- newValue = unmasked + divider + arr . join ( divider ) ;
126+ newValue = iDontYetKnowWhatsGoingOnHere ( unmasked , divider ) ;
128127 }
129128 return newValue ;
130129}
0 commit comments