@@ -17,6 +17,12 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
17
17
18
18
private static emptyFormattedValue : string ;
19
19
20
+ public componentWillMount ( ) : void {
21
+ if ( this . props . delimiter == null || this . props . delimiter . length !== 1 ) {
22
+ throw new Error ( `StringToDecimalModifier: delimiter prop has to be exactly one character long.` ) ;
23
+ }
24
+ }
25
+
20
26
public Format ( value : FieldValue ) : FieldValue {
21
27
if ( value !== EMPTY_VALUE ) {
22
28
return value . toString ( ) ;
@@ -52,7 +58,13 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
52
58
const delimiter = this . props . delimiter ! ;
53
59
54
60
const leadingMinus = negative ? "-" : "" ;
55
- const regex = new RegExp ( `[^0-9\\${ delimiter } ]+` , "g" ) ;
61
+
62
+ // Include delimiter in regex if precision is non-zero
63
+ const regexPattern = this . props . precision === 0 ?
64
+ `[^0-9]+` :
65
+ `[^0-9\\${ delimiter } ]+` ;
66
+
67
+ const regex = new RegExp ( regexPattern , "g" ) ;
56
68
const extractedValue : string = this . LeaveOnlyFirstDelimiter (
57
69
value . replace ( regex , "" ) ,
58
70
delimiter ) ;
@@ -78,7 +90,8 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
78
90
}
79
91
80
92
transitionalValue = leadingMinus + transitionalValue ;
81
- const numValue = Number ( transitionalValue ) ;
93
+ const delimiterFixed = transitionalValue . replace ( delimiter , "." ) ;
94
+ const numValue = Number ( delimiterFixed ) ;
82
95
83
96
return {
84
97
Value : numValue ,
0 commit comments