Skip to content

Commit 128614a

Browse files
committed
Simplify getHeader to operate on ChangelogSections
1 parent d63b2a4 commit 128614a

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎pr-checks/changelog.test.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,26 @@ import { CHANGELOG_FILE } from "./config";
2626
const testDate = new Date(2026, 7, 14);
2727

2828
describe("getHeader", async () => {
29+
function Section(headerLine: string): ChangelogSection {
30+
return {
31+
headerLine,
32+
bodyLines: [],
33+
};
34+
}
2935
await it("returns non-headers unchanged", () => {
30-
assert.equal("foo", getHeader("foo"));
31-
assert.equal("- bar", getHeader("- bar"));
36+
assert.equal("foo", getHeader(Section("foo")));
37+
assert.equal("- bar", getHeader(Section("- bar")));
3238
});
3339
await it("strips octothorpes", async () => {
34-
assert.equal("foo", getHeader("# foo"));
35-
assert.equal("foo", getHeader("## foo"));
36-
assert.equal("foo", getHeader("### foo"));
37-
assert.equal("foo", getHeader("#### foo"));
38-
assert.equal("foo", getHeader("##### foo"));
39-
assert.equal("foo", getHeader("###### foo"));
40+
assert.equal("foo", getHeader(Section("# foo")));
41+
assert.equal("foo", getHeader(Section("## foo")));
42+
assert.equal("foo", getHeader(Section("### foo")));
43+
assert.equal("foo", getHeader(Section("#### foo")));
44+
assert.equal("foo", getHeader(Section("##### foo")));
45+
assert.equal("foo", getHeader(Section("###### foo")));
4046
});
4147
await it("strips whitespace", async () => {
42-
assert.equal("foo", getHeader("# foo "));
48+
assert.equal("foo", getHeader(Section("# foo ")));
4349
});
4450
});
4551

‎pr-checks/changelog.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ export interface Changelog {
3333
sections: ChangelogSection[];
3434
}
3535

36-
/** Returns the text of a CHANGELOG.md header (without the '## ' prefix). */
37-
export function getHeader(headerLine: string): string {
38-
return headerLine.replace(/^#+\s+/, "").trimEnd();
36+
/**
37+
* Returns the text of the header (without the '## ' prefix) of the given section.
38+
* */
39+
export function getHeader(section: ChangelogSection): string {
40+
return section.headerLine.replace(/^#+\s+/, "").trimEnd();
3941
}
4042

4143
/** Returns `date` formatted as `DD Mon YYYY`. */
@@ -147,7 +149,7 @@ export function addBodyLinesToUnreleasedSection(
147149
lines: string[],
148150
) {
149151
const unreleasedSection = changelog.sections[0];
150-
if (getHeader(unreleasedSection.headerLine) !== UNRELEASED_PLACEHOLDER) {
152+
if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) {
151153
throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'");
152154
}
153155

0 commit comments

Comments
 (0)