Fix "undefined" in PF2e stat block weaknesses/resistances
All checks were successful
CI / check (push) Successful in 2m23s
CI / build-image (push) Successful in 30s

Some PF2e creatures (e.g. Giant Mining Bee) have qualitative
weaknesses without a numeric amount, causing "undefined" to
render in the stat block. Handle missing amounts gracefully.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-04-07 12:06:22 +02:00
parent e62c49434c
commit 8dbff66ce1
2 changed files with 109 additions and 14 deletions

View File

@@ -40,8 +40,8 @@ interface RawDefenses {
};
hp?: { hp?: number }[];
immunities?: (string | { name: string })[];
resistances?: { amount: number; name: string; note?: string }[];
weaknesses?: { amount: number; name: string; note?: string }[];
resistances?: { amount?: number; name: string; note?: string }[];
weaknesses?: { amount?: number; name: string; note?: string }[];
}
interface RawAbility {
@@ -150,31 +150,35 @@ function formatImmunities(
function formatResistances(
resistances:
| readonly { amount: number; name: string; note?: string }[]
| readonly { amount?: number; name: string; note?: string }[]
| undefined,
): string | undefined {
if (!resistances || resistances.length === 0) return undefined;
return resistances
.map((r) =>
r.note
? `${capitalize(r.name)} ${r.amount} (${r.note})`
: `${capitalize(r.name)} ${r.amount}`,
)
.map((r) => {
const base =
r.amount == null
? capitalize(r.name)
: `${capitalize(r.name)} ${r.amount}`;
return r.note ? `${base} (${r.note})` : base;
})
.join(", ");
}
function formatWeaknesses(
weaknesses:
| readonly { amount: number; name: string; note?: string }[]
| readonly { amount?: number; name: string; note?: string }[]
| undefined,
): string | undefined {
if (!weaknesses || weaknesses.length === 0) return undefined;
return weaknesses
.map((w) =>
w.note
? `${capitalize(w.name)} ${w.amount} (${w.note})`
: `${capitalize(w.name)} ${w.amount}`,
)
.map((w) => {
const base =
w.amount == null
? capitalize(w.name)
: `${capitalize(w.name)} ${w.amount}`;
return w.note ? `${base} (${w.note})` : base;
})
.join(", ");
}