Skip to content

Commit

Permalink
Add "lowercase" option to the replace derivation method
Browse files Browse the repository at this point in the history
I'm sneaking this in without bumping the format version as this I don't
expect custom repo templates to exist at this point, and this behavior
 is kind of unexpected in the first place
  • Loading branch information
RedNesto committed Aug 9, 2024
1 parent 0fcf7a7 commit b989f7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ class ReplacePropertyDerivation(
val regex: Regex,
val replacement: String,
val maxLength: Int?,
val lowercase: Boolean,
) : PreparedDerivation {

override fun derive(parentValues: List<Any?>): Any? {
val projectName = parentValues.first() as? String
?: return null

val sanitized = projectName.lowercase().replace(regex, replacement)
var sanitized = projectName
if (lowercase) {
sanitized = sanitized.lowercase()
}

sanitized = sanitized.replace(regex, replacement)
if (maxLength != null && sanitized.length > maxLength) {
return sanitized.substring(0, maxLength)
}
Expand Down Expand Up @@ -88,7 +94,8 @@ class ReplacePropertyDerivation(
}

val maxLength = (derivation.parameters["maxLength"] as? Number)?.toInt()
return ReplacePropertyDerivation(regex, replacement, maxLength)
val lowercase = derivation.parameters["lowercase"] as? Boolean == true
return ReplacePropertyDerivation(regex, replacement, maxLength, lowercase)
}
}
}
3 changes: 2 additions & 1 deletion src/test/kotlin/creator/StringCreatorPropertyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class StringCreatorPropertyTest : CreatorTemplateProcessorTestBase() {
"parameters": {
"regex": "[^a-z0-9-_]+",
"replacement": "_",
"maxLength": 32
"maxLength": 32,
"lowercase": true
}
}
}
Expand Down

0 comments on commit b989f7d

Please sign in to comment.