Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed "stringIsFloat" issue for huge decimal numbers #1309

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Fixed "stringIsFloat" issue for huge decimal numbers](https://github.com/multiversx/mx-sdk-dapp/pull/1309)

## [[v3.0.11](https://github.com/multiversx/mx-sdk-dapp/pull/1308)] - 2024-11-19

- [Updated transaction data decode functions](https://github.com/multiversx/mx-sdk-dapp/pull/1307)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/validation/stringIsFloat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export const stringIsFloat = (amount: string) => {
// eslint-disable-next-line
let [wholes, decimals] = amount.split('.');
if (decimals) {
const areAllNumbers = decimals
.split('')
.every((digit) => !isNaN(parseInt(digit)));

BigNumber.set({
DECIMAL_PLACES: areAllNumbers
? decimals.length
: BigNumber.config().DECIMAL_PLACES
});

while (decimals.charAt(decimals.length - 1) === ZERO) {
decimals = decimals.slice(0, -1);
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils/validation/tests/stringIsFloat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,86 @@ describe('stringIsFloat tests', () => {
it('rejects undefined', () => {
expect(stringIsFloat(undefined as any)).toBe(false);
});

it('rejects object', () => {
expect(stringIsFloat({} as any)).toBe(false);
});

it('rejects null', () => {
expect(stringIsFloat(null as any)).toBe(false);
});

it('allows valid numbers', () => {
expect(stringIsFloat('1')).toBe(true);
});

it('allows decimal numbers with zeros', () => {
expect(stringIsFloat('0.1')).toBe(true);
expect(stringIsFloat('0.001')).toBe(true);
});

it('allows large decimal places', () => {
expect(stringIsFloat('999999999999999999999.123456789012345678')).toBe(
true
);

expect(stringIsFloat('0.111111111111111111')).toBe(true);
});

it('allows trailing 0', () => {
expect(stringIsFloat('0.10')).toBe(true);
expect(stringIsFloat('10')).toBe(true);
});

it('denies negative numbers', () => {
expect(stringIsFloat('-1')).toBe(false);
});

it('denies explicit positive', () => {
expect(stringIsFloat('+1')).toBe(false);
});

it('denies leading 0', () => {
expect(stringIsFloat('01')).toBe(false);
});

it('denies string', () => {
expect(stringIsFloat('null')).toBe(false);
});

it('denies hexadecimal', () => {
expect(stringIsFloat('0x2')).toBe(false);
});

it('denies exponential', () => {
expect(stringIsFloat('1e2')).toBe(false);
});

it('denies caret separation', () => {
expect(stringIsFloat('100_200')).toBe(false);
});

it('denies NaN', () => {
expect(stringIsFloat(NaN as any)).toBe(false);
});

it('denies caret separation', () => {
expect(stringIsFloat(Infinity as any)).toBe(false);
});

it('allows large numbers', () => {
expect(stringIsFloat('0.0000000011231723871623178236182376123')).toBe(true);
MiroMargineanu marked this conversation as resolved.
Show resolved Hide resolved
});

it('denies numbers with single comma', () => {
expect(stringIsFloat('0,0000000011231723871623178236182376123')).toBe(
false
);
});

it('denies numbers with multiple commas', () => {
expect(stringIsFloat('0,0000000011231723,87,162.317,8236182376123')).toBe(
false
);
});
});