Files
klz-cables.com/.pnpm-store/v10/files/fe/4b198b4c2307a3a87ecaebeb4b45557de561e8591ce0078b2c081aa0f9889d54df8ff11a7ef856e4ee04060c34fabc3aab73703f05c8df1b9b16ae033d0e7a
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

39 lines
1.7 KiB
Plaintext

/**
* Takes image sizes and a target range and returns the url of the image within that range.
* If no images fit within the range, it selects the next smallest adequate image, the original,
* or the largest smaller image if no better fit exists.
*
* @param sizes The given FileSizes.
* @param targetSizeMax The ideal image maximum width. Defaults to 180.
* @param targetSizeMin The ideal image minimum width. Defaults to 40.
* @param thumbnailURL The thumbnail url set in config. If passed a url, will return early with it.
* @param url The url of the original file.
* @param width The width of the original file.
* @returns A url of the best fit file.
*/ export const getBestFitFromSizes = ({ sizes, targetSizeMax = 180, targetSizeMin = 40, thumbnailURL, url, width })=>{
if (thumbnailURL) {
return thumbnailURL;
}
if (!sizes) {
return url;
}
const bestFit = Object.values(sizes).reduce((closest, current)=>{
if (!current.width || current.width < targetSizeMin) {
return closest;
}
if (current.width >= targetSizeMin && current.width <= targetSizeMax) {
return !closest.width || current.width < closest.width || closest.width < targetSizeMin || closest.width > targetSizeMax ? current : closest;
}
if (!closest.width || !closest.original && closest.width < targetSizeMin && current.width > closest.width || closest.width > targetSizeMax && current.width < closest.width) {
return current;
}
return closest;
}, {
original: true,
url,
width
});
return bestFit.url || url;
};
//# sourceMappingURL=getBestFitFromSizes.js.map