kpdecker / jsdiff

A javascript text differencing implementation.
BSD 3-Clause "New" or "Revised" License
7.92k stars 496 forks source link

structuredPatch then applyPatch doesn't work; createPatch then parsePatch then applyPatch does #401

Closed JESUSrosetolife closed 8 months ago

JESUSrosetolife commented 1 year ago

This is my first time using this library

structuredPatch then applyPatch doesn't work; createPatch then parsePatch then applyPatch does:

Code:

import * as Diff from 'diff';

let a = 'a'
let b = 'b'
let file_name = '';
let header = ''
let structured = Diff.structuredPatch(file_name, a, b, header, header);
console.log(Diff.applyPatch(a, structured))

let patch = Diff.createPatch(file_name, a, b, header, header);
let structured2 = Diff.parsePatch(patch);
console.log(Diff.applyPatch(a, structured2))

Output:

false
b

I'm expecting false to be b

ExplodingCabbage commented 8 months ago

Hmm. I think this issue may have been inspired by a real structuredPatch bug that was fixed by https://github.com/kpdecker/jsdiff/pull/393, but the example code is broken; structuredPatch's first two arguments are file names so you're passing arguments in the wrong positions.

At least with the code that exists on master now, if you correct it to

let structured = Diff.structuredPatch(file_name, file_name, a, b, header, header);

then applying the patch will work:

> console.log(Diff.applyPatch(a, structured))
b