invirt.utils.strings.kt Maven / Gradle / Ivy
package invirt.utils
import java.net.URLEncoder
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.regex.Pattern
import kotlin.math.ceil
val REGEX_NON_ALPHA = "[^a-zA-Z\\d]+".toRegex()
val REGEX_WHITESPACE = "\\s+".toRegex()
val REGEX_ALPHA_AND_DASH_STARTS_WITH_LETTER = "[a-z][a-z\\d-]+".toRegex()
private val REGEX_WORD = "\\W+".toRegex()
fun String.wordCount(): Int {
return this.split(REGEX_WORD)
.filter { it.isNotBlank() }
.size
}
private val randomStringChars = ('a'..'z') + ('0'..'9')
fun randomString(length: Int): String {
return (1..length)
.map { randomStringChars.random() }
.joinToString("")
}
fun String.urlName(maxLength: Int = 90): String {
val text = this.lowercase()
.replace(REGEX_NON_ALPHA, " ")
.cleanWhitespace()
.split(" ")
.joinToString("-")
val chopped = if (text.length < maxLength) {
text
} else {
text.substring(0, text.indexOf('-', maxLength - 1))
}
return chopped + "-" + randomString(8)
}
/**
* The reading time in minutes for an (Int) word count
*/
fun readingTimeMinutes(wordCount: Int): Int {
return ceil(wordCount / 265.0)
.toInt()
.coerceAtLeast(1)
}
fun String.urlEncode(charset: Charset = StandardCharsets.UTF_8): String {
return URLEncoder.encode(this, charset.name())
}
fun String.cleanWhitespace(): String {
return replace(REGEX_WHITESPACE, " ").trim()
}
fun String.titleCaseFirstChar(locale: Locale = Locale.getDefault()): String {
return replaceFirstChar { it.titlecase(locale) }
}
fun String.cleanEmail(): String {
return cleanWhitespace().lowercase()
}
fun String.ellipsis(maxLength: Int): String {
return if (this.length > maxLength) {
this.take(maxLength - 3) + "..."
} else {
this
}
}
fun String?.nullIfBlank(): String? {
return if (this != null && this.isBlank()) {
null
} else {
this
}
}
fun String.anonymizeEmail(visibleEndChars: Int = 0): String {
val elements = this.split("@")
return elements[0].anonymize(visibleEndChars) + "@" + elements[1].anonymize(visibleEndChars)
}
fun String.anonymize(visibleEndChars: Int = 0): String {
val visibleStartIndex = (this.length - visibleEndChars).coerceAtLeast(0)
return String(this
.toCharArray()
.mapIndexed { index, char ->
if (index < visibleStartIndex) '*' else char
}
.toCharArray()
)
}
fun > Enum.enumLabel(): String {
return this.name.enumLabel()
}
fun String.enumLabel(): String {
return this.replace("_", " ").lowercase().titleCaseFirstChar()
}
private val PATTERN_KEBAB = Pattern.compile("-([a-z])")
private val REGEX_CAMEL = "([a-z0-9])([A-Z])".toRegex()
fun String.camelToKebabCase(): String {
return this.replace(REGEX_CAMEL, "$1-$2").lowercase()
}
fun String.kebabToCamelCase(): String {
return PATTERN_KEBAB
.matcher(this)
.replaceAll { mr -> mr.group(1).uppercase() }
}
private val DOMAIN_COLON_SLASHES = ".*://".toRegex()
private val DOMAIN_PORT = ":.*".toRegex()
private val DOMAIN_REST_OF_PATH = "/.*".toRegex()
fun String.domain(): String {
return this
.lowercase()
.replace(DOMAIN_COLON_SLASHES, "")
.replace(DOMAIN_PORT, "")
.replace(DOMAIN_REST_OF_PATH, "")
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy