Skip to content

Commit bdfa8ed

Browse files
committed
Refactor to improve bundle size
1 parent b8842e6 commit bdfa8ed

File tree

2 files changed

+130
-62
lines changed

2 files changed

+130
-62
lines changed

index.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,26 @@ module.exports = findAfter
66

77
function findAfter(parent, index, test) {
88
var is = convert(test)
9-
var children
10-
var child
11-
var length
129

1310
if (!parent || !parent.type || !parent.children) {
1411
throw new Error('Expected parent node')
1512
}
1613

17-
children = parent.children
18-
length = children.length
19-
20-
if (index && index.type) {
21-
index = children.indexOf(index)
22-
}
14+
if (typeof index === 'number') {
15+
if (index < 0 || index === Infinity) {
16+
throw new Error('Expected positive finite number as index')
17+
}
18+
} else {
19+
index = parent.children.indexOf(index)
2320

24-
if (isNaN(index) || index < 0 || index === Infinity) {
25-
throw new Error('Expected positive finite index or child node')
21+
if (index < 0) {
22+
throw new Error('Expected child node or index')
23+
}
2624
}
2725

28-
while (++index < length) {
29-
child = children[index]
30-
31-
if (is(child, index, parent)) {
32-
return child
26+
while (++index < parent.children.length) {
27+
if (is(parent.children[index], index, parent)) {
28+
return parent.children[index]
3329
}
3430
}
3531

test.js

+118-46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
var assert = require('assert')
43
var test = require('tape')
54
var remark = require('remark')
65
var findAfter = require('.')
@@ -26,73 +25,146 @@ test('unist-util-find-after', function (t) {
2625
'should fail without parent node'
2726
)
2827

29-
t.doesNotThrow(function () {
30-
assert.throws(function () {
28+
t.throws(
29+
function () {
3130
findAfter({type: 'foo', children: []})
32-
}, /Expected positive finite index or child node/)
31+
},
32+
/Expected child node or index/,
33+
'should fail without index (#1)'
34+
)
3335

34-
assert.throws(function () {
36+
t.throws(
37+
function () {
3538
findAfter({type: 'foo', children: []}, -1)
36-
}, /Expected positive finite index or child node/)
39+
},
40+
/Expected positive finite number as index/,
41+
'should fail without index (#2)'
42+
)
3743

38-
assert.throws(function () {
44+
t.throws(
45+
function () {
3946
findAfter({type: 'foo', children: []}, {type: 'bar'})
40-
}, /Expected positive finite index or child node/)
41-
}, 'should fail without index')
47+
},
48+
/Expected child node or index/,
49+
'should fail without index (#3)'
50+
)
4251

43-
t.doesNotThrow(function () {
44-
assert.throws(function () {
52+
t.throws(
53+
function () {
4554
findAfter(
4655
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
4756
0,
4857
false
4958
)
50-
}, /Expected function, string, or object as test/)
59+
},
60+
/Expected function, string, or object as test/,
61+
'should fail for invalid `test` (#1)'
62+
)
5163

52-
assert.throws(function () {
64+
t.throws(
65+
function () {
5366
findAfter(
5467
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
5568
0,
5669
true
5770
)
58-
}, /Expected function, string, or object as test/)
59-
}, 'should fail for invalid `test`')
71+
},
72+
/Expected function, string, or object as test/,
73+
'should fail for invalid `test` (#2)'
74+
)
6075

61-
t.doesNotThrow(function () {
62-
assert.strictEqual(findAfter(paragraph, children[1]), children[2])
63-
assert.strictEqual(findAfter(paragraph, 1), children[2])
64-
assert.strictEqual(findAfter(paragraph, 7), null)
65-
}, 'should return the following node when without `test`')
76+
t.strictEqual(
77+
findAfter(paragraph, children[1]),
78+
children[2],
79+
'should return the following node when without `test` (#1)'
80+
)
81+
t.strictEqual(
82+
findAfter(paragraph, 1),
83+
children[2],
84+
'should return the following node when without `test` (#1)'
85+
)
86+
t.strictEqual(
87+
findAfter(paragraph, 7),
88+
null,
89+
'should return the following node when without `test` (#1)'
90+
)
6691

67-
t.doesNotThrow(function () {
68-
assert.strictEqual(findAfter(paragraph, 0, children[6]), children[6])
69-
assert.strictEqual(
70-
findAfter(paragraph, children[0], children[1]),
71-
children[1]
72-
)
73-
assert.strictEqual(findAfter(paragraph, 0, children[1]), children[1])
74-
assert.strictEqual(findAfter(paragraph, children[0], children[0]), null)
75-
assert.strictEqual(findAfter(paragraph, 0, children[0]), null)
76-
assert.strictEqual(findAfter(paragraph, 1, children[1]), null)
77-
}, 'should return `node` when given a `node` and existing')
92+
t.strictEqual(
93+
findAfter(paragraph, 0, children[6]),
94+
children[6],
95+
'should return `node` when given a `node` and existing (#1)'
96+
)
97+
t.strictEqual(
98+
findAfter(paragraph, children[0], children[1]),
99+
children[1],
100+
'should return `node` when given a `node` and existing (#2)'
101+
)
102+
t.strictEqual(
103+
findAfter(paragraph, 0, children[1]),
104+
children[1],
105+
'should return `node` when given a `node` and existing (#3)'
106+
)
107+
t.strictEqual(
108+
findAfter(paragraph, children[0], children[0]),
109+
null,
110+
'should return `node` when given a `node` and existing (#4)'
111+
)
112+
t.strictEqual(
113+
findAfter(paragraph, 0, children[0]),
114+
null,
115+
'should return `node` when given a `node` and existing (#5)'
116+
)
117+
t.strictEqual(
118+
findAfter(paragraph, 1, children[1]),
119+
null,
120+
'should return `node` when given a `node` and existing (#6)'
121+
)
78122

79-
t.doesNotThrow(function () {
80-
assert.strictEqual(findAfter(paragraph, 0, 'strong'), children[3])
81-
assert.strictEqual(findAfter(paragraph, 3, 'strong'), null)
82-
assert.strictEqual(findAfter(paragraph, children[0], 'strong'), children[3])
83-
assert.strictEqual(findAfter(paragraph, children[3], 'strong'), null)
84-
}, 'should return a child when given a `type` and existing')
123+
t.strictEqual(
124+
findAfter(paragraph, 0, 'strong'),
125+
children[3],
126+
'should return a child when given a `type` and existing (#1)'
127+
)
128+
t.strictEqual(
129+
findAfter(paragraph, 3, 'strong'),
130+
null,
131+
'should return a child when given a `type` and existing (#2)'
132+
)
133+
t.strictEqual(
134+
findAfter(paragraph, children[0], 'strong'),
135+
children[3],
136+
'should return a child when given a `type` and existing (#3)'
137+
)
138+
t.strictEqual(
139+
findAfter(paragraph, children[3], 'strong'),
140+
null,
141+
'should return a child when given a `type` and existing (#4)'
142+
)
85143

86-
t.doesNotThrow(function () {
87-
assert.strictEqual(findAfter(paragraph, 0, test), children[5])
88-
assert.strictEqual(findAfter(paragraph, 5, test), null)
89-
assert.strictEqual(findAfter(paragraph, children[4], test), children[5])
90-
assert.strictEqual(findAfter(paragraph, children[6], test), null)
144+
t.strictEqual(
145+
findAfter(paragraph, 0, test),
146+
children[5],
147+
'should return a child when given a `test` and existing (#1)'
148+
)
149+
t.strictEqual(
150+
findAfter(paragraph, 5, test),
151+
null,
152+
'should return a child when given a `test` and existing (#2)'
153+
)
154+
t.strictEqual(
155+
findAfter(paragraph, children[4], test),
156+
children[5],
157+
'should return a child when given a `test` and existing (#3)'
158+
)
159+
t.strictEqual(
160+
findAfter(paragraph, children[6], test),
161+
null,
162+
'should return a child when given a `test` and existing (#4)'
163+
)
91164

92-
function test(node, n) {
93-
return n === 5
94-
}
95-
}, 'should return a child when given a `test` and existing')
165+
function test(node, n) {
166+
return n === 5
167+
}
96168

97169
t.end()
98170
})

0 commit comments

Comments
 (0)