From 518bda9c133f79eff1c0a5177e77bbc0f3d954d6 Mon Sep 17 00:00:00 2001 From: jihobada Date: Fri, 16 Aug 2019 16:53:14 +0900 Subject: [PATCH] feature: replaced diff to diff-match-patch (#3675) --- lib/reporters/base.js | 27 +++++++++++++++++++++++++-- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index b1d026fcce..0062aad6cb 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -8,6 +8,7 @@ var tty = require('tty'); var diff = require('diff'); +var DiffMatchPatch = require('diff-match-patch'); var milliseconds = require('ms'); var utils = require('../utils'); var supportsColor = process.browser ? null : require('supports-color'); @@ -393,6 +394,28 @@ function inlineDiff(actual, expected) { return msg; } +/** + * diff-match-patch + * + * @private + * @param {String} actual + * @param {String} expected + * @return {string} The diff. + */ + +function createPatch(actual, expected) { + var dmp = new DiffMatchPatch(); + var a = dmp.diff_linesToChars_(actual, expected); + var lineText1 = a.chars1; + var lineText2 = a.chars2; + var lineArray = a.lineArray; + var diffs = dmp.diff_main(lineText1, lineText2); + dmp.diff_charsToLines_(diffs, lineArray); + var patch = dmp.patch_make(diffs); + var msg = dmp.patch_toText(patch); + return decodeURI(msg); +} + /** * Returns unified diff between two strings with coloured ANSI output. * @@ -421,8 +444,8 @@ function unifiedDiff(actual, expected) { function notBlank(line) { return typeof line !== 'undefined' && line !== null; } - var msg = diff.createPatch('string', actual, expected); - var lines = msg.split('\n').splice(5); + var msg = createPatch(actual, expected); + var lines = msg.split('\n').splice(1); return ( '\n ' + colorLines('diff added', '+ expected') + diff --git a/package-lock.json b/package-lock.json index 98f63a2678..8655db7af1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4407,6 +4407,11 @@ "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==" }, + "diff-match-patch": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.4.tgz", + "integrity": "sha512-Uv3SW8bmH9nAtHKaKSanOQmj2DnlH65fUpcrMdfdaOxUG02QQ4YGZ8AE7kKOMisF7UqvOlGKVYWRvezdncW9lg==" + }, "diffie-hellman": { "version": "5.0.3", "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", diff --git a/package.json b/package.json index 62e70cd0b9..ac1affa231 100644 --- a/package.json +++ b/package.json @@ -516,6 +516,7 @@ "browser-stdout": "1.3.1", "debug": "3.2.6", "diff": "3.5.0", + "diff-match-patch": "^1.0.4", "escape-string-regexp": "1.0.5", "find-up": "3.0.0", "glob": "7.1.3",