Skip to content

Commit 0e7c20c

Browse files
Add ignoreNewlineAtEof (#530)
* Add ignoreNewlineAtEof * Document new option * Add release notes
1 parent 896c982 commit 0e7c20c

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
4646

4747
Options
4848
* `ignoreWhitespace`: `true` to ignore leading and trailing whitespace characters when checking if two lines are equal. Defaults to `false`.
49+
* `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.
4950
* `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before performing the diff. Defaults to `false`.
5051
This helps to get a useful diff when diffing UNIX text files against Windows text files.
5152
* `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`.

release-notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
- [#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`.)
2626
- [#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`.
2727
- [#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.
28-
- [#521](https://github.com/kpdecker/jsdiff/pull/521) **the `callback` option is now supported by `structuredPatch`, `createPatch`, and `createTwoFilesPatch`**
28+
- [#521](https://github.com/kpdecker/jsdiff/pull/521) **the `callback` option is now supported by `structuredPatch`, `createPatch
2929
- [#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.
30+
- [#530](https://github.com/kpdecker/jsdiff/pull/530) **Added `ignoreNewlineAtEof` option` to `diffLines`**
3031

3132
## v5.2.0
3233

src/diff/line.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ lineDiff.equals = function(left, right, options) {
4545
if (!options.newlineIsToken || !right.includes('\n')) {
4646
right = right.trim();
4747
}
48+
} else if (options.ignoreNewlineAtEof && !options.newlineIsToken) {
49+
if (left.endsWith('\n')) {
50+
left = left.slice(0, -1);
51+
}
52+
if (right.endsWith('\n')) {
53+
right = right.slice(0, -1);
54+
}
4855
}
4956
return Diff.prototype.equals.call(this, left, right, options);
5057
};

test/diff/line.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,27 @@ describe('diff/line', function() {
244244
{value: 'line\nline', count: 2, added: false, removed: false}
245245
]);
246246
});
247+
248+
it('ignores absence of newline on the last line of file wrt equality when ignoreNewlineAtEof', function() {
249+
// Example taken directly from https://github.com/kpdecker/jsdiff/issues/324
250+
expect(diffLines('a\nb\nc', 'a\nb')).to.eql(
251+
[
252+
{ count: 1, added: false, removed: false, value: 'a\n' },
253+
{ count: 2, added: false, removed: true, value: 'b\nc' },
254+
{ count: 1, added: true, removed: false, value: 'b' }
255+
]
256+
);
257+
expect(diffLines('a\nb\nc', 'a\nb', { ignoreNewlineAtEof: true })).to.eql(
258+
[
259+
{ count: 2, added: false, removed: false, value: 'a\nb' },
260+
{ count: 1, added: false, removed: true, value: 'c' }
261+
]
262+
);
263+
expect(diffLines('a\nb', 'a\nb\nc', { ignoreNewlineAtEof: true })).to.eql(
264+
[
265+
{ count: 2, added: false, removed: false, value: 'a\nb\n' },
266+
{ count: 1, added: true, removed: false, value: 'c' }
267+
]
268+
);
269+
});
247270
});

0 commit comments

Comments
 (0)