Skip to content

Commit 81501b8

Browse files
Merge pull request #107 from SimplrJS/dev
Dev to master.
2 parents 1199a57 + ebcbdc1 commit 81501b8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/react-forms/src/modifiers/string-to-decimal.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
1717

1818
private static emptyFormattedValue: string;
1919

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+
2026
public Format(value: FieldValue): FieldValue {
2127
if (value !== EMPTY_VALUE) {
2228
return value.toString();
@@ -52,7 +58,13 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
5258
const delimiter = this.props.delimiter!;
5359

5460
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");
5668
const extractedValue: string = this.LeaveOnlyFirstDelimiter(
5769
value.replace(regex, ""),
5870
delimiter);
@@ -78,7 +90,8 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
7890
}
7991

8092
transitionalValue = leadingMinus + transitionalValue;
81-
const numValue = Number(transitionalValue);
93+
const delimiterFixed = transitionalValue.replace(delimiter, ".");
94+
const numValue = Number(delimiterFixed);
8295

8396
return {
8497
Value: numValue,

0 commit comments

Comments
 (0)