Skip to content

Commit 573b7af

Browse files
authored
Option to strip trailing CR (#344)
* Basic option for ignoring CR when doing linediff * Add tests for supporting `stripTrailingCr` in createPatch * Update documentation for createPatch * Improve english of README changes * Only remove one \r to match GNU diff behaviour * Fix the same bad english in other place in README
1 parent 8e51326 commit 573b7af

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ npm install diff --save
3737

3838
Options
3939
* `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. This is the same as `diffTrimmedLines`
40+
* `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff.
41+
This helps to get a useful diff when diffing UNIX text files against Windows text files.
4042
* `newlineIsToken`: `true` to treat newline characters as separate tokens. 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` and `diffLines` is better suited for patches and other computer friendly output.
4143

4244
Returns a list of [change objects](#change-objects).
@@ -81,6 +83,8 @@ npm install diff --save
8183
- `context` describes how many lines of context should be included.
8284
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
8385
- `newlineIsToken`: `true` to treat newline characters as separate tokens. 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` and `diffLines` is better suited for patches and other computer friendly output.
86+
- `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff.
87+
This helps to get a useful diff when diffing UNIX text files against Windows text files.
8488

8589
* `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
8690

src/diff/line.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {generateOptions} from '../util/params';
33

44
export const lineDiff = new Diff();
55
lineDiff.tokenize = function(value) {
6+
if(this.options.stripTrailingCr) {
7+
// remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
8+
value = value.replace(/\r\n/g, '\n');
9+
}
10+
611
let retLines = [],
712
linesAndNewlines = value.split(/(\n|\r\n)/);
813

test/diff/line.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,10 @@ describe('diff/line', function() {
106106
{value: '\nhello', count: 2, added: true, removed: undefined}
107107
]);
108108
});
109+
110+
describe('Strip trailing CR', function() {
111+
expect(diffLines('line\nline', 'line\r\nline', {stripTrailingCr: true})).to.eql([
112+
{value: 'line\nline', count: 2}
113+
]);
114+
});
109115
});

test/patch/create.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,45 @@ describe('patch/create', function() {
705705
});
706706
});
707707

708+
describe('stripTrailingCr', function() {
709+
it('stripTrailingCr: false', function() {
710+
const expectedResult =
711+
'===================================================================\n'
712+
+ '--- foo\n'
713+
+ '+++ bar\n'
714+
+ '@@ -1,2 +1,2 @@\n'
715+
+ '\-line\n'
716+
+ '\+line\r\n'
717+
+ '\ line\n'
718+
+ '\\ No newline at end of file\n';
719+
expect(createTwoFilesPatch(
720+
'foo',
721+
'bar',
722+
'line\nline',
723+
'line\r\nline',
724+
undefined,
725+
undefined,
726+
{stripTrailingCr: false}
727+
)).to.equal(expectedResult);
728+
});
729+
730+
it('stripTrailingCr: true', function() {
731+
const expectedResult =
732+
'===================================================================\n'
733+
+ '--- foo\n'
734+
+ '+++ bar\n';
735+
expect(createTwoFilesPatch(
736+
'foo',
737+
'bar',
738+
'line\nline',
739+
'line\r\nline',
740+
undefined,
741+
undefined,
742+
{stripTrailingCr: true}
743+
)).to.equal(expectedResult);
744+
});
745+
});
746+
708747
describe('#structuredPatch', function() {
709748
it('should handle files with the last line changed', function() {
710749
const res = structuredPatch(

0 commit comments

Comments
 (0)