Skip to content

Commit 12eb172

Browse files
committed
Document and test newLineIsToken for createPatch.
I feel like this option is probably not that good since I supsect the patches it outputs doesn't really make any sense.
1 parent 423d177 commit 12eb172

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ npm install diff --save
7575
* `newHeader` : Additional information to include in the new file header* `options` : An object with options.
7676
- `context` describes how many lines of context should be included.
7777
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
78+
- `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.
7879

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

test/patch/create.js

+41
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,47 @@ describe('patch/create', function() {
662662
expect(diffResult).to.equal(expectedResult);
663663
});
664664
});
665+
666+
describe('newlineIsToken', function() {
667+
it('newlineIsToken: false', function() {
668+
const expectedResult =
669+
'Index: testFileName\n'
670+
+ '===================================================================\n'
671+
+ '--- testFileName\n'
672+
+ '+++ testFileName\n'
673+
+ '@@ -1,2 +1,2 @@\n'
674+
675+
// Diff is shown as entire row, eventhough text is unchanged
676+
+ '-line\n'
677+
+ '+line\r\n'
678+
679+
+ ' line\n'
680+
+ '\\ No newline at end of file\n';
681+
682+
const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: false});
683+
expect(diffResult).to.equal(expectedResult);
684+
});
685+
686+
it('newlineIsToken: true', function() {
687+
const expectedResult =
688+
'Index: testFileName\n'
689+
+ '===================================================================\n'
690+
+ '--- testFileName\n'
691+
+ '+++ testFileName\n'
692+
+ '@@ -1,3 +1,3 @@\n'
693+
+ ' line\n'
694+
695+
// Newline change is shown as a single diff
696+
+ '-\n'
697+
+ '+\r\n'
698+
699+
+ ' line\n'
700+
+ '\\ No newline at end of file\n';
701+
702+
const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
703+
expect(diffResult).to.equal(expectedResult);
704+
});
705+
});
665706
});
666707

667708
describe('#structuredPatch', function() {

0 commit comments

Comments
 (0)