From badb6770430a30191cfa2a8e9a6264d5cf8c7305 Mon Sep 17 00:00:00 2001 From: Lucas Brandstaetter Date: Sat, 16 Nov 2019 15:47:13 +0100 Subject: [PATCH 1/6] Update error handling Improve error feedback for better easier error handling. --- index.js | 13 ++++++++----- test.js | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index ac8aaf2..fa966c2 100644 --- a/index.js +++ b/index.js @@ -18,12 +18,15 @@ function findAllAfter(parent, index, test) { children = parent.children length = children.length - if (index && index.type) { - index = children.indexOf(index) - } - - if (isNaN(index) || index < 0 || index === Infinity) { + if (index === undefined) { throw new Error('Expected positive finite index or child node') + } else if (index && typeof index !== 'number') { + index = children.indexOf(index) + if (index < 0) { + throw new Error('Expected child node') + } + } else if (index < 0 || index === Infinity) { + throw new Error('Expected positive finite index') } while (++index < length) { diff --git a/test.js b/test.js index 5a31a35..5320b1d 100644 --- a/test.js +++ b/test.js @@ -33,11 +33,11 @@ test('unist-util-find-all-after', function(t) { assert.throws(function() { findAllAfter({type: 'foo', children: []}, -1) - }, /Expected positive finite index or child node/) + }, /Expected positive finite index/) assert.throws(function() { findAllAfter({type: 'foo', children: []}, {type: 'bar'}) - }, /Expected positive finite index or child node/) + }, /Expected child node/) }, 'should fail without index') t.doesNotThrow(function() { From 242c3eac77b56e6fa20e2ee7aaed1ff56cda4efe Mon Sep 17 00:00:00 2001 From: Lucas Brandstaetter Date: Sat, 7 Dec 2019 12:29:51 +0100 Subject: [PATCH 2/6] Add tests for invalid indices Falsey values should also fail. --- test.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 5320b1d..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/) + }, /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 child node/) - }, 'should fail without index') + }, 'should fail with invalid index') t.doesNotThrow(function() { assert.throws(function() { From a6c4b78e6e787e71c4a376556d900372837ecb2b Mon Sep 17 00:00:00 2001 From: Lucas Brandstaetter Date: Sat, 7 Dec 2019 12:30:24 +0100 Subject: [PATCH 3/6] Fix error handling for falsey values --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fa966c2..f6315f6 100644 --- a/index.js +++ b/index.js @@ -25,8 +25,11 @@ function findAllAfter(parent, index, test) { if (index < 0) { throw new Error('Expected child node') } - } else if (index < 0 || index === Infinity) { - throw new Error('Expected positive finite index') + } + + if (typeof index !== 'number' || index < 0 || index === Infinity) { + console.log('Throw finite index') + throw new Error('Expected positive finite number as index') } while (++index < length) { From 1b13ffeda18d21fe20d75006615348edd7681585 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 8 Dec 2019 19:28:50 +0100 Subject: [PATCH 4/6] Update index.js Co-Authored-By: Titus --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f6315f6..4daae8c 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ function findAllAfter(parent, index, test) { children = parent.children length = children.length - if (index === undefined) { + 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) From 6560bd6859b18aa0fac05a745668a9d2b1459dcc Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 8 Dec 2019 19:29:06 +0100 Subject: [PATCH 5/6] Update index.js Co-Authored-By: Titus --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4daae8c..3a282bd 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ function findAllAfter(parent, index, test) { throw new Error('Expected positive finite index or child node') } else if (index && typeof index !== 'number') { index = children.indexOf(index) - if (index < 0) { + if (index === -1) { throw new Error('Expected child node') } } From 4499299ae0495e6963a0106ffb634f4fc231e182 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 8 Dec 2019 19:29:30 +0100 Subject: [PATCH 6/6] Update index.js Co-Authored-By: Titus --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 3a282bd..ab850d3 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,6 @@ function findAllAfter(parent, index, test) { } if (typeof index !== 'number' || index < 0 || index === Infinity) { - console.log('Throw finite index') throw new Error('Expected positive finite number as index') }