Skip to content

Commit

Permalink
fix(pactjs): Fixed PactNumber to prevent the creation of scientific n…
Browse files Browse the repository at this point in the history
…otation for large numbers
  • Loading branch information
javadkh2 committed Oct 2, 2023
1 parent f9de53a commit c143687
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-flowers-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@kadena/pactjs': patch
---

Fixed PactNumber to prevent the creation of scientific notation for large
numbers.
2 changes: 1 addition & 1 deletion packages/libs/pactjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"test": "heft test"
},
"dependencies": {
"bignumber.js": "^9.0.2"
"bignumber.js": "^9.1.2"
},
"devDependencies": {
"@kadena-dev/eslint-config": "workspace:*",
Expand Down
5 changes: 5 additions & 0 deletions packages/libs/pactjs/src/PactNumber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/// <reference path="./extend-bignumber.ts" />
import BigNumber from 'bignumber.js';

// Configure BigNumber to prevent exponential notation (Scientific notation) for numbers.
// The library supports a maximum of 1e9 digits, which I believe is sufficient for our use case.
// It is highly unlikely that someone would pass a number with more than 1,000,000,000 digits.
BigNumber.config({ EXPONENTIAL_AT: [-1e9, 1e9] });

// In order to extend BigNumber methods correctly, I had to add the PactNumber methods to
// the prototype of BigNumber. Then, something like this works:
// const decimal = new PactNumber('0.9').plus("1").toPactDecimal()
Expand Down
23 changes: 23 additions & 0 deletions packages/libs/pactjs/src/tests/PactNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,27 @@ describe('Pact Number', () => {
const pactDecimal = actual.minus('0.9').toPactDecimal();
expect(pactDecimal).toStrictEqual({ decimal: '0.0007199254740991192919' });
});

it('does not return scientific notation for large integer', () => {
const number = new PactNumber(
'0x584712a2542d5c1ab3cd25dec0b14e53aef270e6948140656a8fbf3d2829c729',
);
const int = number.toPactInteger();

expect(int).toEqual({
int: '39929105424737202205146861475763790532040240744253228361760383139526688229161',
});
});

it('does not return scientific notation for large decimals', () => {
const number = new PactNumber(
'3.9929105424737202205146861475763790532040240744253228361760383139526688229161',
);
const decimal = number.toPactDecimal();

expect(decimal).toEqual({
decimal:
'3.9929105424737202205146861475763790532040240744253228361760383139526688229161',
});
});
});
18 changes: 7 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c143687

Please sign in to comment.