refactor(dashboard): extract formatters → src/lib/format.ts#30
Merged
Conversation
First real TS module after the build-infra PR. Moves the five pure helpers (fmtUsd, fmtPct, fmtNum, fmtBytes, fmtRelativeTime) out of the 4768-line god file and into a typed leaf module. They had no dependencies and ~6 panel call-sites, so this validates the extraction pattern without touching state, polling, or buses. - dashboard/src/lib/format.ts (new) — five exports, fully typed. - dashboard/app.js — drops 35 lines of inline definitions, imports from "./src/lib/format". File-header comment shrunk to one line and corrected (no longer claims "no build step"). - tests/dashboard-format.test.ts (new) — 17 cases covering the null/undefined branches, locale separators, byte-unit boundaries, and the time-band edges; mocks Date.now where needed. Bundle output verified: format functions appear once each in dashboard/dist/app.js, CDN imports stay external. 1682/1682 tests pass (was 1665; +17 from this PR).
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
First real TS module extraction after the build-infra PR (#29). Validates that the new pipeline (tsup bundle,
dashboard/src/lib/, layered imports) actually works for code that ships, not just on paper.The five formatters (
fmtUsd,fmtPct,fmtNum,fmtBytes,fmtRelativeTime) are the lowest-blast-radius slice in the entire god file:Closes part of #28 (Stage 1 PR 1.0b).
Changes
dashboard/src/lib/format.ts(new) — five typed exports. Inputs arenumber | null | undefined(orstring | number | null | undefinedforfmtRelativeTime); return type narrows tostring. Bodies are byte-equivalent to the originals.dashboard/app.js— drops the inline definitions, imports from./src/lib/format. File-header comment shrunk from a 4-line essay to one line and corrected (no longer claims "no build step").tests/dashboard-format.test.ts(new) — 17 cases covering the null/undefined branches, locale separators, byte-unit boundaries, and the time-band edges (just-now / m / h / d / fallback ISO). MocksDate.now()for deterministic relative-time tests.Bundle verification
Built locally;
dashboard/dist/app.jscontains exactly one copy of each function (esbuild inlines the lib module), CDN imports stay external as before. Bundle size unchanged within rounding.Test plan
npm run build— bundles cleanly, format functions inlined, CDN imports preserved.npm run typecheck— both root + dashboard projects clean.npm run lint— clean (6 pre-existing warnings unrelated).npm test— 1682/1682 (was 1665; +17 from the new test file).What's next
Once this lands, Stage 2 (drop the editor surface, ~−500 LoC) and Stage 3 (extract
api(),usePoll, buses, error boundary, markdown) start in order. Each panel migration in Stage 4 will follow the same shape this PR established.