diff --git a/index.js b/index.js index c6b283f..9367730 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,9 @@ module.exports = function repetitionRanges(arr, value) { } var results = []; + var arrLastIndex = arr.length - 1; - if (arr.length < 2) { + if (arrLastIndex < 1) { return results; } @@ -24,6 +25,10 @@ module.exports = function repetitionRanges(arr, value) { return results; } + if (i === arrLastIndex) { + return results; + } + var last = arr.lastIndexOf(value); if (i === last) { diff --git a/index.mjs b/index.mjs index feb7f3d..b7deb20 100644 --- a/index.mjs +++ b/index.mjs @@ -10,8 +10,9 @@ export default function repetitionRanges(arr, value) { } var results = []; + var arrLastIndex = arr.length - 1; - if (arr.length < 2) { + if (arrLastIndex < 1) { return results; } @@ -21,6 +22,10 @@ export default function repetitionRanges(arr, value) { return results; } + if (i === arrLastIndex) { + return results; + } + var last = arr.lastIndexOf(value); if (i === last) { diff --git a/test.js b/test.js index 0addcf4..f623697 100644 --- a/test.js +++ b/test.js @@ -26,10 +26,16 @@ test('repetitionRanges()', t => { 'should not include multiple but inconsecutive values to the result.' ); + t.deepEqual( + repetitionRanges([true, 'true'], 'true'), + [], + 'should compare values with the "same value zero" level.' + ); + t.deepEqual( repetitionRanges([1, 2, 3], 123), [], - 'should return an empty array if the search value doesn\'t exist in the array.' + 'should return an empty array if the search value is not found.' ); t.deepEqual(