From b3becdc31e437a0f6a96e7fffe0a7f9efcb2f6e2 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Fri, 24 Feb 2017 19:09:30 +0700 Subject: [PATCH] Use `diff-match-patch` for text diffs `diff` is really slow for large strings --- lib/format-assert-error.js | 14 ++++++++------ package.json | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/format-assert-error.js b/lib/format-assert-error.js index 5d8dd3f30..29f5a3814 100644 --- a/lib/format-assert-error.js +++ b/lib/format-assert-error.js @@ -2,6 +2,7 @@ const indentString = require('indent-string'); const chalk = require('chalk'); const diff = require('diff'); +const DiffMatchPatch = require('diff-match-patch'); const cleanUp = line => { if (line[0] === '+') { @@ -45,18 +46,19 @@ module.exports = err => { } if (err.actualType === 'string' && err.expectedType === 'string') { - const patch = diff.diffChars(err.actual, err.expected); + const diffMatchPatch = new DiffMatchPatch(); + const patch = diffMatchPatch.diff_main(err.actual, 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 part.value; + return part[1]; }) .join(''); diff --git a/package.json b/package.json index 73333bdf4..a9ecfbb66 100644 --- a/package.json +++ b/package.json @@ -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",