Skip to content

Commit 1cb8d85

Browse files
Roang-zero1wooorm
authored andcommitted
Update error messages
Closes GH-6. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 7619505 commit 1cb8d85

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ function findAllAfter(parent, index, test) {
1818
children = parent.children
1919
length = children.length
2020

21-
if (index && index.type) {
21+
if (index === undefined || index === null) {
22+
throw new Error('Expected positive finite index or child node')
23+
} else if (index && typeof index !== 'number') {
2224
index = children.indexOf(index)
25+
if (index === -1) {
26+
throw new Error('Expected child node')
27+
}
2328
}
2429

25-
if (isNaN(index) || index < 0 || index === Infinity) {
26-
throw new Error('Expected positive finite index or child node')
30+
if (typeof index !== 'number' || index < 0 || index === Infinity) {
31+
throw new Error('Expected positive finite number as index')
2732
}
2833

2934
while (++index < length) {

test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,29 @@ test('unist-util-find-all-after', function(t) {
3030
assert.throws(function() {
3131
findAllAfter({type: 'foo', children: []})
3232
}, /Expected positive finite index or child node/)
33+
}, 'should fail without index')
3334

35+
t.doesNotThrow(function() {
3436
assert.throws(function() {
3537
findAllAfter({type: 'foo', children: []}, -1)
36-
}, /Expected positive finite index or child node/)
38+
}, /Expected positive finite number as index/)
39+
40+
assert.throws(function() {
41+
findAllAfter({type: 'foo', children: []}, Infinity)
42+
}, /Expected positive finite number as index/)
43+
44+
assert.throws(function() {
45+
findAllAfter({type: 'foo', children: []}, false)
46+
}, /Expected positive finite number as index/)
47+
48+
assert.throws(function() {
49+
findAllAfter({type: 'foo', children: []}, '')
50+
}, /Expected positive finite number as index/)
3751

3852
assert.throws(function() {
3953
findAllAfter({type: 'foo', children: []}, {type: 'bar'})
40-
}, /Expected positive finite index or child node/)
41-
}, 'should fail without index')
54+
}, /Expected child node/)
55+
}, 'should fail with invalid index')
4256

4357
t.doesNotThrow(function() {
4458
assert.throws(function() {

0 commit comments

Comments
 (0)