Skip to content

Commit d358d63

Browse files
committed
Merge pull request #50 from Shuhrat/diff-refactor
Some refactoring
2 parents 3f69ae8 + f64fbf3 commit d358d63

2 files changed

Lines changed: 57 additions & 24 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+
};

lib/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ var utils = require('./utils'),
66
_ = require('lodash');
77

88
/**
9-
* Converts HTML to DOM Tree
10-
* @param {String} HTML
9+
* Converts HTML document to AST Tree
10+
* @param {String} HTMLDoc
1111
* @param {Object} options
1212
* @returns {AST}
1313
*/
14-
function htmlToAST(HTML, options) {
14+
function htmlToAST(HTMLDoc, options) {
1515
var parser,
1616
parserHandler;
1717

@@ -20,13 +20,13 @@ function htmlToAST(HTML, options) {
2020
}, options);
2121

2222
parser = new htmlParser.Parser(parserHandler);
23-
parser.parseComplete(HTML);
23+
parser.parseComplete(HTMLDoc);
2424

2525
return parserHandler.dom;
2626
}
2727

2828
/**
29-
*
29+
* Recursively sorts the arttibutes value of the AST leafs
3030
* @param {AST} tree
3131
* @param {Object} options
3232
* @returns {AST}
@@ -77,7 +77,7 @@ function modifyASTTree(tree, options) {
7777
* @param {String[]} [options.compareHtmlAttrsAsJSON]
7878
* @param {Boolean} [options.verbose]
7979
* @param {Boolean} [options.ignoreWhitespace=true]
80-
* @param {Boolean} [options.bem=true]
80+
* @param {Boolean} [options.bem=false]
8181
* @constructor
8282
*/
8383
var HtmlDiff = function(options) {
@@ -116,7 +116,7 @@ HtmlDiff.prototype.tokenize = function(value) {
116116
* @param {String[]} [options.compareHtmlAttrsAsJSON]
117117
* @param {Boolean} [options.verbose]
118118
* @param {Boolean} [options.ignoreWhitespace=true]
119-
* @param {Boolean} [options.bem=true]
119+
* @param {Boolean} [options.bem=false]
120120
* @constructor
121121
*/
122122
var HtmlDiffer = function(options) {

0 commit comments

Comments
 (0)