Added accepting numbers in 1b format to faction donations

This commit is contained in:
Daniel Ferri 2021-05-04 19:00:38 +02:00 committed by hydroflame
parent 13206a8c3e
commit 68eb68a89e
2 changed files with 19 additions and 1 deletions

@ -12,6 +12,8 @@ import { Reputation } from "../../ui/React/Reputation";
import { StdButton } from "../../ui/React/StdButton";
import { numeralWrapper } from "../../ui/numeralFormat";
import { dialogBoxCreate } from "../../../utils/DialogBox";
type IProps = {
@ -73,7 +75,7 @@ export class DonateOption extends React.Component<IProps, IState> {
}
handleChange(e: React.ChangeEvent<HTMLInputElement>): void {
const amt = parseFloat(e.target.value);
const amt = numeralWrapper.parse(e.target.value);
if (isNaN(amt)) {
this.setState({

@ -138,6 +138,22 @@ class NumeralFormatter {
formatThreads(n: number): string {
return this.format(n, "0,0");
}
parse(s: string): number {
// numeral library does not handle formats like 1e10 well (returns 110),
// so if both return a valid number, return the biggest one
const numeralValue = numeral(s).value();
const parsed = parseFloat(s);
if (isNaN(parsed) && numeralValue === null) {
return NaN;
} else if (isNaN(parsed)) {
return numeralValue;
} else if (numeralValue === null) {
return parsed;
} else {
return Math.max(numeralValue, parsed);
}
}
}
export const numeralWrapper = new NumeralFormatter();