histogram-diff
v0.1.1
Published
TypeScript starter kit
Readme
histogram-diff
A TypeScript implementation of the histogram diff algorithm.
What is Histogram Diff
Histogram diff is a diff algorithm introduced by the jgit project in 2010, based on Bram Cohen's patience diff. It is used by Git as one of its diff algorithms (git diff --histogram).
Compared to traditional algorithms like Myers, histogram diff produces more readable output by grouping related changes together rather than scattering them across the file.
To learn more about how it works:
Installation
npm install histogram-diffUsage
import { histogramDiff, formatDiff } from 'histogram-diff'
const fileA = ['a', 'b', 'c', 'd']
const fileB = ['a', 'b', 'd', 'e']
const diffs = histogramDiff(fileA, fileB)
console.log(formatDiff(fileA, fileB, diffs))
// Output:
// a
// b
// - c
// d
// + eReal-world example
import { histogramDiff, formatDiff } from 'histogram-diff'
const fileA = [
'function add(a, b) {',
' console.log(a, b)',
' const sum = a + b',
' return sum',
'}',
]
const fileB = [
'function add(a, b) {',
' if (typeof a !== "number" || typeof b !== "number") {',
' throw new Error("a and b must be numbers")',
' }',
' const sum = a + b',
' return sum',
'}',
]
const diffs = histogramDiff(fileA, fileB)
console.log(formatDiff(fileA, fileB, diffs))
// Output:
// function add(a, b) {
// - console.log(a, b)
// + if (typeof a !== "number" || typeof b !== "number") {
// + throw new Error("a and b must be numbers")
// + }
// const sum = a + b
// return sum
// }API
histogramDiff<T = string>(fileA: T[], fileB: T[]): Region[]
Compares two arrays and returns a list of regions that are different.
fileA- The first array (original)fileB- The second array (modified)- Returns an array of
Regiontuples representing the differences
formatDiff<T = string>(fileA: T[], fileB: T[], diffs: Region[]): string
Formats the diff output into a human-readable string with + and - markers.
fileA- The first array (original)fileB- The second array (modified)diffs- The diff regions fromhistogramDiff- Returns a formatted string
Region
A tuple type representing a diff region: [aLo, aHi, bLo, bHi]
aLo- Start index infileA(inclusive)aHi- End index infileA(exclusive)bLo- Start index infileB(inclusive)bHi- End index infileB(exclusive)
License
MIT
