Simplify getHeader to operate on ChangelogSections

This commit is contained in:
Mario Campos
2026-09-18 12:24:19 -05:00
parent d63b2a40db
commit 128614ad8b
2 changed files with 21 additions and 13 deletions

View File

@@ -26,20 +26,26 @@ import { CHANGELOG_FILE } from "./config";
const testDate = new Date(2026, 7, 14);
describe("getHeader", async () => {
function Section(headerLine: string): ChangelogSection {
return {
headerLine,
bodyLines: [],
};
}
await it("returns non-headers unchanged", () => {
assert.equal("foo", getHeader("foo"));
assert.equal("- bar", getHeader("- bar"));
assert.equal("foo", getHeader(Section("foo")));
assert.equal("- bar", getHeader(Section("- bar")));
});
await it("strips octothorpes", async () => {
assert.equal("foo", getHeader("# foo"));
assert.equal("foo", getHeader("## foo"));
assert.equal("foo", getHeader("### foo"));
assert.equal("foo", getHeader("#### foo"));
assert.equal("foo", getHeader("##### foo"));
assert.equal("foo", getHeader("###### foo"));
assert.equal("foo", getHeader(Section("# foo")));
assert.equal("foo", getHeader(Section("## foo")));
assert.equal("foo", getHeader(Section("### foo")));
assert.equal("foo", getHeader(Section("#### foo")));
assert.equal("foo", getHeader(Section("##### foo")));
assert.equal("foo", getHeader(Section("###### foo")));
});
await it("strips whitespace", async () => {
assert.equal("foo", getHeader("# foo "));
assert.equal("foo", getHeader(Section("# foo ")));
});
});

View File

@@ -33,9 +33,11 @@ export interface Changelog {
sections: ChangelogSection[];
}
/** Returns the text of a CHANGELOG.md header (without the '## ' prefix). */
export function getHeader(headerLine: string): string {
return headerLine.replace(/^#+\s+/, "").trimEnd();
/**
* Returns the text of the header (without the '## ' prefix) of the given section.
* */
export function getHeader(section: ChangelogSection): string {
return section.headerLine.replace(/^#+\s+/, "").trimEnd();
}
/** Returns `date` formatted as `DD Mon YYYY`. */
@@ -147,7 +149,7 @@ export function addBodyLinesToUnreleasedSection(
lines: string[],
) {
const unreleasedSection = changelog.sections[0];
if (getHeader(unreleasedSection.headerLine) !== UNRELEASED_PLACEHOLDER) {
if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) {
throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'");
}