|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +import { formatNumber, NUMBER_NOTATIONS, NumberNotation } from "./formatNumber" |
| 4 | +import { SUPPORTED_LOCALES, SupportedLocale } from "./locales" |
| 5 | + |
| 6 | +describe("formatNumber", () => { |
| 7 | + describe("using browser locale en-IN", () => { |
| 8 | + beforeEach(() => { |
| 9 | + vi.stubGlobal("navigator", { language: "en-IN" }) |
| 10 | + }) |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + vi.unstubAllGlobals() |
| 14 | + }) |
| 15 | + |
| 16 | + const testCasesLocaleEnIn: [number, Record<NumberNotation, string>][] = [ |
| 17 | + [-10_000, { standard: "-10,000", compact: "-10K", scientific: "-1E4", engineering: "-10E3" }], |
| 18 | + [0, { standard: "0", compact: "0", scientific: "0E0", engineering: "0E0" }], |
| 19 | + [0.034, { standard: "0.034", compact: "0.034", scientific: "3.4E-2", engineering: "34E-3" }], |
| 20 | + [4, { standard: "4", compact: "4", scientific: "4E0", engineering: "4E0" }], |
| 21 | + [5.789, { standard: "5.789", compact: "5.8", scientific: "5.789E0", engineering: "5.789E0" }], |
| 22 | + [180, { standard: "180", compact: "180", scientific: "1.8E2", engineering: "180E0" }], |
| 23 | + [1_337, { standard: "1,337", compact: "1.3K", scientific: "1.337E3", engineering: "1.337E3" }], |
| 24 | + [42_866.29, { standard: "42,866.29", compact: "43K", scientific: "4.287E4", engineering: "42.866E3" }], |
| 25 | + [ |
| 26 | + 77_502_040_708, |
| 27 | + { standard: "77,50,20,40,708", compact: "7.8KCr", scientific: "7.75E10", engineering: "77.502E9" }, |
| 28 | + ], |
| 29 | + ] |
| 30 | + |
| 31 | + testCasesLocaleEnIn.forEach(([n, expectedForNotation]) => { |
| 32 | + NUMBER_NOTATIONS.forEach((notation) => { |
| 33 | + it(`formats ${n} in ${notation} notation`, () => { |
| 34 | + expect(formatNumber(n, { locale: "browser", notation })).eq(expectedForNotation[notation]) |
| 35 | + }) |
| 36 | + }) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe("using locale de", () => { |
| 41 | + const testCasesLocaleDe: [number, Record<NumberNotation, string>][] = [ |
| 42 | + [-10_000, { standard: "-10.000", compact: "-10.000", scientific: "-1E4", engineering: "-10E3" }], |
| 43 | + [0, { standard: "0", compact: "0", scientific: "0E0", engineering: "0E0" }], |
| 44 | + [0.034, { standard: "0,034", compact: "0,034", scientific: "3,4E-2", engineering: "34E-3" }], |
| 45 | + [4, { standard: "4", compact: "4", scientific: "4E0", engineering: "4E0" }], |
| 46 | + [5.789, { standard: "5,789", compact: "5,8", scientific: "5,789E0", engineering: "5,789E0" }], |
| 47 | + [180, { standard: "180", compact: "180", scientific: "1,8E2", engineering: "180E0" }], |
| 48 | + [1_337, { standard: "1.337", compact: "1337", scientific: "1,337E3", engineering: "1,337E3" }], |
| 49 | + [42_866.29, { standard: "42.866,29", compact: "42.866", scientific: "4,287E4", engineering: "42,866E3" }], |
| 50 | + [ |
| 51 | + 77_502_040_708, |
| 52 | + { standard: "77.502.040.708", compact: "78\u00A0Mrd.", scientific: "7,75E10", engineering: "77,502E9" }, |
| 53 | + ], |
| 54 | + ] |
| 55 | + |
| 56 | + testCasesLocaleDe.forEach(([n, expectedForNotation]) => { |
| 57 | + NUMBER_NOTATIONS.forEach((notation) => { |
| 58 | + it(`formats ${n} in ${notation} notation`, () => { |
| 59 | + expect(formatNumber(n, { locale: "de", notation })).eq(expectedForNotation[notation]) |
| 60 | + }) |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + const testCasesStandardNotation: Record<SupportedLocale, string> = { |
| 66 | + "en-IN": "49,72,92,23,812", |
| 67 | + "en-US": "49,729,223,812", |
| 68 | + "en-GB": "49,729,223,812", |
| 69 | + "en-AU": "49,729,223,812", |
| 70 | + fr: "49\u202F729\u202F223\u202F812", |
| 71 | + de: "49.729.223.812", |
| 72 | + es: "49.729.223.812", |
| 73 | + "es-MX": "49,729,223,812", |
| 74 | + "pt-BR": "49.729.223.812", |
| 75 | + it: "49.729.223.812", |
| 76 | + "zh-CN": "49,729,223,812", |
| 77 | + ru: "49\u00A0729\u00A0223\u00A0812", |
| 78 | + ja: "49,729,223,812", |
| 79 | + ar: "49,729,223,812", |
| 80 | + } |
| 81 | + |
| 82 | + SUPPORTED_LOCALES.forEach((locale) => { |
| 83 | + it(`formats 49,729,223,812 in standard notation for locale ${locale}`, () => { |
| 84 | + expect(formatNumber(49_729_223_812, { locale, notation: "standard" })).eq(testCasesStandardNotation[locale]) |
| 85 | + }) |
| 86 | + }) |
| 87 | +}) |
0 commit comments