Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/io/taig/babel/StringFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object StringFormat {
}
}

private[babel] def decoder[A](n: Int)(f: (String, Map[Int, String]) => A): Decoder[A] = Decoder[String].emap {
private[babel] def decoder[A](n: Int)(f: (String, List[(Int, String)]) => A): Decoder[A] = Decoder[String].emap {
(value, path) =>
val format = parse(value)
val indices = format.segments.map { case (index, _) => index }
Expand All @@ -48,10 +48,10 @@ object StringFormat {
val offenders = duplicates.mkString(", ")
val message = s"StringFormat$n may not have duplicate indices, found duplicates for $offenders"
Left(Decoder.Error(message, path, cause = None))
} else Right(f(format.head, format.segments.toMap))
} else Right(f(format.head, format.segments))
}

private[babel] def build(head: String, segments: Map[Int, String], values: Vector[String]): String = {
private[babel] def build(head: String, segments: List[(Int, String)], values: Vector[String]): String = {
val builder = new StringBuilder(head)

segments.foreach { case (index, segment) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,34 @@ final class StringFormatTest extends FunSuite {
test("decoder has deuplicate placeholders") {
assert(Decoder[StringFormat2].decode(Babel.Value("lorem {0} dolar {0} amet")).isLeft)
}

test("decoder with 5+ placeholders preserves segment order") {
val format = Decoder[StringFormat6].decode(
Babel.Value("a {0} b {1} c {2} d {3} e {4} f {5} g")
)
assertEquals(
obtained = format.map(_.apply("V0", "V1", "V2", "V3", "V4", "V5")),
expected = Right("a V0 b V1 c V2 d V3 e V4 f V5 g")
)
}

test("decoder with 5+ placeholders out of order") {
val format = Decoder[StringFormat6].decode(
Babel.Value("{5} a {3} b {1} c {4} d {0} e {2} f")
)
assertEquals(
obtained = format.map(_.apply("V0", "V1", "V2", "V3", "V4", "V5")),
expected = Right("V5 a V3 b V1 c V4 d V0 e V2 f")
)
}

test("decoder with 8 placeholders") {
val format = Decoder[StringFormat8].decode(
Babel.Value("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}")
)
assertEquals(
obtained = format.map(_.apply("a", "b", "c", "d", "e", "f", "g", "h")),
expected = Right("a-b-c-d-e-f-g-h")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ final class DerivationTest extends FunSuite {
val foo: Foo = {

val messages: Quantities[StringFormat1] = {
val sfOne: StringFormat1 = (v0: String) => StringFormat.build("You clicked ", Map(0 -> " time"), Vector(v0))
val sfMany: StringFormat1 = (v0: String) => StringFormat.build("You clicked ", Map(0 -> " times"), Vector(v0))
val sfOne: StringFormat1 = (v0: String) => StringFormat.build("You clicked ", List(0 -> " time"), Vector(v0))
val sfMany: StringFormat1 = (v0: String) => StringFormat.build("You clicked ", List(0 -> " times"), Vector(v0))
Quantities.from[StringFormat1](sfMany, List(Quantities.Element(Quantity.One, sfOne)))
}

Expand Down