Skip to content

Commit

Permalink
Use diff-match-patch for text diffs (#1285)
Browse files Browse the repository at this point in the history
`diff` is really slow for large strings
  • Loading branch information
lukechilds authored and novemberborn committed Feb 26, 2017
1 parent 9f2ff09 commit bd5ed60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/format-assert-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const indentString = require('indent-string');
const stripAnsi = require('strip-ansi');
const chalk = require('chalk');
const diff = require('diff');
const DiffMatchPatch = require('diff-match-patch');

const cleanUp = line => {
if (line[0] === '+') {
Expand Down Expand Up @@ -46,18 +47,19 @@ module.exports = err => {
}

if (err.actualType === 'string' && err.expectedType === 'string') {
const patch = diff.diffChars(stripAnsi(err.actual), stripAnsi(err.expected));
const diffMatchPatch = new DiffMatchPatch();
const patch = diffMatchPatch.diff_main(stripAnsi(err.actual), stripAnsi(err.expected));
const msg = patch
.map(part => {
if (part.added) {
return chalk.bgGreen.black(part.value);
if (part[0] === 1) {
return chalk.bgGreen.black(part[1]);
}

if (part.removed) {
return chalk.bgRed.black(part.value);
if (part[0] === -1) {
return chalk.bgRed.black(part[1]);
}

return chalk.red(part.value);
return chalk.red(part[1]);
})
.join('');

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"currently-unhandled": "^0.4.1",
"debug": "^2.2.0",
"diff": "^3.0.1",
"diff-match-patch": "^1.0.0",
"dot-prop": "^4.1.0",
"empower-core": "^0.6.1",
"equal-length": "^1.0.0",
Expand Down

0 comments on commit bd5ed60

Please sign in to comment.