From 575970459e9b3109ab0ecf1032aef3bf7dcc54e5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:12:33 +0000 Subject: [PATCH] Refactor overly long formatValue function into specialized formatting functions Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/engine/formatter.ts | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/engine/formatter.ts b/src/engine/formatter.ts index 9f46005..cc656dd 100644 --- a/src/engine/formatter.ts +++ b/src/engine/formatter.ts @@ -1,6 +1,6 @@ // Turns values into display strings: unit/currency formats plus // precision and grouping settings. -import { Decimal, DateVal, EngineSettings, Value } from "./types"; +import { Decimal, DateVal, EngineSettings, Value, Quantity } from "./types"; // Intl.DateTimeFormat construction is relatively expensive and every line with // a date rebuilds one on each render — cache by locale+options instead. @@ -50,29 +50,22 @@ export function formatNumber(v: Decimal, s: EngineSettings): string { return frac !== undefined ? intG + s.decimalSeparator + frac : intG; } -export function formatValue(v: Value, s: EngineSettings): string { - if (v.kind === "chart") return ""; - - if (v.kind === "percent") { - return formatNumber(v.value, s) + "%"; +function formatDate(v: DateVal, s: EngineSettings): string { + const opts: Intl.DateTimeFormatOptions = { timeZone: v.timeZone }; + if (v.timeOnly) { + return dtf(undefined, { ...opts, hour: "2-digit", minute: "2-digit", hour12: false }).format(v.ms); } - - if (v.kind === "date") { - const opts: Intl.DateTimeFormatOptions = { timeZone: v.timeZone }; - if (v.timeOnly) { - return dtf(undefined, { ...opts, hour: "2-digit", minute: "2-digit", hour12: false }).format(v.ms); - } - if (s.dateFormat !== "system") return formatDateFixed(v, s.dateFormat); - if (v.hasTime) { - return dtf(undefined, { - ...opts, hour: "2-digit", minute: "2-digit", hour12: false, - day: "numeric", month: "short", year: "numeric", - }).format(v.ms); - } - return dtf(undefined, { ...opts, day: "numeric", month: "long", year: "numeric" }).format(v.ms); + if (s.dateFormat !== "system") return formatDateFixed(v, s.dateFormat); + if (v.hasTime) { + return dtf(undefined, { + ...opts, hour: "2-digit", minute: "2-digit", hour12: false, + day: "numeric", month: "short", year: "numeric", + }).format(v.ms); } + return dtf(undefined, { ...opts, day: "numeric", month: "long", year: "numeric" }).format(v.ms); +} - // quantity +function formatQuantity(v: Quantity, s: EngineSettings): string { if (v.repr === "hex" || v.repr === "binary" || v.repr === "octal") { if (v.value.abs().gte(new Decimal("1e10000"))) { return formatNumber(v.value, s); @@ -103,6 +96,13 @@ export function formatValue(v: Value, s: EngineSettings): string { return withUnit(num, v.unit.format); } +export function formatValue(v: Value, s: EngineSettings): string { + if (v.kind === "chart") return ""; + if (v.kind === "percent") return formatNumber(v.value, s) + "%"; + if (v.kind === "date") return formatDate(v, s); + return formatQuantity(v, s); +} + function withUnit(num: string, f: string): string { if (f.includes("{}")) { return f.replace("{}", num); // currency template