Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explorer-ui): formatAmount #2956

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/explorer-ui/components/misc/IconAndAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const IconAndAmount = ({
amount = formattedValue / (dec / 10 ** 6)
}

const displayAmount = amount ? formatAmount(amount) : '< 0.001'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Consider caching the result of formatAmount to avoid repeated calculations.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Ensure formatAmount handles all edge cases, especially with large or small numbers.


return (
<div className={`flex items-center ${className}`}>
<div className="flex flex-row items-center text-white">
Expand All @@ -35,11 +37,11 @@ export const IconAndAmount = ({
className={`${iconSize} min-w-[1rem] min-h-[1rem] inline rounded-full`}
/>
<div
data-tooltip-content={amount}
data-tooltip-content={displayAmount}
data-tooltip-id="amount"
className="flex-1 pl-1 mr-1 text-white"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check: Ensure that the formatAmount function handles all edge cases, especially with large or small numbers.

>
{formatAmount(amount)}
{displayAmount}
</div>
</div>
<span className="text-white">{tokenSymbol}</span>
Expand Down
88 changes: 74 additions & 14 deletions packages/explorer-ui/utils/formatAmount.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
import numeral from 'numeral'
interface FormatOptions {
fullAmount?: boolean
standardDigits?: number
useCompactNotation?: boolean
compactDigits?: number
minimumAmount?: number
roundingMode?: string
}

Comment on lines +1 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure roundingMode is validated.

The roundingMode property is a string, but its valid values are not documented or enforced. Consider using a union type or an enum to restrict possible values.

interface FormatOptions {
  fullAmount?: boolean
  standardDigits?: number
  useCompactNotation?: boolean
  compactDigits?: number
  minimumAmount?: number
  roundingMode?: 'ceil' | 'floor' | 'round'
}

export const formatAmount = (
amount: string,
options?: FormatOptions
): string => {
if (amount === '') {
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider validating the amount parameter.

The amount parameter is expected to be a string representing a number. Consider adding validation to ensure it is a valid numeric string before parsing.

export const formatAmount = (
  amount: string,
  options?: FormatOptions
): string => {
  if (isNaN(parseFloat(amount))) {
    throw new TypeError(`"${amount}" is not a valid numeric string`)
  }

return ''
}

Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return '0.0' for empty string input.

Instead of returning an empty string for an empty amount, consider returning '0.0' for consistency with other zero-value returns.

if (amount === '') {
  return '0.0'
}

const floatAmount = parseFloat(amount)

try {
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid re-parsing the amount.

The floatAmount is parsed twice in the function. Consider parsing it once and reusing the value.

const floatAmount = parseFloat(amount)
if (!Number.isFinite(floatAmount)) {
  throw new TypeError(`"${amount}" is not a finite number`)
}

if (!Number.isFinite(floatAmount)) {
throw new TypeError(`"${amount}" is not a finite number`)
}
} catch ({ name, message }) {
// console.error(name, message)
return amount
}

Comment on lines +24 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove try-catch block for finite check.

The try-catch block is unnecessary since the finite check can be done directly. Remove the try-catch block for simplicity.

if (!Number.isFinite(floatAmount)) {
  throw new TypeError(`"${amount}" is not a finite number`)
}

const fullAmount = options?.fullAmount ?? false
const standardDigits = options?.standardDigits ?? 4
const useCompactNotation = options?.useCompactNotation ?? true
const compactDigits = options?.compactDigits ?? (useCompactNotation ? 2 : 0)
const minimumAmount = options?.minimumAmount ?? 0.0001

const locales = 'en-UK'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making locales configurable.

The locales variable is hardcoded to 'en-UK'. Consider making it configurable through the FormatOptions interface.

const locales = options?.locales ?? 'en-UK'

if (!floatAmount) {
return '0.0'
}

if (fullAmount) {
return Intl.NumberFormat(locales).format(floatAmount)
}

Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding locale options for full amount.

When fullAmount is true, the locale should still be configurable.

if (fullAmount) {
  return Intl.NumberFormat(locales).format(floatAmount)
}

if (floatAmount < minimumAmount) {
return `< ${minimumAmount}`
}

export const formatAmount = (value) => {
numeral.nullFormat('--')
const absAmount = Math.abs(floatAmount)

if (absAmount < 0.0001) {
return Intl.NumberFormat(locales, {
maximumSignificantDigits: 1,
}).format(floatAmount)
}

Comment on lines +49 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure significant digits are configurable.

The significant digits for values less than 0.0001 are hardcoded to 1. Consider making this configurable.

if (absAmount < 0.0001) {
  return Intl.NumberFormat(locales, {
    maximumSignificantDigits: options?.significantDigits ?? 1,
  }).format(floatAmount)
}

if (absAmount < 1) {
return Intl.NumberFormat(locales, {
minimumFractionDigits: standardDigits,
}).format(floatAmount)
}

// Round up if the value is less than 0.001
if (value > 0 && value < 0.001) {
value = 0.001
if (absAmount < 1000) {
return Intl.NumberFormat(locales, {
minimumSignificantDigits: standardDigits,
maximumSignificantDigits: standardDigits,
}).format(floatAmount)
}

let format
if (value >= 1000) {
format = '0,0' // No decimal places for values 1000 or greater
} else if (value < 0.01) {
format = '0,0.000' // Four decimal places for values less than 0.01
} else {
format = '0,0.00' // Two decimal places for all other values
if (absAmount < 1000000) {
return Intl.NumberFormat(locales, {
maximumFractionDigits: 0,
}).format(floatAmount)
}

return numeral(value).format(format)
return Intl.NumberFormat(locales, {
minimumFractionDigits: compactDigits,
maximumFractionDigits: compactDigits,
notation: useCompactNotation ? 'compact' : 'standard',
}).format(floatAmount)
}
Loading