From 712fd43fe8a1972ee33d53b2853495eeb8b07c03 Mon Sep 17 00:00:00 2001 From: Pawel Stawicki Date: Thu, 9 Sep 2021 08:39:43 +0200 Subject: [PATCH] Added HyphenNameMapperNoDigits - so it maps e.g. "implS3" to "impl-s3" instead of "impl-s-3" --- .../HyphenNameMapperNoDigits.scala | 11 ++++++ .../ficus/readers/namemappers/package.scala | 3 +- .../HyphenNameMapperNoDigitsSpec.scala | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/net/ceedubs/ficus/readers/namemappers/HyphenNameMapperNoDigits.scala create mode 100644 src/test/scala/net/ceedubs/ficus/readers/HyphenNameMapperNoDigitsSpec.scala diff --git a/src/main/scala/net/ceedubs/ficus/readers/namemappers/HyphenNameMapperNoDigits.scala b/src/main/scala/net/ceedubs/ficus/readers/namemappers/HyphenNameMapperNoDigits.scala new file mode 100644 index 0000000..2823c61 --- /dev/null +++ b/src/main/scala/net/ceedubs/ficus/readers/namemappers/HyphenNameMapperNoDigits.scala @@ -0,0 +1,11 @@ +package net.ceedubs.ficus.readers.namemappers + +import net.ceedubs.ficus.readers.NameMapper + +object HyphenNameMapperNoDigits extends NameMapper { + private lazy val r = "((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))".r + + /** Maps from a camelCasedName to a hyphenated-name + */ + override def map(name: String): String = r.replaceAllIn(name, m => s"-${m.group(1)}").toLowerCase +} diff --git a/src/main/scala/net/ceedubs/ficus/readers/namemappers/package.scala b/src/main/scala/net/ceedubs/ficus/readers/namemappers/package.scala index f1a134c..d740f63 100644 --- a/src/main/scala/net/ceedubs/ficus/readers/namemappers/package.scala +++ b/src/main/scala/net/ceedubs/ficus/readers/namemappers/package.scala @@ -2,6 +2,7 @@ package net.ceedubs.ficus.readers package object namemappers { object implicits { - implicit val hyphenCase: NameMapper = HyphenNameMapper + implicit val hyphenCase: NameMapper = HyphenNameMapper + implicit val hyphenCaseNoDigits: NameMapper = HyphenNameMapperNoDigits } } diff --git a/src/test/scala/net/ceedubs/ficus/readers/HyphenNameMapperNoDigitsSpec.scala b/src/test/scala/net/ceedubs/ficus/readers/HyphenNameMapperNoDigitsSpec.scala new file mode 100644 index 0000000..402bef4 --- /dev/null +++ b/src/test/scala/net/ceedubs/ficus/readers/HyphenNameMapperNoDigitsSpec.scala @@ -0,0 +1,35 @@ +package net.ceedubs.ficus.readers + +import net.ceedubs.ficus.Spec +import net.ceedubs.ficus.readers.namemappers.{HyphenNameMapper, HyphenNameMapperNoDigits} +import org.scalacheck.Arbitrary +import org.scalacheck.Gen._ +import org.specs2.matcher.DataTables + +class HyphenNameMapperNoDigitsSpec extends Spec with DataTables { + def is = s2""" + A HyphenNameMapper should + hyphenate a camelCased name $hyphenateCorrectly + hyphenate a camelCased name containing digits without hyphen before digit $hyphenateWithDigits + """ + + def nonemptyStringListGen = nonEmptyListOf(alphaStr.suchThat(_.length > 1).map(_.toLowerCase)) + + implicit def nonemptyStringList = Arbitrary(nonemptyStringListGen) + + def hyphenateCorrectly = prop { foos: List[String] => + val camelCased = (foos.head +: foos.tail.map(_.capitalize)).mkString + val hyphenated = foos.mkString("-").toLowerCase + + HyphenNameMapper.map(camelCased) must_== hyphenated + } + + def hyphenateWithDigits = + "camelCased" || "hyphenated" |> + "camelCasedName67" !! "camel-cased-name67" | + "1144StartsWithA32422" !! "1144-starts-with-a32422" | + "get13HTML42Snippets" !! "get13-html42-snippets" | + "thisOneIs13InThe43Middle" !! "this-one-is13-in-the43-middle" | { (camelCased, hyphenated) => + HyphenNameMapperNoDigits.map(camelCased) must_== hyphenated + } +}