Skip to content

Commit b999c42

Browse files
committed
Refactor the code according to the Code Style
1 parent aca52fc commit b999c42

7 files changed

Lines changed: 73 additions & 85 deletions

File tree

lib/cli.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module.exports = require('coa').Cmd()
1212
.opt()
1313
.name('version')
1414
.title('Shows the version number')
15+
/*jshint -W024 */
1516
.short('v').long('version')
1617
.flag()
1718
.only()
18-
.act(function() {
19+
.act(function () {
1920
var p = require('../package.json');
2021
return p.name + ' ' + p.version;
2122
})
@@ -30,7 +31,8 @@ module.exports = require('coa').Cmd()
3031
.title('The number of characters around the diff (default: 40)')
3132
.long('chars-around-diff')
3233
.def(40)
33-
.val(function(val) {
34+
.val(function (val) {
35+
/*jshint es3:false */
3436
return parseInt(val);
3537
})
3638
.end()
@@ -44,21 +46,21 @@ module.exports = require('coa').Cmd()
4446
.title('Path to the 2-nd HTML file')
4547
.req()
4648
.end()
47-
.act(function(opts, args) {
49+
.act(function (opts, args) {
4850
return vow.all([
49-
vfs.read(path.resolve(args.path1), 'utf-8'),
50-
vfs.read(path.resolve(args.path2), 'utf-8'),
51-
opts.config ? vfs.read(path.resolve(opts.config)) : undefined
52-
]).spread(function (html1, html2, config) {
53-
config = config ? JSON.parse(config) : {};
51+
vfs.read(path.resolve(args.path1), 'utf-8'),
52+
vfs.read(path.resolve(args.path2), 'utf-8'),
53+
opts.config ? vfs.read(path.resolve(opts.config)) : undefined
54+
]).spread(function (html1, html2, config) {
55+
config = config ? JSON.parse(config) : {};
5456

55-
var options = utils.defaults(config),
56-
loggerOptions = {
57-
charsAroundDiff: opts.charsAroundDiff
58-
},
59-
htmlDiffer = new HtmlDiffer(options);
57+
var options = utils.defaults(config),
58+
loggerOptions = {
59+
charsAroundDiff: opts.charsAroundDiff
60+
},
61+
htmlDiffer = new HtmlDiffer(options);
6062

61-
diffLogger.log(htmlDiffer.diffHtml(html1, html2), loggerOptions);
62-
});
63+
diffLogger.log(htmlDiffer.diffHtml(html1, html2), loggerOptions);
64+
});
6365
})
6466
.run(process.argv.slice(2));

lib/diff-logger.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ function getDiffText(diff, options) {
2424
charsAroundDiff = 40;
2525
}
2626

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

29-
diff.forEach(function(part) {
29+
diff.forEach(function (part) {
3030
var index = diff.indexOf(part),
3131
partValue = part.value,
3232
color = 'grey';
3333

34-
if (part.added) color = 'green';
35-
if (part.removed) color = 'red';
34+
if (part.added) { color = 'green'; }
35+
if (part.removed) { color = 'red'; }
3636

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

lib/index.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ var utils = require('./utils'),
88
/**
99
* Converts the HTML document to the AST Tree
1010
* @param {String} HTMLDoc
11-
* @param {Object} options
1211
* @returns {AST}
1312
*/
1413
function htmlToAST(HTMLDoc) {
1514
var parser,
1615
parserHandler;
1716

18-
parserHandler = new htmlParser.DomHandler(function(err) {
19-
if (err) console.log(err);
17+
parserHandler = new htmlParser.DomHandler(function (err) {
18+
if (err) { console.log(err); }
2019
});
2120

2221
parser = new htmlParser.Parser(parserHandler);
@@ -34,9 +33,8 @@ function htmlToAST(HTMLDoc) {
3433
function modifyASTTree(tree, options) {
3534
var delComments = [];
3635

37-
_.forEach(tree, function(node) {
36+
_.forEach(tree, function (node) {
3837
if (options.ignoreWhitespaces && node.type === 'text') {
39-
4038
node.data = node.data
4139
.replace(/(\n|\r|\t|\v|\f)+/g, '')
4240
.replace(/\s+/g, ' ')
@@ -52,31 +50,30 @@ function modifyASTTree(tree, options) {
5250
}
5351

5452
if (node.hasOwnProperty('attribs')) {
55-
var attrs = utils.sortObj(node['attribs']);
53+
var attrs = utils.sortObj(node.attribs);
5654

5755
if (attrs.hasOwnProperty('class')) {
5856
attrs['class'] = utils.sortCssClasses(attrs['class']);
5957
}
6058

61-
_.forEach(options.compareHtmlAttrsAsJSON, function(attr) {
59+
_.forEach(options.compareHtmlAttrsAsJSON, function (attr) {
6260
var attrValue,
6361
isFunction = (attr === 'onclick' || attr === 'ondblclick'); // @FIXME: should be configurable
6462

6563
if (attrs.hasOwnProperty(attr)) {
66-
6764
attrValue = utils.parseAttr(attrs[attr].replace(/"/g, '"'), isFunction);
6865
attrValue = utils.sortObj(attrValue);
6966
attrValue = JSON.stringify(attrValue);
7067

71-
attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"')
68+
attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"');
7269
}
7370
});
7471

75-
_.forEach(options.ignoreHtmlAttrs, function(attr) {
72+
_.forEach(options.ignoreHtmlAttrs, function (attr) {
7673
attrs.hasOwnProperty(attr) && (attrs[attr] = '');
7774
});
7875

79-
node['attribs'] = attrs;
76+
node.attribs = attrs;
8077
}
8178

8279
if (node.hasOwnProperty('children')) {
@@ -93,19 +90,18 @@ function modifyASTTree(tree, options) {
9390

9491
/**
9592
*
96-
* @param [options]
93+
* @param {Object} [options]
9794
* @param {String[]} [options.ignoreHtmlAttrs]
9895
* @param {String[]} [options.compareHtmlAttrsAsJSON]
9996
* @param {Boolean} [options.verbose]
10097
* @param {Boolean} [options.ignoreWhitespaces=true]
10198
* @param {Boolean} [options.bem=false]
10299
* @constructor
103100
*/
104-
var HtmlDiff = function(options) {
101+
var HtmlDiff = function (options) {
105102
this.options = utils.defaults(options);
106-
};
107-
108-
var Diff = diff.Diff;
103+
},
104+
Diff = diff.Diff;
109105

110106
HtmlDiff.prototype = Diff.prototype;
111107

@@ -114,7 +110,7 @@ HtmlDiff.prototype = Diff.prototype;
114110
* @param {String} value
115111
* @returns {Array}
116112
*/
117-
HtmlDiff.prototype.tokenize = function(value) {
113+
HtmlDiff.prototype.tokenize = function (value) {
118114
var options = this.options,
119115
ASTTree = htmlToAST(value, options);
120116

@@ -132,18 +128,18 @@ HtmlDiff.prototype.tokenize = function(value) {
132128

133129
/**
134130
*
135-
* @param [options]
131+
* @param {Object} [options]
136132
* @param {String[]} [options.ignoreHtmlAttrs]
137133
* @param {String[]} [options.compareHtmlAttrsAsJSON]
138134
* @param {Boolean} [options.verbose]
139135
* @param {Boolean} [options.ignoreWhitespaces=true]
140136
* @param {Boolean} [options.bem=false]
141137
* @constructor
142138
*/
143-
var HtmlDiffer = function(options) {
139+
var HtmlDiffer = function (options) {
144140
options = utils.defaults(options);
145141

146-
if (options['bem']) {
142+
if (options.bem) {
147143
options.ignoreHtmlAttrs = ['id', 'for'];
148144
options.compareHtmlAttrsAsJSON = ['data-bem', 'onclick', 'ondblclick'];
149145
}
@@ -158,7 +154,7 @@ var HtmlDiffer = function(options) {
158154
* @param {Object} [options]
159155
* @returns {Diff}
160156
*/
161-
HtmlDiffer.prototype.diffHtml = function(html1, html2, options) {
157+
HtmlDiffer.prototype.diffHtml = function (html1, html2, options) {
162158
if (options) {
163159
console.warn('WARNING! The third param of \'diffHtml\' method is deprecated!'.bold.red);
164160
}
@@ -177,7 +173,7 @@ HtmlDiffer.prototype.diffHtml = function(html1, html2, options) {
177173
* @param {Object} [options]
178174
* @returns {Boolean}
179175
*/
180-
HtmlDiffer.prototype.isEqual = function(html1, html2, options) {
176+
HtmlDiffer.prototype.isEqual = function (html1, html2, options) {
181177
if (options) {
182178
console.warn('WARNING! The third param of \'isEqual\' method is deprecated!'.bold.red);
183179
}
@@ -211,7 +207,6 @@ function bemDiff(html1, html2) {
211207
logger.log(htmlDiffer.diff(html1, html2), loggerOptions);
212208
}
213209

214-
215210
var htmlDiffer = new HtmlDiffer();
216211

217212
module.exports = {

lib/utils.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,57 @@ require('colors');
77
* @param {Object} obj
88
* @returns {Object}
99
*/
10-
exports.sortObj = function sortObj(obj) {
10+
function sortObj(obj) {
1111
var keys = _.keys(obj).sort(),
1212
sortedObj = {};
1313

14-
_.forEach(keys, function(key) {
14+
_.forEach(keys, function (key) {
1515
var objValue = obj[key];
1616

17-
if(_.isPlainObject(objValue)) {
17+
if (_.isPlainObject(objValue)) {
1818
objValue = sortObj(objValue);
1919
}
2020

2121
sortedObj[key] = objValue;
2222
});
2323

2424
return sortedObj;
25-
};
25+
}
2626

2727
/**
2828
* Parses the given JSON in HTML attribute
2929
* @param {String} val
3030
* @param {Boolean} [isClick]
3131
* @returns {Object}
3232
*/
33-
exports.parseAttr = function(val, isClick) {
33+
function parseAttr(val, isClick) {
3434
if (isClick) {
35+
/*jshint evil: true */
3536
var fn = Function(val);
37+
3638
return fn ? fn() : {};
3739
}
3840

3941
return JSON.parse(val);
40-
};
42+
}
4143

4244
/**
4345
* Sorts the given CSS class attribute
4446
* @param {String} cssClasses
4547
* @returns {String}
4648
*/
47-
exports.sortCssClasses = function(cssClasses) {
49+
function sortCssClasses(cssClasses) {
4850
var classList = (cssClasses || '').split(' ');
4951

5052
return _.filter(classList).sort().join(' ');
51-
};
53+
}
5254

5355
/**
54-
*
55-
* @param options
56+
* Sets options
57+
* @param {Object} options
5658
* @returns {Object}
5759
*/
58-
exports.defaults = function(options) {
60+
function defaults(options) {
5961
if (options && options.hasOwnProperty('ignoreWhitespace')) {
6062
console.log('WARNING! Option \'ignoreWhitespace\' is deprecated, please, use \'ignoreWhitespaces\''.bold.red);
6163
options.ignoreWhitespaces = options.ignoreWhitespace;
@@ -72,4 +74,11 @@ exports.defaults = function(options) {
7274

7375
bem: false
7476
});
77+
}
78+
79+
module.exports = {
80+
sortObj: sortObj,
81+
parseAttr: parseAttr,
82+
sortCssClasses: sortCssClasses,
83+
defaults: defaults
7584
};

test/diffHtml.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
var fs = require('fs'),
2-
HtmlDiffer = require('../lib/index').HtmlDiffer;
3-
4-
function readFiles(f1, f2) {
5-
var files = {};
6-
7-
files.html1 = fs.readFileSync('test/fixtures/' + f1, 'utf-8');
8-
files.html2 = fs.readFileSync('test/fixtures/' + f2, 'utf-8');
9-
10-
return files;
11-
}
1+
var HtmlDiffer = require('../lib/index').HtmlDiffer;
122

133
describe('\'diffHtml\'', function () {
14-
154
it('must set options', function () {
165
var htmlDiffer = new HtmlDiffer({ ignoreHtmlAttrs: ['id', 'for'], ignoreWhitespaces: true }),
17-
files = readFiles('3.html', '_3.html'),
6+
first = '<label for="random">label for input</label><input id="random">',
7+
second = '<label for="sfsdfksdf">label for input</label><input id="sfsdfksdf">',
188
res = [ {
19-
value: '<html><head><title>Test</title></head><body><label for="">label for input</label><input id=""></body></html>',
9+
value: '<label for="">label for input</label><input id="">',
2010
added: undefined,
2111
removed: undefined
2212
} ];
2313

24-
htmlDiffer.diffHtml(files.html1, files.html2).must.be.eql(res);
14+
htmlDiffer.diffHtml(first, second).must.be.eql(res);
2515
});
26-
2716
});

0 commit comments

Comments
 (0)