45 lines
947 B
Swift
45 lines
947 B
Swift
public extension NodeRepresentable {
|
|
@inlinable
|
|
func repeating(_ count: Int, separator: (any NodeRepresentable)? = nil) -> any NodeRepresentable {
|
|
modifier(RepeatingNode(count: count, separator: separator))
|
|
}
|
|
}
|
|
|
|
@usableFromInline
|
|
struct RepeatingNode: NodeModifier {
|
|
|
|
@usableFromInline
|
|
let count: Int
|
|
|
|
@usableFromInline
|
|
let separator: (any NodeRepresentable)?
|
|
|
|
@usableFromInline
|
|
init(
|
|
count: Int,
|
|
separator: (any NodeRepresentable)?
|
|
) {
|
|
self.count = count
|
|
self.separator = separator
|
|
}
|
|
|
|
@usableFromInline
|
|
func render(_ node: any NodeRepresentable) -> any NodeRepresentable {
|
|
let input = node.render()
|
|
var output = input
|
|
let separator = self.separator != nil
|
|
? self.separator!.render()
|
|
: ""
|
|
|
|
guard count > 0 else { return output }
|
|
|
|
var count = count - 1
|
|
while count > 0 {
|
|
output = "\(output)\(separator)\(input)"
|
|
count -= 1
|
|
}
|
|
|
|
return output
|
|
}
|
|
}
|