-
Notifications
You must be signed in to change notification settings - Fork 0
/
many.go
36 lines (34 loc) · 969 Bytes
/
many.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package html
// ForEach applies the view function to each item, then returns a node
// containing the combined results.
//
// Example Usage:
//
// fruits := []string{"apple", "bannana", "orange"}
// list := tag.Ul(ForEach(fruits, func(fruit string) Node {
// return tag.Li(InnerText(fruit))
// })
// list.String() == "<ul><li>apple</li><li>bannana</li><li>orange</li></ul>"
func ForEach[T any](items []T, view func(T) Node) Node {
results := make([]Node, len(items))
for i := range items {
results[i] = view(items[i])
}
return Node{
nodeType: nodeTypeMany,
data: results,
}
}
// Combine multiple Nodes into a single Node. This can be useful for grouping
// nodes without adding a wrapping element.
//
// Example Usage:
//
// c := Combine(tag.Div(InnerText("hello")), tag.Div(InnerText("world")))
// c.String() == "<div>hello</div><div>world</div>"
func Combine(options ...Node) Node {
return Node{
nodeType: nodeTypeMany,
data: options,
}
}