Skip to content

Commit

Permalink
style: 🚨 fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideSegullo committed Jun 26, 2024
1 parent 32a0ba2 commit 99f390f
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/components/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type ElementProps = {
value?: BigNumberish | null;
isLoading?: boolean;
fractionDigits?: number | null;
minimumFractionDigits?: number;
showSign?: ShowSign;
slotLeft?: React.ReactNode;
slotRight?: React.ReactNode;
Expand All @@ -88,7 +89,11 @@ type StyleProps = {
};

export type OutputProps = ElementProps & StyleProps;
export type FormatNumberProps = ElementProps & { decimal?: string; group?: string };
export type FormatNumberProps = ElementProps & {
minimumFractionDigits?: number;
decimal?: string;
group?: string;
};

export const formatNumber = (params: FormatNumberProps) => {
const {
Expand All @@ -101,6 +106,7 @@ export const formatNumber = (params: FormatNumberProps) => {
roundingMode = BigNumber.ROUND_HALF_UP,
decimal: LOCALE_DECIMAL_SEPARATOR,
group: LOCALE_GROUP_SEPARATOR,
minimumFractionDigits,
} = params;

const valueBN = MustBigNumber(value).abs();
Expand All @@ -126,6 +132,19 @@ export const formatNumber = (params: FormatNumberProps) => {
: {}),
};

const getFormattedVal = (
val: BigNumber,
fallbackDecimals: number,
formattingOptions?: FormattingOptions
) => {
const numDigits = fractionDigits ?? fallbackDecimals;
const precisionVal = minimumFractionDigits
? MustBigNumber(val.toPrecision(minimumFractionDigits, roundingMode)).abs()
: val;
const dp = minimumFractionDigits ? precisionVal.decimalPlaces() ?? numDigits : numDigits;
return precisionVal.toFormat(dp, roundingMode, { ...format, ...formattingOptions });
};

let formattedString: string = 'NaN';

switch (type) {
Expand All @@ -143,21 +162,13 @@ export const formatNumber = (params: FormatNumberProps) => {
.toLowerCase();
break;
case OutputType.Number:
formattedString = valueBN.toFormat(fractionDigits ?? 0, roundingMode, {
...format,
});
formattedString = getFormattedVal(valueBN, 0);
break;
case OutputType.Fiat:
formattedString = valueBN.toFormat(fractionDigits ?? USD_DECIMALS, roundingMode, {
...format,
prefix: '$',
});
formattedString = getFormattedVal(valueBN, USD_DECIMALS, { prefix: '$' });
break;
case OutputType.SmallFiat:
formattedString = valueBN.toFormat(fractionDigits ?? SMALL_USD_DECIMALS, roundingMode, {
...format,
prefix: '$',
});
formattedString = getFormattedVal(valueBN, SMALL_USD_DECIMALS, { prefix: '$' });
break;
case OutputType.CompactFiat:
if (!isNumber(value)) {
Expand All @@ -173,31 +184,18 @@ export const formatNumber = (params: FormatNumberProps) => {
.toLowerCase();
break;
case OutputType.Asset:
formattedString = valueBN.toFormat(fractionDigits ?? TOKEN_DECIMALS, roundingMode, {
...format,
});
formattedString = getFormattedVal(valueBN, TOKEN_DECIMALS);
break;
case OutputType.Percent:
formattedString = valueBN
.times(100)
.toFormat(fractionDigits ?? PERCENT_DECIMALS, roundingMode, {
...format,
suffix: '%',
});
formattedString = getFormattedVal(valueBN, PERCENT_DECIMALS, { suffix: '%' });
break;
case OutputType.SmallPercent:
formattedString = valueBN
.times(100)
.toFormat(fractionDigits ?? SMALL_PERCENT_DECIMALS, roundingMode, {
...format,
suffix: '%',
});
formattedString = getFormattedVal(valueBN.times(100), SMALL_PERCENT_DECIMALS, {
suffix: '%',
});
break;
case OutputType.Multiple:
formattedString = valueBN.toFormat(fractionDigits ?? LEVERAGE_DECIMALS, roundingMode, {
...format,
suffix: '×',
});
formattedString = getFormattedVal(valueBN, LEVERAGE_DECIMALS, { suffix: '×' });
break;
default:
break;
Expand All @@ -215,6 +213,7 @@ export const Output = ({
value,
isLoading,
fractionDigits,
minimumFractionDigits,
showSign = ShowSign.Negative,
slotLeft,
slotRight,
Expand Down Expand Up @@ -351,6 +350,7 @@ export const Output = ({
locale,
decimal: LOCALE_DECIMAL_SEPARATOR,
group: LOCALE_GROUP_SEPARATOR,
minimumFractionDigits,
});

const numberRenderers = {
Expand Down

0 comments on commit 99f390f

Please sign in to comment.