Skip to content

Commit

Permalink
Add some name conversions to CName
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Jan 20, 2019
1 parent 8bb207a commit a57f733
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
Expand Up @@ -66,8 +66,6 @@ object CName {
varName
} else {
def translate(varName: String) = {
//var components = Array[String]()

def wikiNameComponents: List[String] = {
findWikiNameComponent(0)
}
Expand Down Expand Up @@ -142,4 +140,38 @@ class CName(val canonicalName: String, val naturalName: String) extends Comparab
}
}

lazy val snakeCase: String = naturalName.toLowerCase.replace(' ', '_')
lazy val dashCase: String = naturalName.toLowerCase.replace(' ', '-')
lazy val upperCamelCase: String = {
val sb = new StringBuilder()
var prevIsWhitespace = false
naturalName.toLowerCase.map { c =>
if (c != ' ') {
if (sb.length == 0 || prevIsWhitespace) {
sb.append(c.toUpper)
} else {
sb.append(c)
}
}
prevIsWhitespace = (c == ' ')
}
sb.toString
}

lazy val lowerCamelCase: String = {
val sb = new StringBuilder()
var prevIsWhitespace = false
naturalName.toLowerCase.map { c =>
if (c != ' ') {
if (prevIsWhitespace) {
sb.append(c.toUpper)
} else {
sb.append(c)
}
}
prevIsWhitespace = (c == ' ')
}
sb.toString
}

}
@@ -0,0 +1,53 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.surface

import wvlet.airframe.AirframeSpec

class CNameTest extends AirframeSpec {

"CName" should {
"convert to snakeCase" in {
assert(CName("AirframeSurface").snakeCase == "airframe_surface")
assert(CName("airframe_surface").snakeCase == "airframe_surface")
assert(CName("airframe-surface").snakeCase == "airframe_surface")
assert(CName("airframeSurface").snakeCase == "airframe_surface")
assert(CName("Airframe Surface").snakeCase == "airframe_surface")
}

"convert to dashCase" in {
assert(CName("AirframeSurface").dashCase == "airframe-surface")
assert(CName("airframe_surface").dashCase == "airframe-surface")
assert(CName("airframe-surface").dashCase == "airframe-surface")
assert(CName("airframeSurface").dashCase == "airframe-surface")
assert(CName("Airframe Surface").dashCase == "airframe-surface")
}

"convert to .upperCamelCase" in {
assert(CName("AirframeSurface").upperCamelCase == "AirframeSurface")
assert(CName("airframe_surface").upperCamelCase == "AirframeSurface")
assert(CName("airframe-surface").upperCamelCase == "AirframeSurface")
assert(CName("airframeSurface").upperCamelCase == "AirframeSurface")
assert(CName("Airframe Surface").upperCamelCase == "AirframeSurface")
}

"convert to lowerCamelCase" in {
assert(CName("AirframeSurface").lowerCamelCase == "airframeSurface")
assert(CName("airframe_surface").lowerCamelCase == "airframeSurface")
assert(CName("airframe-surface").lowerCamelCase == "airframeSurface")
assert(CName("airframeSurface").lowerCamelCase == "airframeSurface")
assert(CName("Airframe Surface").lowerCamelCase == "airframeSurface")
}
}
}

0 comments on commit a57f733

Please sign in to comment.