From e062c792481407d3c904b2183767e636bc9f3484 Mon Sep 17 00:00:00 2001 From: cfresco-fb Date: Tue, 2 Jun 2026 16:47:12 +0200 Subject: [PATCH] fix: iterate all weight matches in getFontPathByWeight (#645) * fix: iterate all weight matches in getFontPathByWeight When fontData has multiple entries for the same weight with different format sets, Array.find() stops at the first match even if that entry does not contain the requested format. The fix uses a for...of loop that returns only when both weight AND format match. * fix: add fallback to first src entry and trailing newline - Use `?? font.src[0]` as fallback when no src entry matches the requested format, as suggested in code review - Add missing trailing newline at end of file Fixes #644 --- src/utils/getFontPathByWeight.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/utils/getFontPathByWeight.ts b/src/utils/getFontPathByWeight.ts index 055ed0e..294439c 100644 --- a/src/utils/getFontPathByWeight.ts +++ b/src/utils/getFontPathByWeight.ts @@ -11,7 +11,12 @@ export function getFontPathByWeight( const style = options?.style ?? "normal"; const format = options?.format ?? "truetype"; - return fonts - .find(font => font.weight === String(weight) && font.style === style) - ?.src.find(file => file.format === format)?.url; + for (const font of fonts) { + if (font.weight === String(weight) && font.style === style) { + const src = font.src.find(file => file.format === format) ?? font.src[0]; + if (src) return src.url; + } + } + + return undefined; }