forked from dydxprotocol/v4-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compact, consistent asset amount rendering in orderbook + trades (
- Loading branch information
Showing
3 changed files
with
130 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { mapValues, range, zipObject } from 'lodash'; | ||
|
||
import { SUPPORTED_LOCALE_STRING_LABELS, SupportedLocales } from '@/constants/localization'; | ||
|
||
import { formatNumberOutput, OutputType } from '@/components/Output'; | ||
|
||
// for each locale, an array of the correct compact number suffix to use for 10^{index} | ||
// e.g. for "en" we have ['', '', '', 'k', 'k', 'k', 'm', 'm', 'm', 'b', 'b', 'b', 't', 't', 't'] | ||
const supportedLocaleToCompactSuffixByPowerOfTen = mapValues( | ||
SUPPORTED_LOCALE_STRING_LABELS, | ||
(name, lang) => | ||
range(15) | ||
.map((a) => | ||
Intl.NumberFormat(lang, { | ||
style: 'decimal', | ||
notation: 'compact', | ||
maximumSignificantDigits: 6, | ||
}).format(Math.abs(10 ** a)) | ||
) | ||
// first capture group grabs all the numbers with normal separator, then we grab any groups of whitespace+numbers | ||
// this is so we know which languages keep whitespace before the suffix | ||
.map((b) => b.replace(/(^[\d,.]+){1}(\s\d+)*/, '')) | ||
.map((b) => b.toLowerCase()) | ||
); | ||
|
||
const zipObjectFn = <T extends string, K>(arr: T[], valueGenerator: (val: T) => K) => | ||
zipObject( | ||
arr, | ||
arr.map((val) => valueGenerator(val)) | ||
); | ||
|
||
// for each locale, look up a given suffix (from map above) and get the correct power of ten to divide numbers by when using this suffix | ||
// e.g. for "en" if you look up "k" you get 3 (1000), if you look up "m" you get 6 (1,000,000) | ||
const supportedLocaleToSuffixPowers = mapValues( | ||
supportedLocaleToCompactSuffixByPowerOfTen, | ||
(values) => zipObjectFn([...new Set(values)], (f) => values.indexOf(f)) | ||
); | ||
|
||
export const getConsistentAssetSizeString = ( | ||
sizeToRender: number, | ||
{ | ||
decimalSeparator, | ||
groupSeparator, | ||
selectedLocale, | ||
stepSize, | ||
stepSizeDecimals, | ||
}: { | ||
selectedLocale: SupportedLocales; | ||
stepSizeDecimals: number; | ||
stepSize: number; | ||
decimalSeparator: string | undefined; | ||
groupSeparator: string | undefined; | ||
} | ||
) => { | ||
const { displayDivisor, displaySuffix } = (() => { | ||
if (stepSizeDecimals !== 0 || stepSize == null || stepSize < 10) { | ||
return { displayDivisor: 1, displaySuffix: '' }; | ||
} | ||
const unitToUse = | ||
supportedLocaleToCompactSuffixByPowerOfTen[selectedLocale][Math.floor(Math.log10(stepSize))]; | ||
if (unitToUse == null) { | ||
return { displayDivisor: 1, displaySuffix: '' }; | ||
} | ||
return { | ||
displayDivisor: 10 ** supportedLocaleToSuffixPowers[selectedLocale][unitToUse], | ||
displaySuffix: unitToUse, | ||
}; | ||
})(); | ||
return `${formatNumberOutput(sizeToRender / displayDivisor, OutputType.Number, { | ||
decimalSeparator, | ||
groupSeparator, | ||
fractionDigits: stepSizeDecimals, | ||
})}${displaySuffix}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters