Skip to content

Commit fffd8a7

Browse files
authored
Document and test options to createPatch (#345)
* Document and test ignoreWhiteSpace for createPatch * 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 92c9d90 commit fffd8a7

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ npm install diff --save
7373
* `newStr` : New string value
7474
* `oldHeader` : Additional information to include in the old file header
7575
* `newHeader` : Additional information to include in the new file header
76-
* `options` : An object with options. Currently, only `context` is supported and describes how many lines of context should be included.
76+
* `options` : An object with options.
77+
- `context` describes how many lines of context should be included.
78+
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
79+
- `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.
7780

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

Diff for: test/patch/create.js

+72
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,78 @@ describe('patch/create', function() {
631631
const diffResult = createTwoFilesPatch('foo', 'bar', '', '');
632632
expect(diffResult).to.equal(expectedResult);
633633
});
634+
635+
describe('ignoreWhitespace', function() {
636+
it('ignoreWhitespace: false', function() {
637+
const expectedResult =
638+
'Index: testFileName\n'
639+
+ '===================================================================\n'
640+
+ '--- testFileName\n'
641+
+ '+++ testFileName\n'
642+
+ '@@ -1,2 +1,2 @@\n'
643+
+ '-line \n'
644+
+ '- line\n'
645+
+ '\\ No newline at end of file\n'
646+
+ '+line\n'
647+
+ '+line\n'
648+
+ '\\ No newline at end of file\n';
649+
650+
const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: false});
651+
expect(diffResult).to.equal(expectedResult);
652+
});
653+
654+
it('ignoreWhitespace: true', function() {
655+
const expectedResult =
656+
'Index: testFileName\n'
657+
+ '===================================================================\n'
658+
+ '--- testFileName\n'
659+
+ '+++ testFileName\n';
660+
661+
const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: true});
662+
expect(diffResult).to.equal(expectedResult);
663+
});
664+
});
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+
});
634706
});
635707

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

0 commit comments

Comments
 (0)