87 lines
4.1 KiB
TypeScript
87 lines
4.1 KiB
TypeScript
import { addDays, addWorkDays } from "khb_common";
|
||
|
||
/** Правила todo в одном месте: сроки, префикс и суффиксы Control, строка Review, давность. */
|
||
|
||
export const CONTROL_PREFIX = "Control:";
|
||
export const SUFFIX_DONE = "(выполнено)";
|
||
export const SUFFIX_OVERDUE = "(просрочено)";
|
||
const SUFFIX_RE = /\s*\((?:выполнено|просрочено)\)\s*$/;
|
||
|
||
/** Срок по умолчанию: dueWorkdays рабочих дней от даты сообщения. */
|
||
export function defaultDue(from: string, workdays: number, nonWorkdays: ReadonlySet<string>): string {
|
||
return addWorkDays(from, workdays, nonWorkdays);
|
||
}
|
||
|
||
/** Срок Control todo: срок исполнителя + offset календарных дней. */
|
||
export function controlDue(executorDue: string, offsetDays: number): string {
|
||
return addDays(executorDue, offsetDays);
|
||
}
|
||
|
||
/** Название без суффикса статуса (всех подряд). */
|
||
export function stripStatusSuffix(name: string): string {
|
||
let n = name;
|
||
while (SUFFIX_RE.test(n)) n = n.replace(SUFFIX_RE, "");
|
||
return n.trim();
|
||
}
|
||
|
||
/** Название Control todo всегда начинается с "Control:". */
|
||
export function ensureControlPrefix(name: string): string {
|
||
const n = name.trim();
|
||
return n.toLowerCase().startsWith(CONTROL_PREFIX.toLowerCase()) ? n : `${CONTROL_PREFIX} ${n}`;
|
||
}
|
||
|
||
/** Желаемое название по состоянию надстройки: суффикс "(выполнено)" важнее "(просрочено)". */
|
||
export function desiredName(baseName: string, flags: { done: boolean; overdue: boolean }): string {
|
||
const base = stripStatusSuffix(baseName);
|
||
if (flags.done) return `${base} ${SUFFIX_DONE}`;
|
||
if (flags.overdue) return `${base} ${SUFFIX_OVERDUE}`;
|
||
return base;
|
||
}
|
||
|
||
const REVIEW_LINE_RE = /(?:\r?\n)*Review: \d{2}\.\d{2}\s*$/;
|
||
|
||
/** Описание с одной строкой "Review: ДД.ММ" в конце (до 4096 знаков); until=null убирает строку. */
|
||
export function withReviewLine(description: string | null, until: string | null): string {
|
||
const base = (description ?? "").replace(REVIEW_LINE_RE, "");
|
||
if (!until) return base;
|
||
const [, m, d] = until.match(/^\d{4}-(\d{2})-(\d{2})$/) ?? [];
|
||
if (!m || !d) return base;
|
||
const line = `Review: ${d}.${m}`;
|
||
return (base ? `${base}\n\n${line}` : line).slice(-4096);
|
||
}
|
||
|
||
/** Дописывает "Keep confirmed <дата>" к описанию (один раз). */
|
||
export function withKeepConfirmed(description: string | null, date: string): string {
|
||
const base = description ?? "";
|
||
if (/Keep confirmed \d{4}-\d{2}-\d{2}/.test(base)) return base;
|
||
return (base ? `${base}\n\n` : "") + `Keep confirmed ${date}`;
|
||
}
|
||
|
||
/** Дата замечания из описания (YYYY-MM-DD или M/D/YYYY), иначе null. */
|
||
export function noticedDate(description: string | null): string | null {
|
||
const text = description ?? "";
|
||
const iso = text.match(/\b(20\d{2})-(\d{2})-(\d{2})\b/);
|
||
if (iso) return `${iso[1]}-${iso[2]}-${iso[3]}`;
|
||
const us = text.match(/\b(\d{1,2})\/(\d{1,2})\/(20\d{2})\b/);
|
||
if (us) return `${us[3]}-${us[1]!.padStart(2, "0")}-${us[2]!.padStart(2, "0")}`;
|
||
return null;
|
||
}
|
||
|
||
/** Дата для проверки давности: дата замечания из описания, иначе дата создания (YYYY-MM-DD). */
|
||
export function ageDate(description: string | null, createdAtIso: string | null): string | null {
|
||
return noticedDate(description) ?? (createdAtIso ? createdAtIso.slice(0, 10) : null);
|
||
}
|
||
|
||
/** Закрыто: progress >= 1. */
|
||
export const isClosed = (progress: number | null): boolean => progress != null && progress >= 1;
|
||
|
||
export type TodoKind = "todo" | "reminder" | "control";
|
||
|
||
/** Вид по названию, когда надстройки ещё нет: "Control:" - control, "Reminder:" - reminder. */
|
||
export function guessKind(name: string): TodoKind {
|
||
const n = name.trim().toLowerCase();
|
||
if (n.startsWith("control:")) return "control";
|
||
if (n.startsWith("reminder:")) return "reminder";
|
||
return "todo";
|
||
}
|