Skip to content

Commit a7d7411

Browse files
committed
Refactor the code
1 parent e452b5c commit a7d7411

9 files changed

Lines changed: 70 additions & 72 deletions

File tree

lib/HtmlDiff.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
var diff = require('diff'),
2-
defaults = require('./utils/defaults'),
2+
defaults = require('./utils/defaults');
33

4-
/**
5-
* @class HtmlDiff
6-
* @constructor
7-
* @augments Diff
8-
* @param {Object} [options]
9-
* @param {String[]} [options.ignoreAttributes]
10-
* @param {String[]} [options.compareAttributesAsJSON]
11-
* @param {Boolean} [options.ignoreWhitespaces=true]
12-
* @param {Boolean} [options.ignoreComments]
13-
* @param {Boolean} [options.ignoreEndTags=false]
14-
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
15-
*/
16-
HtmlDiff = function (options) {
17-
this.options = defaults(options);
18-
},
19-
/**
20-
* @class Diff
21-
* @constructor
22-
*/
23-
Diff = diff.Diff;
4+
/**
5+
* @class HtmlDiff
6+
* @constructor
7+
* @augments Diff
8+
* @param {Object} [options]
9+
* @param {String[]} [options.ignoreAttributes]
10+
* @param {String[]} [options.compareAttributesAsJSON]
11+
* @param {Boolean} [options.ignoreWhitespaces=true]
12+
* @param {Boolean} [options.ignoreComments]
13+
* @param {Boolean} [options.ignoreEndTags=false]
14+
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
15+
*/
16+
var HtmlDiff = function (options) {
17+
this.options = defaults(options);
18+
};
19+
20+
/**
21+
* @class Diff
22+
* @constructor
23+
*/
24+
var Diff = diff.Diff;
2425

2526
HtmlDiff.prototype = Diff.prototype;
2627

lib/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ HtmlDiff.prototype.tokenize = function (html) {
1717
/**
1818
* @class HtmlDiffer
1919
* @constructor
20-
* @param {Object} [options]
20+
* @param {Object} [options]
2121
* @param {String[]} [options.ignoreAttributes]
2222
* @param {String[]} [options.compareAttributesAsJSON]
23-
* @param {Boolean} [options.ignoreWhitespaces=true]
24-
* @param {Boolean} [options.ignoreComments=true]
25-
* @param {Boolean} [options.ignoreEndTags=false]
26-
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
23+
* @param {Boolean} [options.ignoreWhitespaces=true]
24+
* @param {Boolean} [options.ignoreComments=true]
25+
* @param {Boolean} [options.ignoreEndTags=false]
26+
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
2727
*/
2828
var HtmlDiffer = function (options) {
2929
options = defaults(options);
@@ -54,8 +54,7 @@ HtmlDiffer.prototype.diffHtml = function (html1, html2) {
5454
* @returns {Boolean}
5555
*/
5656
HtmlDiffer.prototype.isEqual = function (html1, html2) {
57-
var htmlDiffer = new HtmlDiff(this.options),
58-
diff = htmlDiffer.diff(html1, html2);
57+
var diff = this.diffHtml(html1, html2);
5958

6059
return (diff.length === 1 && !diff[0].added && !diff[0].removed);
6160
};

lib/logger.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
require('colors');
22

3+
/**
4+
* @typedef {Object} Diff
5+
* @property {String} value
6+
* @property {String|undefined} added
7+
* @property {String|undefined} removed
8+
*/
9+
310
/**
411
* Returns readable diff text
5-
* @param {Object[]} diff
12+
* @param {Diff[]} diff
613
* @param {Object} [options]
714
* @param {Number} [options.charsAroundDiff=40]
815
* @returns {String}
@@ -19,15 +26,15 @@ function getDiffText(diff, options) {
1926
charsAroundDiff = 40;
2027
}
2128

22-
if (diff.length === 1 && !diff[0].added && !diff[0].removed) { return output; }
29+
if (diff.length === 1 && !diff[0].added && !diff[0].removed) return output;
2330

2431
diff.forEach(function (part) {
2532
var index = diff.indexOf(part),
2633
partValue = part.value,
2734
color = 'grey';
2835

29-
if (part.added) { color = 'green'; }
30-
if (part.removed) { color = 'red'; }
36+
if (part.added) color = 'green';
37+
if (part.removed) color = 'red';
3138

3239
if (color !== 'grey') {
3340
output += (!index ? '\n' : '') + partValue.inverse[color];

lib/utils/defaults.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ var _ = require('lodash');
22

33
/**
44
* Sets options
5-
* @param {Object} [options]
5+
* @param {Object} [options]
66
* @param {String[]} [options.ignoreAttributes]
77
* @param {String[]} [options.compareAttributesAsJSON]
8-
* @param {Boolean} [options.ignoreWhitespaces=true]
9-
* @param {Boolean} [options.ignoreComments=true]
10-
* @param {Boolean} [options.ignoreClosingTags=false]
11-
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
8+
* @param {Boolean} [options.ignoreWhitespaces=true]
9+
* @param {Boolean} [options.ignoreComments=true]
10+
* @param {Boolean} [options.ignoreClosingTags=false]
11+
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
1212
* returns {Object}
1313
*/
1414
module.exports = function (options) {

lib/utils/modify.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var SimpleApiParser = require('parse5').SimpleApiParser,
55
/**
66
* Parses the given HTML and modifies it according to the given options
77
* @param {String} value
8-
* @param {Object} [options]
8+
* @param {Object} [options]
99
* @param {String[]} [options.ignoreAttributes]
1010
* @param {String[]} [options.compareAttributesAsJSON]
11-
* @param {Boolean} [options.ignoreWhitespaces=true]
12-
* @param {Boolean} [options.ignoreComments=true]
13-
* @param {Boolean} [options.ignoreEndTags=false]
14-
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
11+
* @param {Boolean} [options.ignoreWhitespaces=true]
12+
* @param {Boolean} [options.ignoreComments=true]
13+
* @param {Boolean} [options.ignoreEndTags=false]
14+
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
1515
* returns {String}
1616
*/
1717
function modify(value, options) {

lib/utils/serialize.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
* @returns {String}
1111
*/
1212
doctype: function (name, publicId, systemId) {
13-
if (!name) { return '<!DOCTYPE>'; }
13+
if (!name) return '<!DOCTYPE>';
1414

1515
publicId = publicId || '',
1616
systemId = systemId || '';
@@ -46,24 +46,31 @@ module.exports = {
4646
},
4747
/**
4848
* @param {String} tagName
49+
* @returns {String}
4950
*/
5051
endTag: function (tagName) {
5152
return '</' + tagName + '>';
5253
},
5354
/**
5455
* @param {String} text
56+
* @returns {String}
5557
*/
5658
text: function (text) {
5759
return text;
5860
},
5961
/**
6062
* @param {String} text
63+
* @returns {String}
6164
*/
6265
comment: function (text) {
6366
return '<!--' + text + '-->';
6467
}
6568
};
6669

70+
/**
71+
* @param {String} str
72+
* @returns {String}
73+
*/
6774
function escape(str) {
6875
return String(str)
6976
.replace(/&/g, '&amp;')

lib/utils/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function _getIndexesInArray(attrs, attr) {
1212
var res = [];
1313

1414
for (var i = 0; i < attrs.length; i++) {
15-
if (attrs[i].name === attr) { res.push(i); }
15+
if (attrs[i].name === attr) res.push(i);
1616
}
1717

1818
return res;

test/diff/diffHtml.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/diff/getDiffText.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ require('colors');
44

55
describe('\'getDiffText\'', function () {
66
it('must return an empty string', function () {
7-
var inp = [ {
7+
var inp = [{
88
value: 'text',
99
added: undefined,
1010
removed: undefined
11-
} ];
11+
}];
1212

1313
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.equal('');
1414
});
1515

1616
it('must return a diff string', function () {
17-
var inp = [ {
17+
var inp = [{
1818
value: 'texttexttexttexttexttexttexttexttexttexttext',
1919
added: undefined,
2020
removed: undefined
@@ -34,41 +34,41 @@ describe('\'getDiffText\'', function () {
3434
value: 'texttexttexttext',
3535
added: undefined,
3636
removed: undefined
37-
} ],
37+
}],
3838
out = '\n...\n' + 'texttexttexttexttext'.grey + '!'.inverse.green + 'Text'.grey + '!'.inverse.green +
3939
'texttexttexttext'.grey;
4040

4141
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.eql(out);
4242
});
4343

4444
it('must consider negative value of \'charsAroundDiff\' option', function () {
45-
var inp = [ {
45+
var inp = [{
4646
value: 'text',
4747
added: undefined,
4848
removed: undefined
49-
} ];
49+
}];
5050

5151
diffLoger.getDiffText(inp, { charsAroundDiff: -5 }).must.be.equal('');
5252
});
5353

5454
it('must return a diff when there is nothing else in the input', function () {
55-
var inp = [ {
55+
var inp = [{
5656
value: 'texttexttexttexttexttexttexttexttexttexttext',
5757
added: true,
5858
removed: undefined
5959
}, {
6060
value: 'ololoololoololoololoololoololoololoololoolol',
6161
added: false,
6262
removed: true
63-
} ],
63+
}],
6464
out = '\n' + 'texttexttexttexttexttexttexttexttexttexttext'.inverse.green +
6565
'ololoololoololoololoololoololoololoololoolol'.inverse.red;
6666

6767
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.eql(out);
6868
});
6969

7070
it('must return a diff on the beginning of the input', function () {
71-
var inp = [ {
71+
var inp = [{
7272
value: 'texttexttext',
7373
added: undefined,
7474
removed: undefined
@@ -80,14 +80,14 @@ describe('\'getDiffText\'', function () {
8080
value: 'texttexttexttext',
8181
added: undefined,
8282
removed: undefined
83-
} ],
83+
}],
8484
out = '\n' + 'texttexttext'.grey + 'text'.inverse.red + 'texttexttexttext'.grey;
8585

8686
diffLoger.getDiffText(inp).must.be.eql(out);
8787
});
8888

8989
it('must return several diffs', function () {
90-
var inp = [ {
90+
var inp = [{
9191
value: 'texttexttexttexttexttexttexttexttexttexttext',
9292
added: undefined,
9393
removed: undefined
@@ -115,7 +115,7 @@ describe('\'getDiffText\'', function () {
115115
value: 'texttexttexttexttext',
116116
added: undefined,
117117
removed: undefined
118-
} ],
118+
}],
119119
out = '\n...\n' + 'texttexttexttexttext'.grey + 'text'.inverse.red +
120120
'texttexttexttexttext'.grey + '\n...\n' + 'texttexttexttexttext'.grey +
121121
'!'.inverse.green + 'text'.grey + '!'.inverse.green + 'texttexttexttexttext'.grey;

0 commit comments

Comments
 (0)