all repos — gomponents @ b5675a9e960661545c38053e8a50a3f20d100a81

HTML components in pure Go

add MapIter2 for iterating over iter.Seq2 values

Alan Pearce
commit

b5675a9e960661545c38053e8a50a3f20d100a81

parent

c22895f8478dd318de71a6c16065cb1d51826c95

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

changed files
M gomponents_test.gogomponents_test.go
@@ -4,6 +4,7 @@ import (
"errors" "fmt" "io" + "maps" "os" "slices" "strings"
@@ -318,6 +319,25 @@ return g.El("li", g.Text(value))
})) _ = e.Render(os.Stdout) // Output: <ul><li>party hat</li><li>super hat</li></ul> +} + +func TestMapIter2(t *testing.T) { + t.Run("maps a iter.Seq2 of items", func(t *testing.T) { + items := maps.All(map[string]string{"party": "hat", "super": "hat"}) + e := g.El("ul", g.MapIter2(items, func(key string, value string) g.Node { + return g.El("li", g.Textf("%v: %v", key, value)) + })) + assert.Equal(t, `<ul><li>party: hat</li><li>super: hat</li></ul>`, e) + }) +} + +func ExampleMapIter2() { + items := maps.All(map[string]string{"party": "hat", "super": "hat"}) + e := g.El("ul", g.MapIter2(items, func(key string, value string) g.Node { + 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 TestGroup(t *testing.T) {