diff --git a/README.md b/README.md index be7b4ec8a..ec13ab6dc 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,10 @@ npm install diff --save * `newStr` : New string value * `oldHeader` : Additional information to include in the old file header * `newHeader` : Additional information to include in the new file header - * `options` : An object with options. Currently, only `context` is supported and describes how many lines of context should be included. + * `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. diff --git a/test/patch/create.js b/test/patch/create.js index 1315df564..2ea3895df 100644 --- a/test/patch/create.js +++ b/test/patch/create.js @@ -631,6 +631,78 @@ describe('patch/create', function() { const diffResult = createTwoFilesPatch('foo', 'bar', '', ''); expect(diffResult).to.equal(expectedResult); }); + + describe('ignoreWhitespace', function() { + it('ignoreWhitespace: false', function() { + const expectedResult = + 'Index: testFileName\n' + + '===================================================================\n' + + '--- testFileName\n' + + '+++ testFileName\n' + + '@@ -1,2 +1,2 @@\n' + + '-line \n' + + '- line\n' + + '\\ No newline at end of file\n' + + '+line\n' + + '+line\n' + + '\\ No newline at end of file\n'; + + const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: false}); + expect(diffResult).to.equal(expectedResult); + }); + + it('ignoreWhitespace: true', function() { + const expectedResult = + 'Index: testFileName\n' + + '===================================================================\n' + + '--- testFileName\n' + + '+++ testFileName\n'; + + const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: true}); + 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() {