Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Snakify now handles abbreviations. Closes #381

  • Loading branch information...
commit 606ec2698a4985e5d37d52b5df4f8b1a7eda2fe3 1 parent 88bede6
Jeppe Nejsum Madsen jeppenejsum authored
18 framework/lift-base/lift-util/src/main/scala/net/liftweb/util/StringHelpers.scala
@@ -77,20 +77,14 @@ trait StringHelpers {
77 77
78 78 /**
79 79 * Turn a string of format "FooBar" into snake case "foo_bar"
80   - *
  80 + *
  81 + * Note: snakify is not reversible, ie. in general the following will _not_ be true:
  82 + *
  83 + * s == camelify(snakify(s))
  84 + *
81 85 * @return the underscored string
82 86 */
83   - def snakify(name : String) = {
84   - def loop(x : List[Char]) : List[Char] = x match {
85   - case c :: rest if (Character.isUpperCase(c)) => '_' :: Character.toLowerCase(c) :: loop(rest)
86   - case c :: rest => c :: loop(rest)
87   - case Nil => Nil
88   - }
89   - if (name.isEmpty)
90   - ""
91   - else
92   - (Character.toLowerCase(name.charAt(0)) :: loop(name.substring(1).toList)).mkString
93   - }
  87 + def snakify(name : String) = name.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z\\d])([A-Z])", "$1_$2").toLowerCase
94 88
95 89
96 90 /**
29 framework/lift-base/lift-util/src/test/scala/net/liftweb/util/StringHelpersSpec.scala
@@ -27,11 +27,34 @@ import _root_.org.specs.ScalaCheck
27 27 import common._
28 28
29 29 object StringHelpersSpec extends Specification with StringHelpers with StringGenerators with ScalaCheck {
30   - "The unCamelCase function" should {
31   - "Uncamel a name, replacing upper cases with underscores" in {
32   - forAllProp(camelCasedStrings)((name: String) => camelify(snakify(name)) == name) must pass
  30 + "The snakify function" should {
  31 + "replace upper case with underscore" in {
  32 + snakify("MyCamelCase") must_== "my_camel_case"
  33 + snakify("CamelCase") must_== "camel_case"
  34 + snakify("Camel") must_== "camel"
  35 + snakify("MyCamel12Case") must_== "my_camel12_case"
  36 + snakify("CamelCase12") must_== "camel_case12"
  37 + snakify("Camel12") must_== "camel12"
  38 + }
  39 +
  40 + "not modify existing snake case strings" in {
  41 + snakify("my_snake_case") must_== "my_snake_case"
  42 + snakify("snake") must_== "snake"
  43 + }
  44 +
  45 + "handle abbeviations" in {
  46 + snakify("HTML") must_== "html"
  47 + snakify("HTMLEditor") must_== "html_editor"
  48 + snakify("EditorTOC") must_== "editor_toc"
  49 + snakify("HTMLEditorTOC") must_== "html_editor_toc"
  50 +
  51 + snakify("HTML5") must_== "html5"
  52 + snakify("HTML5Editor") must_== "html5_editor"
  53 + snakify("Editor2TOC") must_== "editor2_toc"
  54 + snakify("HTML5Editor2TOC") must_== "html5_editor2_toc"
33 55 }
34 56 }
  57 +
35 58 "The camelify function" should {
36 59 "CamelCase a name which is underscored, removing each underscore and capitalizing the next letter" in {
37 60 def previousCharacterIsUnderscore(name: String, i: Int) = i > 1 && name.charAt(i - 1) == '_'

0 comments on commit 606ec26

Please sign in to comment.
Something went wrong with that request. Please try again.