Skip to content

Commit

Permalink
prettier string comparison output
Browse files Browse the repository at this point in the history
make the comarison between huges strings look better

close axross#13
  • Loading branch information
zkochan committed May 3, 2016
1 parent 47f8045 commit 9330766
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 8 deletions.
76 changes: 73 additions & 3 deletions distributions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var _jsondiffpatch = require('jsondiffpatch');

var _jsondiffpatch2 = _interopRequireDefault(_jsondiffpatch);

var _pdiff = require('pdiff');

var _pdiff2 = _interopRequireDefault(_pdiff);

var INDENT = ' ';
var FIG_TICK = _figures2['default'].tick;
var FIG_CROSS = _figures2['default'].cross;
Expand Down Expand Up @@ -108,6 +112,74 @@ var createReporter = function createReporter() {
});
};

function decodeNewlines(_x3) {
var _again = true;

_function: while (_again) {
var str = _x3;
_again = false;

if (str.match(/([^\\])\\n/g)) {
_x3 = str.replace(/([^\\])\\n/g, '$1\n');
_again = true;
continue _function;
}
return str;
}
}

var diffStrings = function diffStrings(actual, expected) {
var padding = ' ';
var line = 1;
var diff_ = _pdiff2['default'].addLineNumbers(_pdiff2['default'].diff(decodeNewlines(actual), decodeNewlines(expected)));
var diff = _pdiff2['default'].extractDiff(diff_, line);
var maxLine = diff_.length;
var digit = String(maxLine).length;
var spaces = '';
for (var i = 0; i < digit - 1; i++) {
spaces += ' ';
}console.log('');
diff.forEach(function (group, i) {
group.forEach(function (delta) {
var text = padding;
// Add line numbers
if (delta.lineNumberOfLhs != undefined) {
text += _chalk2['default'].magenta((spaces + (delta.lineNumberOfLhs + 1)).substr(-digit));
} else {
text += spaces + _chalk2['default'].magenta('-');
}
text += ' ';
if (delta.lineNumberOfRhs != undefined) {
text += _chalk2['default'].magenta((spaces + (delta.lineNumberOfRhs + 1)).substr(-digit));
} else {
text += spaces + _chalk2['default'].magenta('-');
}
text += ' ';

// Add the value of this line
delta.values.forEach(function (value) {
if (value.added) {
text += _chalk2['default'].green.inverse(value.value);
return;
}
if (value.removed) {
text += _chalk2['default'].red.inverse(value.value);
return;
}
text += _chalk2['default'].dim(value.value);
});

// Ouput the delta
console.log(text);
});

if (i != diff.length - 1) {
console.log(padding + _chalk2['default'].dim('...'));
}
});
console.log('');
};

var _assert$diag = assert.diag;
var at = _assert$diag.at;
var actual = _assert$diag.actual;
Expand Down Expand Up @@ -148,9 +220,7 @@ var createReporter = function createReporter() {
} else if (expected === 'undefined' && actual === 'undefined') {
;
} else if (expected_type === 'string') {
var compared = (0, _diff.diffWords)(actual, expected).map(writeDiff).join('');

println(compared, 4);
diffStrings(actual, expected);
} else {
println(_chalk2['default'].red.inverse(actual) + _chalk2['default'].green.inverse(expected), 4);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"duplexer": "^0.1.1",
"figures": "^1.4.0",
"jsondiffpatch": "^0.1.41",
"pdiff": "^2.0.0",
"pretty-ms": "^2.1.0",
"tap-parser": "^1.2.2",
"through2": "^2.0.0"
Expand Down
63 changes: 58 additions & 5 deletions sources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import through2 from 'through2';
import parser from 'tap-parser';
import prettyMs from 'pretty-ms';
import jsondiffpatch from 'jsondiffpatch';
import pdiff from 'pdiff';

const INDENT = ' ';
const FIG_TICK = figures.tick;
Expand Down Expand Up @@ -64,6 +65,62 @@ const createReporter = () => {
return value.replace(/(^\s*)(.*)/g, (m, one, two) => one + style(two))
};

function decodeNewlines(str) {
if (str.match(/([^\\])\\n/g)) return decodeNewlines(str.replace(/([^\\])\\n/g, '$1\n'));
return str;
}

const diffStrings = (actual, expected) => {
const padding = ' ';
const line = 1;
const diff_ = pdiff.addLineNumbers(pdiff.diff(decodeNewlines(actual), decodeNewlines(expected)));
const diff = pdiff.extractDiff(diff_, line);
const maxLine = diff_.length;
const digit = String(maxLine).length;
let spaces = '';
for (let i = 0; i < digit - 1; i++) spaces += ' ';
console.log('');
diff.forEach((group, i) => {
group.forEach(delta => {
let text = padding;
// Add line numbers
if (delta.lineNumberOfLhs != undefined) {
text += chalk.magenta((spaces + (delta.lineNumberOfLhs + 1)).substr(-digit));
} else {
text += spaces + chalk.magenta('-');
}
text += ' ';
if (delta.lineNumberOfRhs != undefined) {
text += chalk.magenta((spaces + (delta.lineNumberOfRhs + 1)).substr(-digit));
} else {
text += spaces + chalk.magenta('-');
}
text += ' ';

// Add the value of this line
delta.values.forEach(value => {
if (value.added) {
text += chalk.green.inverse(value.value);
return;
}
if (value.removed) {
text += chalk.red.inverse(value.value);
return;
}
text += chalk.dim(value.value);
})

// Ouput the delta
console.log(text);
});

if (i != diff.length - 1) {
console.log(padding + chalk.dim('...'));
}
});
console.log('');
};

let {
at,
actual,
Expand Down Expand Up @@ -108,11 +165,7 @@ const createReporter = () => {
} else if (expected === 'undefined' && actual === 'undefined') {
;
} else if (expected_type === 'string') {
const compared = diffWords(actual, expected)
.map(writeDiff)
.join('');

println(compared, 4);
diffStrings(actual, expected)
} else {
println(
chalk.red.inverse(actual) + chalk.green.inverse(expected),
Expand Down

0 comments on commit 9330766

Please sign in to comment.