Skip to content

Commit 2a656f6

Browse files
committed
Refactor to improve bundle size
1 parent 7d4b219 commit 2a656f6

File tree

2 files changed

+133
-80
lines changed

2 files changed

+133
-80
lines changed

index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,31 @@ module.exports = findBefore
66

77
function findBefore(parent, index, test) {
88
var is = convert(test)
9-
var children
10-
var child
119

1210
if (!parent || !parent.type || !parent.children) {
1311
throw new Error('Expected parent node')
1412
}
1513

16-
children = parent.children
17-
18-
if (index && index.type) {
19-
index = children.indexOf(index)
20-
}
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)
2120

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

2626
// Performance.
27-
if (index > children.length) {
28-
index = children.length
27+
if (index > parent.children.length) {
28+
index = parent.children.length
2929
}
3030

3131
while (index--) {
32-
child = children[index]
33-
34-
if (is(child, index, parent)) {
35-
return child
32+
if (is(parent.children[index], index, parent)) {
33+
return parent.children[index]
3634
}
3735
}
3836

test.js

Lines changed: 120 additions & 65 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 findBefore = require('.')
@@ -28,82 +27,138 @@ test('unist-util-find-before', function (t) {
2827
'should fail without parent node'
2928
)
3029

31-
t.doesNotThrow(function () {
32-
assert.throws(function () {
30+
t.throws(
31+
function () {
3332
findBefore({type: 'foo', children: []})
34-
}, /Expected positive finite index or child node/)
33+
},
34+
/Expected child node or index/,
35+
'should fail without index (#1)'
36+
)
3537

36-
assert.throws(function () {
38+
t.throws(
39+
function () {
3740
findBefore({type: 'foo', children: []}, -1)
38-
}, /Expected positive finite index or child node/)
41+
},
42+
/Expected positive finite number as index/,
43+
'should fail without index (#2)'
44+
)
3945

40-
assert.throws(function () {
46+
t.throws(
47+
function () {
4148
findBefore({type: 'foo', children: []}, {type: 'bar'})
42-
}, /Expected positive finite index or child node/)
43-
}, 'should fail without index')
49+
},
50+
/Expected child node or index/,
51+
'should fail without index (#3)'
52+
)
4453

45-
t.doesNotThrow(function () {
46-
assert.throws(function () {
47-
findBefore(
48-
{
49-
type: 'foo',
50-
children: [{type: 'bar'}]
51-
},
52-
1,
53-
false
54-
)
55-
}, /Expected function, string, or object as test/)
54+
t.throws(
55+
function () {
56+
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
57+
},
58+
/Expected function, string, or object as test/,
59+
'should fail for invalid `test` (#1)'
60+
)
5661

57-
assert.throws(function () {
58-
findBefore(
59-
{
60-
type: 'foo',
61-
children: [{type: 'bar'}]
62-
},
63-
1,
64-
true
65-
)
66-
}, /Expected function, string, or object as test/)
67-
}, 'should fail for invalid `test`')
62+
t.throws(
63+
function () {
64+
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, true)
65+
},
66+
/Expected function, string, or object as test/,
67+
'should fail for invalid `test` (#2)'
68+
)
6869

69-
t.doesNotThrow(function () {
70-
assert.strictEqual(findBefore(paragraph, children[1]), children[0])
71-
assert.strictEqual(findBefore(paragraph, 1), children[0])
72-
assert.strictEqual(findBefore(paragraph, 0), null)
73-
}, 'should return the preceding node when without `test`')
70+
t.strictEqual(
71+
findBefore(paragraph, children[1]),
72+
children[0],
73+
'should return the preceding node when without `test` (#1)'
74+
)
75+
t.strictEqual(
76+
findBefore(paragraph, 1),
77+
children[0],
78+
'should return the preceding node when without `test` (#2)'
79+
)
80+
t.strictEqual(
81+
findBefore(paragraph, 0),
82+
null,
83+
'should return the preceding node when without `test` (#3)'
84+
)
7485

75-
t.doesNotThrow(function () {
76-
assert.strictEqual(findBefore(paragraph, 100, children[0]), children[0])
77-
assert.strictEqual(
78-
findBefore(paragraph, children[1], children[0]),
79-
children[0]
80-
)
81-
assert.strictEqual(findBefore(paragraph, 1, children[0]), children[0])
82-
assert.strictEqual(findBefore(paragraph, children[0], children[0]), null)
83-
assert.strictEqual(findBefore(paragraph, 0, children[0]), null)
84-
assert.strictEqual(findBefore(paragraph, 1, children[1]), null)
85-
}, 'should return `node` when given a `node` and existing')
86+
t.strictEqual(
87+
findBefore(paragraph, 100, children[0]),
88+
children[0],
89+
'should return `node` when given a `node` and existing (#1)'
90+
)
91+
t.strictEqual(
92+
findBefore(paragraph, children[1], children[0]),
93+
children[0],
94+
'should return `node` when given a `node` and existing (#2)'
95+
)
96+
t.strictEqual(
97+
findBefore(paragraph, 1, children[0]),
98+
children[0],
99+
'should return `node` when given a `node` and existing (#3)'
100+
)
101+
t.strictEqual(
102+
findBefore(paragraph, children[0], children[0]),
103+
null,
104+
'should return `node` when given a `node` and existing (#4)'
105+
)
106+
t.strictEqual(
107+
findBefore(paragraph, 0, children[0]),
108+
null,
109+
'should return `node` when given a `node` and existing (#5)'
110+
)
111+
t.strictEqual(
112+
findBefore(paragraph, 1, children[1]),
113+
null,
114+
'should return `node` when given a `node` and existing (#6)'
115+
)
86116

87-
t.doesNotThrow(function () {
88-
assert.strictEqual(findBefore(paragraph, 100, 'strong'), children[3])
89-
assert.strictEqual(findBefore(paragraph, 3, 'strong'), null)
90-
assert.strictEqual(
91-
findBefore(paragraph, children[4], 'strong'),
92-
children[3]
93-
)
94-
assert.strictEqual(findBefore(paragraph, children[3], 'strong'), null)
95-
}, 'should return a child when given a `type` and existing')
117+
t.strictEqual(
118+
findBefore(paragraph, 100, 'strong'),
119+
children[3],
120+
'should return a child when given a `type` and existing (#1)'
121+
)
122+
t.strictEqual(
123+
findBefore(paragraph, 3, 'strong'),
124+
null,
125+
'should return a child when given a `type` and existing (#2)'
126+
)
127+
t.strictEqual(
128+
findBefore(paragraph, children[4], 'strong'),
129+
children[3],
130+
'should return a child when given a `type` and existing (#3)'
131+
)
132+
t.strictEqual(
133+
findBefore(paragraph, children[3], 'strong'),
134+
null,
135+
'should return a child when given a `type` and existing (#4)'
136+
)
96137

97-
t.doesNotThrow(function () {
98-
assert.strictEqual(findBefore(paragraph, 100, test), children[3])
99-
assert.strictEqual(findBefore(paragraph, 3, test), null)
100-
assert.strictEqual(findBefore(paragraph, children[4], test), children[3])
101-
assert.strictEqual(findBefore(paragraph, children[3], test), null)
138+
t.strictEqual(
139+
findBefore(paragraph, 100, test),
140+
children[3],
141+
'should return a child when given a `test` and existing (#1)'
142+
)
143+
t.strictEqual(
144+
findBefore(paragraph, 3, test),
145+
null,
146+
'should return a child when given a `test` and existing (#2)'
147+
)
148+
t.strictEqual(
149+
findBefore(paragraph, children[4], test),
150+
children[3],
151+
'should return a child when given a `test` and existing (#3)'
152+
)
153+
t.strictEqual(
154+
findBefore(paragraph, children[3], test),
155+
null,
156+
'should return a child when given a `test` and existing (#4)'
157+
)
102158

103-
function test(node, n) {
104-
return n === 3
105-
}
106-
}, 'should return a child when given a `test` and existing')
159+
function test(node, n) {
160+
return n === 3
161+
}
107162

108163
t.end()
109164
})

0 commit comments

Comments
 (0)