Spaces:
Paused
Paused
Download frontend/packages/shared/src/utils/diff.ts from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/frontend/packages/shared/src/utils/diff.ts
- Command line
-
hf download hf://spaces/3SCError404/miracle/frontend/packages/shared/src/utils/diff.ts
-
curl -L -o diff.ts https://huggingface.co/spaces/3SCError404/miracle/resolve/main/frontend/packages/shared/src/utils/diff.ts
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; | |
| } | |