diff --git a/README.md b/README.md index 67010ac5..a8aefc97 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/release-notes.md b/release-notes.md index 16d7c997..9fa328f1 100644 --- a/release-notes.md +++ b/release-notes.md @@ -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 diff --git a/src/diff/line.js b/src/diff/line.js index 1ad05e20..5d313013 100644 --- a/src/diff/line.js +++ b/src/diff/line.js @@ -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); }; diff --git a/test/diff/line.js b/test/diff/line.js index 1461b264..7ce78a68 100644 --- a/test/diff/line.js +++ b/test/diff/line.js @@ -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' } + ] + ); + }); });