Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT licensed NPM Version Donation Donation Donation

Parse and apply unified diffs. Applies patches deterministically, reports rejected hunks, and provides TypeScript types for safety.

Features

  • Strict parsing of unified diff format. (No git/svn file operations, No multi-files)
  • Deterministic application with clear rejection output.
  • TypeScript types with ESM and CJS builds.
  • Small footprint.

Installation

npm add @zokugun/unidiff-strict

Quick Start

import { applyUnidiff } from '@zokugun/unidiff-strict';

const source = 'line1\nline2\n';
const diff = `--- a/file\n+++ b/file\n@@ -1,2 +1,3 @@\n line1\n+added\n line2\n`;

// Apply directly from a diff string
const result = applyUnidiff(source, diff, { reject: true });
if(result.fails) {
    throw new Error(res.error);
}

console.log('patched:', res.value.output);
console.log('rejects:', res.value.reject);

API reference

  • applyUnidiff(source: string, diff: string | Unidiff, options: ApplyUnidiffOptions): Result<string | { output: string; reject: string }, string>: Apply a diff (string or Unidiff) to source. Returns either the patched string or an object { output, reject } when options.reject is true.
  • mergeUnidiff(diffs: Array<Unidiff | string>): DResult<Unidiff>: Merge several unified-diffs into a single Unidiff structure.
  • parseUnidiff(source: string): Result<Unidiff, string>: Returns a Unidiff structure from a unified-diff string.
  • stringifyUnidiff(diff: Unidiff): string: Returns a unified-diff string from a Unidiff structure.
type ApplyUnidiffOptions = {
	ignorePreviouslyApplied?: boolean;
	reject?: boolean;
};

type Unidiff = {
	hunks: Hunk[];
	newFile?: File;
	oldFile?: File;
};

type Hunk = {
	changes: Change[];
	newLength: number;
	newStart: number;
	oldLength: number;
	oldStart: number;
};

type Change = {
	kind: LineKind;
	text: string;
};

type LineKind = 'context' | 'deletion' | 'insertion';

type File = {
	name: string;
	timestamp?: string;
};

Contributions

Contributions are most welcome. Please:

  • Open issues and feature requests under the repository discussions.
  • Follow the CONTRIBUTING.md.

Donations

Support this project by becoming a financial contributor.

Ko-fi ko-fi.com/daiyam
Liberapay liberapay.com/daiyam/donate
PayPal paypal.me/daiyam99

License

Copyright © 2026-present Baptiste Augrain

Licensed under the MIT license.

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages