fix(shared): default non-finite string sample limits

This commit is contained in:
Peter Steinberger
2026-05-28 22:35:00 -04:00
parent 19d9e71b84
commit 7f6579e416
2 changed files with 11 additions and 1 deletions

View File

@@ -43,6 +43,15 @@ describe("summarizeStringEntries", () => {
).toBe("a, b, c, d, e, f (+1)");
});
it("uses the default limit for non-finite limits", () => {
expect(
summarizeStringEntries({
entries: ["a", "b", "c", "d", "e", "f", "g"],
limit: Number.NaN,
}),
).toBe("a, b, c, d, e, f (+1)");
});
it("does not add a suffix when the limit exactly matches the entry count", () => {
expect(
summarizeStringEntries({

View File

@@ -7,7 +7,8 @@ export function summarizeStringEntries(params: {
if (entries.length === 0) {
return params.emptyText ?? "";
}
const limit = Math.max(1, Math.floor(params.limit ?? 6));
const rawLimit = params.limit ?? 6;
const limit = Number.isFinite(rawLimit) ? Math.max(1, Math.floor(rawLimit)) : 6;
const sample = entries.slice(0, limit);
const suffix = entries.length > sample.length ? ` (+${entries.length - sample.length})` : "";
return `${sample.join(", ")}${suffix}`;