Skip to content

Commit

Permalink
return early if the first index is the end of array
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Sep 30, 2018
1 parent a5e466a commit bace1c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bace1c5

Please sign in to comment.