Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor cleanup for "get best icon variant" code #563

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/icon-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const SIZE_AS_NUMERIC = {
* 3. Matching pixel density. If no exact match:
* 1. If smaller ones exist, prefer closest smaller density.
* 2. Otherwise prefer closest larger density.
* 4. If everything else is the same, use the blob version ID as the tiebreaker.
*
* @param {IconVariants} variants
* @param {BitmapOpts | SvgOpts} opts
Expand Down Expand Up @@ -179,24 +180,26 @@ export function getBestVariant(variants, opts) {
const aSizeNum = SIZE_AS_NUMERIC[a.size]
const bSizeNum = SIZE_AS_NUMERIC[b.size]

const aSizeDiff = aSizeNum - wantedSizeNum
const bSizeDiff = bSizeNum - wantedSizeNum

// Both variants match desired size, use pixel density to determine preferred match
if (aSizeDiff === 0 && bSizeDiff === 0) {
// Pixel density doesn't matter for svg but prefer lower for consistent results
if (opts.mimeType === 'image/svg+xml') {
return a.pixelDensity <= b.pixelDensity ? -1 : 1
}
const bothMatchDesiredSize =
aSizeNum === wantedSizeNum && bSizeNum === wantedSizeNum
if (!bothMatchDesiredSize) {
return determineSortValue(wantedSizeNum, aSizeNum, bSizeNum)
}

const aPixelDensity = 'pixelDensity' in a ? a.pixelDensity : 1
const bPixelDensity = 'pixelDensity' in b ? b.pixelDensity : 1
const bothMatchDesiredPixelDensity =
aPixelDensity === wantedPixelDensity &&
bPixelDensity === wantedPixelDensity
if (!bothMatchDesiredPixelDensity) {
return determineSortValue(
wantedPixelDensity,
a.pixelDensity,
b.pixelDensity
aPixelDensity,
bPixelDensity
)
}

return determineSortValue(wantedSizeNum, aSizeNum, bSizeNum)
return a.blobVersionId > b.blobVersionId ? -1 : 1
})

// Closest match will be first element
Expand Down
Loading