Skip to content

Commit

Permalink
Merge pull request #699 from massimosiani/stripMargin
Browse files Browse the repository at this point in the history
add the stripMargin utility
  • Loading branch information
mpilquist authored Nov 29, 2022
2 parents 900efef + 2ee6276 commit 125b9b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions modules/core/shared/src/main/scala/Fragment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ final case class Fragment[A](
def ~[B](fb: Fragment[B]): Fragment[A ~ B] =
product(fb)

def stripMargin: Fragment[A] = stripMargin('|')

def stripMargin(marginChar: Char): Fragment[A] = {
val ps = parts.map {
_.bimap(
_.stripMargin(marginChar).replaceAll("\n", " "),
_.map(_.stripMargin(marginChar).replaceAll("\n", " "))
)
}
Fragment(ps, encoder, origin)
}

def apply(a: A): AppliedFragment =
AppliedFragment(this, a)

Expand Down
15 changes: 14 additions & 1 deletion modules/tests/shared/src/test/scala/FragmentTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,18 @@ class FragmentTest extends SkunkTest {
} yield "ok"
}
}
}

pureTest("stripMargin") {
val f = sql"""select
|$int4
|""".stripMargin
f.sql.trim == sql"select $int4".sql
}

pureTest("stripMargin with char") {
val f = sql"""select
^$int4
^""".stripMargin('^')
f.sql.trim == sql"select $int4".sql
}
}

0 comments on commit 125b9b2

Please sign in to comment.