Zero-dependency line diff utility. Compute, format, and summarize text diffs.
pnpm add @ekaone/diff-kitLine-by-line diff using LCS algorithm.
import { diff } from '@ekaone/diff-kit'
const lines = diff("hello\nworld", "hello\nearth")
// [
// { type: "unchanged", content: "hello" },
// { type: "removed", content: "world" },
// { type: "added", content: "earth" },
// ]Groups diff lines into chunks with surrounding context.
import { diff, formatDiff } from '@ekaone/diff-kit'
const lines = diff(before, after)
const chunks = formatDiff(lines, { context: 3 })
chunks.forEach(chunk => {
chunk.forEach(line => {
const prefix = line.type === "added" ? "+" : line.type === "removed" ? "-" : " "
console.log(`${prefix} ${line.content}`)
})
})Summary counts from a diff.
import { diff, diffStats } from '@ekaone/diff-kit'
const stats = diffStats(diff(a, b))
// { added: 2, removed: 1, unchanged: 5 }type DiffType = "added" | "removed" | "unchanged"
interface DiffLine {
type: DiffType
content: string
}
interface DiffOptions {
context?: number // default: 3
}
interface DiffStats {
added: number
removed: number
unchanged: number
}MIT © Eka Prasetia
⭐ If this library helps you, please consider giving it a star on GitHub!