Spaces:
Paused

dushyantsingh0
fix: knowledge base diff checker normalization
2829e9b
Raw History Blame Contribute Delete
1.09 kB
export interface DiffLine {
type: 'added' | 'removed' | 'unchanged';
value: string;
}
export function computeDiff(oldText: string, newText: string): DiffLine[] {
// Normalize line endings to avoid ghost diffs from CRLF/LF mismatch
const cleanOld = (oldText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
const cleanNew = (newText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
const oldLines = cleanOld.split('\n').map(l => l.trimEnd());
const newLines = cleanNew.split('\n').map(l => l.trimEnd());
const diff: DiffLine[] = [];
let i = 0;
let j = 0;
while (i < oldLines.length || j < newLines.length) {
if (i < oldLines.length && j < newLines.length && oldLines[i] === newLines[j]) {
diff.push({ type: 'unchanged', value: oldLines[i] });
i++;
j++;
} else if (j < newLines.length && (i === oldLines.length || !oldLines.slice(i).includes(newLines[j]))) {
diff.push({ type: 'added', value: newLines[j] });
j++;
} else {
diff.push({ type: 'removed', value: oldLines[i] });
i++;
}
}
return diff;
}