diff --git a/.changeset/nice-humans-help.md b/.changeset/nice-humans-help.md new file mode 100644 index 00000000..6c8b3218 --- /dev/null +++ b/.changeset/nice-humans-help.md @@ -0,0 +1,5 @@ +--- +'@ryanatkn/belt': patch +--- + +use `Intl.Segmenter` without a fallback diff --git a/src/lib/string.ts b/src/lib/string.ts index d4e1fa21..d1a215bc 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -68,16 +68,6 @@ export const plural = (count: number | undefined | null, suffix = 's'): string = /** * Returns the count of graphemes in a string, the individually rendered characters. - * @param str */ -export const to_grapheme_count = (str: string): number => { - try { - return [...new Intl.Segmenter().segment(str)].length; - } catch (err) { - // TODO The fallback code here is very broken, - // and currently returns down to 1/8 the correct number of graphemes. - // Remove it when Intl.Segmenter is supported in Firefox: - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter - return Math.ceil([...str].length / 8); - } -}; +export const to_grapheme_count = (str: string): number => + [...new Intl.Segmenter().segment(str)].length;