Skip to content

Commit

Permalink
Merge pull request #168 from nearprotocol/parse-allow-commas
Browse files Browse the repository at this point in the history
Parse NEAR amount with commas
  • Loading branch information
vgrichina authored Jan 8, 2020
2 parents 594fc37 + d350a9d commit 7780388
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src.ts/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function formatNearAmount(balance: string, fracDigits: number = NEAR_NOMI
*/
export function parseNearAmount(amt?: string): string | null {
if (!amt) { return amt; }
amt = amt.trim();
amt = cleanupAmount(amt);
const split = amt.split('.');
const wholePart = split[0];
const fracPart = split[1] || '';
Expand All @@ -60,6 +60,10 @@ export function parseNearAmount(amt?: string): string | null {
return trimLeadingZeroes(wholePart + fracPart.padEnd(NEAR_NOMINATION_EXP, '0'));
}

function cleanupAmount(amount: string): string {
return amount.replace(/,/g, '').trim();
}

function trimTrailingZeroes(value: string): string {
return value.replace(/\.?0*$/, '');
}
Expand Down
3 changes: 2 additions & 1 deletion test/utils/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ test('converting near to account balance units', async() => {
expect(nearlib.utils.format.parseNearAmount('0.000001')).toEqual('1000000000000000000');
expect(nearlib.utils.format.parseNearAmount('.000001')).toEqual('1000000000000000000');
expect(nearlib.utils.format.parseNearAmount('000000.000001')).toEqual('1000000000000000000');

expect(nearlib.utils.format.parseNearAmount('1,000,000.1')).toEqual('1000000100000000000000000000000');

try {
// Too many decimals
expect(nearlib.utils.format.parseNearAmount('0.0000080990999998370878871')).toFail();
Expand Down

0 comments on commit 7780388

Please sign in to comment.