Skip to content

Commit e9855f0

Browse files
committed
Refactor to improve bundle size
1 parent 2da9400 commit e9855f0

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

convert.js

Lines changed: 19 additions & 31 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 typeFactory(test)
6+
if (test == null) {
7+
return ok
88
}
99

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

1414
if (typeof test === 'object') {
15-
return ('length' in test ? anyFactory : matchesFactory)(test)
15+
return 'length' in test ? anyFactory(test) : allFactory(test)
1616
}
1717

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

25-
function convertAll(tests) {
26-
var results = []
27-
var length = tests.length
28-
var index = -1
29-
30-
while (++index < length) {
31-
results[index] = convert(tests[index])
32-
}
33-
34-
return results
35-
}
36-
3725
// Utility assert each property in `test` is represented in `node`, and each
3826
// values are strictly equal.
39-
function matchesFactory(test) {
40-
return matches
27+
function allFactory(test) {
28+
return all
4129

42-
function matches(node) {
30+
function all(node) {
4331
var key
4432

4533
for (key in test) {
46-
if (node[key] !== test[key]) {
47-
return false
48-
}
34+
if (node[key] !== test[key]) return
4935
}
5036

5137
return true
5238
}
5339
}
5440

5541
function anyFactory(tests) {
56-
var checks = convertAll(tests)
57-
var length = checks.length
42+
var checks = []
43+
var index = -1
44+
45+
while (++index < tests.length) {
46+
checks[index] = convert(tests[index])
47+
}
5848

59-
return matches
49+
return any
6050

61-
function matches() {
51+
function any() {
6252
var index = -1
6353

64-
while (++index < length) {
54+
while (++index < checks.length) {
6555
if (checks[index].apply(this, arguments)) {
6656
return true
6757
}
6858
}
69-
70-
return false
7159
}
7260
}
7361

@@ -77,7 +65,7 @@ function typeFactory(test) {
7765
return type
7866

7967
function type(node) {
80-
return Boolean(node && node.type === test)
68+
return node && node.type === test
8169
}
8270
}
8371

index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,25 @@ is.convert = convert
88

99
// Assert if `test` passes for `node`.
1010
// When a `parent` node is known the `index` of node should also be given.
11-
// eslint-disable-next-line max-params
1211
function is(node, test, index, parent, context) {
13-
var hasParent = parent !== null && parent !== undefined
14-
var hasIndex = index !== null && index !== undefined
1512
var check = convert(test)
1613

1714
if (
18-
hasIndex &&
15+
index != null &&
1916
(typeof index !== 'number' || index < 0 || index === Infinity)
2017
) {
21-
throw new Error('Expected positive finite index or child node')
18+
throw new Error('Expected positive finite index')
2219
}
2320

24-
if (hasParent && (!is(parent) || !parent.children)) {
21+
if (parent != null && (!is(parent) || !parent.children)) {
2522
throw new Error('Expected parent node')
2623
}
2724

28-
if (!node || !node.type || typeof node.type !== 'string') {
29-
return false
30-
}
31-
32-
if (hasParent !== hasIndex) {
25+
if ((parent == null) !== (index == null)) {
3326
throw new Error('Expected both parent and index')
3427
}
3528

36-
return Boolean(check.call(context, node, index, parent))
29+
return node && node.type && typeof node.type === 'string'
30+
? Boolean(check.call(context, node, index, parent))
31+
: false
3732
}

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@
7070
"prettier": true,
7171
"esnext": false,
7272
"rules": {
73+
"eqeqeq": [
74+
"error",
75+
"always",
76+
{
77+
"null": "ignore"
78+
}
79+
],
80+
"max-params": "off",
81+
"no-eq-null": "off",
7382
"unicorn/prefer-type-error": "off",
7483
"unicorn/prefer-reflect-apply": "off"
7584
},

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ test('unist-util-is', function (t) {
1919
function () {
2020
is(node, null, -1, parent)
2121
},
22-
/Expected positive finite index or child node/,
22+
/Expected positive finite index/,
2323
'should throw when `index` is invalid (#1)'
2424
)
2525

2626
t.throws(
2727
function () {
2828
is(node, null, Infinity, parent)
2929
},
30-
/Expected positive finite index or child node/,
30+
/Expected positive finite index/,
3131
'should throw when `index` is invalid (#2)'
3232
)
3333

3434
t.throws(
3535
function () {
3636
is(node, null, false, parent)
3737
},
38-
/Expected positive finite index or child node/,
38+
/Expected positive finite index/,
3939
'should throw when `index` is invalid (#3)'
4040
)
4141

0 commit comments

Comments
 (0)