Skip to content

Commit

Permalink
use Intl.Segmenter without a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Apr 23, 2024
1 parent e87326d commit f9f62e0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-humans-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/belt': patch
---

use `Intl.Segmenter` without a fallback
14 changes: 2 additions & 12 deletions src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit f9f62e0

Please sign in to comment.