Skip to content

Commit 736bb62

Browse files
author
Derek Hammond
committed
update prop name, update type definition, update README
1 parent 4c714af commit 736bb62

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Diff extends PureComponent {
7676
|newValue |`string` |`''` |New value as string. |
7777
|splitView |`boolean` |`true` |Switch between `unified` and `split` view. |
7878
|disableWordDiff |`boolean` |`false` |Show and hide word diff in a diff line. |
79-
|jsDiffCompareMethod |`string` |`'diffChars'` |JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api |
79+
|compareMethod |`string` |`'diffChars'` |JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api |
8080
|hideLineNumbers |`boolean` |`false` |Show and hide line numbers. |
8181
|renderContent |`function` |`undefined` |Render Prop API to render code in the diff viewer. Helpful for [syntax highlighting](#syntax-highlighting) |
8282
|onLineNumberClick |`function` |`undefined` |Event handler for line number click. `(lineId: string) => void` |
@@ -175,7 +175,7 @@ class Diff extends PureComponent {
175175
<ReactDiffViewer
176176
oldValue={oldCode}
177177
newValue={newCode}
178-
jsDiffCompareMethod={DiffMethod.WORDS}
178+
compareMethod={DiffMethod.WORDS}
179179
splitView={true}
180180
renderContent={this.highlightSyntax}
181181
/>

examples/src/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ExampleState {
1919
highlightLine?: string[];
2020
language?: string;
2121
enableSyntaxHighlighting?: boolean;
22-
jsDiffCompareMethod?: string;
22+
compareMethod?: string;
2323
}
2424

2525
const P = (window as any).Prism;
@@ -32,7 +32,7 @@ class Example extends React.Component<{}, ExampleState> {
3232
highlightLine: [],
3333
language: 'javascript',
3434
enableSyntaxHighlighting: true,
35-
jsDiffCompareMethod: 'diffChars'
35+
compareMethod: 'diffChars'
3636
};
3737
}
3838

@@ -47,8 +47,8 @@ class Example extends React.Component<{}, ExampleState> {
4747
private onLanguageChange = (e: any): void =>
4848
this.setState({ language: e.target.value, highlightLine: [] });
4949

50-
private onJsDiffCompareMethodChange = (e: any): void =>
51-
this.setState({ jsDiffCompareMethod: e.target.value });
50+
private onCompareMethodChange = (e: any): void =>
51+
this.setState({ compareMethod: e.target.value });
5252

5353
private onLineNumberClick = (
5454
id: string,
@@ -168,8 +168,8 @@ class Example extends React.Component<{}, ExampleState> {
168168
<select
169169
name="js_diff_compare_method"
170170
id="js_diff_compare_method"
171-
onChange={this.onJsDiffCompareMethodChange}
172-
value={this.state.jsDiffCompareMethod}
171+
onChange={this.onCompareMethodChange}
172+
value={this.state.compareMethod}
173173
>
174174
<option value="disabled">DISABLE</option>
175175
<option value="diffChars">diffChars</option>
@@ -220,8 +220,8 @@ class Example extends React.Component<{}, ExampleState> {
220220
</div>
221221
<div className="diff-viewer">
222222
<ReactDiff
223-
disableWordDiff={ this.state.jsDiffCompareMethod === 'disabled' }
224-
jsDiffCompareMethod={ this.state.jsDiffCompareMethod }
223+
disableWordDiff={ this.state.compareMethod === 'disabled' }
224+
compareMethod={ this.state.compareMethod }
225225
highlightLines={this.state.highlightLine}
226226
onLineNumberClick={this.onLineNumberClick}
227227
oldValue={oldValue}

src/compute-lines.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum DiffType {
88
REMOVED = 2,
99
}
1010

11+
// See https://github.com/kpdecker/jsdiff/tree/v4.0.1#api for more info on the below JsDiff methods
1112
export enum DiffMethod {
1213
CHARS = 'diffChars',
1314
WORDS = 'diffWords',
@@ -19,8 +20,6 @@ export enum DiffMethod {
1920
}
2021

2122
export interface DiffInformation {
22-
added?: boolean;
23-
removed?: boolean;
2423
value?: string | DiffInformation[];
2524
lineNumber?: number;
2625
type?: DiffType;
@@ -41,6 +40,13 @@ export interface ComputedDiffInformation {
4140
right?: DiffInformation[];
4241
}
4342

43+
// See https://github.com/kpdecker/jsdiff/tree/v4.0.1#change-objects for more info on JsDiff Change Objects
44+
export interface JsDiffChangeObject {
45+
added?: boolean;
46+
removed?: boolean;
47+
value?: string;
48+
}
49+
4450
/**
4551
* Splits diff text by new line and computes final list of diff lines based on
4652
* conditions.
@@ -78,16 +84,16 @@ const constructLines = (value: string): string[] => {
7884
*
7985
* @param oldValue Old word in the line.
8086
* @param newValue New word in the line.
81-
* @param jsDiffCompareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
87+
* @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
8288
*/
83-
const computeDiff = (oldValue: string, newValue: string, jsDiffCompareMethod: string): ComputedDiffInformation => {
84-
const diffArray = jsDiff[jsDiffCompareMethod](oldValue, newValue);
89+
const computeDiff = (oldValue: string, newValue: string, compareMethod: string): ComputedDiffInformation => {
90+
const diffArray: Array<JsDiffChangeObject> = jsDiff[compareMethod](oldValue, newValue);
8591
const computedDiff: ComputedDiffInformation = {
8692
left: [],
8793
right: [],
8894
};
8995
diffArray
90-
.forEach(({ added, removed, value }: DiffInformation): DiffInformation => {
96+
.forEach(({ added, removed, value }): DiffInformation => {
9197
const diffInformation: DiffInformation = {};
9298
if (added) {
9399
diffInformation.type = DiffType.ADDED;
@@ -121,13 +127,13 @@ const computeDiff = (oldValue: string, newValue: string, jsDiffCompareMethod: st
121127
* @param oldString Old string to compare.
122128
* @param newString New string to compare with old string.
123129
* @param disableWordDiff Flag to enable/disable word diff.
124-
* @param jsDiffCompareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
130+
* @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
125131
*/
126132
const computeLineInformation = (
127133
oldString: string,
128134
newString: string,
129135
disableWordDiff: boolean = false,
130-
jsDiffCompareMethod: string = DiffMethod.CHARS,
136+
compareMethod: string = DiffMethod.CHARS,
131137
): ComputedLineInformation => {
132138
const diffArray = diff.diffLines(
133139
oldString.trimRight(),
@@ -190,10 +196,10 @@ const computeLineInformation = (
190196
right.type = type;
191197
// Do word level diff and assign the corresponding values to the
192198
// left and right diff information object.
193-
if (disableWordDiff || !(<any>Object).values(DiffMethod).includes(jsDiffCompareMethod)) {
199+
if (disableWordDiff || !(<any>Object).values(DiffMethod).includes(compareMethod)) {
194200
right.value = rightValue;
195201
} else {
196-
const computedDiff = computeDiff(line, rightValue as string, jsDiffCompareMethod);
202+
const computedDiff = computeDiff(line, rightValue as string, compareMethod);
197203
right.value = computedDiff.right;
198204
left.value = computedDiff.left;
199205
}

src/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ReactDiffViewerProps {
3131
// Enable/Disable word diff.
3232
disableWordDiff?: boolean;
3333
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
34-
jsDiffCompareMethod?: string;
34+
compareMethod?: string;
3535
// Number of unmodified lines surrounding each line diff.
3636
extraLinesSurroundingDiff?: number;
3737
// Show/hide line number.
@@ -71,7 +71,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
7171
splitView: true,
7272
highlightLines: [],
7373
disableWordDiff: false,
74-
jsDiffCompareMethod: DiffMethod.CHARS,
74+
compareMethod: DiffMethod.CHARS,
7575
styles: {},
7676
hideLineNumbers: false,
7777
extraLinesSurroundingDiff: 3,
@@ -83,7 +83,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
8383
newValue: PropTypes.string.isRequired,
8484
splitView: PropTypes.bool,
8585
disableWordDiff: PropTypes.bool,
86-
jsDiffCompareMethod: PropTypes.oneOf(Object.values(DiffMethod)),
86+
compareMethod: PropTypes.oneOf(Object.values(DiffMethod)),
8787
renderContent: PropTypes.func,
8888
onLineNumberClick: PropTypes.func,
8989
extraLinesSurroundingDiff: PropTypes.number,
@@ -425,12 +425,12 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
425425
* Generates the entire diff view.
426426
*/
427427
private renderDiff = (): JSX.Element[] => {
428-
const { oldValue, newValue, splitView, disableWordDiff, jsDiffCompareMethod } = this.props;
428+
const { oldValue, newValue, splitView, disableWordDiff, compareMethod } = this.props;
429429
const { lineInformation, diffLines } = computeLineInformation(
430430
oldValue,
431431
newValue,
432432
disableWordDiff,
433-
jsDiffCompareMethod,
433+
compareMethod,
434434
);
435435
const extraLines = this.props.extraLinesSurroundingDiff < 0
436436
? 0

test/compute-lines-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('Testing compute lines utils', (): void => {
199199
});
200200
});
201201

202-
it('Should call "diffChars" jsDiff method when jsDiffCompareMethod is not provided', (): void => {
202+
it('Should call "diffChars" jsDiff method when compareMethod is not provided', (): void => {
203203
const oldCode = `Hello World`;
204204
const newCode = `My Updated Name
205205
Also this info`;
@@ -277,7 +277,7 @@ Also this info`;
277277
});
278278
});
279279

280-
it('Should call "diffWords" jsDiff method when a jsDiffCompareMethod IS provided', (): void => {
280+
it('Should call "diffWords" jsDiff method when a compareMethod IS provided', (): void => {
281281
const oldCode = `Hello World`;
282282
const newCode = `My Updated Name
283283
Also this info`;

0 commit comments

Comments
 (0)