diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index d1eb1570b..a9598e249 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -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. diff --git a/app/src/test/java/com/geeksville/mesh/ExampleUnitTest.kt b/app/src/test/java/com/geeksville/mesh/ExampleUnitTest.kt deleted file mode 100644 index b184cc8cc..000000000 --- a/app/src/test/java/com/geeksville/mesh/ExampleUnitTest.kt +++ /dev/null @@ -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 . - */ - -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) - } -} diff --git a/app/src/test/java/com/geeksville/mesh/ui/UIUnitTest.kt b/app/src/test/java/com/geeksville/mesh/ui/UIUnitTest.kt index dbbf6f51f..fb4ed6b54 100644 --- a/app/src/test/java/com/geeksville/mesh/ui/UIUnitTest.kt +++ b/app/src/test/java/com/geeksville/mesh/ui/UIUnitTest.kt @@ -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")) } }