feat: add findClosestNumber for better cache

This commit is contained in:
Xyphyn committed 2023-07-31 10:38:01 -07:00
1 parent 6256d6107d
commit c4e12b5b30
2 files changed
+8 -1

No files matched your search

+4 -1
View File
@@ -1,7 +1,10 @@
<script lang="ts"> <script lang="ts">
import { findClosestNumber } from '$lib/util.js'
import { createAvatar } from '@dicebear/core' import { createAvatar } from '@dicebear/core'
import * as initials from '@dicebear/initials' import * as initials from '@dicebear/initials'
const sizes = [48, 64, 128, 256, 512]
export let url: string | undefined export let url: string | undefined
export let alt: string = '' export let alt: string = ''
@@ -10,7 +13,7 @@
{#if url} {#if url}
<img <img
src="{url}?format=webp&thumbnail={width * 2}" src="{url}?format=webp&thumbnail={findClosestNumber(sizes, width)}"
{alt} {alt}
{width} {width}
loading="lazy" loading="lazy"
+4
View File
@@ -0,0 +1,4 @@
export const findClosestNumber = (numbers: number[], target: number): number =>
numbers.reduce((prev, curr) =>
Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev
)