Last active
February 11, 2020 07:09
-
-
Save nicemak/b49bbc64f8ffbf8e885a8dc266f208d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.text.Editable | |
import android.text.SpannableString | |
import android.text.SpannableStringBuilder | |
import android.text.TextUtils | |
import android.text.style.AbsoluteSizeSpan | |
import androidx.core.text.inSpans | |
import androidx.lifecycle.MutableLiveData | |
import java.text.DecimalFormat | |
import java.util.* | |
fun String.checkEmpty() : String = if (this.isEmpty()) "N/A" else this | |
// used for validate if the current String is an email | |
fun String.isEmailValid(): Boolean = !TextUtils.isEmpty(this) && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches() | |
fun String.toEditable(): Editable = Editable.Factory.getInstance().newEditable(this) | |
fun String.getDigits(): String = replace(Regex("\\D*"), "") | |
fun String.cleanFromSpaces() = this.replace("\\s+".toRegex(), "") | |
fun Double.formatWithTwoDecimal(): String = try { DecimalFormat("##.##").format(this) } catch (e: Exception) { this.toString() } | |
fun printFizzBuzz() | |
{ | |
println((1..100).map { i -> mapOf(0 to i, i % 3 to "Fizz", i % 5 to "Buzz", i % 15 to "FizzBuzz")[0] }) | |
} | |
fun String.capitalizeWords(): String = split(" ").map { it.toLowerCase(Locale.ENGLISH).capitalize() }.joinToString(" ") | |
fun String.isPhone(): Boolean = matches("^1([34578])\\d{9}\$".toRegex()) | |
fun String.isEmail(): Boolean = matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)\$".toRegex()) | |
fun String.isNumeric(): Boolean = matches("^[0-9]+$".toRegex()) | |
fun String.prependZero(): String = "0".plus(this) | |
fun String.replaceChars(values: Map<String,String>): String | |
{ | |
var text = this | |
values.forEach { (t, u) -> | |
text = text.replace(t, u) | |
} | |
return text | |
} | |
fun SpannableString.spanWith(target: String, apply: SpannableBuilder.() -> Unit) { | |
val builder = SpannableBuilder() | |
apply(builder) | |
val start = this.indexOf(target) | |
val end = start + target.length | |
setSpan(builder.what, start, end, builder.flags) | |
// Usage | |
/*val world = "World" | |
val mySpannedText = SpannableString("Hello ${world}!") | |
mySpannedText.spanWith(world) { | |
what = BackgroundColorSpan(Color.RED) | |
flags = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | |
}*/ | |
} | |
inline fun SpannableStringBuilder.fontSize(fontSize: Int, builderAction: SpannableStringBuilder.() -> Unit): SpannableStringBuilder { | |
return inSpans(AbsoluteSizeSpan(fontSize), builderAction = builderAction) | |
} | |
class SpannableBuilder { | |
lateinit var what: Any | |
var flags: Int = 0 | |
} | |
fun <T : Any?> mutableLiveData(defaultValue: T) = MutableLiveData<T>().apply { setValue(defaultValue) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment