Thanks to visit codestin.com
Credit goes to github.com

Skip to content

vulh1209/histogram-algorithm-diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

62 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ imara-diff TypeScript

A high-performance diff library for TypeScript, ported from the Rust imara-diff.

โœจ Features

  • ๐Ÿš€ High Performance: Optimized Histogram algorithm, 10-100% faster than Myers
  • ๐Ÿ”’ Type Safe: Strict TypeScript with branded types
  • ๐Ÿ›ก๏ธ Memory Safe: Generation-based validation prevents use-after-free
  • โœ… Well Tested: 108 tests with ~1,450 property test runs
  • ๐Ÿ“ Line-based Diffs: Perfect for text files and source code
  • ๐ŸŽฏ Fallback Strategy: SimpleMyers handles repetitive content

๐Ÿ“ฆ Installation

pnpm add imara-diff

๐ŸŽฏ Quick Start

import { Diff, Algorithm, InternedInput, StringLines } from 'imara-diff';

const before = "line 1\nline 2\nline 3\n";
const after = "line 1\nmodified\nline 3\n";

// Create interned input
const input = InternedInput.new(
  new StringLines(before),
  new StringLines(after)
);

// Compute diff
const diff = Diff.compute(Algorithm.Histogram, input);

// Get results
console.log(`Changes: +${diff.countAdditions()}/-${diff.countRemovals()}`);

// Iterate over changes
for (const hunk of diff.hunks()) {
  console.log(`Changed: lines ${hunk.before.start}-${hunk.before.end}`);
}

๐Ÿ“ Project Structure

typescript/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ core/              # Core types and utilities
โ”‚   โ”‚   โ”œโ”€โ”€ types.ts       # Branded types, assertions
โ”‚   โ”‚   โ”œโ”€โ”€ util.ts        # Utility functions
โ”‚   โ”‚   โ”œโ”€โ”€ sources.ts     # TokenSource implementations
โ”‚   โ”‚   โ””โ”€โ”€ intern.ts      # Token interning system
โ”‚   โ”œโ”€โ”€ algorithms/        # Diff algorithms
โ”‚   โ”‚   โ”œโ”€โ”€ histogram.ts   # Histogram algorithm (primary)
โ”‚   โ”‚   โ”œโ”€โ”€ myers-simple.ts # SimpleMyers (fallback)
โ”‚   โ”‚   โ””โ”€โ”€ list-pool.ts   # Memory pool allocator
โ”‚   โ”œโ”€โ”€ api/               # Public API
โ”‚   โ”‚   โ””โ”€โ”€ diff.ts        # Main Diff class
โ”‚   โ””โ”€โ”€ index.ts           # Public exports
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ unit/              # Unit tests
โ”‚   โ”œโ”€โ”€ integration/       # Integration tests
โ”‚   โ””โ”€โ”€ property/          # Property-based tests
โ”œโ”€โ”€ docs/                  # Documentation
โ”‚   โ”œโ”€โ”€ examples/          # Usage examples
โ”‚   โ”œโ”€โ”€ SUMMARY.md         # Technical overview
โ”‚   โ”œโ”€โ”€ TESTING.md         # Testing guide
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ benchmarks/            # Performance benchmarks
โ””โ”€โ”€ package.json

๐Ÿงช Development

# Install dependencies
pnpm install

# Run tests
pnpm test

# Run tests in watch mode
pnpm test --watch

# Run with coverage
pnpm test --coverage

# Build
pnpm build

# Lint
pnpm lint

# Type check
pnpm typecheck

๐Ÿ“Š Performance

Scenario Size Time Status
Small file 50 lines < 50ms โœ…
Medium file 500 lines < 200ms โœ…
Large file 2,000 lines < 1s โœ…

Expected: 3-5x slower than Rust (acceptable for TypeScript)

๐Ÿงช Testing

108 tests with comprehensive coverage:

  • โœ… 35 unit tests
  • โœ… 45 edge case tests
  • โœ… 15 property tests (~1,450 runs)
  • โœ… 13 integration tests

See TESTING.md for details.

๐Ÿ“š Documentation

๐ŸŽฏ API Reference

Main Classes

Diff

class Diff {
  static compute<T>(algorithm: Algorithm, input: InternedInput<T>): Diff;
  countAdditions(): number;
  countRemovals(): number;
  isRemoved(idx: number): boolean;
  isAdded(idx: number): boolean;
  hunks(): IterableIterator<Hunk>;
  getAllHunks(): Hunk[];
}

InternedInput<T>

class InternedInput<T> {
  static new<T>(before: TokenSource<T>, after: TokenSource<T>): InternedInput<T>;
  before: Token[];
  after: Token[];
  interner: Interner<T>;
}

Algorithm

enum Algorithm {
  Histogram = 'histogram'  // Primary algorithm with fallback
}

๐ŸŽ“ Examples

See docs/examples/ for more examples:

  • Basic usage
  • Real-world scenarios
  • Performance benchmarks
  • Error handling

๐Ÿค Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Ensure all tests pass
  5. Submit a pull request

๐Ÿ“„ License

Apache-2.0 (same as original Rust version)

๐Ÿ™ Acknowledgments

Ported from imara-diff by Pascal Kuthe.

๐Ÿ“ˆ Status

โœ… Production Ready

  • Core functionality complete
  • Comprehensive test coverage (108 tests)
  • Well documented
  • Type and memory safe

๐Ÿ”ฎ Future Enhancements

Optional features (not required for core functionality):

  • Postprocessing with slider heuristics
  • UnifiedDiff output format
  • Additional algorithms
  • Performance optimizations

Made with โค๏ธ in TypeScript

About

Reliably performant diffing

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 14