aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/utils/Utils.scala
blob: 63b057247758b3765a241687c7a2266a9ad1387d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package xyz.driver.pdsuicommon.utils

import java.time.LocalDateTime
import java.util.regex.{Matcher, Pattern}

object Utils {

  implicit val localDateTimeOrdering: Ordering[LocalDateTime] = Ordering.fromLessThan(_ isBefore _)

  /**
    * Hack to avoid scala compiler bug with getSimpleName
    * @see https://issues.scala-lang.org/browse/SI-2034
    */
  def getClassSimpleName(klass: Class[_]): String = {
    try {
      klass.getSimpleName
    } catch {
      case _: InternalError =>
        val fullName      = klass.getName.stripSuffix("$")
        val fullClassName = fullName.substring(fullName.lastIndexOf(".") + 1)
        fullClassName.substring(fullClassName.lastIndexOf("$") + 1)
    }
  }

  def toSnakeCase(str: String): String =
    str
      .replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2")
      .replaceAll("([a-z\\d])([A-Z])", "$1_$2")
      .toLowerCase

  def toCamelCase(str: String): String = {
    val sb = new StringBuffer()
    def loop(m: Matcher): Unit = if (m.find()) {
      m.appendReplacement(sb, m.group(1).toUpperCase())
      loop(m)
    }
    val m: Matcher = Pattern.compile("_(.)").matcher(str)
    loop(m)
    m.appendTail(sb)
    sb.toString
  }

}