Skip to content

Commit 3aa02e3

Browse files
committed
Refactor to improve bundle size
1 parent fbe9ee0 commit 3aa02e3

File tree

3 files changed

+164
-101
lines changed

3 files changed

+164
-101
lines changed

index.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,26 @@ module.exports = findAllAfter
77
function findAllAfter(parent, index, test) {
88
var is = convert(test)
99
var results = []
10-
var children
11-
var child
12-
var length
1310

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

18-
children = parent.children
19-
length = children.length
20-
21-
if (index === undefined || index === null) {
22-
throw new Error('Expected positive finite index or child node')
23-
} else if (index && typeof index !== 'number') {
24-
index = children.indexOf(index)
25-
if (index === -1) {
26-
throw new Error('Expected child node')
15+
if (typeof index === 'number') {
16+
if (index < 0 || index === Infinity) {
17+
throw new Error('Expected positive finite number as index')
2718
}
28-
}
19+
} else {
20+
index = parent.children.indexOf(index)
2921

30-
if (typeof index !== 'number' || index < 0 || index === Infinity) {
31-
throw new Error('Expected positive finite number as index')
22+
if (index < 0) {
23+
throw new Error('Expected child node or index')
24+
}
3225
}
3326

34-
while (++index < length) {
35-
child = children[index]
36-
37-
if (is(child, index, parent)) {
38-
results.push(child)
27+
while (++index < parent.children.length) {
28+
if (is(parent.children[index], index, parent)) {
29+
results.push(parent.children[index])
3930
}
4031
}
4132

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@
7474
"prettier": true,
7575
"esnext": false,
7676
"rules": {
77-
"guard-for-in": "off"
77+
"eqeqeq": [
78+
"error",
79+
"always",
80+
{
81+
"null": "ignore"
82+
}
83+
],
84+
"guard-for-in": "off",
85+
"no-eq-null": "off"
7886
},
7987
"ignore": [
8088
"unist-util-find-all-after.js",

test.js

Lines changed: 144 additions & 80 deletions
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 findAllAfter = require('.')
@@ -26,105 +25,170 @@ test('unist-util-find-all-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
findAllAfter({type: 'foo', children: []})
32-
}, /Expected positive finite index or child node/)
33-
}, 'should fail without index')
31+
},
32+
/Expected child node or index/,
33+
'should fail without index'
34+
)
3435

35-
t.doesNotThrow(function () {
36-
assert.throws(function () {
36+
t.throws(
37+
function () {
3738
findAllAfter({type: 'foo', children: []}, -1)
38-
}, /Expected positive finite number as index/)
39+
},
40+
/Expected positive finite number as index/,
41+
'should fail with invalid index (#1)'
42+
)
3943

40-
assert.throws(function () {
44+
t.throws(
45+
function () {
4146
findAllAfter({type: 'foo', children: []}, Infinity)
42-
}, /Expected positive finite number as index/)
47+
},
48+
/Expected positive finite number as index/,
49+
'should fail with invalid index (#2)'
50+
)
4351

44-
assert.throws(function () {
52+
t.throws(
53+
function () {
4554
findAllAfter({type: 'foo', children: []}, false)
46-
}, /Expected positive finite number as index/)
55+
},
56+
/Expected child node or index/,
57+
'should fail with invalid index (#3)'
58+
)
4759

48-
assert.throws(function () {
49-
findAllAfter({type: 'foo', children: []}, '')
50-
}, /Expected positive finite number as index/)
60+
t.throws(
61+
function () {
62+
findAllAfter({type: 'foo', children: []}, -1)
63+
},
64+
/Expected positive finite number as index/,
65+
'should fail with invalid index (#4)'
66+
)
5167

52-
assert.throws(function () {
68+
t.throws(
69+
function () {
5370
findAllAfter({type: 'foo', children: []}, {type: 'bar'})
54-
}, /Expected child node/)
55-
}, 'should fail with invalid index')
71+
},
72+
/Expected child node/,
73+
'should fail with invalid index (#5)'
74+
)
5675

57-
t.doesNotThrow(function () {
58-
assert.throws(function () {
76+
t.throws(
77+
function () {
5978
findAllAfter(
60-
{
61-
type: 'foo',
62-
children: [{type: 'bar'}, {type: 'baz'}]
63-
},
79+
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
6480
0,
6581
false
6682
)
67-
}, /Expected function, string, or object as test/)
83+
},
84+
/Expected function, string, or object as test/,
85+
'should fail for invalid `test` (#1)'
86+
)
6887

69-
assert.throws(function () {
88+
t.throws(
89+
function () {
7090
findAllAfter(
71-
{
72-
type: 'foo',
73-
children: [{type: 'bar'}, {type: 'baz'}]
74-
},
91+
{type: 'foo', children: [{type: 'bar'}, {type: 'baz'}]},
7592
0,
7693
true
7794
)
78-
}, /Expected function, string, or object as test/)
79-
}, 'should fail for invalid `test`')
80-
81-
t.doesNotThrow(function () {
82-
var result = children.slice(2)
83-
84-
assert.deepStrictEqual(findAllAfter(paragraph, children[1]), result)
85-
assert.deepStrictEqual(findAllAfter(paragraph, 1), result)
86-
assert.deepStrictEqual(findAllAfter(paragraph, 7), [])
87-
}, 'should return the following node when without `test`')
88-
89-
t.doesNotThrow(function () {
90-
assert.deepStrictEqual(findAllAfter(paragraph, 0, children[6]), [
91-
children[6]
92-
])
93-
assert.deepStrictEqual(findAllAfter(paragraph, children[0], children[1]), [
94-
children[1]
95-
])
96-
assert.deepStrictEqual(findAllAfter(paragraph, 0, children[1]), [
97-
children[1]
98-
])
99-
assert.deepStrictEqual(
100-
findAllAfter(paragraph, children[0], children[0]),
101-
[]
102-
)
103-
assert.deepStrictEqual(findAllAfter(paragraph, 0, children[0]), [])
104-
assert.deepStrictEqual(findAllAfter(paragraph, 1, children[1]), [])
105-
}, 'should return `node` when given a `node` and existing')
106-
107-
t.doesNotThrow(function () {
108-
assert.deepStrictEqual(findAllAfter(paragraph, 0, 'strong'), [children[3]])
109-
assert.deepStrictEqual(findAllAfter(paragraph, 3, 'strong'), [])
110-
assert.deepStrictEqual(findAllAfter(paragraph, children[0], 'strong'), [
111-
children[3]
112-
])
113-
assert.deepStrictEqual(findAllAfter(paragraph, children[3], 'strong'), [])
114-
}, 'should return a child when given a `type` and existing')
115-
116-
t.doesNotThrow(function () {
117-
var result = children.slice(5)
118-
119-
assert.deepStrictEqual(findAllAfter(paragraph, 0, test), result)
120-
assert.deepStrictEqual(findAllAfter(paragraph, 6, test), [])
121-
assert.deepStrictEqual(findAllAfter(paragraph, children[4], test), result)
122-
assert.deepStrictEqual(findAllAfter(paragraph, children[6], test), [])
123-
124-
function test(node, n) {
125-
return n >= 5
126-
}
127-
}, 'should return a child when given a `test` and existing')
95+
},
96+
/Expected function, string, or object as test/,
97+
'should fail for invalid `test` (#2)'
98+
)
99+
100+
t.deepEqual(
101+
findAllAfter(paragraph, children[1]),
102+
children.slice(2),
103+
'should return the following node when without `test` (#1)'
104+
)
105+
t.deepEqual(
106+
findAllAfter(paragraph, 1),
107+
children.slice(2),
108+
'should return the following node when without `test` (#1)'
109+
)
110+
t.deepEqual(
111+
findAllAfter(paragraph, 7),
112+
[],
113+
'should return the following node when without `test` (#1)'
114+
)
115+
116+
t.deepEqual(
117+
findAllAfter(paragraph, 0, children[6]),
118+
[children[6]],
119+
'should return `node` when given a `node` and existing (#1)'
120+
)
121+
t.deepEqual(
122+
findAllAfter(paragraph, children[0], children[1]),
123+
[children[1]],
124+
'should return `node` when given a `node` and existing (#2)'
125+
)
126+
t.deepEqual(
127+
findAllAfter(paragraph, 0, children[1]),
128+
[children[1]],
129+
'should return `node` when given a `node` and existing (#3)'
130+
)
131+
t.deepEqual(
132+
findAllAfter(paragraph, children[0], children[0]),
133+
[],
134+
'should return `node` when given a `node` and existing (#4)'
135+
)
136+
t.deepEqual(
137+
findAllAfter(paragraph, 0, children[0]),
138+
[],
139+
'should return `node` when given a `node` and existing (#5)'
140+
)
141+
t.deepEqual(
142+
findAllAfter(paragraph, 1, children[1]),
143+
[],
144+
'should return `node` when given a `node` and existing (#6)'
145+
)
146+
147+
t.deepEqual(
148+
findAllAfter(paragraph, 0, 'strong'),
149+
[children[3]],
150+
'should return a child when given a `type` and existing (#1)'
151+
)
152+
t.deepEqual(
153+
findAllAfter(paragraph, 3, 'strong'),
154+
[],
155+
'should return a child when given a `type` and existing (#2)'
156+
)
157+
t.deepEqual(
158+
findAllAfter(paragraph, children[0], 'strong'),
159+
[children[3]],
160+
'should return a child when given a `type` and existing (#3)'
161+
)
162+
t.deepEqual(
163+
findAllAfter(paragraph, children[3], 'strong'),
164+
[],
165+
'should return a child when given a `type` and existing (#4)'
166+
)
167+
168+
t.deepEqual(
169+
findAllAfter(paragraph, 0, test),
170+
children.slice(5),
171+
'should return a child when given a `test` and existing (#1)'
172+
)
173+
t.deepEqual(
174+
findAllAfter(paragraph, 6, test),
175+
[],
176+
'should return a child when given a `test` and existing (#2)'
177+
)
178+
t.deepEqual(
179+
findAllAfter(paragraph, children[4], test),
180+
children.slice(5),
181+
'should return a child when given a `test` and existing (#3)'
182+
)
183+
t.deepEqual(
184+
findAllAfter(paragraph, children[6], test),
185+
[],
186+
'should return a child when given a `test` and existing (#4)'
187+
)
188+
189+
function test(node, n) {
190+
return n >= 5
191+
}
128192

129193
t.end()
130194
})

0 commit comments

Comments
 (0)