all repos — gomponents @ 85fff9ebf5a3177d8c4e48d7314e402c0be27cee

HTML components in pure Go

Add MapIter to map over iterators

Alan Pearce
commit

85fff9ebf5a3177d8c4e48d7314e402c0be27cee

parent

7ada5cf0a0ac21780e8ef5abd4be308446fa3110

1 file changed, 18 insertions(+), 0 deletions(-)

changed files
M gomponents_test.gogomponents_test.go
@@ -5,6 +5,7 @@ "errors"
"fmt" "io" "os" + "slices" "strings" "testing"
@@ -300,6 +301,23 @@ return g.El("li", g.Textf("%v: %v", key, value))
})) _ = e.Render(os.Stdout) // Output: <ul><li>party: hat</li><li>super: hat</li></ul> +} + +func TestMapIter(t *testing.T) { + items := slices.Values([]string{"party hat", "super hat"}) + e := g.El("ul", g.MapIter(items, func(value string) g.Node { + return g.El("li", g.Text(value)) + })) + assert.Equal(t, `<ul><li>party hat</li><li>super hat</li></ul>`, e) +} + +func ExampleMapIter() { + items := slices.Values([]string{"party hat", "super hat"}) + e := g.El("ul", g.MapIter(items, func(value string) g.Node { + return g.El("li", g.Text(value)) + })) + _ = e.Render(os.Stdout) + // Output: <ul><li>party hat</li><li>super hat</li></ul> } func TestGroup(t *testing.T) {