Skip to content

Commit a3da580

Browse files
committed
Refactor code-style
1 parent 8076f0c commit a3da580

File tree

4 files changed

+146
-202
lines changed

4 files changed

+146
-202
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,remarkrc,eslintrc,sh}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,45 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2016 Titus Wormer
4-
* @license MIT
5-
* @module rehype:lint:util:is-element
6-
*/
7-
81
'use strict';
92

10-
/* eslint-env commonjs */
3+
/* Expose. */
4+
module.exports = isElement;
115

12-
/**
13-
* Check if a node is a (certain) element.
14-
*
15-
* @param {*} node - Thing to check.
16-
* @param {string|Array.<string>?} [tagNames] - Name of element.
17-
* @return {boolean} - Whether a node is a (certain) element.
18-
* @throws {Error} - When `tagNames` is given but invalid.
19-
*/
6+
/* Check if, whether `tagNames` is given, a node is an element
7+
* or an element matching `tagNames`. */
208
function isElement(node, tagNames) {
21-
var name;
22-
23-
if (
24-
!(
25-
tagNames === null ||
26-
tagNames === undefined ||
27-
typeof tagNames === 'string' ||
28-
(typeof tagNames === 'object' && tagNames.length)
29-
)
30-
) {
31-
throw new Error(
32-
'Expected `string` or `Array.<string>` for ' +
33-
'`tagNames`, not `' + tagNames + '`'
34-
);
35-
}
36-
37-
if (
38-
!node ||
39-
typeof node !== 'object' ||
40-
node.type !== 'element' ||
41-
typeof node.tagName !== 'string'
42-
) {
43-
return false;
44-
}
45-
46-
if (tagNames === null || tagNames === undefined) {
47-
return true;
48-
}
49-
50-
name = node.tagName;
51-
52-
if (typeof tagNames === 'string') {
53-
return name === tagNames;
54-
}
55-
56-
return tagNames.indexOf(name) !== -1;
9+
var name;
10+
11+
if (
12+
!(
13+
tagNames === null ||
14+
tagNames === undefined ||
15+
typeof tagNames === 'string' ||
16+
(typeof tagNames === 'object' && tagNames.length !== 0)
17+
)
18+
) {
19+
throw new Error(
20+
'Expected `string` or `Array.<string>` for ' +
21+
'`tagNames`, not `' + tagNames + '`'
22+
);
23+
}
24+
25+
if (
26+
!node ||
27+
typeof node !== 'object' ||
28+
node.type !== 'element' ||
29+
typeof node.tagName !== 'string'
30+
) {
31+
return false;
32+
}
33+
34+
if (tagNames === null || tagNames === undefined) {
35+
return true;
36+
}
37+
38+
name = node.tagName;
39+
40+
if (typeof tagNames === 'string') {
41+
return name === tagNames;
42+
}
43+
44+
return tagNames.indexOf(name) !== -1;
5745
}
58-
59-
/*
60-
* Expose.
61-
*/
62-
63-
module.exports = isElement;

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,31 @@
2626
],
2727
"devDependencies": {
2828
"browserify": "^13.0.0",
29-
"eslint": "^2.0.0",
3029
"esmangle": "^1.0.1",
3130
"istanbul": "^0.4.0",
32-
"jscs": "^3.0.0",
33-
"jscs-jsdoc": "^2.0.0",
3431
"remark": "^4.0.0",
3532
"remark-comment-config": "^3.0.0",
3633
"remark-github": "^4.0.1",
3734
"remark-lint": "^3.0.0",
3835
"remark-usage": "^3.0.0",
3936
"remark-validate-links": "^3.0.0",
40-
"tape": "^4.4.0"
37+
"tape": "^4.4.0",
38+
"xo": "^0.17.0"
4139
},
4240
"scripts": {
4341
"build-md": "remark . --quiet --frail",
4442
"build-bundle": "browserify index.js --bare -s hastUtilIsElement > hast-util-is-element.js",
4543
"build-mangle": "esmangle hast-util-is-element.js > hast-util-is-element.min.js",
4644
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
47-
"lint-api": "eslint .",
48-
"lint-style": "jscs --reporter inline .",
49-
"lint": "npm run lint-api && npm run lint-style",
45+
"lint": "xo",
5046
"test-api": "node test.js",
5147
"test-coverage": "istanbul cover test.js",
5248
"test": "npm run build && npm run lint && npm run test-coverage"
49+
},
50+
"xo": {
51+
"space": true,
52+
"ignores": [
53+
"hast-util-is-element.js"
54+
]
5355
}
5456
}

test.js

Lines changed: 96 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,105 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2016 Titus Wormer
4-
* @license MIT
5-
* @module hast:util:is-element
6-
* @fileoverview Test suite for `is-element`.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env node */
12-
13-
/*
14-
* Module dependencies.
15-
*/
16-
3+
/* Dependencies. */
174
var test = require('tape');
185
var isElement = require('./index.js');
196

20-
/*
21-
* Tests.
22-
*/
23-
7+
/* Tests. */
248
test('isElement', function (t) {
25-
t.equal(isElement(), false, 'should return `false` without node');
26-
t.equal(isElement(null), false, 'should return `false` with `null`');
27-
28-
t.throws(
29-
function () {
30-
isElement(null, true)
31-
},
32-
'Expected `string` or `Array.<string>` for `tagNames`, not `true`',
33-
'should throw when the second parameter is invalid'
9+
t.equal(isElement(), false, 'should return `false` without node');
10+
t.equal(isElement(null), false, 'should return `false` with `null`');
11+
12+
t.throws(
13+
function () {
14+
isElement(null, true);
15+
},
16+
'Expected `string` or `Array.<string>` for `tagNames`, not `true`',
17+
'should throw when the second parameter is invalid'
18+
);
19+
20+
t.test('isElement(node)', function (st) {
21+
st.equal(
22+
isElement({type: 'text'}),
23+
false,
24+
'should return `false` when without `element`'
25+
);
26+
27+
st.equal(
28+
isElement({type: 'element'}),
29+
false,
30+
'should return `false` when with invalid `element`'
31+
);
32+
33+
st.equal(
34+
isElement({type: 'element', tagName: 'div'}),
35+
true,
36+
'should return `true` when with valid `element`'
37+
);
38+
39+
st.end();
40+
});
41+
42+
t.test('isElement(node, tagName)', function (st) {
43+
st.equal(
44+
isElement({type: 'text'}, 'div'),
45+
false,
46+
'should return `false` when without `element`'
47+
);
48+
49+
st.equal(
50+
isElement({type: 'element'}, 'div'),
51+
false,
52+
'should return `false` when with invalid `element`'
53+
);
54+
55+
st.equal(
56+
isElement({type: 'element', tagName: 'strong'}, 'div'),
57+
false,
58+
'should return `false` when without matching `element`'
3459
);
3560

36-
t.test('isElement(node)', function (st) {
37-
st.equal(isElement({
38-
'type': 'text'
39-
}), false, 'should return `false` when without `element`');
40-
41-
st.equal(isElement({
42-
'type': 'element'
43-
}), false, 'should return `false` when with invalid `element`');
44-
45-
st.equal(isElement({
46-
'type': 'element',
47-
'tagName': 'div'
48-
}), true, 'should return `true` when with valid `element`');
49-
50-
st.end();
51-
});
52-
53-
t.test('isElement(node, tagName)', function (st) {
54-
st.equal(
55-
isElement({
56-
'type': 'text'
57-
}, 'div'),
58-
false,
59-
'should return `false` when without `element`'
60-
);
61-
62-
st.equal(
63-
isElement({
64-
'type': 'element'
65-
}, 'div'),
66-
false,
67-
'should return `false` when with invalid `element`'
68-
);
69-
70-
st.equal(
71-
isElement({
72-
'type': 'element',
73-
'tagName': 'strong'
74-
}, 'div'),
75-
false,
76-
'should return `false` when without matching `element`'
77-
);
78-
79-
st.equal(
80-
isElement({
81-
'type': 'element',
82-
'tagName': 'div'
83-
}, 'div'),
84-
true,
85-
'should return `true` when with matching `element`'
86-
);
87-
88-
st.end();
89-
});
90-
91-
t.test('isElement(node, tagNames)', function (st) {
92-
st.equal(
93-
isElement({
94-
'type': 'text'
95-
}, ['div']),
96-
false,
97-
'should return `false` when without `element`'
98-
);
99-
100-
st.equal(
101-
isElement({
102-
'type': 'element'
103-
}, ['div']),
104-
false,
105-
'should return `false` when with invalid `element`'
106-
);
107-
108-
st.equal(
109-
isElement({
110-
'type': 'element',
111-
'tagName': 'strong'
112-
}, ['div']),
113-
false,
114-
'should return `false` when without matching `element`'
115-
);
116-
117-
st.equal(
118-
isElement({
119-
'type': 'element',
120-
'tagName': 'div'
121-
}, ['div', 'strong', 'em']),
122-
true,
123-
'should return `true` when with matching `element` (#1)'
124-
);
125-
126-
st.equal(
127-
isElement({
128-
'type': 'element',
129-
'tagName': 'em'
130-
}, ['div', 'strong', 'em']),
131-
true,
132-
'should return `true` when with matching `element` (#2)'
133-
);
134-
135-
st.end();
136-
});
137-
138-
t.end();
61+
st.equal(
62+
isElement({type: 'element', tagName: 'div'}, 'div'),
63+
true,
64+
'should return `true` when with matching `element`'
65+
);
66+
67+
st.end();
68+
});
69+
70+
t.test('isElement(node, tagNames)', function (st) {
71+
st.equal(
72+
isElement({type: 'text'}, ['div']),
73+
false,
74+
'should return `false` when without `element`'
75+
);
76+
77+
st.equal(
78+
isElement({type: 'element'}, ['div']),
79+
false,
80+
'should return `false` when with invalid `element`'
81+
);
82+
83+
st.equal(
84+
isElement({type: 'element', tagName: 'strong'}, ['div']),
85+
false,
86+
'should return `false` when without matching `element`'
87+
);
88+
89+
st.equal(
90+
isElement({type: 'element', tagName: 'div'}, ['div', 'strong', 'em']),
91+
true,
92+
'should return `true` when with matching `element` (#1)'
93+
);
94+
95+
st.equal(
96+
isElement({type: 'element', tagName: 'em'}, ['div', 'strong', 'em']),
97+
true,
98+
'should return `true` when with matching `element` (#2)'
99+
);
100+
101+
st.end();
102+
});
103+
104+
t.end();
139105
});

0 commit comments

Comments
 (0)