Skip to content

Commit

Permalink
fix: use custom stable sort function to fix hermes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu authored and kylerjensen committed Jun 24, 2021
1 parent 5cde709 commit 871ff0d
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/utilities/matchFontFace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ import { FontStyle } from '../types/FontStyle';
import { FontWeight } from '../types/FontWeight';
import { normalizeFontWeight } from './normalizeFontWeight';

/**
* The Hermes JS engine does not do a stable sort: /~https://github.com/facebook/hermes/issues/212
*/
export function stableSort<T>(array: T[], ...compareFns: Array<(a: T, b: T) => number>) {
return compareFns.reduce((final, compareFn) => {
return final
.map((item, index) => ({ item, index }))
.sort((a, b) => compareFn(a.item, b.item) || a.index - b.index)
.map(({ item }) => item)
}, array);
}

/**
* Uses a font matching algorithm to choose from the list of given font faces,
* the most appropriate font face (if any) to use for the given text style.
*
* @see https://www.w3.org/TR/CSS2/fonts.html
*/
export function matchFontFace(fontFaces: FontFace[], textStyle: TextStyle): FontFace | undefined {
return fontFaces
.filter(ff => filterFontFamilyincludes(ff, textStyle))
.sort((a, b) => compareFontWeightDistance(a, b, textStyle))
.sort((a, b) => compareFontStyleExactMatch(a, b, textStyle))
.sort((a, b) => compareFontFamilyStartsWith(a, b, textStyle))
.find(() => true);
return stableSort(
fontFaces.filter(ff => filterFontFamilyincludes(ff, textStyle)),
(a, b) => compareFontWeightDistance(a, b, textStyle),
(a, b) => compareFontStyleExactMatch(a, b, textStyle),
(a, b) => compareFontFamilyStartsWith(a, b, textStyle)
).find(() => true)
}

function filterFontFamilyincludes(ff: FontFace, textStyle: TextStyle): boolean {
Expand Down

0 comments on commit 871ff0d

Please sign in to comment.