Skip to content

Commit 17e217d

Browse files
committed
Refactor to improve bundle size
1 parent c653e82 commit 17e217d

File tree

3 files changed

+149
-88
lines changed

3 files changed

+149
-88
lines changed

index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,31 @@ module.exports = findAllBefore
77
function findAllBefore(parent, index, test) {
88
var is = convert(test)
99
var results = []
10-
var children
11-
var child
1210

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

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

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

2727
// Performance.
28-
if (index > children.length) {
29-
index = children.length
28+
if (index > parent.children.length) {
29+
index = parent.children.length
3030
}
3131

3232
while (index--) {
33-
child = children[index]
34-
35-
if (is(child, index, parent)) {
36-
results.push(child)
33+
if (is(parent.children[index], index, parent)) {
34+
results.push(parent.children[index])
3735
}
3836
}
3937

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+
"guard-for-in": "off",
81+
"no-eq-null": "off",
7382
"unicorn/prefer-number-properties": "off"
7483
},
7584
"ignore": [

test.js

Lines changed: 127 additions & 73 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 findAllBefore = require('.')
@@ -26,85 +25,140 @@ test('unist-util-find-all-before', function (t) {
2625
'should fail without parent node'
2726
)
2827

29-
t.doesNotThrow(function () {
30-
assert.throws(function () {
28+
t.throws(
29+
function () {
3130
findAllBefore({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
findAllBefore({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
findAllBefore({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
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
46-
}, /Expected function, string, or object as test/)
47-
48-
assert.throws(function () {
49-
findAllBefore(
50-
{
51-
type: 'foo',
52-
children: [{type: 'bar'}]
53-
},
54-
1,
55-
true
56-
)
57-
}, /Expected function, string, or object as test/)
58-
}, 'should fail for invalid `test`')
59-
60-
t.doesNotThrow(function () {
61-
assert.deepStrictEqual(findAllBefore(paragraph, children[1]), [children[0]])
62-
assert.deepStrictEqual(findAllBefore(paragraph, 1), [children[0]])
63-
assert.deepStrictEqual(findAllBefore(paragraph, 0), [])
64-
}, 'should return the preceding nodes when without `test`')
65-
66-
t.doesNotThrow(function () {
67-
assert.deepStrictEqual(findAllBefore(paragraph, 100, children[0]), [
68-
children[0]
69-
])
70-
assert.deepStrictEqual(findAllBefore(paragraph, children[1], children[0]), [
71-
children[0]
72-
])
73-
assert.deepStrictEqual(findAllBefore(paragraph, 1, children[0]), [
74-
children[0]
75-
])
76-
assert.deepStrictEqual(
77-
findAllBefore(paragraph, children[0], children[0]),
78-
[]
79-
)
80-
assert.deepStrictEqual(findAllBefore(paragraph, 0, children[0]), [])
81-
assert.deepStrictEqual(findAllBefore(paragraph, 1, children[1]), [])
82-
}, 'should return `[node]` when given a `node` and existing')
83-
84-
t.doesNotThrow(function () {
85-
var result = [children[3]]
86-
87-
assert.deepStrictEqual(findAllBefore(paragraph, 100, 'strong'), result)
88-
assert.deepStrictEqual(findAllBefore(paragraph, 3, 'strong'), [])
89-
assert.deepStrictEqual(
90-
findAllBefore(paragraph, children[4], 'strong'),
91-
result
92-
)
93-
assert.deepStrictEqual(findAllBefore(paragraph, children[3], 'strong'), [])
94-
}, 'should return children when given a `type` and existing')
95-
96-
t.doesNotThrow(function () {
97-
var result = children.slice(4).reverse()
98-
99-
assert.deepStrictEqual(findAllBefore(paragraph, 100, test), result)
100-
assert.deepStrictEqual(findAllBefore(paragraph, 3, test), [])
101-
assert.deepStrictEqual(findAllBefore(paragraph, children[4], test), [])
102-
assert.deepStrictEqual(findAllBefore(paragraph, children[3], test), [])
103-
104-
function test(node, n) {
105-
return n > 3
106-
}
107-
}, 'should return children when given a `test` and existing')
55+
},
56+
/Expected function, string, or object as test/,
57+
'should fail for invalid `test` (#1)'
58+
)
59+
60+
t.throws(
61+
function () {
62+
findAllBefore({type: 'foo', children: [{type: 'bar'}]}, 1, true)
63+
},
64+
/Expected function, string, or object as test/,
65+
'should fail for invalid `test` (#2)'
66+
)
67+
68+
t.deepEqual(
69+
findAllBefore(paragraph, children[1]),
70+
[children[0]],
71+
'should return the preceding nodes when without `test` (#1)'
72+
)
73+
t.deepEqual(
74+
findAllBefore(paragraph, 1),
75+
[children[0]],
76+
'should return the preceding nodes when without `test` (#1)'
77+
)
78+
t.deepEqual(
79+
findAllBefore(paragraph, 0),
80+
[],
81+
'should return the preceding nodes when without `test` (#1)'
82+
)
83+
84+
t.deepEqual(
85+
findAllBefore(paragraph, 100, children[0]),
86+
[children[0]],
87+
'should return `[node]` when given a `node` and existing (#1)'
88+
)
89+
t.deepEqual(
90+
findAllBefore(paragraph, children[1], children[0]),
91+
[children[0]],
92+
'should return `[node]` when given a `node` and existing (#2)'
93+
)
94+
t.deepEqual(
95+
findAllBefore(paragraph, 1, children[0]),
96+
[children[0]],
97+
'should return `[node]` when given a `node` and existing (#3)'
98+
)
99+
t.deepEqual(
100+
findAllBefore(paragraph, children[0], children[0]),
101+
[],
102+
'should return `[node]` when given a `node` and existing (#4)'
103+
)
104+
t.deepEqual(
105+
findAllBefore(paragraph, 0, children[0]),
106+
[],
107+
'should return `[node]` when given a `node` and existing (#5)'
108+
)
109+
t.deepEqual(
110+
findAllBefore(paragraph, 1, children[1]),
111+
[],
112+
'should return `[node]` when given a `node` and existing (#6)'
113+
)
114+
115+
t.deepEqual(
116+
findAllBefore(paragraph, 100, 'strong'),
117+
[children[3]],
118+
'should return children when given a `type` and existing (#1)'
119+
)
120+
t.deepEqual(
121+
findAllBefore(paragraph, 3, 'strong'),
122+
[],
123+
'should return children when given a `type` and existing (#2)'
124+
)
125+
t.deepEqual(
126+
findAllBefore(paragraph, children[4], 'strong'),
127+
[children[3]],
128+
'should return children when given a `type` and existing (#3)'
129+
)
130+
t.deepEqual(
131+
findAllBefore(paragraph, children[3], 'strong'),
132+
[],
133+
'should return children when given a `type` and existing (#4)'
134+
)
135+
136+
var result = children.slice(4).reverse()
137+
138+
t.deepEqual(
139+
findAllBefore(paragraph, 100, test),
140+
result,
141+
'should return children when given a `test` and existing (#1)'
142+
)
143+
t.deepEqual(
144+
findAllBefore(paragraph, 3, test),
145+
[],
146+
'should return children when given a `test` and existing (#2)'
147+
)
148+
t.deepEqual(
149+
findAllBefore(paragraph, children[4], test),
150+
[],
151+
'should return children when given a `test` and existing (#3)'
152+
)
153+
t.deepEqual(
154+
findAllBefore(paragraph, children[3], test),
155+
[],
156+
'should return children when given a `test` and existing (#4)'
157+
)
158+
159+
function test(node, n) {
160+
return n > 3
161+
}
108162

109163
t.end()
110164
})

0 commit comments

Comments
 (0)