Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ function findAllAfter(parent, index, test) {
children = parent.children
length = children.length

if (index && index.type) {
if (index === undefined || index === null) {
throw new Error('Expected positive finite index or child node')
} else if (index && typeof index !== 'number') {
index = children.indexOf(index)
if (index === -1) {
throw new Error('Expected child node')
}
}

if (isNaN(index) || index < 0 || index === Infinity) {
throw new Error('Expected positive finite index or child node')
if (typeof index !== 'number' || index < 0 || index === Infinity) {
throw new Error('Expected positive finite number as index')
}

while (++index < length) {
Expand Down
20 changes: 17 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ test('unist-util-find-all-after', function(t) {
assert.throws(function() {
findAllAfter({type: 'foo', children: []})
}, /Expected positive finite index or child node/)
}, 'should fail without index')

t.doesNotThrow(function() {
assert.throws(function() {
findAllAfter({type: 'foo', children: []}, -1)
}, /Expected positive finite index or child node/)
}, /Expected positive finite number as index/)

assert.throws(function() {
findAllAfter({type: 'foo', children: []}, Infinity)
}, /Expected positive finite number as index/)

assert.throws(function() {
findAllAfter({type: 'foo', children: []}, false)
}, /Expected positive finite number as index/)

assert.throws(function() {
findAllAfter({type: 'foo', children: []}, '')
}, /Expected positive finite number as index/)

assert.throws(function() {
findAllAfter({type: 'foo', children: []}, {type: 'bar'})
}, /Expected positive finite index or child node/)
}, 'should fail without index')
}, /Expected child node/)
}, 'should fail with invalid index')

t.doesNotThrow(function() {
assert.throws(function() {
Expand Down