Skip to content

Add option to opt out of text diffing #314

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ var jsondiffpatch = require('jsondiffpatch').create({
includeValueOnMove: false
},
textDiff: {
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
// default 60, minimum string length (left and right sides) to use text diff algorithm: google-diff-match-patch
minLength: 60
// default true, set to false to completely opt out of text diffing
enabled: true,
},
propertyFilter: function(name, context) {
/*
Expand Down
10 changes: 9 additions & 1 deletion docs/deltas.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ an item was moved to a different position in the same array
``` javascript
delta = [ '', destinationIndex, 3]
```
> Note: '' represents the moved item value, suppresed by default
> Note: '' represents the moved item value, suppressed by default

> Note: 3 is the magical number that indicates "array move"

Expand Down Expand Up @@ -118,3 +118,11 @@ delta = [ unidiff, 0, 2 ]

> Note: unidiff is actually a character-based variation of Unidiff format that is explained [here](https://code.google.com/p/google-diff-match-patch/wiki/Unidiff)

You can completely opt out of text diffing:
``` javascript
var customDiffPatch = jsondiffpatch.create({
textDiff: {
enabled: false // default true
}
});
```
13 changes: 13 additions & 0 deletions src/filters/texts.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ export const diffFilter = function textsDiffFilter(context) {
if (context.leftType !== 'string') {
return;
}

// opt out of text diffing
let enabled =
(context.options &&
context.options.textDiff &&
context.options.textDiff.enabled)
if (enabled === false) {
context.setResult([context.left, context.right]).exit();
return;
}

let minLength =
(context.options &&
context.options.textDiff &&
Expand All @@ -69,6 +80,7 @@ export const diffFilter = function textsDiffFilter(context) {
context.setResult([context.left, context.right]).exit();
return;
}

// large text, try to use a text-diff algorithm
let diffMatchPatch = getDiffMatchPatch();
if (!diffMatchPatch) {
Expand All @@ -77,6 +89,7 @@ export const diffFilter = function textsDiffFilter(context) {
context.setResult([context.left, context.right]).exit();
return;
}

let diff = diffMatchPatch.diff;
context.setResult([diff(context.left, context.right), 0, TEXT_DIFF]).exit();
};
Expand Down
6 changes: 4 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ export interface Config {
};

textDiff?: {
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
minLength: number,
// default 60, minimum string length (left and right sides) to use text diff algorithm: google-diff-match-patch
minLength?: number,
// default true, set to false to completely opt out of text diffing
enabled?: boolean,
};

/**
Expand Down