Skip to content

Commit

Permalink
Add .repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jul 31, 2024
1 parent 5160ae1 commit bc1c84b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import eu.iamgio.quarkdown.function.value.OutputValue
import eu.iamgio.quarkdown.function.value.Value
import eu.iamgio.quarkdown.function.value.VoidValue
import eu.iamgio.quarkdown.function.value.data.Lambda
import eu.iamgio.quarkdown.function.value.data.Range
import eu.iamgio.quarkdown.function.value.wrappedAsValue

/**
Expand All @@ -27,6 +28,7 @@ val Flow: Module =
::`if`,
::ifNot,
::forEach,
::repeat,
::function,
::variable,
::let,
Expand Down Expand Up @@ -79,6 +81,19 @@ fun forEach(
return GeneralCollectionValue(values)
}

/**
* Repeats content `N` times.
* The current index (starting from 1) can be accessed via the lambda argument.
* This is shorthand for `foreach {..N} {body}`.
* @param times amount of times to repeat the content
* @param body content, output of each iteration. Accepts 1 parameter (the current element).
* @return a collection that contains the output of each iteration
*/
fun repeat(
times: Int,
body: Lambda,
): IterableValue<OutputValue<*>> = forEach(Range(1, times), body)

/**
* Custom functions (via [function]) and variables (via [variable]) are saved in a [Library]
* whose name begins by this string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ class FullPipelineTest {
assertEquals("<h2>Hello 1</h2><p><strong>Hi</strong>!</p><h2>Hello 2</h2><p><strong>Hi</strong>!</p>", it)
}

execute(
"""
.repeat {2}
## Hello .1
.repeat {1}
**Hi**!
""".trimIndent(),
) {
assertEquals("<h2>Hello 1</h2><p><strong>Hi</strong>!</p><h2>Hello 2</h2><p><strong>Hi</strong>!</p>", it)
}

execute(
"""
.foreach {..2}
Expand Down Expand Up @@ -535,7 +546,7 @@ class FullPipelineTest {
layoutFunction +
"""
.foreach {..4}
.repeat {4}
n:
.mylayout {world} {.n}
""".trimIndent(),
Expand Down

0 comments on commit bc1c84b

Please sign in to comment.