« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gen-thumbs.js16
-rw-r--r--src/write/bind-utilities.js5
2 files changed, 21 insertions, 0 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index 51b2c72d..741cdff3 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -168,11 +168,23 @@ getThumbnailsAvailableForDimensions.all =
     .map(([name, {size}]) => [name, size])
     .sort((a, b) => b[1] - a[1]);
 
+export function checkIfImagePathHasCachedThumbnails(imagePath, cache) {
+  // Generic utility for checking if the thumbnail cache includes any info for
+  // the provided image path, so that the other functions don't hard-code the
+  // cache format.
+
+  return !!cache[imagePath];
+}
+
 export function getDimensionsOfImagePath(imagePath, cache) {
   // This function is really generic. It takes the gen-thumbs image cache and
   // returns the dimensions in that cache, so that other functions don't need
   // to hard-code the cache format.
 
+  if (!cache[imagePath]) {
+    throw new Error(`Expected imagePath to be included in cache, got ${imagePath}`);
+  }
+
   const [width, height] = cache[imagePath].slice(1);
   return [width, height];
 }
@@ -185,6 +197,10 @@ export function getThumbnailEqualOrSmaller(preferred, imagePath, cache) {
   // one which is being thumbnail-ified, this just returns the name of the
   // selected thumbnail size.
 
+  if (!cache[imagePath]) {
+    throw new Error(`Expected imagePath to be included in cache, got ${imagePath}`);
+  }
+
   const {size: preferredSize} = thumbnailSpec[preferred];
   const [width, height] = getDimensionsOfImagePath(imagePath, cache);
   const available = getThumbnailsAvailableForDimensions([width, height]);
diff --git a/src/write/bind-utilities.js b/src/write/bind-utilities.js
index c32035f1..942cce89 100644
--- a/src/write/bind-utilities.js
+++ b/src/write/bind-utilities.js
@@ -11,6 +11,7 @@ import {bindOpts} from '#sugar';
 import {thumb} from '#urls';
 
 import {
+  checkIfImagePathHasCachedThumbnails,
   getDimensionsOfImagePath,
   getThumbnailEqualOrSmaller,
   getThumbnailsAvailableForDimensions,
@@ -54,6 +55,10 @@ export function bindUtilities({
 
   bound.find = bindFind(wikiData, {mode: 'warn'});
 
+  bound.checkIfImagePathHasCachedThumbnails =
+    (imagePath) =>
+      checkIfImagePathHasCachedThumbnails(imagePath, thumbsCache);
+
   bound.getDimensionsOfImagePath =
     (imagePath) =>
       getDimensionsOfImagePath(imagePath, thumbsCache);