Skip to content

Commit

Permalink
feat(core-components-amount): allow withZeroMinorPart formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancar committed Jun 26, 2020
1 parent f946776 commit e86529d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 0 additions & 2 deletions packages/amount/src/__snapshots__/component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ exports[`Amount should match snapshot 1`] = `
<span
class="minorPartAndCurrency"
>
,
00
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ exports[`PureAmount should match snapshot 1`] = `
data-test-id="test-id"
>
1
,
00
</span>
Expand Down
32 changes: 31 additions & 1 deletion packages/amount/src/utils/formatAmount.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitAmount } from './formatAmount';
import { splitAmount, formatAmount } from './formatAmount';

test('splitAmount', () => {
expect(splitAmount('100', 3, ' ')).toBe('100');
Expand All @@ -8,3 +8,33 @@ test('splitAmount', () => {
expect(splitAmount('1000000', 3, ' ')).toBe('1 000 000');
expect(splitAmount('10000000', 3, ' ')).toBe('10 000 000');
});

test('formatAmount', () => {
const { minorPart } = formatAmount({
withZeroMinorPart: true,
value: 1234500,
currency: { code: 'RUR', minority: 100 },
});

expect(minorPart).toBe('00');
});

test('formatAmount', () => {
const { minorPart } = formatAmount({
withZeroMinorPart: true,
value: 1234500,
currency: { code: 'RUR', minority: 100 },
});

expect(minorPart).toBe('00');
});

test('should return null minorPart if passed withZeroMinorPart = false and value has zero minor part', () => {
const { minorPart } = formatAmount({
withZeroMinorPart: false,
value: 1234500,
currency: { code: 'RUR', minority: 100 },
});

expect(minorPart).toBeNull();
});
9 changes: 7 additions & 2 deletions packages/amount/src/utils/formatAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ type AmountType = {
withZeroMinorPart?: boolean;
};

export const formatAmount = ({ value, currency: { code, minority } }: AmountType) => {
export const formatAmount = ({
value,
currency: { code, minority },
withZeroMinorPart,
}: AmountType) => {
if (value === null) {
return {
majorPart: '',
Expand All @@ -80,6 +84,7 @@ export const formatAmount = ({ value, currency: { code, minority } }: AmountType

const fractionDigits = Math.log(minority) * Math.LOG10E;
const valueAbsStr = (Math.abs(value) / minority).toFixed(fractionDigits);
// TODO: проверить что toFixed возврает всегда точку.

const [majorPart, minorPart] = valueAbsStr.split('.');

Expand All @@ -99,7 +104,7 @@ export const formatAmount = ({ value, currency: { code, minority } }: AmountType

return {
majorPart: majorPartFormatted,
minorPart,
minorPart: !withZeroMinorPart && value % minority === 0 ? null : minorPart,
value: formattedValueStr,
currencySymbol: getCurrencySymbol(code),
};
Expand Down

0 comments on commit e86529d

Please sign in to comment.