fixes #1557: Filter out emojis when creating short names (#1578)

* Filter out emojis from text when finding initials

* Confirm non-English non-emoji unicde isn't filtered

* Remove unused example unit test

---------

Co-authored-by: James Rich <2199651+jamesarich@users.noreply.github.com>
pull/1556/head^2
Joshua Soberg 2025-02-15 23:25:35 -05:00 zatwierdzone przez GitHub
rodzic e11d726e27
commit 24abd1ac4a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 21 dodań i 50 usunięć

Wyświetl plik

@ -86,13 +86,13 @@ import javax.inject.Inject
import kotlin.math.roundToInt
// Given a human name, strip out the first letter of the first three words and return that as the initials for
// that user. If the original name is only one word, strip vowels from the original name and if the result is
// 3 or more characters, use the first three characters. If not, just take the first 3 characters of the
// original name.
// that user, ignoring emojis. If the original name is only one word, strip vowels from the original
// name and if the result is 3 or more characters, use the first three characters. If not, just take
// the first 3 characters of the original name.
fun getInitials(nameIn: String): String {
val nchars = 4
val minchars = 2
val name = nameIn.trim()
val name = nameIn.trim().withoutEmojis()
val words = name.split(Regex("\\s+")).filter { it.isNotEmpty() }
val initials = when (words.size) {
@ -109,6 +109,8 @@ fun getInitials(nameIn: String): String {
return initials.take(nchars)
}
private fun String.withoutEmojis(): String = filterNot { char -> char.isSurrogate() }
/**
* Builds a [Channel] list from the difference between two [ChannelSettings] lists.
* Only changes are included in the resulting list.

Wyświetl plik

@ -1,34 +0,0 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.geeksville.mesh
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}

Wyświetl plik

@ -18,22 +18,25 @@
package com.geeksville.mesh.ui
import com.geeksville.mesh.model.getInitials
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class UIUnitTest {
@Test
fun initialsGood() {
Assert.assertEquals("KH", getInitials("Kevin Hester"))
Assert.assertEquals("KHLC", getInitials(" Kevin Hester Lesser Cat "))
Assert.assertEquals("", getInitials(" "))
Assert.assertEquals("gksv", getInitials("geeksville"))
Assert.assertEquals("geek", getInitials("geek"))
Assert.assertEquals("gks1", getInitials("geeks1"))
assertEquals("KH", getInitials("Kevin Hester"))
assertEquals("KHLC", getInitials(" Kevin Hester Lesser Cat "))
assertEquals("", getInitials(" "))
assertEquals("gksv", getInitials("geeksville"))
assertEquals("geek", getInitials("geek"))
assertEquals("gks1", getInitials("geeks1"))
}
@Test
fun ignoreEmojisWhenCreatingInitials() {
assertEquals("TG", getInitials("The \uD83D\uDC10 Goat"))
assertEquals("TT", getInitials("The \uD83E\uDD14Thinker"))
assertEquals("TCH", getInitials("\uD83D\uDC4F\uD83C\uDFFFThe Clapping Hands"))
assertEquals("山羊", getInitials("山羊\uD83D\uDC10"))
}
}