Skip to content

Add Trim operation #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import string.IsBlank
import string.Length
import string.Lowercase
import string.ToArray
import string.Trim
import string.Uppercase

object OperationsProvider {
Expand All @@ -32,7 +33,8 @@ object OperationsProvider {
"joinToString" to JoinToString,

"drop" to Drop,
"reverse" to Reverse
"reverse" to Reverse,
"trim" to Trim
)

val functionalOperations: Map<String, FunctionalLogicOperation> = mutableMapOf(
Expand Down
6 changes: 3 additions & 3 deletions operations-stdlib/src/commonMain/kotlin/array/JoinToString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ object JoinToString : StandardLogicOperation {

override fun evaluateLogic(expression: Any?, data: Any?): Any? = expression.asList.toOperationArguments()?.join()

private fun JoinToStringArguments.join() =
elementsToJoin.joinToString(separator, prefix, postfix, limit, truncated)

private fun List<Any?>.toOperationArguments(): JoinToStringArguments? = runCatching {
checkLimitArg()?.let { limit ->
JoinToStringArguments(
Expand All @@ -35,6 +32,9 @@ object JoinToString : StandardLogicOperation {
private fun List<Any?>.checkLimitArg() = (get(LIMIT_ARG_INDEX) as Number).takeIf {
it.toDouble() == it.toInt().toDouble()
}?.toInt()

private fun JoinToStringArguments.join() =
elementsToJoin.joinToString(separator, prefix, postfix, limit, truncated)
}

private data class JoinToStringArguments(
Expand Down
49 changes: 49 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/string/Trim.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package string

import operation.StandardLogicOperation
import utils.asList

object Trim : StandardLogicOperation, StringUnwrapStrategy {
private const val TEXT_ARG_INDEX = 0
private const val CHAR_ARG_INDEX = 1
private const val MODE_ARG_INDEX = 2

override fun evaluateLogic(expression: Any?, data: Any?): Any? =
expression.asList.toOperationArguments()?.invokeTrim()

private fun List<Any?>.toOperationArguments(): TrimArguments? = runCatching {
TrimArguments(
text = get(TEXT_ARG_INDEX) as String,
char = (get(CHAR_ARG_INDEX) as String).single(),
mode = (get(MODE_ARG_INDEX) as String).toTrimMode()
)
}.fold(
onSuccess = { it },
onFailure = { null }
)

private fun String?.toTrimMode() = when (this) {
"start" -> TrimMode.Start
"end" -> TrimMode.End
"bothEnds" -> TrimMode.BothEnds
else -> throw IllegalStateException("Invalid TrimMode value")
}

private fun TrimArguments.invokeTrim() = when (mode) {
TrimMode.Start -> this.text.trimStart(char)
TrimMode.End -> this.text.trimEnd(char)
TrimMode.BothEnds -> this.text.trim(char)
}
}

private data class TrimArguments(
val text: String,
val char: Char,
val mode: TrimMode
)

private sealed class TrimMode {
object Start : TrimMode()
object End : TrimMode()
object BothEnds : TrimMode()
}
133 changes: 133 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/string/TrimTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import JsonLogicResult.Failure
import JsonLogicResult.Success
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import string.Trim

class TrimTest : FunSpec({
val operatorName = "trim"
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperations(
mapOf(
operatorName to Trim
)
).build()

withData(
nameFn = { input -> "Should evaluated ${input.expression} with given ${input.data} result in ${input.result}" },
ts = listOf(
TestInput(
expression = mapOf(
operatorName to listOf("_cat_", "_", "start")
),
result = Success("cat_")
),
TestInput(
expression = mapOf(
operatorName to listOf("_____cat_", "_", "start")
),
result = Success("cat_")
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_", "_", "end")
),
result = Success("_cat")
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_____", "_", "end")
),
result = Success("_cat")
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_", "_", "bothEnds")
),
result = Success("cat")
),
TestInput(
expression = mapOf(
operatorName to listOf("____cat____", "_", "bothEnds")
),
result = Success("cat")
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_", "_", "unknown")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(listOf("_cat_"), "_", "bothEnds")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(listOf("_cat_"), "_____", "bothEnds")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(listOf("_cat_"), "", "bothEnds")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_", "_")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf("_cat_")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf("")
),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(mapOf("var" to "key"), "_", "bothEnds")
),
data = mapOf("key" to "_cat_"),
result = Success("cat")
),
TestInput(
expression = mapOf(
operatorName to listOf(mapOf("var" to "key"), "_", "bothEnds")
),
data = mapOf("key" to listOf("_cat_", "_cat_")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to 1.3),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to null),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to true),
result = Failure.NullResult
),
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})