Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Oct 1, 2023
1 parent 2166e5d commit 739a077
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
18 changes: 9 additions & 9 deletions src/main/scala/casing/Casing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package casing

import scala.util.matching.Regex

protected class Casing {
class Casing {
// finds a lower char followed by an upper char and returns them as capture groups
// e.g. fooBar -> foo, Bar
private val LOWER_UPPER_REGEX: Regex = "([\\p{Ll}])(\\p{Lu})".r
private val LOWER_UPPER_REGEX: Regex = "(\\p{Ll})(\\p{Lu})".r
// finds an upper char followed by and upper and lower char and return them as capture groups
// e.g. FOOBar -> FOO, Bar
private val UPPER_UPPER_LOWER_REGEX: Regex = "(\\p{Lu})([\\p{Lu}][\\p{Ll}])".r
private val UPPER_UPPER_LOWER_REGEX: Regex = "(\\p{Lu})(\\p{Lu}\\p{Ll})".r
// finds a number followed by a letter and returns them as capture groups
private val NUMBER_LETTER_REGEX: Regex = "(\\d)(\\p{L})".r
// finds a letter followed by a number and returns them as capture groups
private val LETTER_NUMBER_REGEX: Regex = "(\\p{L})(\\d)".r
// finds all non-alphanumeric characters (including non-ASCII)
private val NON_ALPHANUMERIC_REGEXP: Regex = "[^\\p{L}\\d]+".r
// use a null character as a delimeter
private val DELIMETER = "\u0000"
// use a null character as a delimiter
private val DELIMITER = "\u0000"
// a regex replacement value that will be used to replace the matched value with
// the first capture group followed by a delimeter and the second capture group
private val REPLACEMENT_SEPARATOR: String = s"$$1$DELIMETER$$2"
// the first capture group followed by a delimiter and the second capture group
private val REPLACEMENT_SEPARATOR: String = s"$$1$DELIMITER$$2"

def caseSplit(input: String, options: SplitOptions = SplitOptions()): Seq[String] = {
var result = input
Expand All @@ -32,9 +32,9 @@ protected class Casing {
.replaceAll(LETTER_NUMBER_REGEX.pattern.pattern(), REPLACEMENT_SEPARATOR)
}

result = result.replaceAll(NON_ALPHANUMERIC_REGEXP.pattern.pattern(), DELIMETER)
result = result.replaceAll(NON_ALPHANUMERIC_REGEXP.pattern.pattern(), DELIMITER)

result.split(DELIMETER).filter(_.nonEmpty)
result.split(DELIMITER).filter(_.nonEmpty)
}

// foo bar -> fooBar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import casing._

object Example extends App {
private object Example extends App {
println(camelCase("foo_bar")) // fooBar
println(pascalCase("foo bar")) // FooBar
println(snakeCase("fooBar")) // foo_bar
Expand Down
27 changes: 14 additions & 13 deletions src/test/scala/casing/CasingTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package casing

import minitest._
import casing.test.TestUtils.expect
import casing._
import casing.TestUtils.expect

object CasingTest extends SimpleTestSuite {
test("split") {
Expand Down Expand Up @@ -47,8 +48,8 @@ object CasingTest extends SimpleTestSuite {

test("isCamelCase") {
assert(isCamelCase("fooBar"))
assert(isCamelCase("FooBar") == false)
assert(isCamelCase("foo_bar") == false)
assert(!isCamelCase("FooBar"))
assert(!isCamelCase("foo_bar"))
}

test("PascalCase") {
Expand All @@ -58,8 +59,8 @@ object CasingTest extends SimpleTestSuite {

test("isPascalCase") {
assert(isPascalCase("FooBar"))
assert(isPascalCase("fooBar") == false)
assert(isPascalCase("foo_bar") == false)
assert(!isPascalCase("fooBar"))
assert(!isPascalCase("foo_bar"))
}

test("snake_case") {
Expand All @@ -74,8 +75,8 @@ object CasingTest extends SimpleTestSuite {

test("isSnakeCase") {
assert(isSnakeCase("foo_bar"))
assert(isSnakeCase("fooBar") == false)
assert(isSnakeCase("FOO_BAR") == false)
assert(!isSnakeCase("fooBar"))
assert(!isSnakeCase("FOO_BAR"))
}

test("CONSTANT_CASE") {
Expand All @@ -90,8 +91,8 @@ object CasingTest extends SimpleTestSuite {

test("isConstantCase") {
assert(isConstantCase("FOO_BAR"))
assert(isConstantCase("fooBar") == false)
assert(isConstantCase("foo_bar") == false)
assert(!isConstantCase("fooBar"))
assert(!isConstantCase("foo_bar"))
}

test("kebab-case") {
Expand All @@ -106,8 +107,8 @@ object CasingTest extends SimpleTestSuite {

test("isKebabCase") {
assert(isKebabCase("foo-bar"))
assert(isKebabCase("FOO-BAR") == false)
assert(isKebabCase("FOO_BAR") == false)
assert(isKebabCase("fooBar") == false)
assert(!isKebabCase("FOO-BAR"))
assert(!isKebabCase("FOO_BAR"))
assert(!isKebabCase("fooBar"))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package casing.test
package casing

import minitest.api._

Expand Down

0 comments on commit 739a077

Please sign in to comment.