11
2- export default function mask ( value , precision , decimalSeparator , thousandSeparator , allowNegative , prefix , suffix ) {
2+ export default function mask ( value , precision = 2 , decimalSeparator = '.' , thousandSeparator = ',' , allowNegative = false , prefix = '' , suffix = '' ) {
33 // provide some default values and arg validation.
4- if ( decimalSeparator === undefined ) { decimalSeparator = "." ; } // default to '.' as decimal separator
5- if ( thousandSeparator === undefined ) { thousandSeparator = "," ; } // default to ',' as thousand separator
6- if ( allowNegative === undefined ) { allowNegative = false ; } // default to not allowing negative numbers
7- if ( precision === undefined ) { precision = 2 ; } // by default, 2 decimal places
8- if ( precision < 0 ) { precision = 0 ; } // precision cannot be negative.
9- if ( precision > 20 ) { precision = 20 ; } // precision cannot greater than 20
10- if ( prefix === undefined ) { prefix = '' ; } // default prefix to empty string
11- if ( suffix === undefined ) { suffix = '' ; } // default suffix to empty string
4+ if ( precision < 0 ) { precision = 0 ; } // precision cannot be negative
5+ if ( precision > 20 ) { precision = 20 ; } // precision cannot be greater than 20
6+ value = String ( value ) ; //if the given value is a Number, let's convert into String to manipulate that
127
8+ // extract digits. if no digits, fill in a zero.
9+ let digits = value . match ( / \d / g) || [ '0' ] ;
10+
1311 let numberIsNegative = false ;
1412 if ( allowNegative ) {
1513 let negativeSignCount = ( value . match ( / - / g) || [ ] ) . length ;
1614 // number will be negative if we have an odd number of "-"
1715 // ideally, we should only ever have 0, 1 or 2 (positive number, making a number negative
1816 // and making a negative number positive, respectively)
1917 numberIsNegative = negativeSignCount % 2 === 1 ;
20- }
21-
22- // extract digits. if no digits, fill in a zero.
23- let digits = value . match ( / \d / g) || [ '0' ] ;
24-
25- if ( allowNegative ) {
26- // if every digit in the array is '0', then the number should
27- // never be negative
18+
19+ // if every digit in the array is '0', then the number should never be negative
2820 let allDigitsAreZero = true ;
29- for ( let idx = 0 ; idx < digits . length ; idx += 1 ) {
21+ for ( let idx = 0 ; idx < digits . length ; idx += 1 ) {
3022 if ( digits [ idx ] !== '0' ) {
3123 allDigitsAreZero = false ;
3224 break ;
3325 }
3426 }
35- if ( allDigitsAreZero ) {
27+ if ( allDigitsAreZero ) {
3628 numberIsNegative = false ;
3729 }
3830 }
3931
4032 // zero-pad a input
41- while ( digits . length <= precision ) { digits . unshift ( '0' ) }
33+ while ( digits . length <= precision ) { digits . unshift ( '0' ) ; }
4234
43- if ( precision > 0 ) {
35+ if ( precision > 0 ) {
4436 // add the decimal separator
4537 digits . splice ( digits . length - precision , 0 , "." ) ;
4638 }
@@ -53,19 +45,19 @@ export default function mask(value, precision, decimalSeparator, thousandSeparat
5345 if ( precision > 0 ) {
5446 // set the final decimal separator
5547 digits [ decimalpos ] = decimalSeparator ;
56- } else {
48+ } else {
5749 // when precision is 0, there is no decimal separator.
58- decimalpos = digits . length
50+ decimalpos = digits . length ;
5951 }
6052
6153 // add in any thousand separators
62- for ( let x = decimalpos - 3 ; x > 0 ; x = x - 3 ) {
54+ for ( let x = decimalpos - 3 ; x > 0 ; x = x - 3 ) {
6355 digits . splice ( x , 0 , thousandSeparator ) ;
6456 }
6557
6658 // if we have a prefix or suffix, add them in.
67- if ( prefix . length > 0 ) { digits . unshift ( prefix ) ; }
68- if ( suffix . length > 0 ) { digits . push ( suffix ) ; }
59+ if ( prefix . length > 0 ) { digits . unshift ( prefix ) ; }
60+ if ( suffix . length > 0 ) { digits . push ( suffix ) ; }
6961
7062 // if the number is negative, insert a "-" to
7163 // the front of the array
0 commit comments