diff --git a/index.js b/index.js index ac8aaf2..ab850d3 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test.js b/test.js index 5a31a35..516c9da 100644 --- a/test.js +++ b/test.js @@ -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() {