mirror of
https://github.com/bitwarden/android.git
synced 2025-03-15 18:58:59 +03:00
Add support for zero-width space (#304)
This commit is contained in:
parent
85e750cc08
commit
da6d40790e
6 changed files with 84 additions and 44 deletions
|
@ -1,7 +1,7 @@
|
|||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.util.orNullIfBlank
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.orNullIfBlank
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
package com.x8bit.bitwarden.data.platform.util
|
||||
|
||||
/**
|
||||
* Returns the original [String] only if:
|
||||
*
|
||||
* - it is non-null
|
||||
* - it is not blank (where blank refers to empty strings of those containing only white space)
|
||||
*
|
||||
* Otherwise `null` is returned.
|
||||
*/
|
||||
fun String?.orNullIfBlank(): String? =
|
||||
this?.takeUnless { it.isBlank() }
|
|
@ -7,11 +7,11 @@ import com.x8bit.bitwarden.R
|
|||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.util.orNullIfBlank
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.Text
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.isValidUri
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.orNullIfBlank
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
|
|
@ -8,6 +8,24 @@ import androidx.core.graphics.toColorInt
|
|||
import java.net.URI
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* This character takes up no space but can be used to ensure a string is not empty. It can also
|
||||
* be used to insert "safe" line-break positions in a string.
|
||||
*
|
||||
* Note: Is a string only contains this charactor, it is _not_ considered blank.
|
||||
*/
|
||||
const val ZERO_WIDTH_CHARACTER: String = "\u200B"
|
||||
|
||||
/**
|
||||
* Returns the original [String] only if:
|
||||
*
|
||||
* - it is non-null
|
||||
* - it is not blank (where blank refers to empty strings of those containing only white space)
|
||||
*
|
||||
* Otherwise [ZERO_WIDTH_CHARACTER] is returned.
|
||||
*/
|
||||
fun String?.orZeroWidthSpace(): String = this.orNullIfBlank() ?: ZERO_WIDTH_CHARACTER
|
||||
|
||||
/**
|
||||
* Whether or not string is a valid email address.
|
||||
*
|
||||
|
@ -28,6 +46,16 @@ fun String.isValidUri(): Boolean =
|
|||
false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the original [String] only if:
|
||||
*
|
||||
* - it is non-null
|
||||
* - it is not blank (where blank refers to empty strings of those containing only white space)
|
||||
*
|
||||
* Otherwise `null` is returned.
|
||||
*/
|
||||
fun String?.orNullIfBlank(): String? = this?.takeUnless { it.isBlank() }
|
||||
|
||||
/**
|
||||
* Returns the given [String] in a lowercase form using the primary [Locale] from the current
|
||||
* context.
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.x8bit.bitwarden.data.platform.base.util
|
||||
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.orNullIfBlank
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.orZeroWidthSpace
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class StringExtensionsTest {
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for a null String`() {
|
||||
assertNull((null as String?).orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for an empty String`() {
|
||||
assertNull("".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for a blank String`() {
|
||||
assertNull(" ".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns the original value for a non-blank String`() {
|
||||
assertEquals("test", "test".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns the original value for a zero-width space String`() {
|
||||
assertEquals("\u200B", "\u200B".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orZeroWidthSpace returns null for a null String`() {
|
||||
assertEquals("\u200B", null.orZeroWidthSpace())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orZeroWidthSpace returns zero-width space for an empty String`() {
|
||||
assertEquals("\u200B", "".orZeroWidthSpace())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orZeroWidthSpace returns zero-width space for a blank String`() {
|
||||
assertEquals("\u200B", " ".orZeroWidthSpace())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orZeroWidthSpace returns the original value for a non-blank string`() {
|
||||
assertEquals("test", "test".orZeroWidthSpace())
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package com.x8bit.bitwarden.data.platform.util
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class StringExtensionsTest {
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for a null String`() {
|
||||
assertNull((null as String?).orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for an empty String`() {
|
||||
assertNull("".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns null for a blank String`() {
|
||||
assertNull(" ".orNullIfBlank())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `orNullIfBlank returns the original value for a non-blank String`() {
|
||||
assertEquals(
|
||||
"test",
|
||||
"test".orNullIfBlank(),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue