forked from ceedubs/ficus
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HyphenNameMapperNoDigits - so it maps e.g. "implS3" to "impl-s3…
…" instead of "impl-s-3" (#140)
- Loading branch information
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/scala/net/ceedubs/ficus/readers/namemappers/HyphenNameMapperNoDigits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/scala/net/ceedubs/ficus/readers/HyphenNameMapperNoDigitsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |