Add Map function (#40) `Map` makes it easier to build lists of elements without having to iterate.
1 file changed, 17 insertions(+), 0 deletions(-)
changed files
M gomponents.go → gomponents.go
@@ -217,3 +217,20 @@ // Render must happen through a parent element created with El or a helper. func Group(children []Node) Node { return group{children: children} } + +// Map something enumerable to a list of Nodes. +// Example: +// items := []string{"hat", "partyhat"} +// +// lis := g.Map(len(items), func(i int) g.Node { +// return g.El("li", g.Text(items[i])) +// }) +// +// list := g.El("ul", lis...) +func Map(length int, cb func(i int) Node) []Node { + var nodes []Node + for i := 0; i < length; i++ { + nodes = append(nodes, cb(i)) + } + return nodes +}