Rebasely Beta: data may be purged

justintsus-experiment Public

Text · 143 lines · 5.2 KiB · at 8e57616

// The values of each qualifier: what is:, no:, sort:, label:, author: and review-requested: accept,// and the date filters of created: and updated:. Each reader takes the value as written and returns// it normalized, or None when it is not valid for the key and the target.import { Some, None, type Option } from "@tsus/runtime";import type { DateFilter, DateOp, Day, SortValue, StateValue, Target, Unit, UserRef } from "./ast.tsus";export const DAY_MS = 86_400_000;export const isValue = (raw: string, target: Target): Option<StateValue> =>  match (raw.toLowerCase()) {    "open" => Some<StateValue>("open"),    "closed" => Some<StateValue>("closed"),    "merged" => target === "pulls" ? Some<StateValue>("merged") : None,    "draft" => target === "pulls" ? Some<StateValue>("draft") : None,    _ => None,  };export const sortValue = (raw: string, target: Target): Option<SortValue> =>  match (raw.toLowerCase()) {    "created-desc" => Some<SortValue>("created-desc"),    "created-asc" => Some<SortValue>("created-asc"),    "updated-desc" => Some<SortValue>("updated-desc"),    "updated-asc" => Some<SortValue>("updated-asc"),    "comments-desc" => target === "issues" ? Some<SortValue>("comments-desc") : None,    "comments-asc" => target === "issues" ? Some<SortValue>("comments-asc") : None,    _ => None,  };export const noValue = (raw: string): boolean => raw.toLowerCase() === "label";const LABEL = /^[a-z0-9][a-z0-9 ._-]*$/;export const LABEL_MAX = 32;// Labels as packages/shared/issues.ts cleanLabels keeps them: trimmed, lowercase, one space inside.export const labelValue = (raw: string): Option<string> => {  const v = raw.trim().toLowerCase().replace(/\s+/g, " ");  return v.length <= LABEL_MAX && LABEL.test(v) ? Some(v) : None;};const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;// AGENT_SLUG in packages/shared/agents.tsconst SLUG = /^[a-z0-9][a-z0-9-]{0,38}$/;export const userValue = (raw: string): Option<UserRef> => {  const v = raw.toLowerCase();  if (v === "@me") return Some<UserRef>({ tag: "Me" });  if (UUID.test(v)) return Some<UserRef>({ tag: "User", id: v });  if (SLUG.test(v)) return Some<UserRef>({ tag: "Agent", slug: v });  return None;};// --- datesconst YMD = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;// YYYY-MM-DD, a real day from 1970-01-01 to 9999-12-31, at 00:00:00.000Z.export const dayValue = (text: string): Option<Day> => {  if (!YMD.test(text)) return None;  const y = Number(text.slice(0, 4));  const m = Number(text.slice(5, 7));  const d = Number(text.slice(8, 10));  if (y < 1970 || m < 1 || m > 12 || d < 1) return None;  const start = Date.UTC(y, m - 1, d);  const back = new Date(start);  return back.getUTCMonth() === m - 1 && back.getUTCDate() === d ? Some({ text, start }) : None;};const AGE = /^([0-9]+)([hdw])$/;const unitOf = (u: string): Option<Unit> =>  match (u) {    "h" => Some<Unit>("h"),    "d" => Some<Unit>("d"),    "w" => Some<Unit>("w"),    _ => None,  };export const unitMs = (u: Unit): number =>  match (u) {    "h" => 3_600_000,    "d" => DAY_MS,    "w" => 604_800_000,  };const ageOf = (op: DateOp, rest: string): Option<DateFilter> => {  if (!AGE.test(rest)) return None;  const count = Number(rest.slice(0, -1));  if (!(count >= 1 && count <= 9999)) return None;  return unitOf(rest.slice(-1)).map((unit): DateFilter => ({ tag: "Age", op, count, unit }));};const compareOf = (op: DateOp, rest: string): Option<DateFilter> =>  match (dayValue(rest)) {    Some(day) => Some<DateFilter>({ tag: "Compare", op, day }),    None => ageOf(op, rest),  };type Split = { readonly op: DateOp; readonly rest: string };const opOf = (v: string): Option<Split> => {  if (v.startsWith("<=")) return Some<Split>({ op: "<=", rest: v.slice(2) });  if (v.startsWith(">=")) return Some<Split>({ op: ">=", rest: v.slice(2) });  if (v.startsWith("<")) return Some<Split>({ op: "<", rest: v.slice(1) });  if (v.startsWith(">")) return Some<Split>({ op: ">", rest: v.slice(1) });  return None;};// One end of a range: a day, or * for open.type End = { readonly tag: "Open" } | { readonly tag: "At"; readonly day: Day };const endOf = (text: string): Option<End> =>  text === "*" ? Some<End>({ tag: "Open" }) : dayValue(text).map((day): End => ({ tag: "At", day }));const dayOfEnd = (e: End): Option<Day> =>  match (e) {    Open(_o) => None,    At(a) => Some(a.day),  };const between = (from: End, to: End): Option<DateFilter> => {  const a = dayOfEnd(from);  const b = dayOfEnd(to);  if (a.isNone() && b.isNone()) return None;  if (a.isSome() && b.isSome() && a.value.start > b.value.start) return None;  return Some<DateFilter>({ tag: "Between", from: a, to: b });};const rangeOf = (v: string): Option<DateFilter> => {  const parts = v.split("..");  if (parts.length !== 2) return None;  const from = endOf(parts.slice(0, 1).join(""))?;  const to = endOf(parts.slice(1).join(""))?;  return between(from, to);};export const dateValue = (raw: string): Option<DateFilter> => {  const v = raw.toLowerCase();  return match (opOf(v)) {    Some(s) => compareOf(s.op, s.rest),    None => v.includes("..") ? rangeOf(v) : dayValue(v).map((day): DateFilter => ({ tag: "On", day })),  };};