fix: scientific notation
This commit is contained in:
parent
bf600c32c8
commit
4f731bdaee
1 changed files with 19 additions and 30 deletions
|
|
@ -3,20 +3,10 @@ import Decimal from 'decimal.js';
|
|||
export function validateTokensInput(input: string | number, decimal_point = 12) {
|
||||
let inputVal = input;
|
||||
|
||||
if (typeof inputVal === 'number') {
|
||||
// scientific notation check
|
||||
if (inputVal.toString().toLowerCase().includes('e')) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Scientific notation is not allowed',
|
||||
};
|
||||
}
|
||||
|
||||
inputVal = inputVal.toString();
|
||||
} else if (/^[+-]?\d+(\.\d+)?[eE][+-]?\d+$/.test(inputVal)) {
|
||||
if (decimal_point < 0 || decimal_point > 18) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Scientific notation is not allowed',
|
||||
error: 'Invalid decimal point',
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -27,27 +17,36 @@ export function validateTokensInput(input: string | number, decimal_point = 12)
|
|||
};
|
||||
}
|
||||
|
||||
inputVal = inputVal.replace(/[^0-9.,]/g, '');
|
||||
|
||||
const MAX_NUMBER = new Decimal(2).pow(64).minus(1);
|
||||
|
||||
if (decimal_point < 0 || decimal_point > 18) {
|
||||
// Normalize input using Decimal
|
||||
try {
|
||||
const decimal = new Decimal(inputVal);
|
||||
inputVal = decimal.toFixed();
|
||||
} catch (e) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Invalid decimal point',
|
||||
error: 'Invalid number format',
|
||||
};
|
||||
}
|
||||
|
||||
inputVal = inputVal.replace(/[^0-9.,]/g, '');
|
||||
const dotInput = inputVal.replace(/,/g, '');
|
||||
|
||||
const MAX_NUMBER = new Decimal(2).pow(64).minus(1);
|
||||
const decimalDevider = new Decimal(10).pow(decimal_point);
|
||||
const maxAllowedNumber = MAX_NUMBER.div(decimalDevider);
|
||||
const minAllowedNumber = new Decimal(1).div(decimalDevider);
|
||||
|
||||
const decimalsAmount = dotInput.split('.')[1]?.length || 0;
|
||||
if (decimalsAmount > decimal_point) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Invalid amount - too many decimal points',
|
||||
};
|
||||
}
|
||||
|
||||
const rounded = (() => {
|
||||
if (dotInput.replace('.', '').length > 20) {
|
||||
const decimalParts = dotInput.split('.');
|
||||
|
||||
if (decimalParts.length === 2 && decimalParts[1].length > 1) {
|
||||
const beforeDotLength = decimalParts[0].length;
|
||||
const roundedInput = new Decimal(dotInput).toFixed(
|
||||
|
|
@ -58,21 +57,11 @@ export function validateTokensInput(input: string | number, decimal_point = 12)
|
|||
return roundedInput;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return dotInput;
|
||||
})();
|
||||
|
||||
const decimalsAmount = dotInput.split('.')[1]?.length || 0;
|
||||
|
||||
if (decimalsAmount > decimal_point) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Invalid amount - too many decimal points',
|
||||
};
|
||||
}
|
||||
|
||||
if (rounded === false) {
|
||||
return {
|
||||
valid: false,
|
||||
|
|
@ -107,4 +96,4 @@ export function validateTokensInput(input: string | number, decimal_point = 12)
|
|||
return {
|
||||
valid: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue