Skip to content

Commit f64fbf3

Browse files
committed
Some modifications to the diff logger
1 parent baa3b62 commit f64fbf3

1 file changed

Lines changed: 50 additions & 17 deletions

File tree

lib/diff-logger.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,72 @@
11
require('colors');
22

3-
exports.log = function(diff, options) {
3+
/**
4+
* Returns readable diff text
5+
* @param {Object[]} diff
6+
* @param {Object} [options]
7+
* @param {Number} [options.charsAroundDiff=20]
8+
* @returns {String}
9+
*/
10+
function getDiffText(diff, options) {
11+
options = options || {
12+
charsAroundDiff: 20
13+
};
414

5-
options = options || { showCharacters: 20 };
15+
if (options.showCharacters) {
16+
console.log('Option "showCharacters" is deprecated, please use "charsAroundDiff"');
17+
options.charsAroundDiff = options.showCharacters;
18+
}
619

7-
var showCharacters = options.showCharacters;
20+
var charsAroundDiff = options.charsAroundDiff,
21+
output = '';
822

9-
showCharacters = showCharacters > 0 ? showCharacters : 20;
10-
11-
var output = '';
23+
if (charsAroundDiff > 0) {
24+
charsAroundDiff = 20;
25+
}
1226

1327
if (diff.length === 1 && !diff[0].added && !diff[0].removed) return output;
1428

1529
diff.forEach(function(part) {
16-
var color = part.added ? 'green' :
17-
part.removed ? 'red' : 'grey',
18-
indexOfPart = diff.indexOf(part);
30+
var index = diff.indexOf(part),
31+
partValue = part.value,
32+
color = 'grey';
1933

20-
if (color !== 'grey') {
34+
if (part.added) color = 'green';
35+
if (part.removed) color = 'red';
2136

22-
output += (!indexOfPart ? '\n' : '') + part.value[color];
37+
if (color !== 'grey') {
38+
output += (!index ? '\n' : '') + partValue[color];
2339

2440
return;
2541
}
2642

27-
if (part.value.length < showCharacters * 2) {
28-
output += (indexOfPart ? '' : '\n') + part.value.grey;
43+
if (partValue.length < charsAroundDiff * 2) {
44+
output += (index ? '' : '\n') + partValue.grey;
2945
} else {
30-
indexOfPart && (output += part.value.substr(0, showCharacters).grey);
46+
index && (output += partValue.substr(0, charsAroundDiff).grey);
3147

32-
if (indexOfPart < diff.length - 1) {
33-
output += '\n...\n' + part.value.substr(part.value.length - showCharacters, showCharacters).grey;
48+
if (index < diff.length - 1) {
49+
output += '\n...\n' + partValue.substr(partValue.length - charsAroundDiff, charsAroundDiff).grey;
3450
}
3551
}
3652
});
3753

38-
console.log('Differences:' + output);
54+
return output;
3955
}
56+
57+
/**
58+
* Logs diff of the given HTML docs to the console
59+
* @param {Object[]} diff
60+
* @param {Object} [options]
61+
* @param {Number} [options.charsAroundDiff=20]
62+
*/
63+
function log(diff, options) {
64+
var diffText = getDiffText(diff, options);
65+
66+
console.log(diffText);
67+
}
68+
69+
module.exports = {
70+
getDiffText: getDiffText,
71+
log: log
72+
};

0 commit comments

Comments
 (0)