Skip to content

Commit e062c79

Browse files
authored
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
1 parent ca42b6b commit e062c79

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/utils/getFontPathByWeight.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ export function getFontPathByWeight(
1111
const style = options?.style ?? "normal";
1212
const format = options?.format ?? "truetype";
1313

14-
return fonts
15-
.find(font => font.weight === String(weight) && font.style === style)
16-
?.src.find(file => file.format === format)?.url;
14+
for (const font of fonts) {
15+
if (font.weight === String(weight) && font.style === style) {
16+
const src = font.src.find(file => file.format === format) ?? font.src[0];
17+
if (src) return src.url;
18+
}
19+
}
20+
21+
return undefined;
1722
}

0 commit comments

Comments
 (0)