Skip to content

Commit 6a7b89f

Browse files
committed
Merge branch 'frontend-format-number-fn-refactor'
2 parents 6400996 + f19cf36 commit 6a7b89f

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

frontends/web/src/components/rates/rates.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ export const currenciesWithDisplayName: FiatWithDisplayName[] = [
4747
{ currency: 'BTC', displayName: 'Bitcoin' }
4848
];
4949

50-
export function formatNumber(amount: number, maxDigits: number): string {
51-
let formatted = amount.toFixed(maxDigits);
52-
let position = formatted.indexOf('.') - 3;
53-
while (position > 0) {
54-
formatted = formatted.slice(0, position) + '\'' + formatted.slice(position);
55-
position = position - 3;
56-
}
57-
return formatted;
58-
}
59-
6050
type TProvidedProps = {
6151
amount?: IAmount;
6252
tableRow?: boolean;

frontends/web/src/routes/account/summary/chart.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import { Component, createRef, ReactChild } from 'react';
1919
import { ISummary } from '../../../api/account';
2020
import { translate, TranslateProps } from '../../../decorators/translate';
2121
import { Skeleton } from '../../../components/skeleton/skeleton';
22-
import { formatNumber } from '../../../components/rates/rates';
22+
import { formatNumber } from '../../../utils/rates';
2323
import { Amount } from '../../../components/amount/amount';
24-
import styles from './chart.module.css';
2524
import Filters from './filters';
2625
import { getDarkmode } from '../../../components/darkmode/darkmode';
2726
import { TChartDisplay, TChartFiltersProps } from './types';
27+
import styles from './chart.module.css';
2828

2929
export interface FormattedLineData extends LineData {
3030
formattedValue: string;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2024 Shift Crypto AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { describe, expect, it } from 'vitest';
18+
import { formatNumber } from './rates';
19+
20+
describe('rates utils', () => {
21+
describe('formatNumber', () => {
22+
it('formats positive number without thousand separator', () => {
23+
expect(formatNumber(532.12, 2)).toBe('532.12');
24+
});
25+
26+
it('formats positive number with thousand separator', () => {
27+
expect(formatNumber(1532.12, 2)).toBe('1\'532.12');
28+
});
29+
30+
it('formats negative number without thousand separator', () => {
31+
expect(formatNumber(-532.12, 2)).toBe('-532.12');
32+
});
33+
34+
it('formats negative number with thousand separator', () => {
35+
expect(formatNumber(-1532.12, 2)).toBe('-1\'532.12');
36+
});
37+
38+
it('handles zero correctly', () => {
39+
expect(formatNumber(0, 2)).toBe('0.00');
40+
});
41+
42+
it('formats number with multiple thousand separators', () => {
43+
expect(formatNumber(1234567.89, 2)).toBe('1\'234\'567.89');
44+
});
45+
46+
it('rounds decimal places correctly', () => {
47+
expect(formatNumber(1234.5678, 2)).toBe('1\'234.57');
48+
});
49+
50+
it('formats negative number close to zero without separator', () => {
51+
expect(formatNumber(-100, 2)).toBe('-100.00');
52+
});
53+
54+
it('formats large negative number with separators', () => {
55+
expect(formatNumber(-123456.789, 3)).toBe('-123\'456.789');
56+
});
57+
58+
});
59+
});

frontends/web/src/utils/rates.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2024 Shift Crypto AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export function formatNumber(amount: number, maxDigits: number): string {
18+
let formatted = amount.toFixed(maxDigits);
19+
let position = formatted.indexOf('.') - 3;
20+
const start = formatted[0] === '-' ? 1 : 0;
21+
22+
while (position > start) {
23+
formatted = formatted.slice(0, position) + '\'' + formatted.slice(position);
24+
position -= 3;
25+
}
26+
return formatted;
27+
}

0 commit comments

Comments
 (0)