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
126
137 if ( value === null ) {
14- return {
15- value : '' ,
16- maskedValue : ''
17- } ;
18- }
19-
8+ return {
9+ value : '' ,
10+ maskedValue : ''
11+ } ;
12+ }
13+
14+ value = String ( value ) ; //if the given value is a Number, let's convert into String to manipulate that
15+
16+ // extract digits. if no digits, fill in a zero.
17+ let digits = value . match ( / \d / g) || [ '0' ] ;
18+
2019 let numberIsNegative = false ;
2120 if ( allowNegative ) {
2221 let negativeSignCount = ( value . match ( / - / g) || [ ] ) . length ;
2322 // number will be negative if we have an odd number of "-"
2423 // ideally, we should only ever have 0, 1 or 2 (positive number, making a number negative
2524 // and making a negative number positive, respectively)
2625 numberIsNegative = negativeSignCount % 2 === 1 ;
27- }
28-
29- // extract digits. if no digits, fill in a zero.
30- let digits = value . match ( / \d / g) || [ '0' ] ;
31-
32- if ( allowNegative ) {
33- // if every digit in the array is '0', then the number should
34- // never be negative
26+
27+ // if every digit in the array is '0', then the number should never be negative
3528 let allDigitsAreZero = true ;
36- for ( let idx = 0 ; idx < digits . length ; idx += 1 ) {
29+ for ( let idx = 0 ; idx < digits . length ; idx += 1 ) {
3730 if ( digits [ idx ] !== '0' ) {
3831 allDigitsAreZero = false ;
3932 break ;
4033 }
4134 }
42- if ( allDigitsAreZero ) {
35+ if ( allDigitsAreZero ) {
4336 numberIsNegative = false ;
4437 }
4538 }
4639
4740 // zero-pad a input
48- while ( digits . length <= precision ) { digits . unshift ( '0' ) }
41+ while ( digits . length <= precision ) { digits . unshift ( '0' ) ; }
4942
50- if ( precision > 0 ) {
43+ if ( precision > 0 ) {
5144 // add the decimal separator
5245 digits . splice ( digits . length - precision , 0 , "." ) ;
5346 }
@@ -60,19 +53,19 @@ export default function mask(value, precision, decimalSeparator, thousandSeparat
6053 if ( precision > 0 ) {
6154 // set the final decimal separator
6255 digits [ decimalpos ] = decimalSeparator ;
63- } else {
56+ } else {
6457 // when precision is 0, there is no decimal separator.
65- decimalpos = digits . length
58+ decimalpos = digits . length ;
6659 }
6760
6861 // add in any thousand separators
69- for ( let x = decimalpos - 3 ; x > 0 ; x = x - 3 ) {
62+ for ( let x = decimalpos - 3 ; x > 0 ; x = x - 3 ) {
7063 digits . splice ( x , 0 , thousandSeparator ) ;
7164 }
7265
7366 // if we have a prefix or suffix, add them in.
74- if ( prefix . length > 0 ) { digits . unshift ( prefix ) ; }
75- if ( suffix . length > 0 ) { digits . push ( suffix ) ; }
67+ if ( prefix . length > 0 ) { digits . unshift ( prefix ) ; }
68+ if ( suffix . length > 0 ) { digits . push ( suffix ) ; }
7669
7770 // if the number is negative, insert a "-" to
7871 // the front of the array
0 commit comments