Skip to content

Commit cfeca76

Browse files
committed
Refactor to improve bundle size
1 parent 6ff545c commit cfeca76

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

convert.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
module.exports = convert
44

55
function convert(test) {
6-
if (typeof test === 'string') {
7-
return tagNameFactory(test)
6+
if (test == null) {
7+
return element
88
}
99

10-
if (test === null || test === undefined) {
11-
return element
10+
if (typeof test === 'string') {
11+
return tagNameFactory(test)
1212
}
1313

1414
if (typeof test === 'object') {
15-
return any(test)
15+
return anyFactory(test)
1616
}
1717

1818
if (typeof test === 'function') {
@@ -22,28 +22,20 @@ function convert(test) {
2222
throw new Error('Expected function, string, or array as test')
2323
}
2424

25-
function convertAll(tests) {
26-
var length = tests.length
25+
function anyFactory(tests) {
2726
var index = -1
28-
var results = []
27+
var checks = []
2928

30-
while (++index < length) {
31-
results[index] = convert(tests[index])
29+
while (++index < tests.length) {
30+
checks[index] = convert(tests[index])
3231
}
3332

34-
return results
35-
}
36-
37-
function any(tests) {
38-
var checks = convertAll(tests)
39-
var length = checks.length
40-
41-
return matches
33+
return any
4234

43-
function matches() {
35+
function any() {
4436
var index = -1
4537

46-
while (++index < length) {
38+
while (++index < checks.length) {
4739
if (checks[index].apply(this, arguments)) {
4840
return true
4941
}

index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,24 @@ isElement.convert = convert
88

99
// Check if if `node` is an `element` and whether it passes the given test.
1010
function isElement(node, test, index, parent, context) {
11-
var hasParent = parent !== null && parent !== undefined
12-
var hasIndex = index !== null && index !== undefined
1311
var check = convert(test)
1412

1513
if (
16-
hasIndex &&
14+
index != null &&
1715
(typeof index !== 'number' || index < 0 || index === Infinity)
1816
) {
1917
throw new Error('Expected positive finite index for child node')
2018
}
2119

22-
if (hasParent && (!parent.type || !parent.children)) {
20+
if (parent != null && (!parent.type || !parent.children)) {
2321
throw new Error('Expected parent node')
2422
}
2523

2624
if (!node || !node.type || typeof node.type !== 'string') {
2725
return false
2826
}
2927

30-
if (hasParent !== hasIndex) {
28+
if ((parent == null) !== (index == null)) {
3129
throw new Error('Expected both parent and index')
3230
}
3331

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,22 @@
5858
"xo": {
5959
"prettier": true,
6060
"esnext": false,
61+
"ignores": [
62+
"hast-util-is-element.js"
63+
],
6164
"rules": {
65+
"eqeqeq": [
66+
"error",
67+
"always",
68+
{
69+
"null": "ignore"
70+
}
71+
],
6272
"max-params": "off",
63-
"unicorn/prefer-includes": "off",
73+
"no-eq-null": "off",
74+
"unicorn/prefer-type-error": "off",
6475
"unicorn/prefer-reflect-apply": "off"
65-
},
66-
"ignores": [
67-
"hast-util-is-element.js"
68-
]
76+
}
6977
},
7078
"nyc": {
7179
"check-coverage": true,

0 commit comments

Comments
 (0)