feat: Working on node builder

This commit is contained in:
2024-12-01 11:45:05 -05:00
parent 55d8888961
commit ff49b12198
20 changed files with 495 additions and 227 deletions

View File

@@ -0,0 +1,44 @@
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
}
}