Skip to content

Commit

Permalink
Document and test newLineIsToken for createPatch.
Browse files Browse the repository at this point in the history
I feel like this option is probably not that good since I supsect the patches it outputs doesn't really make any sense.
  • Loading branch information
oBusk committed Feb 5, 2022
1 parent 423d177 commit 12eb172
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ npm install diff --save
* `newHeader` : Additional information to include in the new file header* `options` : An object with options.
- `context` describes how many lines of context should be included.
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
- `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.

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

Expand Down
41 changes: 41 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,47 @@ describe('patch/create', function() {
expect(diffResult).to.equal(expectedResult);
});
});

describe('newlineIsToken', function() {
it('newlineIsToken: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'

// Diff is shown as entire row, eventhough text is unchanged
+ '-line\n'
+ '+line\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: false});
expect(diffResult).to.equal(expectedResult);
});

it('newlineIsToken: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,3 +1,3 @@\n'
+ ' line\n'

// Newline change is shown as a single diff
+ '-\n'
+ '+\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
expect(diffResult).to.equal(expectedResult);
});
});
});

describe('#structuredPatch', function() {
Expand Down

0 comments on commit 12eb172

Please sign in to comment.