diff --git a/src/any-map.ts b/src/any-map.ts index 8791fde..c6909f1 100644 --- a/src/any-map.ts +++ b/src/any-map.ts @@ -32,7 +32,7 @@ export const AnyMap: AnyMap = function (map, mapUrl) { const no = sections[i + 1].offset; addSection(sections[i], mapUrl, mappings, sources, sourcesContent, names, no.line, no.column); } - for (; i < sections.length; i++) { + if (sections.length > 0) { addSection(sections[i], mapUrl, mappings, sources, sourcesContent, names, -1, -1); } diff --git a/src/binary-search.ts b/src/binary-search.ts index faf5fe6..d0f8845 100644 --- a/src/binary-search.ts +++ b/src/binary-search.ts @@ -97,6 +97,7 @@ export function memoizedBinarySearch( let high = haystack.length - 1; if (key === lastKey) { if (needle === lastNeedle) { + found = haystack[lastIndex][COLUMN] === needle; return lastIndex; } diff --git a/test/binary-search.test.ts b/test/binary-search.test.ts index eb4554b..309239f 100644 --- a/test/binary-search.test.ts +++ b/test/binary-search.test.ts @@ -1,4 +1,4 @@ -import { binarySearch } from '../src/binary-search'; +import { binarySearch, found, memoizedState, memoizedBinarySearch } from '../src/binary-search'; import { test, describe } from './setup'; type SourceMapSegment = [number]; @@ -376,3 +376,24 @@ describe('binary search', () => { }); }); }); + +describe('memoizedBinarySearch', () => { + const array: SourceMapSegment[] = [[0], [5], [10]]; + + test('refinds same index', (t) => { + const memo = memoizedState(); + + t.is(memoizedBinarySearch(array, 6, memo, 0), 1); + t.is(memoizedBinarySearch(array, 6, memo, 0), 1); + }); + + test('restores found state', (t) => { + const memo = memoizedState(); + + t.is(memoizedBinarySearch(array, 6, memo, 0), 1); + binarySearch(array, 0, 0, array.length - 1); + t.is(found, true); + t.is(memoizedBinarySearch(array, 6, memo, 0), 1); + t.is(found, false); + }); +});