From 54aa7a9f893c3bdf2ad5b6fed9ff3293ed206563 Mon Sep 17 00:00:00 2001 From: Bill Wallace Date: Wed, 13 Oct 2021 10:30:53 -0400 Subject: [PATCH] fix(stackPrefetch.js): Prevent undefined imageId prefetch Change the prefetch nearest range to always return valid indices --- src/stackTools/stackPrefetch.js | 24 ++++++------------------ src/util/uuidv4.js | 4 ++-- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/stackTools/stackPrefetch.js b/src/stackTools/stackPrefetch.js index ebab9df84..44188948b 100755 --- a/src/stackTools/stackPrefetch.js +++ b/src/stackTools/stackPrefetch.js @@ -40,32 +40,20 @@ function range(lowEnd, highEnd) { return arr; } -const max = function(arr) { - return Math.max.apply(null, arr); -}; - -const min = function(arr) { - return Math.min.apply(null, arr); -}; - function nearestIndex(arr, x) { // Return index of nearest values in array // http://stackoverflow.com/questions/25854212/return-index-of-nearest-values-in-an-array - const l = []; - const h = []; - - arr.forEach(function(v) { + let low = 0; + let high = arr.length - 1; + arr.forEach((v, idx) => { if (v < x) { - l.push(v); + low = Math.max(idx, low); } else if (v > x) { - h.push(v); + high = Math.min(idx, high); } }); - return { - low: arr.indexOf(max(l)), - high: arr.indexOf(min(h)), - }; + return { low, high }; } function prefetch(element) { diff --git a/src/util/uuidv4.js b/src/util/uuidv4.js index aa9d2f7ce..19ddd7433 100644 --- a/src/util/uuidv4.js +++ b/src/util/uuidv4.js @@ -1,7 +1,7 @@ export default function uuidv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - let r = (Math.random() * 16) | 0, - v = c == 'x' ? r : (r & 0x3) | 0x8; + const r = (Math.random() * 16) | 0; // eslint-disable-line no-bitwise + const v = c === 'x' ? r : (r & 0x3) | 0x8; // eslint-disable-line no-bitwise return v.toString(16); });