SPB Git

Repository Public

logic.ts 1.7 KB 45 lines typescript

1// Pure helpers of the settings screens (unit-tested).23/** Groups permissions by their first segment: "nodes.read" → group "nodes". Groups and members sorted. */4export function groupPermissions(permissions: readonly string[]): { group: string; permissions: string[] }[] {5  const map = new Map<string, string[]>();6  for (const p of permissions) {7    const g = p.split(".")[0] ?? p;8    const list = map.get(g);9    if (list) list.push(p);10    else map.set(g, [p]);11  }12  return [...map.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([group, list]) => ({ group, permissions: [...list].sort() }));13}1415/** TTL in minutes from a form field: integer within [1, max]; null when invalid. */16export function parseTtl(raw: string, max = 1440): number | null {17  if (!/^\d+$/.test(raw.trim())) return null;18  const n = Number(raw.trim());19  return n >= 1 && n <= max ? n : null;20}2122export const HANDLE_RE = /^[a-z0-9][a-z0-9._-]{1,31}$/;2324/** Parses the text of the JSON-aware setting editor. */25export function parseSettingValue(text: string): { ok: true; value: unknown } | { ok: false } {26  try {27    return { ok: true, value: JSON.parse(text) as unknown };28  } catch {29    return { ok: false };30  }31}32export function formatSettingValue(value: unknown): string {33  return JSON.stringify(value, null, 2) ?? "null";34}3536/** Passkeys that still count for the "last passkey" protection. */37export function activeCount(passkeys: readonly { revoked_at?: string | undefined }[]): number {38  return passkeys.filter((p) => !p.revoked_at).length;39}4041/** Word the owner must type to toggle the emergency stop. */42export function freezeWord(frozen: boolean): "freeze" | "unfreeze" {43  return frozen ? "unfreeze" : "freeze";44}45
Files main
Menu

Theme