Skip to content

Handle case where the user explicitly passes maxEditLength: 0 the way you'd expect #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#435](https://github.com/kpdecker/jsdiff/pull/435) Fix `parsePatch` handling of control characters. `parsePatch` used to interpret various unusual control characters - namely vertical tabs, form feeds, lone carriage returns without a line feed, and EBCDIC NELs - as line breaks when parsing a patch file. This was inconsistent with the behavior of both JsDiff's own `diffLines` method and also the Unix `diff` and `patch` utils, which all simply treat those control characters as ordinary characters. The result of this discrepancy was that some well-formed patches - produced either by `diff` or by JsDiff itself and handled properly by the `patch` util - would be wrongly parsed by `parsePatch`, with the effect that it would disregard the remainder of a hunk after encountering one of these control characters.
- [#439](https://github.com/kpdecker/jsdiff/pull/439) Prefer diffs that order deletions before insertions. When faced with a choice between two diffs with an equal total edit distance, the Myers diff algorithm generally prefers one that does deletions before insertions rather than insertions before deletions. For instance, when diffing `abcd` against `acbd`, it will prefer a diff that says to delete the `b` and then insert a new `b` after the `c`, over a diff that says to insert a `c` before the `b` and then delete the existing `c`. JsDiff deviated from the published Myers algorithm in a way that led to it having the opposite preference in many cases, including that example. This is now fixed, meaning diffs output by JsDiff will more accurately reflect what the published Myers diff algorithm would output.
- [#455](https://github.com/kpdecker/jsdiff/pull/455) The `added` and `removed` properties of change objects are now guaranteed to be set to a boolean value. (Previously, they would be set to `undefined` or omitted entirely instead of setting them to false.)
- [#464](https://github.com/kpdecker/jsdiff/pull/464) Specifying `{maxEditLength: 0}` now sets a max edit length of 0 instead of no maximum.

## Development

Expand Down
2 changes: 1 addition & 1 deletion src/diff/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Diff.prototype = {
let newLen = newString.length, oldLen = oldString.length;
let editLength = 1;
let maxEditLength = newLen + oldLen;
if(options.maxEditLength) {
if(options.maxEditLength != null) {
maxEditLength = Math.min(maxEditLength, options.maxEditLength);
}

Expand Down
20 changes: 20 additions & 0 deletions test/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ describe('diff/line', function() {
'line\nnew value\nline', { callback, maxEditLength: 1 });
});
});

describe('given options.maxEditLength === 0', function() {
it('returns normally if the strings are identical', function() {
const diffResult = diffLines(
'foo\nbar\nbaz\nqux\n',
'foo\nbar\nbaz\nqux\n',
{ maxEditLength: 0 }
);
expect(convertChangesToXML(diffResult)).to.equal('foo\nbar\nbaz\nqux\n');
});

it('terminates early if there is even a single change', function() {
const diffResult = diffLines(
'foo\nbar\nbaz\nqux\n',
'fox\nbar\nbaz\nqux\n',
{ maxEditLength: 0 }
);
expect(diffResult).to.be.undefined;
});
});
});

// Trimmed Line Diff
Expand Down