@@ -4,6 +4,10 @@ module.exports = class Core {
4
4
this . args = new Args ( args ) ;
5
5
}
6
6
7
+ isFloat ( number ) {
8
+ return number % 1 !== 0 ;
9
+ }
10
+
7
11
completer ( size = 1 ) {
8
12
return this . args . fixed ? '' . padEnd ( size , '0' ) : '' . padEnd ( size , '_' ) ;
9
13
}
@@ -13,113 +17,83 @@ module.exports = class Core {
13
17
}
14
18
15
19
onlyNumber ( value ) {
20
+ const hasDecimalSeparator = value . toString ( ) . indexOf ( this . args . decimalSeparator ) ;
21
+ let putDecimalSeparator = false ;
16
22
let retorno = '' ;
17
23
18
- for ( let i = 0 ; i < value . length ; i ++ ) {
19
- if ( isFinite ( value [ i ] ) ) retorno += value [ i ] ;
24
+ for ( let i = value . length - 1 ; i >= 0 ; i -- ) {
25
+ if ( isFinite ( value [ i ] ) ) {
26
+ retorno = value [ i ] + retorno ;
27
+ } else if ( hasDecimalSeparator !== - 1 && ! putDecimalSeparator && value [ i ] === this . args . decimalSeparator ) {
28
+ retorno = value [ i ] . replace ( this . args . decimalSeparator , '.' ) + retorno ;
29
+ putDecimalSeparator = true ;
30
+ }
20
31
}
21
32
22
- return retorno ;
33
+ return retorno [ 0 ] === '.' ? `0 ${ retorno } ` : retorno ;
23
34
}
24
35
25
36
addingPrefix ( value ) {
26
37
return `${ this . args . prefix } ${ value } ` ;
27
38
}
28
39
29
40
removingPrefix ( value ) {
30
- return value . replace ( this . args . prefix , '' ) ;
41
+ const position = value . indexOf ( this . args . prefix , 0 ) ;
42
+
43
+ if ( position !== - 1 ) {
44
+ value = value . substring ( this . args . prefix . length , value . length ) ;
45
+ }
46
+
47
+ return value ;
31
48
}
32
49
33
50
addingSuffix ( value ) {
34
51
return `${ value } ${ this . args . suffix } ` ;
35
52
}
36
53
37
54
removingSuffix ( value ) {
38
- if ( value . includes ( this . args . suffix , value . length - this . args . fractionDigits ) ) {
39
- value = value . replace ( this . args . suffix , '' ) ;
40
- } else {
41
- value = value . substring ( 0 , value . length - 1 ) ;
42
- }
43
- return value ;
44
- }
45
-
46
- addingCompleterFromStart ( value , completer ) {
47
- while ( value . length < this . args . fractionDigits ) {
48
- value = `${ completer } ${ value } ` ;
49
- }
50
- return value ;
51
- }
55
+ const position = value . indexOf ( this . args . suffix , value . length - this . args . suffix . length ) ;
52
56
53
- addingCompleterFromEnd ( value , completer ) {
54
- while ( value . length < this . args . fractionDigits ) {
55
- value = ` ${ value } ${ completer } ` ;
57
+ if ( position !== - 1 ) {
58
+ const start = value . substring ( 0 , position ) ;
59
+ value = start + value . substring ( start . length + this . args . suffix . length - 1 , value . length - 1 ) ;
56
60
}
57
- return value ;
58
- }
59
61
60
- removingCompleterFromStart ( value , completer ) {
61
- while ( value [ 0 ] === completer ) {
62
- value = value . replace ( completer , '' ) ;
63
- }
64
62
return value ;
65
63
}
66
64
67
- removingCompleterFromEnd ( value , completer ) {
68
- while ( value [ value . length - 1 ] === completer ) {
69
- value = value . substring ( 0 , value . length - 1 ) ;
65
+ addingCompleter ( value , completer , length , start = true ) {
66
+ while ( value . length < length ) {
67
+ value = start ? ` ${ completer } ${ value } ` : ` ${ value } ${ completer } ` ;
70
68
}
71
- return value ;
72
- }
73
69
74
- addingAutoComplete ( value ) {
75
- const n = `${ value } ${ this . addingCompleterFromEnd ( '' , '0' ) } ` ;
76
- return n ;
77
- }
78
-
79
- autoComplete ( value ) {
80
- const regexp = new RegExp ( `\\${ this . args . decimalSeparator } ` , 'g' ) ;
81
- const array = value . match ( regexp ) || [ ] ;
82
- if ( array . length > 1 ) {
83
- value = this . addingAutoComplete ( value ) ;
84
- }
85
70
return value ;
86
71
}
87
72
88
- addingDecimalSeparator ( value , completer , separator ) {
89
- let length = value . length - this . args . fractionDigits ;
90
-
91
- let regexpFraction ;
92
- let decimals = '$1' ;
93
- let dezenas = completer ;
94
- let character = isFinite ( completer ) ? 'd' : 'w' ;
95
-
96
- regexpFraction = `(\\${ character } {${ this . args . fractionDigits } })` ;
73
+ removingCompleter ( value , completer , start = true ) {
74
+ const position = start ? 0 : value . length - 1 ;
97
75
98
- if ( length > 0 ) {
99
- regexpFraction = `(\\${ character } {${ length } })${ regexpFraction } ` ;
100
- dezenas = decimals ;
101
- decimals = '$2' ;
76
+ while ( value [ position ] === completer ) {
77
+ value = value . substring ( 0 , position - 1 ) + value . substring ( position + 1 , value . length ) ;
102
78
}
103
79
104
- return value . replace (
105
- new RegExp ( regexpFraction ) ,
106
- `${ dezenas } ${ separator } ${ decimals } `
107
- ) ;
80
+ return value ;
108
81
}
109
82
110
- addingHundredsSeparator ( value ) {
83
+ addingSeparators ( value ) {
111
84
let size = value . length - this . args . fractionDigits ;
85
+ let character = this . args . fixed ? 'd' : 'w' ;
86
+ let regexp = `\\,?||\\.?(\\${ character } )` ;
112
87
let hundreds = Math . ceil ( size / 3 ) ;
113
- let regexpHundreds = '(\\d)' ;
114
88
115
89
let replacement = `${ this . args . decimalSeparator } $${ hundreds + 1 } ` ;
116
90
117
91
for ( let i = hundreds ; i !== 0 ; i -- ) {
118
92
if ( size >= 3 ) {
119
- regexpHundreds = `(\\d{ 3})${ regexpHundreds } ` ;
93
+ regexp = `(\\${ character } { 3})${ regexp } ` ;
120
94
size -= 3 ;
121
95
} else {
122
- regexpHundreds = `(\\d{ ${ size } })${ regexpHundreds } ` ;
96
+ regexp = `(\\${ character } { ${ size } })${ regexp } ` ;
123
97
}
124
98
125
99
if ( i > 1 ) {
@@ -129,21 +103,31 @@ module.exports = class Core {
129
103
}
130
104
}
131
105
132
- return value . replace ( new RegExp ( regexpHundreds ) , replacement ) ;
106
+ return value . replace ( new RegExp ( regexp ) , replacement ) ;
133
107
}
134
108
135
- removeSeparator ( value , separator ) {
136
- return value . replace ( new RegExp ( `\\${ separator } ` , 'g' ) , '' ) ;
109
+ replaceSeparator ( value , separator , replacer = '' ) {
110
+ return value . replace ( new RegExp ( `\\${ separator } ` , 'g' ) , replacer ) ;
137
111
}
138
112
139
- formatDecimal ( value , completer , separator ) {
140
- value = this . addingCompleterFromStart ( value , completer ) ;
141
- value = this . addingDecimalSeparator ( value , completer , separator ) ;
142
- return value ;
113
+ adjustDotPosition ( value ) {
114
+ let fractionDigits ;
115
+ let retorno = value . toString ( ) ;
116
+
117
+ retorno = retorno . replace ( '.' , '' ) ;
118
+ fractionDigits = retorno . length - this . args . fractionDigits ;
119
+ retorno = `${ retorno . substring ( 0 , fractionDigits ) } .${ retorno . substring ( fractionDigits ) } ` ;
120
+
121
+ return retorno ;
143
122
}
144
123
145
- textToNumber ( input ) {
146
- let retorno = input . toString ( ) ;
124
+ checkNumberStart ( value ) {
125
+ const retorno = value . toString ( ) ;
126
+ return retorno [ 0 ] === '.' ? `0${ retorno } ` : retorno ;
127
+ }
128
+
129
+ textToNumber ( value , input ) {
130
+ let retorno = value . toString ( ) ;
147
131
let completer = this . completer ( ) ;
148
132
149
133
if ( this . args . prefix ) {
@@ -154,32 +138,41 @@ module.exports = class Core {
154
138
retorno = this . removingSuffix ( retorno ) ;
155
139
}
156
140
157
- retorno = this . removeSeparator ( retorno , this . args . thousandsSeparator ) ;
158
- retorno = this . removeSeparator ( retorno , this . args . decimalSeparator ) ;
159
-
160
141
retorno = this . onlyNumber ( retorno ) ;
161
142
162
- retorno = this . removingCompleterFromStart (
163
- retorno ,
164
- completer
165
- ) ;
143
+ if ( retorno ) {
144
+ if ( input ) {
145
+ retorno = this . adjustDotPosition ( retorno ) ;
146
+ }
147
+
148
+ retorno = this . removingCompleter ( retorno , completer ) ;
149
+
150
+ retorno = this . checkNumberStart ( retorno ) ;
151
+ }
166
152
167
153
return retorno || ( this . args . fixed ? '0' : '' ) ;
168
154
}
169
155
170
- numberToText ( input ) {
156
+ numberToText ( value ) {
171
157
let retorno = this . emptyOrInvalid ( ) ;
158
+ value = this . replaceSeparator ( value . toString ( ) , this . args . decimalSeparator , '.' ) ;
159
+
160
+ if ( ! isNaN ( parseFloat ( value ) ) ) {
161
+ if ( this . isFloat ( value ) ) {
162
+ const number = value . split ( '.' ) ;
163
+ let hundreds = number [ 0 ] ;
164
+ let decimals = number [ 1 ] ;
172
165
173
- if ( ! isNaN ( parseFloat ( input ) ) ) {
174
- if ( input . length <= this . args . fractionDigits ) {
175
- retorno = this . formatDecimal (
176
- input ,
177
- this . completer ( ) ,
178
- this . args . decimalSeparator
179
- ) ;
166
+ decimals = this . addingCompleter ( decimals || '' , this . completer ( ) , this . args . fractionDigits , false ) ;
167
+
168
+ retorno = `${ hundreds } ${ decimals } ` ;
180
169
} else {
181
- retorno = this . addingHundredsSeparator ( input ) ;
170
+ retorno = this . replaceSeparator ( value , '.' ) ;
171
+ retorno = this . addingCompleter ( retorno || '' , this . completer ( ) , this . args . fractionDigits ) ;
172
+ retorno = this . args . fractionDigits >= retorno . length ? `${ this . completer ( ) } ${ retorno } ` : retorno ;
182
173
}
174
+
175
+ retorno = this . addingSeparators ( retorno ) ;
183
176
}
184
177
185
178
if ( this . args . prefix ) {
0 commit comments