Skip to content

Commit 3535a04

Browse files
authored
feat(chore): add option to strip additional lines in summary (#59)
1 parent 2747c78 commit 3535a04

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

packages/core/src/countBallotsFromGit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export default async function countFromGit<T extends BufferSource = BufferSource
233233

234234
await Promise.all(decryptPromises);
235235

236-
const result = vote.count({ discardedCommits });
236+
const result = vote.count({ discardedCommits, keepOnlyFirstLineInSummary: vote.voteFileData.keepOnlyFirstLineInSummary });
237237

238238
if (commitJsonSummary != null) {
239239
if (lastCommitRef !== "HEAD") {

packages/core/src/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface VoteFileFormat {
1616
checksum?: string;
1717

1818
canShuffleCandidates?: boolean;
19+
keepOnlyFirstLineInSummary?: boolean;
1920
requireSignedBallots?: boolean;
2021
}
2122
function instanceOfVoteFile(object): object is VoteFileFormat {

packages/core/src/summary/condorcetSummary.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ interface BallotSummarize {
7373
minCandidates?: VoteCandidate[];
7474
orderedCandidates?: VoteCandidate[][];
7575
}
76-
export function getSummarizedBallot(ballot: Ballot): BallotSummarize {
76+
export function getSummarizedBallot(
77+
ballot: Ballot,
78+
keepOnlyFirstLineInSummary?: boolean,
79+
): BallotSummarize {
7780
let maxNote = Number.MIN_SAFE_INTEGER;
7881
let minNote = Number.MAX_SAFE_INTEGER;
7982
for (const [, score] of ballot.preferences) {
@@ -91,7 +94,8 @@ export function getSummarizedBallot(ballot: Ballot): BallotSummarize {
9194
for (const [candidate, score] of ballot.preferences) {
9295
const candidatesForThisScore = orderedPreferences.get(score);
9396
const markdownCandidate = `**${cleanMarkdown(
94-
candidate.replace(/\.$/, ""),
97+
candidate,
98+
keepOnlyFirstLineInSummary,
9599
)}**`;
96100
if (candidatesForThisScore == null) {
97101
orderedPreferences.set(score, [markdownCandidate]);
@@ -106,7 +110,7 @@ export function getSummarizedBallot(ballot: Ballot): BallotSummarize {
106110
};
107111
}
108112
const group = score === minNote ? minCandidates : maxCandidates;
109-
group.push(`**${cleanMarkdown(candidate).replace(/\.$/, "")}**`);
113+
group.push(`**${cleanMarkdown(candidate, keepOnlyFirstLineInSummary)}**`);
110114
}
111115
return { minCandidates, maxCandidates };
112116
}
@@ -116,7 +120,7 @@ export default class CondorcetElectionSummary extends ElectionSummary {
116120

117121
summarizeBallot(ballot: Ballot) {
118122
return summarizeCondorcetBallotForElectionSummary(
119-
getSummarizedBallot(ballot),
123+
getSummarizedBallot(ballot, this.keepOnlyFirstLineInSummary),
120124
4,
121125
);
122126
}

packages/core/src/summary/electionSummary.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import type { Actor, Ballot, VoteCandidate, VoteCommit } from "../vote";
22
import type { CandidateScores } from "../votingMethods/VotingMethodImplementation";
33
import cleanMarkdown from "../utils/cleanMarkdown.js";
44

5-
function displayWinners(winners: VoteCandidate[]) {
5+
function displayWinners(winners: VoteCandidate[], keepOnlyFirstLineInSummary?: boolean) {
66
if (winners.length === 0) return "None.";
7-
if (winners.length === 1) return cleanMarkdown(winners[0]);
7+
if (winners.length === 1) return cleanMarkdown(winners[0], keepOnlyFirstLineInSummary);
88
const delimiter = "\n - ";
9-
return delimiter + winners.map(cleanMarkdown).join(delimiter);
9+
return delimiter + winners.map(
10+
keepOnlyFirstLineInSummary
11+
? l => cleanMarkdown(l, keepOnlyFirstLineInSummary)
12+
: l => cleanMarkdown(l).split("\n").join("\n "),
13+
).join(delimiter);
1014
}
1115

1216
export interface DiscardedCommit {
@@ -23,6 +27,7 @@ export interface ElectionSummaryOptions {
2327
ballots: Ballot[];
2428
privateKey: string;
2529
discardedCommits?: DiscardedCommit[];
30+
keepOnlyFirstLineInSummary?: boolean;
2631
}
2732
export default abstract class ElectionSummary {
2833
subject: string;
@@ -35,6 +40,7 @@ export default abstract class ElectionSummary {
3540
privateKey: string;
3641
participants: Actor[];
3742
discardedCommits: DiscardedCommit[];
43+
keepOnlyFirstLineInSummary: boolean;
3844

3945
abstract scoreText: string;
4046

@@ -48,6 +54,7 @@ export default abstract class ElectionSummary {
4854
ballots: unsortedBallots,
4955
privateKey,
5056
discardedCommits,
57+
keepOnlyFirstLineInSummary,
5158
}: ElectionSummaryOptions) {
5259
this.subject = subject;
5360
this.startDate = startDate;
@@ -56,6 +63,7 @@ export default abstract class ElectionSummary {
5663
this.winners = winners;
5764
this.result = result;
5865
this.discardedCommits = discardedCommits;
66+
this.keepOnlyFirstLineInSummary = !!keepOnlyFirstLineInSummary;
5967

6068
this.sortedBallots = unsortedBallots
6169
.slice()
@@ -80,6 +88,7 @@ ${this.startDate ? `**Start date**: ${this.startDate} \n` : ""}**End date**: ${
8088
8189
**Winning candidate${this.winners.length === 1 ? "" : "s"}**: ${displayWinners(
8290
this.winners,
91+
this.keepOnlyFirstLineInSummary,
8392
)}
8493
8594
### Table of results
@@ -88,7 +97,7 @@ ${this.startDate ? `**Start date**: ${this.startDate} \n` : ""}**End date**: ${
8897
| --------- | ------------------- |
8998
${Array.from(this.result)
9099
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
91-
.map(result => `| ${cleanMarkdown(result[0])} | ${result[1]} |`)
100+
.map(result => `| ${cleanMarkdown(result[0], this.keepOnlyFirstLineInSummary)} | ${result[1]} |`)
92101
.join("\n")}
93102
94103
## Voting data

packages/core/src/utils/cleanMarkdown.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ function cleanUnsupportedMarkdown(txt: string): string {
33
return txt.replace(/([_~*\\[\]<>`])/g, "\\$1");
44
}
55

6-
export default function cleanMarkdown(txt: string): string {
6+
function reduceToFirstLine(candidate: string) {
7+
const i = candidate.indexOf("\n");
8+
return i === -1 ? candidate : candidate.slice(0, i);
9+
}
10+
export default function cleanMarkdown(txt: string, keepOnlyFirstLineInSummary?: boolean): string {
11+
if (keepOnlyFirstLineInSummary) txt = reduceToFirstLine(txt);
12+
if (txt.endsWith(".") || txt.endsWith(":")) txt = txt.slice(0, -1);
13+
714
// Escape backticks for edge case scenarii (no code span support).
815
if (txt.includes("``") || txt.includes("\\`")) {
916
return cleanUnsupportedMarkdown(txt);

0 commit comments

Comments
 (0)