From bd5ed603356f7a038fed62f64494b35598741d67 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 26 Feb 2017 22:22:57 +0700 Subject: [PATCH] Use `diff-match-patch` for text diffs (#1285) `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 068fc6fa2..55654274b 100644 --- a/lib/format-assert-error.js +++ b/lib/format-assert-error.js @@ -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] === '+') { @@ -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(''); 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",