@@ -9,31 +9,33 @@ export default function NumberFormat(opt = options) {
99 this . input = this . options . null_value
1010 this . number = this . options . null_value
1111 this . isClean = false
12- this . clean = ( ) => {
13- this . isClean = true
12+ this . clean = ( clean = false ) => {
13+ this . isClean = clean
1414 return this
1515 }
16- this . negative = ( ) => {
17- const negetive = ( this . input . toString ( ) . indexOf ( '-' ) >= 0 && this . realNumber ( ) > 0 ) ? '-' : ''
18- return negetive
16+ this . sign = ( ) => {
17+ const sign = ( this . input . toString ( ) . indexOf ( '-' ) >= 0 && this . realNumber ( ) > 0 ) ? '-' : ''
18+ return sign
1919 }
20+ this . toNumber = ( string ) => Number ( string )
21+ this . isNegative = this . sign ( ) === '-'
2022 this . numbers = ( ) => {
2123 if ( typeof this . input === 'number' ) {
2224 this . number = this . input . toFixed ( this . options . precision ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
25+ // eslint-disable-next-line no-restricted-globals
26+ } else if ( ! isNaN ( this . toNumber ( this . input ) ) ) {
27+ this . number = this . input . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
2328 } else {
24- this . number = this . numberOnly ( )
29+ const regExp = new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' )
30+ this . number = this . input . toString ( ) . replace ( regExp , '' )
31+ this . number = this . parts ( this . number ) . join ( this . options . decimal )
2532 }
2633 return this . number
2734 }
28- this . numberOnly = ( ) => {
29- const regExp = new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' )
30- this . number = this . input . toString ( ) . replace ( regExp , '' )
31- return this . parts ( this . number ) . join ( this . options . decimal )
32- }
33- this . realNumber = ( ) => Number ( this . numbers ( ) . toString ( ) . replace ( this . options . decimal , '.' ) )
35+ this . realNumber = ( ) => this . toNumber ( this . numbers ( ) . toString ( ) . replace ( this . options . decimal , '.' ) )
3436 this . parts = ( number = '' , decimal = this . options . decimal ) => {
3537 var parts = number . toString ( ) . split ( decimal )
36- parts [ 0 ] = ( Number ( parts [ 0 ] ) ? Number ( parts [ 0 ] ) : 0 )
38+ parts [ 0 ] = this . toNumber ( parts [ 0 ] ) || 0
3739 if ( parts . length > 1 ) {
3840 parts [ 1 ] = parts . slice ( 1 , parts . length ) . join ( '' )
3941 parts = parts . slice ( 0 , 2 )
@@ -47,19 +49,29 @@ export default function NumberFormat(opt = options) {
4749 var parts = this . numbers ( ) . split ( this . options . decimal )
4850 parts [ 0 ] = parts [ 0 ] . toString ( ) . replace ( / ( \d ) (? = (?: \d { 3 } ) + \b ) / gm, `$1${ this . options . separator } ` )
4951 if ( this . isClean ) {
50- parts [ 1 ] = Number ( `.${ parts [ 1 ] } ` ) . toString ( ) . replace ( '0.' , '' )
52+ parts [ 1 ] = this . toNumber ( `.${ parts [ 1 ] } ` ) . toString ( ) . replace ( '0.' , '' )
5153 return parts [ 1 ] && parts [ 1 ] > 0 ? parts . join ( this . options . decimal ) : parts [ 0 ]
5254 }
5355 return parts . join ( this . options . decimal )
5456 }
57+ /**
58+ * Format the input with default config if there is no constructor config
59+ * @param {Number, String } input
60+ * @return {String }
61+ */
5562 this . format = ( input ) => {
5663 if ( input === '' ) return this . options . null_value
5764 this . input = input
58- return this . negative ( ) + this . options . prefix + this . addSeparator ( ) + this . options . suffix
65+ return this . sign ( ) + this . options . prefix + this . addSeparator ( ) + this . options . suffix
5966 }
67+ /**
68+ * Unformat the input with default config if there is no constructor config
69+ * @param {Number, String } input
70+ * @return {String }
71+ */
6072 this . unformat = ( input ) => {
6173 if ( input === '' ) return this . options . null_value
6274 this . input = input
63- return this . negative ( ) + this . realNumber ( )
75+ return this . sign ( ) + this . realNumber ( )
6476 }
6577}
0 commit comments