kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
* 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
rodzic
e11d726e27
commit
24abd1ac4a
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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"))
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue