Skip to content

Commit

Permalink
Merge pull request #225 from kaleido-io/large-nums-as-strings
Browse files Browse the repository at this point in the history
Large nums as strings
  • Loading branch information
EnriqueL8 authored Sep 18, 2024
2 parents 15e43f6 + 3d90bf8 commit 831071f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dagre": "^0.8.5",
"dayjs": "^1.11.1",
"i18next": "^21.6.16",
"lossless-json": "^4.0.2",
"pretty-bytes": "^6.0.0",
"react": "^17.0.2",
"react-copy-to-clipboard": "^5.1.0",
Expand Down Expand Up @@ -60,6 +61,7 @@
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/lossless-json": "^1.0.4",
"@types/node": "^17.0.25",
"@types/react": "^17.0.43",
"@types/react-copy-to-clipboard": "^5.0.2",
Expand Down
11 changes: 9 additions & 2 deletions src/utils/fetches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
ITokenTransferWithPool,
} from '../interfaces';

import { parse, isSafeNumber, LosslessNumber } from 'lossless-json';

export const fetchWithCredentials = (
resource: string,
options?: RequestInit
Expand All @@ -20,12 +22,17 @@ export const fetchWithCredentials = (
);
};

export function parseLargeNumbersAsStrings(value: any) {
return isSafeNumber(value, { approx: false })
? parseFloat(value) // Smaller numbers are kept as Javascript numbers
: new LosslessNumber(value).toString(); // Large numbers are safely stringified
}

export const fetchCatcher = async (resource: string): Promise<any> => {
const response = await fetchWithCredentials(resource);
if (!response.ok) {
console.log(`error fetching ${resource}`);
} else {
return await response.json();
return parse(await response.text(), null, parseLargeNumbersAsStrings);
}
};

Expand Down

0 comments on commit 831071f

Please sign in to comment.