@@ -9,7 +9,7 @@ export default function NumberFormat(opt = options) {
99 this . input = ''
1010 this . number = ''
1111 this . isClean = false
12- this . isNull = ( input = this . input ) => ! input . toString ( ) . replace ( new RegExp ( '[^0-9]+' , 'gi' ) , '' )
12+ this . isNull = ( input = this . input ) => ! this . numberOnly ( input , new RegExp ( '[^0-9]+' , 'gi' ) )
1313 this . clean = ( clean = false ) => {
1414 this . isClean = clean
1515 return this
@@ -18,16 +18,32 @@ export default function NumberFormat(opt = options) {
1818 const sign = ( this . input . toString ( ) . indexOf ( '-' ) >= 0 && this . realNumber ( ) > 0 ) ? '-' : ''
1919 return sign
2020 }
21+ function between ( min , n , max ) {
22+ return Math . max ( min , Math . min ( n , max ) )
23+ }
24+ // Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
25+ function fixed ( precision ) {
26+ return between ( 0 , precision , 20 )
27+ }
28+ function toFixed ( numbers , precision ) {
29+ // eslint-disable-next-line no-restricted-properties
30+ var exp = Math . pow ( 10 , precision )
31+ var float = parseFloat ( numbers ) / exp
32+ return float . toFixed ( fixed ( precision ) )
33+ }
2134 this . toNumber = ( string ) => Number ( string )
35+ this . numberOnly = ( string , regExp ) => string . toString ( ) . replace ( regExp , '' )
2236 this . isNegative = this . sign ( ) === '-'
2337 this . numbers = ( ) => {
24- if ( typeof this . input === 'number' ) {
38+ if ( this . options . reverseFill ) {
39+ this . number = toFixed ( this . numberOnly ( this . input , / \D + / g) , this . options . precision ) . replace ( '.' , this . options . decimal )
40+ } else if ( typeof this . input === 'number' ) {
2541 this . number = this . toNumber ( this . input . toFixed ( this . options . precision ) ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
2642 // eslint-disable-next-line no-restricted-globals
2743 } else if ( ! isNaN ( this . toNumber ( this . input ) ) ) {
2844 this . number = this . input . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
2945 } else {
30- this . number = this . input . toString ( ) . replace ( new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' ) , '' )
46+ this . number = this . numberOnly ( this . input , new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' ) )
3147 this . number = this . parts ( this . number ) . join ( this . options . decimal )
3248 }
3349 return this . number
0 commit comments