Skip to content

Latest commit

 

History

History
97 lines (67 loc) · 1.77 KB

File metadata and controls

97 lines (67 loc) · 1.77 KB

@ekaone/diff-kit

Zero-dependency line diff utility. Compute, format, and summarize text diffs.

Install

# Using pnpm
pnpm add @ekaone/diff-kit

# Using npm
npm install @ekaone/diff-kit

# Using yarn
yarn add @ekaone/diff-kit

API

diff(a, b): DiffLine[]

Line-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" },
// ]

formatDiff(lines, options?): DiffLine[][]

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}`)
  })
})

diffStats(lines): DiffStats

Summary counts from a diff.

import { diff, diffStats } from '@ekaone/diff-kit'

const stats = diffStats(diff(a, b))
// { added: 2, removed: 1, unchanged: 5 }

Types

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
}

License

MIT © Eka Prasetia

Links


⭐ If this library helps you, please consider giving it a star on GitHub!