From f9f62e072a66240b7d5add861ca7334ac96faff3 Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Mon, 22 Apr 2024 19:16:49 -0600 Subject: [PATCH] use `Intl.Segmenter` without a fallback --- .changeset/nice-humans-help.md | 5 +++++ src/lib/string.ts | 14 ++------------ 2 files changed, 7 insertions(+), 12 deletions(-) create mode 100644 .changeset/nice-humans-help.md 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;