Skip to content

Commit

Permalink
chore: Prepend zero to decimal amounts when dot is the first input (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
luads authored Dec 3, 2024
1 parent 469fb90 commit 2039ab6
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const VALID_NUMBER_REGEX = /^(0|[1-9]\d*)(\.\d*)?$/;
* @returns An object containing the sanitized value, the float amount, and a boolean indicating if the amount is valid
*/
export const validateToAmount = (amount: string) => {
const value = amount || '';
let value = amount || '';

if (amount === '.') {
value = '0.';
}

const sanitizedValue = value.replace(/^0+(?=\d)/, '');
const isValid = VALID_NUMBER_REGEX.test(sanitizedValue);
const floatAmount = isValid ? parseFloat(sanitizedValue) : NaN;
Expand Down

0 comments on commit 2039ab6

Please sign in to comment.