Skip to content

Commit b3becdc

Browse files
committed
Use diff-match-patch for text diffs
`diff` is really slow for large strings
1 parent 3573896 commit b3becdc

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
@@ -2,6 +2,7 @@
22
const indentString = require('indent-string');
33
const chalk = require('chalk');
44
const diff = require('diff');
5+
const DiffMatchPatch = require('diff-match-patch');
56

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

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

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

59-
return part.value;
61+
return part[1];
6062
})
6163
.join('');
6264

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)