Skip to content
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

Added HyphenNameMapperNoDigits - it maps e.g. "implS3" to "impl-s3" #140

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}