From 036ca6bb52fe110bb83faef173764f5d9b1f5f82 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Tue, 13 Feb 2024 12:20:35 +0100 Subject: [PATCH] chore: add comment to explain why counting graphemes Signed-off-by: Riccardo Montagnin --- x/posts/types/models.go | 4 ++++ x/reactions/types/models.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/x/posts/types/models.go b/x/posts/types/models.go index a05ec7eafa..05e1378315 100644 --- a/x/posts/types/models.go +++ b/x/posts/types/models.go @@ -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) } diff --git a/x/reactions/types/models.go b/x/reactions/types/models.go index caf68d091d..7ce5ea7353 100644 --- a/x/reactions/types/models.go +++ b/x/reactions/types/models.go @@ -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) }