@@ -4,91 +4,107 @@ import options from './options'
44 * Number format function
55 * @param {Object } options
66 */
7- export default function NumberFormat ( opt = options ) {
8- this . options = Object . assign ( options , opt )
7+ export default function NumberFormat ( config = options ) {
8+ this . options = Object . assign ( options , config )
99 this . input = ''
1010 this . number = ''
1111 this . isClean = false
12+
1213 this . isNull = ( input = this . input ) => ! this . numberOnly ( input , new RegExp ( '[^0-9]+' , 'gi' ) )
14+
1315 this . clean = ( clean = false ) => {
1416 this . isClean = clean
1517 return this
1618 }
19+
1720 this . sign = ( ) => {
1821 const sign = ( this . input . toString ( ) . indexOf ( '-' ) >= 0 && this . realNumber ( ) > 0 ) ? '-' : ''
1922 return sign
2023 }
24+
2125 function between ( min , n , max ) {
2226 return Math . max ( min , Math . min ( n , max ) )
2327 }
28+
2429 // Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed
2530 function fixed ( precision ) {
2631 return between ( 0 , precision , 20 )
2732 }
33+
2834 function toFixed ( numbers , precision ) {
2935 // eslint-disable-next-line no-restricted-properties
3036 var exp = Math . pow ( 10 , precision )
3137 var float = parseFloat ( numbers ) / exp
3238 return float . toFixed ( fixed ( precision ) )
3339 }
40+
3441 this . toNumber = ( string ) => Number ( string )
42+
3543 this . numberOnly = ( string , regExp ) => string . toString ( ) . replace ( regExp , '' )
44+
3645 this . isNegative = this . sign ( ) === '-'
46+
3747 this . numbers = ( ) => {
3848 if ( this . options . reverseFill ) {
3949 this . number = toFixed ( this . numberOnly ( this . input , / \D + / g) , this . options . precision ) . replace ( '.' , this . options . decimal )
4050 } else if ( typeof this . input === 'number' ) {
41- this . number = this . toNumber ( this . input . toFixed ( this . options . precision ) ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
42- // eslint-disable-next-line no-restricted-globals
43- } else if ( ! isNaN ( this . toNumber ( this . input ) ) ) {
44- this . number = this . input . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
51+ if ( this . isClean ) {
52+ this . number = this . toNumber ( this . input . toFixed ( this . options . precision ) ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
53+ } else {
54+ this . number = this . toNumber ( this . input ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
55+ }
4556 } else {
4657 this . number = this . numberOnly ( this . input , new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' ) )
4758 this . number = this . parts ( this . number ) . join ( this . options . decimal )
4859 }
4960 return this . number
5061 }
62+
5163 this . realNumber = ( ) => this . toNumber ( this . numbers ( ) . toString ( ) . replace ( this . options . decimal , '.' ) )
64+
5265 this . parts = ( number = '' , decimal = this . options . decimal ) => {
5366 var parts = number . toString ( ) . split ( decimal )
5467 parts [ 0 ] = this . toNumber ( parts [ 0 ] ) || 0
5568 if ( parts . length > 1 ) {
5669 parts [ 1 ] = parts . slice ( 1 , parts . length ) . join ( '' )
5770 parts = parts . slice ( 0 , 2 )
58- if ( parts [ 1 ] . length > this . options . precision ) {
71+ if ( this . isClean && parts [ 1 ] . length > this . options . precision ) {
5972 parts [ 1 ] = this . toNumber ( `.${ parts [ 1 ] } ` ) . toFixed ( this . options . precision ) . toString ( ) . replace ( '0.' , '' )
6073 }
6174 }
6275 return parts . slice ( 0 , 2 )
6376 }
77+
6478 this . addSeparator = ( ) => {
6579 var parts = this . numbers ( ) . split ( this . options . decimal )
6680 parts [ 0 ] = parts [ 0 ] . toString ( ) . replace ( / ( \d ) (? = (?: \d { 3 } ) + \b ) / gm, `$1${ this . options . separator } ` )
6781 if ( this . isClean ) {
6882 parts [ 1 ] = this . toNumber ( `.${ parts [ 1 ] } ` ) . toString ( ) . replace ( '0.' , '' )
69- return parts [ 1 ] && parts [ 1 ] > 0 ? parts . join ( this . options . decimal ) : parts [ 0 ]
83+ return parts [ 1 ] && parts [ 1 ] >= 0 ? parts . join ( this . options . decimal ) : parts [ 0 ]
7084 }
7185 return parts . join ( this . options . decimal )
7286 }
87+
7388 /**
7489 * Format the input with default config if there is no constructor config
7590 * @param {Number, String } input
7691 * @return {String }
7792 */
7893 this . format = ( input ) => {
7994 if ( input === '' ) return this . options . null_value
80- this . input = input
95+ this . input = input || this . options . null_value
8196 if ( this . isNull ( ) ) return this . options . null_value
8297 return this . sign ( ) + this . options . prefix + this . addSeparator ( ) + this . options . suffix
8398 }
99+
84100 /**
85101 * Unformat the input with default config if there is no constructor config
86102 * @param {Number, String } input
87103 * @return {String }
88104 */
89105 this . unformat = ( input ) => {
90106 if ( input === '' ) return this . options . null_value
91- this . input = input
107+ this . input = input || this . options . null_value
92108 if ( this . isNull ( ) ) return this . options . null_value
93109 return this . toNumber ( this . sign ( ) + this . realNumber ( ) )
94110 }
0 commit comments