Skip to content

Commit b19244c

Browse files
author
Alexej Yaroshevich
committed
initial presets functionality
Fixes #118
1 parent f07ac24 commit b19244c

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

lib/HtmlDiff.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var diff = require('diff'),
55
* @class HtmlDiff
66
* @constructor
77
* @augments Diff
8-
* @param {Object} [options]
8+
* @param {Object|String} [options] options or preset name
9+
* @param {String} [options.preset]
910
* @param {String[]} [options.ignoreAttributes]
1011
* @param {String[]} [options.compareAttributesAsJSON]
1112
* @param {Boolean} [options.ignoreWhitespaces=true]

lib/utils/defaults.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
var _ = require('lodash');
22

3+
var presets = {
4+
bem: require('../../presets/bem.json')
5+
};
6+
37
/**
48
* Sets options
5-
* @param {Object} [options]
9+
* @param {Object|String} [options] options or preset name
10+
* @param {String} [options.preset]
611
* @param {String[]} [options.ignoreAttributes]
712
* @param {String[]} [options.compareAttributesAsJSON]
813
* @param {Boolean} [options.ignoreWhitespaces=true]
914
* @param {Boolean} [options.ignoreComments=true]
10-
* @param {Boolean} [options.ignoreClosingTags=false]
15+
* @param {Boolean} [options.ignoreEndTags=false]
1116
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
1217
* returns {Object}
1318
*/
1419
module.exports = function (options) {
1520
if (typeof options === 'string') {
16-
if (options === 'bem') {
17-
options = {
18-
// ignore generated attributes
19-
ignoreAttributes: ['id', 'for', 'aria-labelledby', 'aria-describedby'],
20-
compareAttributesAsJSON: [
21-
'data-bem',
22-
{ name: 'onclick', isFunction: true },
23-
{ name: 'ondblclick', isFunction: true }
24-
]
25-
};
26-
} else {
27-
console.error(options.bold.red + ' is an invalid preset name. Use ' + 'bem'.bold.green + ' instead.');
28-
process.exit(1);
21+
options = { preset: options };
22+
}
23+
24+
if (options && options.preset) {
25+
var preset = String(options.preset);
26+
if (!presets.hasOwnProperty(preset)) {
27+
throw Error(preset + ' is an invalid preset name.');
2928
}
29+
30+
delete options.preset;
31+
options = _.defaults(options, presets[preset]);
3032
}
3133

3234
return _.defaults(options || {}, {

presets/bem.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ignoreAttributes": ["id", "for", "aria-labelledby", "aria-describedby"],
3+
"compareAttributesAsJSON": [
4+
"data-bem",
5+
{ "name": "onclick", "isFunction": true },
6+
{ "name": "ondblclick", "isFunction": true }
7+
]
8+
}

0 commit comments

Comments
 (0)