|
1 | 1 | require('colors'); |
2 | 2 |
|
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 | + }; |
4 | 14 |
|
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 | + } |
6 | 19 |
|
7 | | - var showCharacters = options.showCharacters; |
| 20 | + var charsAroundDiff = options.charsAroundDiff, |
| 21 | + output = ''; |
8 | 22 |
|
9 | | - showCharacters = showCharacters > 0 ? showCharacters : 20; |
10 | | - |
11 | | - var output = ''; |
| 23 | + if (charsAroundDiff > 0) { |
| 24 | + charsAroundDiff = 20; |
| 25 | + } |
12 | 26 |
|
13 | 27 | if (diff.length === 1 && !diff[0].added && !diff[0].removed) return output; |
14 | 28 |
|
15 | 29 | 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'; |
19 | 33 |
|
20 | | - if (color !== 'grey') { |
| 34 | + if (part.added) color = 'green'; |
| 35 | + if (part.removed) color = 'red'; |
21 | 36 |
|
22 | | - output += (!indexOfPart ? '\n' : '') + part.value[color]; |
| 37 | + if (color !== 'grey') { |
| 38 | + output += (!index ? '\n' : '') + partValue[color]; |
23 | 39 |
|
24 | 40 | return; |
25 | 41 | } |
26 | 42 |
|
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; |
29 | 45 | } else { |
30 | | - indexOfPart && (output += part.value.substr(0, showCharacters).grey); |
| 46 | + index && (output += partValue.substr(0, charsAroundDiff).grey); |
31 | 47 |
|
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; |
34 | 50 | } |
35 | 51 | } |
36 | 52 | }); |
37 | 53 |
|
38 | | - console.log('Differences:' + output); |
| 54 | + return output; |
39 | 55 | } |
| 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