Skip to content

Commit

Permalink
chore: add comment to explain why counting graphemes
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Montagnin <[email protected]>
  • Loading branch information
RiccardoM committed Feb 13, 2024
1 parent 173288b commit 036ca6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions x/posts/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (p Post) GetMentionedUsers() []string {

// GetTextLength returns the length of the post text
func (p Post) GetTextLength() int {
// Counting graphemes instead of runes of bytes can provide a more accurate length of the text.
// This will also ensure that emojis are counted as a single character, which will grant a more consistent
// user experience with clients as well.
// Example: 🏳️‍🌈 (rainbow flag emoji) is 1 grapheme, 4 runes, and 14 bytes.
return uniseg.GraphemeClusterCount(p.Text)
}

Expand Down
4 changes: 4 additions & 0 deletions x/reactions/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (v *FreeTextValue) isReactionValue() {}

// GetLength returns the length of the reaction value
func (v *FreeTextValue) GetLength() int {
// Counting graphemes instead of runes of bytes can provide a more accurate length of the text.
// This will also ensure that emojis are counted as a single character, which will grant a more consistent
// user experience with clients as well.
// Example: 🏳️‍🌈 (rainbow flag emoji) is 1 grapheme, 4 runes, and 14 bytes.
return uniseg.GraphemeClusterCount(v.Text)

}
Expand Down

0 comments on commit 036ca6b

Please sign in to comment.