commonMain.net.ormr.fuzzywuzzy.ToStringFunction.kt Maven / Gradle / Ivy
package net.ormr.fuzzywuzzy
/**
* Transforms and item of type [T] to a [String].
*
* @param [T] The type of the item to transform.
*/
public fun interface ToStringFunction {
/**
* Transforms the input [item] to a [String].
*
* @param [item] the item to transform.
*
* @return a string to use for comparing the item.
*/
public operator fun invoke(item: T): String
/**
* A [ToStringFunction] that returns the input string.
*/
public data object NoProcess : ToStringFunction {
override fun invoke(item: String): String = item
}
public data object StringDefault : ToStringFunction {
// Kotlin doesn't have a flag for the 'UNICODE_CHARACTER_CLASS' like Java Pattern does,
// so we need to embed the flag directly into the unicode
// https://youtrack.jetbrains.com/issue/KT-21094
private val REGEX: Regex = try {
"""(?U)(?ui)\W""".toRegex()
} catch (e: IllegalArgumentException) {
// if parsing the (?U) flag failed, fall back to pattern without it
"""(?ui)\W""".toRegex()
}
public override fun invoke(item: String): String = item
.replaceNonAlphaNumeric(" ")
.lowercase()
.trim { it <= ' ' }
/**
* Replaces all non-alphanumeric in `this` with [sub].
*/
private fun String.replaceNonAlphaNumeric(sub: String): String = when {
REGEX.containsMatchIn(this) -> replace(REGEX, sub)
else -> this
}
}
}