Skip to content

Commit

Permalink
Added HyphenNameMapperNoDigits - so it maps e.g. "implS3" to "impl-s3…
Browse files Browse the repository at this point in the history
…" instead of "impl-s-3" (#140)
  • Loading branch information
amorfis authored Sep 9, 2021
1 parent f14bd7c commit 537fd6f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 537fd6f

Please sign in to comment.