From 20c6822f31a1057e5b00ab8d8af9012a8550ef9e Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Sat, 5 Feb 2022 04:19:41 +0100 Subject: [PATCH 1/2] Document and test ignoreWhiteSpace for createPatch --- README.md | 4 +++- test/patch/create.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be7b4ec8a..8992863cd 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,9 @@ 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. * `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..e35904071 100644 --- a/test/patch/create.js +++ b/test/patch/create.js @@ -631,6 +631,37 @@ 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('#structuredPatch', function() { From 0a0b0815311cf122d5d96feded13645bbc05f1b5 Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Sat, 5 Feb 2022 04:20:57 +0100 Subject: [PATCH 2/2] 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. --- README.md | 1 + test/patch/create.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 8992863cd..ec13ab6dc 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ npm install diff --save * `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 e35904071..2ea3895df 100644 --- a/test/patch/create.js +++ b/test/patch/create.js @@ -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() {