Skip to content

Commit

Permalink
chore: remove trailing zeros (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick134-bit authored Nov 8, 2024
1 parent 6d0c947 commit 64883fb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions util/amountInputValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
*/
export const amountInputValidation = (input: string): string => {
const amount = input.
// Limiting the number of characters to be 32
// Limiting the number of characters to be 32
slice(0, 32).
// Disallowing leading decimal place and multiple zeros before decimal place
// Disallowing leading decimal place and multiple zeros before decimal place
replace(/^(?:\.|0{2,}\.)/u, '0.').
// Eliminating multiple leading zeros before the numbers between 1-9
replace(/^0+(?=[0-9])/u, '')
// Eliminating multiple leading zeros before the numbers between 1-9
replace(/^0+(?=[0-9])/u, '').
// Remove excess zeros after decimal point
replace(/\.(\d+?)0{2,}$/u, '.$10')
// Ensuring multiple decimal points can't be used
return input.indexOf('.') !== amount.lastIndexOf('.') ?
amount.slice(0, amount.indexOf('.') + 1) + amount.slice(amount.indexOf('.')).replaceAll('.', '')
Expand Down

0 comments on commit 64883fb

Please sign in to comment.