Skip to content

Commit bd5ed60

Browse files
lukechildsnovemberborn
authored andcommitted
Use diff-match-patch for text diffs (#1285)
`diff` is really slow for large strings
1 parent 9f2ff09 commit bd5ed60

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lib/format-assert-error.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const indentString = require('indent-string');
33
const stripAnsi = require('strip-ansi');
44
const chalk = require('chalk');
55
const diff = require('diff');
6+
const DiffMatchPatch = require('diff-match-patch');
67

78
const cleanUp = line => {
89
if (line[0] === '+') {
@@ -46,18 +47,19 @@ module.exports = err => {
4647
}
4748

4849
if (err.actualType === 'string' && err.expectedType === 'string') {
49-
const patch = diff.diffChars(stripAnsi(err.actual), stripAnsi(err.expected));
50+
const diffMatchPatch = new DiffMatchPatch();
51+
const patch = diffMatchPatch.diff_main(stripAnsi(err.actual), stripAnsi(err.expected));
5052
const msg = patch
5153
.map(part => {
52-
if (part.added) {
53-
return chalk.bgGreen.black(part.value);
54+
if (part[0] === 1) {
55+
return chalk.bgGreen.black(part[1]);
5456
}
5557

56-
if (part.removed) {
57-
return chalk.bgRed.black(part.value);
58+
if (part[0] === -1) {
59+
return chalk.bgRed.black(part[1]);
5860
}
5961

60-
return chalk.red(part.value);
62+
return chalk.red(part[1]);
6163
})
6264
.join('');
6365

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"currently-unhandled": "^0.4.1",
116116
"debug": "^2.2.0",
117117
"diff": "^3.0.1",
118+
"diff-match-patch": "^1.0.0",
118119
"dot-prop": "^4.1.0",
119120
"empower-core": "^0.6.1",
120121
"equal-length": "^1.0.0",

0 commit comments

Comments
 (0)