114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
// The versions of an article, for the list of open proposals (Yvv, 2026-10-30:
|
|
// « Quand un article a plusieurs versions, faut pas ajouter un article dans la
|
|
// liste »): one line per article, its best version first, the others inside.
|
|
|
|
import type { Proposal, Unit } from './types';
|
|
|
|
export type Filter = 'all' | 'wellrated' | 'mine';
|
|
|
|
export interface Article {
|
|
unit: Unit;
|
|
/** The open proposals of the article, best first. */
|
|
versions: Proposal[];
|
|
/** The best version is strictly ahead of the next one (not a tie). */
|
|
leads: boolean;
|
|
}
|
|
|
|
/** How far a version has risen toward its threshold, from 0 to 1: its water level. */
|
|
export function level(p: Proposal): number {
|
|
const required = p.evaluation?.required ?? 0;
|
|
return required > 0 ? Math.min(1, p.tally.for / required) : 0;
|
|
}
|
|
|
|
/**
|
|
* Negative when `a` comes before `b`: the highest water, then more for, then fewer
|
|
* against. A version held back by an objection comes after the others: it is not
|
|
* votable, and cannot enter into force, until the objection is lifted.
|
|
*/
|
|
export function compare(a: Proposal, b: Proposal): number {
|
|
return (
|
|
Number(a.blocked) - Number(b.blocked) ||
|
|
level(b) - level(a) ||
|
|
b.tally.for - a.tally.for ||
|
|
a.tally.against - b.tally.against
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Plain excerpts of sibling versions, each starting a few words before the versions
|
|
* part ways — two variants often share a long opening, and a one-line excerpt would
|
|
* show only that.
|
|
*/
|
|
export function excerpts(texts: string[]): string[] {
|
|
const LEAD = 3;
|
|
const words = texts.map((t) => t.split(/\s+/).filter(Boolean));
|
|
const [first] = words;
|
|
if (!first || words.length < 2) return texts;
|
|
let common = 0;
|
|
while (common < first.length && words.every((w) => w[common] === first[common])) common++;
|
|
const start = common - LEAD;
|
|
// A short common start reads well as it is.
|
|
if (start < 4) return texts;
|
|
return words.map((w) => `… ${w.slice(start).join(' ')}`);
|
|
}
|
|
|
|
export function passes(p: Proposal, filter: Filter): boolean {
|
|
if (filter === 'wellrated') return !!p.evaluation?.wellRated;
|
|
if (filter === 'mine') return p.mine;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* One article per unit, in the order of the document; an article is kept when at
|
|
* least one of its versions passes the filter, and then shows all of them.
|
|
*/
|
|
export function articles(items: { unit: Unit; proposal: Proposal }[], filter: Filter = 'all'): Article[] {
|
|
const byUnit = new Map<string, { unit: Unit; versions: Proposal[] }>();
|
|
for (const { unit, proposal } of items) {
|
|
const entry = byUnit.get(unit.id) ?? { unit, versions: [] };
|
|
entry.versions.push(proposal);
|
|
byUnit.set(unit.id, entry);
|
|
}
|
|
return [...byUnit.values()]
|
|
.filter(({ versions }) => versions.some((p) => passes(p, filter)))
|
|
.map(({ unit, versions }) => {
|
|
// Array.prototype.sort is stable: equal versions keep the order of the document.
|
|
const ranked = [...versions].sort(compare);
|
|
const [best, next] = ranked;
|
|
return { unit, versions: ranked, leads: !!best && !!next && compare(best, next) < 0 };
|
|
});
|
|
}
|
|
|
|
/** A section of « Propositions en cours » (lot U): its articles, and its order when it closes here. */
|
|
export interface DeepSection {
|
|
key: string | null;
|
|
articles: Article[];
|
|
/** The order of the section, when the section is not read in the text above. */
|
|
order?: Unit;
|
|
}
|
|
|
|
/**
|
|
* The open proposals by section, in the order of the document (`keys`, the sections as the
|
|
* document lists them): each section holds its articles, then its order when the order
|
|
* closes it here — a section read in the text above has its order there. A section with
|
|
* only its order to show is kept.
|
|
*/
|
|
export function deepSections(articles: Article[], orders: Unit[], keys: (string | null)[]): DeepSection[] {
|
|
const byKey = new Map<string | null, DeepSection>();
|
|
for (const a of articles) {
|
|
const s = byKey.get(a.unit.section) ?? { key: a.unit.section, articles: [] };
|
|
s.articles.push(a);
|
|
byKey.set(a.unit.section, s);
|
|
}
|
|
for (const order of orders) {
|
|
const s = byKey.get(order.section) ?? { key: order.section, articles: [] };
|
|
s.order = order;
|
|
byKey.set(order.section, s);
|
|
}
|
|
const rank = (k: string | null) => {
|
|
const i = keys.indexOf(k);
|
|
return i < 0 ? keys.length : i;
|
|
};
|
|
return [...byKey.values()].sort((a, b) => rank(a.key) - rank(b.key));
|
|
}
|