refactor(pdf): deduplicate parseFiniteNumber/parsePxValue/parseFontSize

Export parseFiniteNumber and parsePxValue from icon-size.ts (already the
canonical home of these helpers). Remove the three private copies in
rich-text-spacing.ts and import the shared ones instead.

Claude-Session: https://claude.ai/code/session_012Bnvt1MghwHj4qQRxuQUGa
This commit is contained in:
Amruth Pillai
2026-07-04 19:49:39 +02:00
parent 7e35e8b657
commit fcc10c6b31
2 changed files with 4 additions and 16 deletions
@@ -2,10 +2,10 @@ import type { Style } from "@react-pdf/types";
import type { StyleInput } from "./styles";
import { composeStyles } from "./styles";
const parseFiniteNumber = (value: unknown): number | undefined =>
export const parseFiniteNumber = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? value : undefined;
const parsePxValue = (value: unknown): number | undefined => {
export const parsePxValue = (value: unknown): number | undefined => {
if (typeof value !== "string" || !value.endsWith("px")) return undefined;
const parsedValue = Number.parseFloat(value);
@@ -1,6 +1,7 @@
import type { Style } from "@react-pdf/types";
import type { StyleInput } from "./styles";
import { NodeType } from "node-html-parser";
import { parseFiniteNumber, parsePxValue, parseStyleFontSize } from "./icon-size";
import { composeStyles } from "./styles";
type RichTextSpacingElement = {
@@ -18,19 +19,6 @@ type RichTextProseSpacing = {
listItem: Style;
};
const parseFiniteNumber = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? value : undefined;
const parsePxValue = (value: unknown): number | undefined => {
if (typeof value !== "string" || !value.endsWith("px")) return undefined;
const parsedValue = Number.parseFloat(value);
return Number.isFinite(parsedValue) ? parsedValue : undefined;
};
const parseFontSize = (fontSize: Style["fontSize"]): number | undefined =>
parseFiniteNumber(fontSize) ?? parsePxValue(fontSize);
const parseLineHeight = (
lineHeight: Style["lineHeight"],
): { type: "multiplier" | "absolute"; value: number } | undefined => {
@@ -129,7 +117,7 @@ export const resolveRichTextBodyLineHeight = (...styles: StyleInput[]): number |
let bodyLineHeight: ReturnType<typeof parseLineHeight>;
for (const style of composeStyles(...styles)) {
const fontSize = parseFontSize(style.fontSize);
const fontSize = parseStyleFontSize(style.fontSize);
if (fontSize !== undefined) bodyFontSize = fontSize;
const lineHeight = parseLineHeight(style.lineHeight);