@zokugun/unidiff-strict
v0.2.1
Published
Parse and apply unified diffs
Readme
@zokugun/unidiff-strict
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-strictQuick 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 adiff(string orUnidiff) tosource. Returns either the patched string or an object{ output, reject }whenoptions.rejectis true.mergeUnidiff(diffs: Array<Unidiff | string>): DResult<Unidiff>: Merge several unified-diffs into a singleUnidiffstructure.parseUnidiff(source: string): Result<Unidiff, string>: Returns aUnidiffstructure from a unified-diff string.stringifyUnidiff(diff: Unidiff): string: Returns a unified-diff string from aUnidiffstructure.
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.
License
Copyright © 2026-present Baptiste Augrain
Licensed under the MIT license.
