feat(wallet) skip picking flags in the random emoji picker function

Also add a test to validate the knowledge of the emoji picker function
about flags position

Closes #13715
This commit is contained in:
Stefan 2024-03-19 22:03:16 +02:00 committed by Stefan Dunca
parent 631ae08a8d
commit a09defbef7
2 changed files with 47 additions and 13 deletions

View File

@ -63,7 +63,7 @@ QtObject {
function fromCodePoint(value) { function fromCodePoint(value) {
return Twemoji.twemoji.convert.fromCodePoint(value) return Twemoji.twemoji.convert.fromCodePoint(value)
} }
// This regular expression looks for html tag `img` with following attributes in any order: // This regular expression looks for html tag `img` with following attributes in any order:
// - `src` containig with "/assets/twemoji/" substring // - `src` containig with "/assets/twemoji/" substring
// - `alt` (this one is captured) // - `alt` (this one is captured)
@ -125,22 +125,28 @@ QtObject {
return undefined return undefined
} }
function getRandomEmoji(size) { // Used to exclude flags emojis from the random emoji picker
var randomEmoji = EmojiJSON.emoji_json[Math.floor(Math.random() * EmojiJSON.emoji_json.length)] // Based on the knowledge that flags emojis are contiguous in the emoji list
readonly property int firstFlagIndex: 1003
readonly property int lastFlagIndex: 1259
readonly property int flagsCount: lastFlagIndex - firstFlagIndex + 1
const extenstionIndex = randomEmoji.unicode.lastIndexOf('.'); // Returns a random emoji excluding flags emojis
function getRandomEmoji(size) {
let whitelistedIndex = Math.floor(Math.random() * (EmojiJSON.emoji_json.length - flagsCount))
// Compensating for the missing flags emojis index
if (whitelistedIndex >= firstFlagIndex) {
whitelistedIndex += flagsCount
}
var randomEmoji = EmojiJSON.emoji_json[whitelistedIndex]
const extensionIndex = randomEmoji.unicode.lastIndexOf('.');
let iconCodePoint = randomEmoji.unicode let iconCodePoint = randomEmoji.unicode
if (extenstionIndex > -1) { if (extensionIndex > -1) {
iconCodePoint = iconCodePoint.substring(0, extenstionIndex) iconCodePoint = iconCodePoint.substring(0, extensionIndex)
} }
// Split the unicode to get all the parts and then encode them from hex to utf8 const encodedIcon = getEmojiCodepoint(iconCodePoint)
const splitCodePoint = iconCodePoint.split('-')
let codePointParts = []
splitCodePoint.forEach(function (codePoint) {
codePointParts.push(`0x${codePoint}`)
})
const encodedIcon = String.fromCodePoint(...codePointParts);
// Adding a space because otherwise, some emojis would fuse since emoji is just a string // Adding a space because otherwise, some emojis would fuse since emoji is just a string
return Emoji.parse(encodedIcon, size || undefined) + ' ' return Emoji.parse(encodedIcon, size || undefined) + ' '

View File

@ -0,0 +1,28 @@
import QtQuick 2.15
import QtTest 1.0
import StatusQ.Core.Utils 0.1
TestCase {
id: testCase
name: "TestEmoji"
// Test if the knowledge of the first and last index of the flags is still valid or need to be updated
function test_flags_indexes_are_valid() {
let emojis = Emoji.emojiJSON.emoji_json
let firstIndex = emojis.findIndex(function(emoji) {
return (emoji.category === "flags")
})
compare(Emoji.firstFlagIndex, firstIndex, "First flag index is still valid")
let lastIndex = -1;
for (let i = emojis.length - 1; i >= 0; i--) {
if (emojis[i].category === "flags") {
lastIndex = i;
break; // Exit the loop once the last flag is found
}
}
compare(Emoji.lastFlagIndex, lastIndex, "Last flag index is still valid")
}
}