Skip to content

Commit

Permalink
refactor: Update label generation in Defaultable.scala and Form.scala
Browse files Browse the repository at this point in the history
This commit refactors the label generation in Defaultable.scala and Form.scala to use the NameUtils.titleCase function. This function converts a string to title case, which is used to generate more readable labels for types and parameters.

The changes in Defaultable.scala update the label generation for the type to use the titleCase function on the class name. This improves the readability of the label.

In Form.scala, the label generation for parameters without a FieldName annotation is also updated to use the titleCase function. This ensures consistent and more readable labels for form fields.

A new utility class, NameUtils, is added to the project. This class provides the titleCase function, which splits a string into words and capitalizes the first letter of each word. This function is used in the label generation changes.

These changes improve the clarity and readability of the codebase.
  • Loading branch information
cheleb committed Oct 7, 2024
1 parent 9e8dd47 commit 8d29bac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ trait Defaultable[A] {

/** The label for the type.
*/
def label: String = default.getClass.getSimpleName()
def label: String =
NameUtils.titleCase(default.getClass.getSimpleName)
}

object Defaultable {
Expand Down
11 changes: 3 additions & 8 deletions modules/core/src/main/scala/dev/cheleb/scalamigen/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ object Form extends AutoDerivation[Form] {

val fieldName = param.annotations
.find(_.isInstanceOf[FieldName]) match
case None => titleCase(param.label)
case None => NameUtils.titleCase(param.label)
case Some(value) =>
value.asInstanceOf[FieldName].value
tr(
Expand Down Expand Up @@ -580,7 +580,7 @@ object Form extends AutoDerivation[Form] {

val fieldName = param.annotations
.find(_.isInstanceOf[FieldName]) match
case None => titleCase(param.label)
case None => NameUtils.titleCase(param.label)
case Some(value) =>
value.asInstanceOf[FieldName].value

Expand Down Expand Up @@ -648,11 +648,6 @@ object Form extends AutoDerivation[Form] {
def getSubtypeLabel[T](sub: Subtype[Typeclass, T, ?]): String =
sub.annotations
.collectFirst { case label: FieldName => label.value }
.getOrElse(titleCase(sub.typeInfo.short))

/** someParameterName -> Some Parameter Name camelCase -> Title Case
*/
private def titleCase(string: String): String =
string.split("(?=[A-Z])").map(_.capitalize).mkString(" ")
.getOrElse(NameUtils.titleCase(sub.typeInfo.short))

}
13 changes: 13 additions & 0 deletions modules/core/src/main/scala/dev/cheleb/scalamigen/NameUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.cheleb.scalamigen

object NameUtils {

/** someParameterName -> Some Parameter Name camelCase -> Title Case
*/
def titleCase(string: String): String =
string
.filter(_.isLetter)
.split("(?=[A-Z])")
.map(_.capitalize)
.mkString(" ")
}

0 comments on commit 8d29bac

Please sign in to comment.