diff options
author | Khuddite <62555977+khuddite@users.noreply.github.com> | 2024-11-23 14:12:25 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-23 19:12:25 +0000 |
commit | e4284744785495c5832234c79703c1a2f8052b8b (patch) | |
tree | b464e59dacc1e2e5aa444a282cf096eaa915756d /src/view/com/util/numeric/__tests__/format-test.ts | |
parent | 4dd62e24ea6e2fc3904678ca7af1236972e067e0 (diff) | |
download | voidsky-e4284744785495c5832234c79703c1a2f8052b8b.tar.zst |
Fix inconsistent number formatting between mobile and web (#6384)
* Manual truncation & identify factor points for each lang * Reduce indirection * Add test Co-authored-by: khuddite <biliie811028@hotmail.com> * Handle big numbers, clarify special case * Clarify the reason --------- Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Diffstat (limited to 'src/view/com/util/numeric/__tests__/format-test.ts')
-rw-r--r-- | src/view/com/util/numeric/__tests__/format-test.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/view/com/util/numeric/__tests__/format-test.ts b/src/view/com/util/numeric/__tests__/format-test.ts new file mode 100644 index 000000000..74df4be4c --- /dev/null +++ b/src/view/com/util/numeric/__tests__/format-test.ts @@ -0,0 +1,92 @@ +import {describe, expect, it} from '@jest/globals' + +import {APP_LANGUAGES} from '#/locale/languages' +import {formatCount} from '../format' + +const formatCountRound = (locale: string, num: number) => { + const options: Intl.NumberFormatOptions = { + notation: 'compact', + maximumFractionDigits: 1, + } + return new Intl.NumberFormat(locale, options).format(num) +} + +const formatCountTrunc = (locale: string, num: number) => { + const options: Intl.NumberFormatOptions = { + notation: 'compact', + maximumFractionDigits: 1, + // @ts-ignore + roundingMode: 'trunc', + } + return new Intl.NumberFormat(locale, options).format(num) +} + +// prettier-ignore +const testNums = [ + 1, + 5, + 9, + 11, + 55, + 99, + 111, + 555, + 999, + 1111, + 5555, + 9999, + 11111, + 55555, + 99999, + 111111, + 555555, + 999999, + 1111111, + 5555555, + 9999999, + 11111111, + 55555555, + 99999999, + 111111111, + 555555555, + 999999999, + 1111111111, + 5555555555, + 9999999999, + 11111111111, + 55555555555, + 99999999999, + 111111111111, + 555555555555, + 999999999999, + 1111111111111, + 5555555555555, + 9999999999999, + 11111111111111, + 55555555555555, + 99999999999999, + 111111111111111, + 555555555555555, + 999999999999999, + 1111111111111111, + 5555555555555555, +] + +describe('formatCount', () => { + for (const appLanguage of APP_LANGUAGES) { + const locale = appLanguage.code2 + it('truncates for ' + locale, () => { + const mockI8nn = { + locale, + number(num: number) { + return formatCountRound(locale, num) + }, + } + for (const num of testNums) { + const formatManual = formatCount(mockI8nn as any, num) + const formatOriginal = formatCountTrunc(locale, num) + expect(formatManual).toEqual(formatOriginal) + } + }) + } +}) |