Skip to content

Commit 6cd4d85

Browse files
committed
refactor: refactoring and renaming
1 parent 6a637d3 commit 6cd4d85

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed
Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,30 @@
11
import { describe, it, expect } from 'vitest';
2-
import { formatBalance } from '../formatBalance';
2+
import { formatBalance } from '../formatBalance.js';
3+
import { TypinkError } from '../errors.js';
34

45
describe('formatBalance', () => {
5-
6-
it('case 1', () => {
6+
it('should work probably', () => {
77
expect(formatBalance(1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('1 AZERO');
8-
});
9-
10-
it('case 2', () => {
118
expect(formatBalance(1e12, { decimals: 13, symbol: 'AZERO' })).toEqual('0.1 AZERO');
12-
});
13-
14-
it('case 3', () => {
15-
expect(formatBalance(1e12, { decimals: 13 })).toEqual('0.1');
16-
});
17-
18-
it('case 4', () => {
199
expect(formatBalance(12_023_172_837_123, { decimals: 12 })).toEqual('12.023172837123');
20-
});
21-
22-
it('case 5', () => {
2310
expect(formatBalance('1', { decimals: 12 })).toEqual('0.000000000001');
11+
expect(formatBalance(1e12, { decimals: 13 })).toEqual('0.1');
12+
expect(formatBalance(-10000, { decimals: 4 })).toEqual('-1');
13+
expect(formatBalance('-10200', { decimals: 4 })).toEqual('-1.02');
14+
expect(formatBalance(-1e12, { decimals: 12, symbol: 'AZERO' })).toEqual('-1 AZERO');
2415
});
2516

26-
// Unhandled case
27-
/*
28-
it('case 6', () => {
29-
expect(formatBalance('1.000000000001', { decimals: 12 })).toEqual('1.000000000001');
30-
});
31-
*/
32-
33-
it('case 7', () => {
34-
expect(() => formatBalance('1', { decimals: 12.2, symbol: 'AZERO' })).toThrow(new Error('Invalid decimals'));
17+
it('should throw error if input has bad chars', () => {
18+
expect(() => formatBalance('1.000000000001', { decimals: 12 })).toThrow(
19+
new TypinkError('Invalid value at position 1, bigint was expected'),
20+
);
21+
expect(() => formatBalance('1,2', { decimals: 12, symbol: 'AZERO' })).toThrow(
22+
new TypinkError('Invalid value at position 1, bigint was expected'),
23+
);
3524
});
3625

37-
it('case 8', () => {
38-
expect(() => formatBalance('1,2', { decimals: 12, symbol: 'AZERO' })).toThrow(new Error('Invalid value at position 1'));
26+
it('should throw error if decimals is invalid', () => {
27+
expect(() => formatBalance('1', { decimals: 12.2, symbol: 'AZERO' })).toThrow(new TypinkError('Invalid decimals'));
28+
expect(() => formatBalance('1', { decimals: -12, symbol: 'AZERO' })).toThrow(new TypinkError('Invalid decimals'));
3929
});
4030
});
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TypinkError } from './errors.js';
2+
13
export interface FormatBalanceOptions {
24
decimals: number;
35
symbol?: string;
@@ -6,31 +8,27 @@ export interface FormatBalanceOptions {
68
export function formatBalance(value: number | bigint | string, options: FormatBalanceOptions): string {
79
const { decimals, symbol } = options;
810

9-
// Do it with string
1011
let valueStr = value.toString();
1112

12-
// Verify decimals in an integer
1313
if (!Number.isInteger(decimals) || decimals < 0) {
14-
throw new Error('Invalid decimals');
14+
throw new TypinkError('Invalid decimals');
1515
}
1616

17-
// Make sure the valueStr does have any bad characters
17+
const isNegative = valueStr.at(0) === '-';
18+
if (isNegative) {
19+
valueStr = valueStr.slice(1);
20+
}
1821

19-
const badChars = valueStr.match(/[^0-9.]/);
22+
const badChars = valueStr.match(/[^0-9]/);
2023
if (badChars) {
21-
k throw new Error(`Invalid value at position ${badChars.index ?? 0}`);
24+
throw new TypinkError(`Invalid value at position ${badChars.index ?? 0}, bigint was expected`);
2225
}
2326

24-
// Should we handle negative values? and how?
25-
// Should we handle the case where the value is a string with a decimal point?
26-
2727
const tmpStr = valueStr.padStart(decimals, '0').padEnd(decimals, '0');
28-
const wholePart = tmpStr.slice(0, tmpStr.length - decimals).padStart(1, '0');
29-
const decimalPart = tmpStr.slice(tmpStr.length - decimals).replace(/0+$/, '');
3028

31-
if (symbol) {
32-
return `${wholePart}${decimalPart ? `.${decimalPart}` : ''} ${symbol}`;
33-
}
29+
// If wholePart is empty, pad it with 0
30+
const wholePart = tmpStr.slice(0, tmpStr.length - decimals).padStart(1, '0');
31+
const decimalPart = tmpStr.slice(tmpStr.length - decimals).replace(/0+$/, '');
3432

35-
return `${wholePart}${decimalPart ? `.${decimalPart}` : ''}`;
33+
return `${isNegative ? '-' : ''}${wholePart}${decimalPart ? `.${decimalPart}` : ''}${symbol ? ` ${symbol}` : ''}`;
3634
}

0 commit comments

Comments
 (0)