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
This commit is contained in:
cfresco-fb
2026-06-02 16:47:12 +02:00
committed by GitHub
parent ca42b6b218
commit e062c79248
+8 -3
View File
@@ -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;
}