Skip to content

Commit

Permalink
Add ignoreNewlineAtEof (#530)
Browse files Browse the repository at this point in the history
* Add ignoreNewlineAtEof

* Document new option

* Add release notes
  • Loading branch information
ExplodingCabbage authored Jun 24, 2024
1 parent 896c982 commit 0e7c20c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform

Options
* `ignoreWhitespace`: `true` to ignore leading and trailing whitespace characters when checking if two lines are equal. Defaults to `false`.
* `ignoreNewlineAtEof`: `true` to ignore a missing newline character at the end of the last line when comparing it to other lines. (By default, the line `'b\n'` in text `'a\nb\nc'` is not considered equal to the line `'b'` in text `'a\nb'`; this option makes them be considered equal.) Ignored if `ignoreWhitespace` or `newlineIsToken` are also true.
* `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before performing the diff. Defaults to `false`.
This helps to get a useful diff when diffing UNIX text files against Windows text files.
* `newlineIsToken`: `true` to treat the newline character at the end of each line as its own token. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines`; the default behavior with this option turned off is better suited for patches and other computer friendly output. Defaults to `false`.
Expand Down
3 changes: 2 additions & 1 deletion release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
- [#490](https://github.com/kpdecker/jsdiff/pull/490) **When calling diffing functions in async mode by passing a `callback` option, the diff result will now be passed as the *first* argument to the callback instead of the second.** (Previously, the first argument was never used at all and would always have value `undefined`.)
- [#489](github.com/kpdecker/jsdiff/pull/489) **`this.options` no longer exists on `Diff` objects.** Instead, `options` is now passed as an argument to methods that rely on options, like `equals(left, right, options)`. This fixes a race condition in async mode, where diffing behaviour could be changed mid-execution if a concurrent usage of the same `Diff` instances overwrote its `options`.
- [#518](https://github.com/kpdecker/jsdiff/pull/518) **`linedelimiters` no longer exists** on patch objects; instead, when a patch with Windows-style CRLF line endings is parsed, **the lines in `lines` will end with `\r`**. There is now a **new `autoConvertLineEndings` option, on by default**, which makes it so that when a patch with Windows-style line endings is applied to a source file with Unix style line endings, the patch gets autoconverted to use Unix-style line endings, and when a patch with Unix-style line endings is applied to a source file with Windows-style line endings, it gets autoconverted to use Windows-style line endings.
- [#521](https://github.com/kpdecker/jsdiff/pull/521) **the `callback` option is now supported by `structuredPatch`, `createPatch`, and `createTwoFilesPatch`**
- [#521](https://github.com/kpdecker/jsdiff/pull/521) **the `callback` option is now supported by `structuredPatch`, `createPatch
- [#529](https://github.com/kpdecker/jsdiff/pull/529) **`parsePatch` can now parse patches where lines starting with `--` or `++` are deleted/inserted**; previously, there were edge cases where the parser would choke on valid patches or give wrong results.
- [#530](https://github.com/kpdecker/jsdiff/pull/530) **Added `ignoreNewlineAtEof` option` to `diffLines`**

## v5.2.0

Expand Down
7 changes: 7 additions & 0 deletions src/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ lineDiff.equals = function(left, right, options) {
if (!options.newlineIsToken || !right.includes('\n')) {
right = right.trim();
}
} else if (options.ignoreNewlineAtEof && !options.newlineIsToken) {
if (left.endsWith('\n')) {
left = left.slice(0, -1);
}
if (right.endsWith('\n')) {
right = right.slice(0, -1);
}
}
return Diff.prototype.equals.call(this, left, right, options);
};
Expand Down
23 changes: 23 additions & 0 deletions test/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,27 @@ describe('diff/line', function() {
{value: 'line\nline', count: 2, added: false, removed: false}
]);
});

it('ignores absence of newline on the last line of file wrt equality when ignoreNewlineAtEof', function() {
// Example taken directly from https://github.com/kpdecker/jsdiff/issues/324
expect(diffLines('a\nb\nc', 'a\nb')).to.eql(
[
{ count: 1, added: false, removed: false, value: 'a\n' },
{ count: 2, added: false, removed: true, value: 'b\nc' },
{ count: 1, added: true, removed: false, value: 'b' }
]
);
expect(diffLines('a\nb\nc', 'a\nb', { ignoreNewlineAtEof: true })).to.eql(
[
{ count: 2, added: false, removed: false, value: 'a\nb' },
{ count: 1, added: false, removed: true, value: 'c' }
]
);
expect(diffLines('a\nb', 'a\nb\nc', { ignoreNewlineAtEof: true })).to.eql(
[
{ count: 2, added: false, removed: false, value: 'a\nb\n' },
{ count: 1, added: true, removed: false, value: 'c' }
]
);
});
});

0 comments on commit 0e7c20c

Please sign in to comment.