SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
3 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
1.2 KB · 18 lines typescript
Raw Blame History
1/** Line-based diff (LCS) producing a compact unified-style summary. Good enough for page-change inspection. */2export interface LineDiff { added: string[]; removed: string[]; addedCount: number; removedCount: number; ratio: number }34export function diffLines(oldText: string, newText: string, maxLines = 400): LineDiff {5  const a = oldText.split("\n").map((s) => s.trim()).filter(Boolean);6  const b = newText.split("\n").map((s) => s.trim()).filter(Boolean);7  const setA = new Map<string, number>();8  const setB = new Map<string, number>();9  for (const l of a) setA.set(l, (setA.get(l) ?? 0) + 1);10  for (const l of b) setB.set(l, (setB.get(l) ?? 0) + 1);11  const removed: string[] = [];12  const added: string[] = [];13  for (const [l, n] of setA) { const m = setB.get(l) ?? 0; for (let i = 0; i < n - m; i++) removed.push(l); }14  for (const [l, n] of setB) { const m = setA.get(l) ?? 0; for (let i = 0; i < n - m; i++) added.push(l); }15  const total = Math.max(1, a.length + b.length);16  return { added: added.slice(0, maxLines), removed: removed.slice(0, maxLines), addedCount: added.length, removedCount: removed.length, ratio: (added.length + removed.length) / total };17}18