Results export as plain text: procès-verbal, compact, liste, barres, markdown and csv templates with a preview, copy and download, from the share tab or a closed question; tally maths shared with the results bars

This commit is contained in:
2026-11-19 01:01:54 +01:00
parent 3ebbb67817
commit 41b4ac1a78
5 changed files with 258 additions and 18 deletions
+148
View File
@@ -0,0 +1,148 @@
// plain text results export, pure functions (no dom) so node can run them too
export function tally(q) {
const r = q.results;
const ex = r.extra || { counts: r.counts.map(() => 0), blank: 0, ballots: 0 };
const extra = r.counts.map((_, i) => ex.counts[i] || 0);
const tot = r.counts.map((c, i) => c + extra[i]);
const ballots = r.ballots + ex.ballots, blank = r.blank + ex.blank;
return { phone: r.counts, extra, tot, ballots, blank, base: ballots - blank, exBallots: ex.ballots, exBlank: ex.blank };
}
export function pct(n, d) {
return d ? Math.round((n / d) * 1000) / 10 : 0;
}
// fr-FR toLocaleString puts a narrow nbsp in thousands, which breaks column alignment
const num = (n) => String(n).replace(".", ",");
const pctTxt = (n, d) => `${num(pct(n, d))} %`;
const plural = (n, word) => `${n} ${word}${n > 1 ? "s" : ""}`;
function foot(q, t) {
let s = plural(t.ballots, "bulletin");
if (q.allow_blank) s += `, dont ${plural(t.blank, "blanc")}`;
if (t.exBallots) s += `, dont ${t.exBallots} ajouté${t.exBallots > 1 ? "s" : ""} à la main`;
return s;
}
const nb = (qs, k) => (qs.length > 1 ? `${k + 1}. ` : "");
const kindNote = (q) => (q.kind === "multi" ? `Choix multiple, ${q.max_choices} max` : "");
function header(poll, voters, now) {
return [poll.title, `Résultats du ${now} · ${plural(voters, "inscription")}`];
}
// visible length, emoji and accents count as one column
const width = (s) => [...s].length;
const padEnd = (s, n) => s + " ".repeat(Math.max(0, n - width(s)));
const padStart = (s, n) => " ".repeat(Math.max(0, n - width(s))) + s;
function rows(q, t) {
const out = q.options.map((label, i) => ({ label, n: t.tot[i], p: pctTxt(t.tot[i], t.base) }));
if (q.allow_blank) out.push({ label: "Blanc", n: t.blank, p: "" });
return out;
}
function minutes(poll, voters, qs, now) {
const out = [...header(poll, voters, now)];
qs.forEach((q, k) => {
const t = tally(q), rs = rows(q, t);
const lw = Math.max(...rs.map((r) => width(r.label)));
const nw = Math.max(...rs.map((r) => String(r.n).length));
out.push("", `${nb(qs, k)}${q.text}`);
if (kindNote(q)) out.push(` ${kindNote(q)}`);
for (const r of rs) out.push(` ${padEnd(r.label, lw)} ${padStart(String(r.n), nw)} ${padStart(r.p, 7)}`.trimEnd());
out.push(` ${foot(q, t)}`);
});
return out.join("\n");
}
// no column alignment, survives a paste in an email or a chat
function compact(poll, voters, qs) {
const out = [poll.title];
for (const q of qs) {
const t = tally(q);
const parts = rows(q, t).map((r) => (r.p ? `${r.label} ${r.n} (${r.p})` : `${r.label} ${r.n}`));
out.push("", q.text + (kindNote(q) ? ` (${kindNote(q).toLowerCase()})` : ""),
`${parts.join(" · ")} · ${plural(t.ballots, "bulletin")}`);
}
return out.join("\n");
}
// the long npppav label would eat the whole line
const SHORT = { "ne prend pas part au vote": "NPPPAV" };
function list(poll, voters, qs) {
const out = [poll.title];
for (const q of qs) {
const t = tally(q);
const parts = rows(q, t).map((r) => `${SHORT[r.label.toLowerCase()] || r.label} (${r.n})`);
out.push("", `${q.text} [${plural(t.ballots, "bulletin")}]`, parts.join(" / "));
}
return out.join("\n");
}
function bars(poll, voters, qs) {
const out = [poll.title];
const W = 20;
for (const q of qs) {
const t = tally(q), rs = rows(q, t);
const lw = Math.max(...rs.map((r) => width(r.label)));
out.push("", q.text);
for (const r of rs) {
const full = Math.round((t.base ? r.n / t.base : 0) * W);
const bar = r.p ? "█".repeat(Math.min(W, full)) + "░".repeat(Math.max(0, W - full)) : " ".repeat(W);
out.push(`${padEnd(r.label, lw)} ${bar} ${r.n}${r.p ? ` · ${r.p}` : ""}`);
}
out.push(foot(q, t));
}
return out.join("\n");
}
const mdCell = (s) => String(s).replace(/\|/g, "\\|");
function markdown(poll, voters, qs, now) {
const [title, sub] = header(poll, voters, now);
const out = [`# ${mdCell(title)}`, "", sub];
qs.forEach((q, k) => {
const t = tally(q);
out.push("", `## ${nb(qs, k)}${q.text}`, "");
if (kindNote(q)) out.push(`*${kindNote(q)}*`, "");
out.push("| Réponse | Voix | % |", "| --- | ---: | ---: |");
for (const r of rows(q, t)) out.push(`| ${mdCell(r.label)} | ${r.n} | ${r.p} |`);
out.push("", `${foot(q, t)}.`);
});
return out.join("\n");
}
// semicolons, the decimal comma makes plain commas ambiguous in french spreadsheets
const csvCell = (v) => {
const s = String(v);
return /[;"\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
};
function csv(poll, voters, qs) {
const out = [["n", "question", "réponse", "voix", "dont ajoutées", "% exprimés"]];
qs.forEach((q, k) => {
const t = tally(q);
q.options.forEach((o, i) => out.push([k + 1, q.text, o, t.tot[i], t.extra[i], num(pct(t.tot[i], t.base))]));
if (q.allow_blank) out.push([k + 1, q.text, "Blanc", t.blank, t.exBlank, ""]);
out.push([k + 1, q.text, "Bulletins", t.ballots, t.exBallots, ""]);
});
return out.map((r) => r.map(csvCell).join(";")).join("\n");
}
export const FORMATS = [
{ id: "pv", label: "Procès-verbal", ext: "txt", mime: "text/plain", build: minutes },
{ id: "compact", label: "Compact", ext: "txt", mime: "text/plain", build: compact },
{ id: "list", label: "Liste", ext: "txt", mime: "text/plain", build: list },
{ id: "bars", label: "Barres", ext: "txt", mime: "text/plain", build: bars },
{ id: "md", label: "Markdown", ext: "md", mime: "text/markdown", build: markdown },
{ id: "csv", label: "CSV", ext: "csv", mime: "text/csv", build: csv },
];
// closed questions only (nobody sees open tallies), in the order they were asked
export function exportText(fmt, poll, voters, questions, now) {
const qs = questions.filter((q) => q.state === "closed" && q.results).sort((a, b) => a.id - b.id);
return fmt.build(poll, voters, qs, now);
}