Skip to content

Commit 6394c0c

Browse files
committed
Allowed passing options to the execute method
1 parent ecdc19c commit 6394c0c

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/Diff.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import Operation from "./Operation";
66
import * as Utils from "./Utils";
77
import * as WordSplitter from "./WordSplitter";
88

9-
// This value defines balance between speed and memory utilization. The higher it is the faster it works and more memory consumes.
10-
const MatchGranularityMaximum = 4;
11-
129
const specialCaseClosingTags = new Map([
1310
["</strong>", 0],
1411
["</em>", 0],
@@ -38,20 +35,30 @@ class HtmlDiff {
3835
ignoreWhiteSpaceDifferences: boolean;
3936
orphanMatchThreshold: number;
4037

41-
constructor(oldText: string, newText: string) {
38+
constructor(
39+
oldText: string,
40+
newText: string,
41+
options?: {
42+
repeatingWordsAccuracy?: number;
43+
ignoreWhiteSpaceDifferences?: boolean;
44+
orphanMatchThreshold?: number;
45+
matchGranularity?: number;
46+
}
47+
) {
4248
this.content = [];
4349
this.newText = newText;
4450
this.oldText = oldText;
4551

4652
this.specialTagDiffStack = [];
4753
this.newWords = [];
4854
this.oldWords = [];
49-
this.matchGranularity = 0;
55+
this.matchGranularity = options?.matchGranularity ?? 4;
5056
this.blockExpressions = [];
5157

52-
this.repeatingWordsAccuracy = 1.0;
53-
this.ignoreWhiteSpaceDifferences = false;
54-
this.orphanMatchThreshold = 0.0;
58+
this.repeatingWordsAccuracy = options?.repeatingWordsAccuracy ?? 1;
59+
this.ignoreWhiteSpaceDifferences =
60+
options?.ignoreWhiteSpaceDifferences ?? false;
61+
this.orphanMatchThreshold = options?.orphanMatchThreshold ?? 0;
5562
}
5663

5764
build(): string {
@@ -62,10 +69,11 @@ class HtmlDiff {
6269
this.splitInputsIntoWords();
6370

6471
this.matchGranularity = Math.min(
65-
MatchGranularityMaximum,
72+
this.matchGranularity,
6673
this.oldWords.length,
6774
this.newWords.length
6875
);
76+
6977
const operations = this.operations();
7078

7179
for (const item of operations) {
@@ -437,8 +445,17 @@ class HtmlDiff {
437445
return null;
438446
}
439447

440-
static execute(oldText: string, newText: string): string {
441-
return new HtmlDiff(oldText, newText).build();
448+
static execute(
449+
oldText: string,
450+
newText: string,
451+
options?: {
452+
repeatingWordsAccuracy?: number;
453+
ignoreWhiteSpaceDifferences?: boolean;
454+
orphanMatchThreshold?: number;
455+
matchGranularity?: number;
456+
}
457+
): string {
458+
return new HtmlDiff(oldText, newText, options).build();
442459
}
443460
}
444461

0 commit comments

Comments
 (0)