Skip to content

Commit

Permalink
Merge pull request #472 from kevin-lee/task/466/add-toSnakeLowerCase
Browse files Browse the repository at this point in the history
Close #466 - [`extras-string`] Add case conversion - `string.toSnakeLowerCase`
  • Loading branch information
kevin-lee authored Oct 27, 2023
2 parents fbe7714 + 9f3a564 commit ebc87ce
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ object cases extends cases {
.map(_.toUpperCase(Locale.ENGLISH))
.mkString("_")

def toSnakeLowerCase: String =
s.split("[\\s_-]+")
.flatMap(_.splitByCase)
.map(_.toLowerCase(Locale.ENGLISH))
.mkString("_")

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
def splitByCase: Vector[String] = {
@scala.annotation.tailrec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package extras.strings.syntax.casesSpec.stringCaseOpsSpec

import extras.strings.syntax.casesSpec.Gens
import hedgehog._
import hedgehog.runner._

import java.util.Locale

/** @author Kevin Lee
* @since 2023-10-26
*/
object ToSnakeLowerCaseSpec extends Properties {
// String.toSnakeLowerCase

import extras.strings.syntax.cases._

def tests: List[Test] = List(
property(
"test String.toCamelCase with already snake_case",
testToSnakeLowerCaseWithAlreadySnakeCase,
),
property("test String.toSnakeLowerCase with PascalCases", testToSnakeLowerCaseWithPascalCases),
property("test String.toSnakeLowerCase with camelCases", testToSnakeLowerCaseWithCamelCases),
property("test String.toSnakeLowerCase with all UPPERCASE", testToSnakeLowerCaseWithUpperCase),
property("test String.toSnakeLowerCase with all lowercase", testToSnakeLowerCaseWithLowerCase),
property("test String.toSnakeLowerCase with all Snake_Cases", testToSnakeLowerCaseWithSnakeCases),
property("test String.toSnakeLowerCase with all lower_snake_cases", testToSnakeLowerCaseWithLowerSnakeCases),
property("test String.toSnakeLowerCase with all UPPER_SNAKE_CASES", testToSnakeLowerCaseWithUpperSnakeCases),
property("test String.toSnakeLowerCase with all Kebab-Cases", testToSnakeLowerCaseWithKebabCases),
property("test String.toSnakeLowerCase with all lower-kebab-cases", testToSnakeLowerCaseWithLowerKebabCases),
property("test String.toSnakeLowerCase with all UPPER-KEBAB-CASES", testToSnakeLowerCaseWithUpperKebabCases),
property(
"test String.toSnakeLowerCase with all space separated String values",
testToSnakeLowerCaseWithSpaceSeparatedStringValues,
),
property(
"test String.toSnakeLowerCase with all lower space separated String values",
testToSnakeLowerCaseWithLowerSpaceSeparatedStringValues,
),
property(
"test String.toSnakeLowerCase with all UPPER SPACE SEPARATED STRING VALUES",
testToSnakeLowerCaseWithUpperSpaceSeparatedStringValues,
),
)

def testToSnakeLowerCaseWithAlreadySnakeCase: Property = for {
input <- Gen
.string(Gen.lower, Range.linear(1, 10))
.list(Range.linear(1, 10))
.map(_.mkString("_"))
.log("input")
expected <- Gen.constant(input.map(_.toLower)).log("expected")
} yield {
val actual = input.toSnakeLowerCase
actual ==== expected
}

def testToSnakeLowerCaseWithPascalCases: Property = for {
ss <- Gens.genPascalCase(2, 10).list(Range.linear(1, 4)).log("ss")
input <- Gen.constant(ss.mkString).log("input")
} yield {
val expected = ss.map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithCamelCases: Property = for {
s <- Gen.string(Gen.lower, Range.linear(1, 10)).log("s")
ss <- Gens.genPascalCase(2, 10).list(Range.linear(1, 4)).log("ss")
input <- Gen.constant(s + ss.mkString).log("input")
} yield {
val expected = (s :: ss).map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithUpperCase: Property = for {
expected <- Gen.string(Gen.lower, Range.linear(1, 10)).log("expected")
input <- Gen.constant(expected.toUpperCase(Locale.ENGLISH)).log("input")
} yield {
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithLowerCase: Property = for {
input <- Gen.string(Gen.lower, Range.linear(1, 10)).log("input")
expected <- Gen.constant(input).log("expected")
} yield {
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithSnakeCases: Property = for {
ss <- Gens.genPascalCase(1, 10).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("_")).log("input")
} yield {
val expected = ss.map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithLowerSnakeCases: Property = for {
ss <- Gen.string(Gen.lower, Range.linear(1, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("_")).log("input")
} yield {
val expected = ss.mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithUpperSnakeCases: Property = for {
ss <- Gen.string(Gen.upper, Range.linear(1, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("_")).log("input")
} yield {
val expected = ss.map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithKebabCases: Property = for {
ss <- Gens.genPascalCase(1, 10).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("-")).log("input")
} yield {
val expected = ss.map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithLowerKebabCases: Property = for {
ss <- Gen.string(Gen.lower, Range.linear(1, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("-")).log("input")
} yield {
val expected = ss.mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithUpperKebabCases: Property = for {
ss <- Gen.string(Gen.upper, Range.linear(1, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant(ss.mkString("-")).log("input")
} yield {
val expected = ss.map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithSpaceSeparatedStringValues: Property = for {
s <- Gens.genPascalCase(1, 10).log("ss")
ss <- Gens.genPascalCase(2, 10).list(Range.linear(1, 4)).log("ss")
input <- Gen.constant((s :: ss).mkString(" ")).log("input")
} yield {
val expected = (s :: ss).map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithLowerSpaceSeparatedStringValues: Property = for {
s <- Gen.string(Gen.lower, Range.linear(1, 10)).log("s")
ss <- Gen.string(Gen.lower, Range.linear(2, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant((s :: ss).mkString(" ")).log("input")
} yield {
val expected = (s :: ss).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}

def testToSnakeLowerCaseWithUpperSpaceSeparatedStringValues: Property = for {
s <- Gen.string(Gen.upper, Range.linear(1, 10)).log("s")
ss <- Gen.string(Gen.upper, Range.linear(2, 10)).list(Range.linear(1, 5)).log("ss")
input <- Gen.constant((s :: ss).mkString(" ")).log("input")
} yield {
val expected = (s :: ss).map(_.toLowerCase(Locale.ENGLISH)).mkString("_")
val actual = input.toSnakeLowerCase

val info =
s"""
|> input: $input
|> actual: $actual
|> expected: $expected
|""".stripMargin

(actual ==== expected).log(info)
}
}

0 comments on commit ebc87ce

Please sign in to comment.