Skip to content

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed Oct 3, 2023
1 parent d870784 commit 535a556
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { useMemo } from 'react';
import { NumberFormatter } from '@internationalized/number';
import Decimal from 'decimal.js-light';

// any number under this limit is considered very small decimal
const decimalLimit = 0.00000000009;

// number used to replace very small decimals with <$0.00000001
const overDecimalLimitIndicator = 0.00000001;

const standardLimitDigits = 6;

type UseCurrencyFormatterProps = {
currency?: string;
compact?: boolean;
Expand Down Expand Up @@ -54,20 +59,26 @@ const useCurrencyFormatter = (options: UseCurrencyFormatterProps = {}): UseCurre
return standard.format(value);
}

const isVerySmallDecimal = new Decimal(value).lte(decimalLimit);

// checks if decimal is lower than the limit
if (new Decimal(value).lte(decimalLimit)) {
if (isVerySmallDecimal) {
return `<${decimal.format(overDecimalLimitIndicator)}`;
}

const length = value.toFixed(0).length;

// checks if the number is solely decimal
const isOnlyDecimal = length === 1 && value < 1;

if (isOnlyDecimal) {
return decimal.format(value);
}

return length > 6 ? compact.format(value) : standard.format(value);
// any number above limit digits amount will be formatted with compact approach
const shouldCompact = length > standardLimitDigits;

return shouldCompact ? compact.format(value) : standard.format(value);
};
};

Expand Down

0 comments on commit 535a556

Please sign in to comment.