-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
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
47
operations-stdlib/src/commonMain/kotlin/string/ReplaceMode.kt
This file contains hidden or 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
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
133
operations-stdlib/src/commonTest/kotlin/string/ReplaceTest.kt
This file contains hidden or 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
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 | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.