import { TestCase } from "./framework/test_case.js"; import { groupDigits, compact, formatNumber, splitCompact, hours, improvements } from "../shared/text_format.js"; export class TextFormatTest extends TestCase { test_number_and_list_formatting() { this.assertEqual(groupDigits(0), "0"); this.assertEqual(groupDigits(123), "123"); // The separators are non-breaking spaces, so a number never wraps. this.assertEqual(groupDigits(1234), "1\u00a0234"); this.assertEqual(groupDigits(1234567), "1\u00a0234\u00a0567"); this.assertEqual(groupDigits(-1234), "-1\u00a0234"); this.assertEqual(compact(0), "0"); this.assertEqual(compact(999), "999"); this.assertEqual(compact(1234), "1.23K"); this.assertEqual(compact(1234567), "1.23M"); this.assertEqual(compact(999950), "1M"); this.assertEqual(compact(-2500), "-2.5K"); this.assertEqual(hours(0.5), "0.50 h"); this.assertEqual(hours(2.25), "2.3 h"); this.assertEqual(improvements([]), "None"); this.assertEqual(improvements(["Eiffel Tower", "Louvre"]), "Eiffel Tower, Louvre"); } test_the_canonical_number_carries_a_sign_and_decimals() { this.assertEqual(formatNumber(0), "0"); this.assertEqual(formatNumber(1234), "1\u00a0234"); this.assertEqual(formatNumber(1234567), "1\u00a0234\u00a0567"); this.assertEqual(formatNumber(-1234), "\u22121\u00a0234", "a stock still shows its sign"); this.assertEqual(formatNumber(1234, { signed: true }), "+1\u00a0234"); this.assertEqual(formatNumber(-1234, { signed: true }), "\u22121\u00a0234"); this.assertEqual(formatNumber(1.5, { decimals: 2 }), "1.50"); this.assertEqual(formatNumber(1234.567, { decimals: 2 }), "1\u00a0234.57"); } test_a_compact_figure_splits_into_number_and_suffix() { this.assertEqual(splitCompact(999), { number: "999", suffix: "" }); this.assertEqual(splitCompact(1234), { number: "1.23", suffix: "K" }); this.assertEqual(splitCompact(1234567), { number: "1.23", suffix: "M" }); this.assertEqual(splitCompact(-2500), { number: "\u22122.5", suffix: "K" }); } }