diff --git a/src/binary-search.ts b/src/binary-search.ts index d0f8845..1632808 100644 --- a/src/binary-search.ts +++ b/src/binary-search.ts @@ -97,7 +97,7 @@ export function memoizedBinarySearch( let high = haystack.length - 1; if (key === lastKey) { if (needle === lastNeedle) { - found = haystack[lastIndex][COLUMN] === needle; + found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle; return lastIndex; } diff --git a/test/binary-search.test.ts b/test/binary-search.test.ts index 309239f..2555db6 100644 --- a/test/binary-search.test.ts +++ b/test/binary-search.test.ts @@ -378,7 +378,7 @@ describe('binary search', () => { }); describe('memoizedBinarySearch', () => { - const array: SourceMapSegment[] = [[0], [5], [10]]; + const array: SourceMapSegment[] = [[1], [5], [10]]; test('refinds same index', (t) => { const memo = memoizedState(); @@ -388,12 +388,19 @@ describe('memoizedBinarySearch', () => { }); test('restores found state', (t) => { - const memo = memoizedState(); + const memo1 = memoizedState(); + const memo2 = memoizedState(); - t.is(memoizedBinarySearch(array, 6, memo, 0), 1); - binarySearch(array, 0, 0, array.length - 1); + t.is(memoizedBinarySearch(array, 0, memo1, 0), -1); + t.is(found, false); + + t.is(memoizedBinarySearch(array, 5, memo2, 0), 1); t.is(found, true); - t.is(memoizedBinarySearch(array, 6, memo, 0), 1); + + t.is(memoizedBinarySearch(array, 0, memo1, 0), -1); t.is(found, false); + + t.is(memoizedBinarySearch(array, 5, memo2, 0), 1); + t.is(found, true); }); });