feat: Working on node builder
This commit is contained in:
44
Sources/CliDoc/Modifiers/Repeating.swift
Normal file
44
Sources/CliDoc/Modifiers/Repeating.swift
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user