Skip to content

Added replace operation #80

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 1 commit into from
Jun 24, 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
2 changes: 2 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/OperationsProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import string.Capitalize
import string.IsBlank
import string.Length
import string.Lowercase
import string.Replace
import string.ToArray
import string.Uppercase

Expand All @@ -19,6 +20,7 @@ object OperationsProvider {
"isBlank" to IsBlank,
"length" to Length,
"lowercase" to Lowercase,
"replace" to Replace,
"uppercase" to Uppercase,
"toArray" to ToArray,

Expand Down
27 changes: 27 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/string/Replace.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package string

import operation.StandardLogicOperation
import kotlin.runCatching
import utils.asList

object Replace: StandardLogicOperation, StringUnwrapStrategy {
private const val REPLACE_CANDIDATE_INDEX = 0
private const val OLD_STRING_INDEX = 1
private const val NEW_STRING_INDEX = 2
private const val MODE_INDEX = 3

override fun evaluateLogic(expression: Any?, data: Any?): Any? =
expression.asList.runCatching {
val replaceData = ReplaceData(
get(REPLACE_CANDIDATE_INDEX) as String,
get(OLD_STRING_INDEX) as String,
get(NEW_STRING_INDEX) as String,
)
val mode = ReplaceMode.from(get(MODE_INDEX) as String, replaceData)

mode()
}.fold(
onSuccess = { it },
onFailure = { null }
)
}
47 changes: 47 additions & 0 deletions operations-stdlib/src/commonMain/kotlin/string/ReplaceMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package string

internal data class ReplaceData(
val replaceCandidate: String,
val oldString: String,
val newString: String,
)

internal sealed class ReplaceMode: ()->Any? {
abstract val replaceData: ReplaceData

companion object {
fun from(mode: String, replaceData: ReplaceData) = when {
mode == AllReplace.name -> {
AllReplace(replaceData)
}
mode.toIntOrNull() != null -> {
FewReplace(replaceData, mode.toInt())
}
else -> throw IllegalArgumentException(mode)
}
}
}

private class AllReplace(override val replaceData: ReplaceData) : ReplaceMode() {
override fun invoke() =
replaceData.replaceCandidate.replace(replaceData.oldString, replaceData.newString)

companion object {
const val name = "all"
}

}

private class FewReplace(override val replaceData: ReplaceData, val times: Int):ReplaceMode() {
override fun invoke() =
replaceData.replaceCandidate.replace(replaceData.oldString, replaceData.newString, times)
}

private fun String.replace(oldValue:String, newValue:String, times: Int) : String {
return if (times > 0) {
replaceFirst(oldValue, newValue)
.replace(oldValue, newValue, times - 1)
} else {
this
}
}
133 changes: 133 additions & 0 deletions operations-stdlib/src/commonTest/kotlin/string/ReplaceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import JsonLogicResult.Success
import JsonLogicResult.Failure
import string.Replace
import string.Trim

class ReplaceTest:FunSpec({
val operatorName = "replace"
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperations(
mapOf(
operatorName to Replace,
"trim" 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("crazy cat", "razy", "ute", "all")),
result = Success("cute cat")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat", "c", "C", "1")),
result = Success("Crazy cat")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat", "c", "C", "0")),
result = Success("crazy cat")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat", "c", "C", "2")),
result = Success("Crazy Cat")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat", "c", "C", "3")),
result = Success("Crazy Cat")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat cuts cotton clothes with success", "c", "C", "5")),
result = Success("Crazy Cat Cuts Cotton Clothes with success")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat", "cat", "hamster", "all")),
result = Success("crazy hamster")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy cat cat cat", "cat", "hamster", "all")),
result = Success("crazy hamster hamster hamster")
),
TestInput(
expression = mapOf(operatorName to listOf("", "", "hamster", "all")),
result = Success("hamster")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy hamster", "cat", "dog", "all")),
result = Success("crazy hamster")
),
TestInput(
expression = mapOf(operatorName to listOf("crazy hamster", "cat", "dog", "luke")),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(mapOf("var" to "key"), "_", "@", "all")
),
data = mapOf("key" to "_cat_"),
result = Success("@cat@")
),
TestInput(
expression = mapOf(
operatorName to listOf(mapOf("var" to "key"), "_", "@", "all")
),
data = mapOf("key1" to "_cat_"),
result = Failure.NullResult
),
TestInput(
expression = mapOf(
operatorName to listOf(
mapOf(
"trim" to listOf("cute cat ", " ", "end")
),
"cat",
"dog",
"all"
)
),
result = Success("cute dog")
),
TestInput(
expression = mapOf(operatorName to null),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(null)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to true),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(false)),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to emptyList<Any>()),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to listOf(emptyList<Any>())),
result = Failure.NullResult
),
TestInput(
expression = mapOf(operatorName to 1.3),
result = Failure.NullResult
),
)
// given
) { testInput: TestInput ->
// when
val evaluationResult = logicEngine.evaluate(testInput.expression, testInput.data)

// then
evaluationResult shouldBe testInput.result
}
})