Skip to content

Commit 6852e05

Browse files
committed
chore: make diff-match-patch optional
1 parent 5ba61b3 commit 6852e05

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

lib/filters/texts.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
/* global diff_match_patch */
2-
import dmp from 'diff-match-patch';
3-
41
let TEXT_DIFF = 2;
52
let DEFAULT_MIN_LENGTH = 60;
63
let cachedDiffPatch = null;
74

8-
let getDiffMatchPatch = function(required) {
5+
let getDiffMatchPatch = function(options = {}, required) {
6+
97
/* jshint camelcase: false */
108

119
if (!cachedDiffPatch) {
1210
let instance;
13-
/* eslint-disable camelcase, new-cap */
14-
if (typeof diff_match_patch !== 'undefined') {
15-
16-
// already loaded, probably a browser
17-
instance =
18-
typeof diff_match_patch === 'function'
19-
? new diff_match_patch()
20-
: new diff_match_patch.diff_match_patch();
21-
} else if (dmp) {
22-
try {
23-
instance = dmp && new dmp();
24-
} catch (err) {
25-
instance = null;
26-
}
11+
12+
const {
13+
textDiff: {
14+
diffMatchPatch
15+
} = {}
16+
} = options;
17+
18+
try {
19+
instance = diffMatchPatch && new diffMatchPatch();
20+
} catch (err) {
21+
instance = null;
2722
}
23+
2824
/* eslint-enable camelcase, new-cap */
2925
if (!instance) {
3026
if (!required) {
@@ -72,7 +68,7 @@ export const diffFilter = function textsDiffFilter(context) {
7268
}
7369

7470
// large text, try to use a text-diff algorithm
75-
let diffMatchPatch = getDiffMatchPatch();
71+
let diffMatchPatch = getDiffMatchPatch(context.options);
7672
if (!diffMatchPatch) {
7773

7874
// diff-match-patch library not available,
@@ -94,7 +90,7 @@ export const patchFilter = function textsPatchFilter(context) {
9490
}
9591

9692
// text-diff, use a text-patch algorithm
97-
const patch = getDiffMatchPatch(true).patch;
93+
const patch = getDiffMatchPatch(context.options, true).patch;
9894
context.setResult(patch(context.left, context.delta[0])).exit();
9995
};
10096
patchFilter.filterName = 'texts';

lib/index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export interface Config {
2121
};
2222
textDiff?: {
2323
// default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
24-
minLength: number,
24+
minLength?: number,
25+
// a diff-match-patch constructor for text diffing
26+
diffMatchPatch?: any
2527
};
2628
/*
2729
this optional function can be specified to ignore object properties (eg. volatile data)

package-lock.json

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
"patch"
3131
],
3232
"dependencies": {
33-
"diff-match-patch": "^1.0.0",
3433
"picocolors": "^1.0.1"
3534
},
3635
"devDependencies": {
3736
"chai": "^5.1.1",
37+
"diff-match-patch": "^1.0.5",
3838
"eslint": "^8.57.0",
3939
"eslint-plugin-bpmn-io": "^1.0.1",
4040
"eslint-plugin-import": "^2.8.0",

test/examples/diffpatch.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import DiffMatchPatch from 'diff-match-patch';
2+
13
const examples = {};
24

35
const exampleDate = () => new Date(2020, 10, 30, 15, 10, 3);
@@ -570,6 +572,7 @@ examples.text = [
570572
right: largeText,
571573
delta: [ shortText, largeText ],
572574
reverse: [ largeText, shortText ],
575+
options: { textDiff: { diffMatchPatch: DiffMatchPatch } },
573576
},
574577
{
575578
left: largeText,
@@ -589,24 +592,27 @@ examples.text = [
589592
2,
590593
],
591594
exactReverse: false,
595+
options: { textDiff: { diffMatchPatch: DiffMatchPatch } },
592596
},
593597
{
594598
name: 'larger than min length',
595599
options: {
596600
textDiff: {
601+
diffMatchPatch: DiffMatchPatch,
597602
minLength: 10,
598603
},
599604
},
600605
left: largeText.substr(0, 10),
601606
right: largeText.substr(0, 11).replace(/Madre/g, 'Padre'),
602607
delta: [ '@@ -1,10 +1,11 @@\n -\n-M\n+P\n adre,%0Acu\n+a\n', 0, 2 ],
603608
reverse: [ '@@ -1,11 +1,10 @@\n -\n-P\n+M\n adre,%0Acu\n-a\n', 0, 2 ],
604-
exactReverse: false,
609+
exactReverse: false
605610
},
606611
{
607612
name: 'shorter than min length',
608613
options: {
609614
textDiff: {
615+
diffMatchPatch: DiffMatchPatch,
610616
minLength: 10,
611617
},
612618
},

0 commit comments

Comments
 (0)