Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom compareMethod #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compute-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ const constructLines = (value: string): string[] => {
const computeDiff = (
oldValue: string,
newValue: string,
compareMethod: string = DiffMethod.CHARS,
compareMethod: string | ((oldStr: string, newStr: string) => diff.Change[]) = DiffMethod.CHARS,
): ComputedDiffInformation => {
const diffArray: JsDiffChangeObject[] = jsDiff[compareMethod](
const diffArray: JsDiffChangeObject[] = ((typeof compareMethod === 'string') ? jsDiff[compareMethod] : compareMethod)(
oldValue,
newValue,
);
Expand Down Expand Up @@ -143,7 +143,7 @@ const computeLineInformation = (
oldString: string,
newString: string,
disableWordDiff: boolean = false,
compareMethod: string = DiffMethod.CHARS,
compareMethod: string | ((oldStr: string, newStr: string) => diff.Change[]) = DiffMethod.CHARS,
linesOffset: number = 0,
): ComputedLineInformation => {
const diffArray = diff.diffLines(
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import * as PropTypes from 'prop-types';
import cn from 'classnames';
import { Change } from 'diff';

import {
computeLineInformation,
Expand Down Expand Up @@ -36,7 +37,7 @@ export interface ReactDiffViewerProps {
// Enable/Disable word diff.
disableWordDiff?: boolean;
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
compareMethod?: DiffMethod;
compareMethod?: DiffMethod | ((oldStr: string, newStr: string) => Change[]);
// Number of unmodified lines surrounding each line diff.
extraLinesSurroundingDiff?: number;
// Show/hide line number.
Expand Down Expand Up @@ -99,7 +100,7 @@ class DiffViewer extends React.Component<
newValue: PropTypes.string.isRequired,
splitView: PropTypes.bool,
disableWordDiff: PropTypes.bool,
compareMethod: PropTypes.oneOf(Object.values(DiffMethod)),
compareMethod: PropTypes.oneOfType([PropTypes.oneOf(Object.values(DiffMethod)), PropTypes.func]),
renderContent: PropTypes.func,
onLineNumberClick: PropTypes.func,
extraLinesSurroundingDiff: PropTypes.number,
Expand Down