-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from evolution-gaming/coding-strategy-refactor
Improved name coding strategy
- Loading branch information
Showing
4 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 17 additions & 9 deletions
26
play-json-generic/src/main/scala/com/evolutiongaming/util/generic/NameCodingStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
package com.evolutiongaming.util.generic | ||
|
||
trait NameCodingStrategy { | ||
def encode(s: String): String | ||
def decode(s: String): String | ||
def encode(input: String): String | ||
def matches(encoded: String, compareAgainst: String): Boolean | ||
} | ||
|
||
trait LowPriority { | ||
implicit val default: NameCodingStrategy = new NameCodingStrategy { | ||
override def encode(s: String): String = s | ||
override def decode(s: String): String = s | ||
override def encode(input: String): String = input | ||
override def matches(encoded: String, compareAgainst: String): Boolean = encoded == compareAgainst | ||
} | ||
} | ||
|
||
object NameCodingStrategy extends LowPriority | ||
|
||
object NameCodingStrategies { | ||
implicit val kebabCase: NameCodingStrategy = new NameCodingStrategy { | ||
override def encode(s: String): String = | ||
s.foldLeft(List.empty[String]) { | ||
private def lowerCaseSepCoding(sep: String): NameCodingStrategy = new NameCodingStrategy { | ||
|
||
override def encode(input: String): String = | ||
input.foldLeft(List.empty[String]) { | ||
case (ll, n) if n.isUpper => | ||
n.toLower.toString :: ll | ||
case (h :: t, n) => | ||
h + n :: t | ||
case (nil, n) => | ||
n.toString :: nil | ||
}.reverse.mkString("-") | ||
}.reverse.mkString(sep) | ||
|
||
override def decode(s: String): String = s.split("-").map(_.capitalize).mkString("") | ||
override def matches(encoded: String, compareAgainst: String): Boolean = | ||
encoded == encode(compareAgainst) | ||
} | ||
|
||
implicit val kebabCase: NameCodingStrategy = lowerCaseSepCoding("-") | ||
|
||
implicit val snakeCase: NameCodingStrategy = lowerCaseSepCoding("_") | ||
|
||
implicit val noSepCase: NameCodingStrategy = lowerCaseSepCoding("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version in ThisBuild := "0.2.1-SNAPSHOT" | ||
version in ThisBuild := "0.2.1" |