all repos — gomponents @ 267d40bbea6036f913c4047a6a2055b3e5d7bb96

HTML components in pure Go

Add Map function (#40) `Map` makes it easier to build lists of elements without having to iterate.

Markus Wüstenberg
commit

267d40bbea6036f913c4047a6a2055b3e5d7bb96

parent

6c8f0c235287edf7252fe239d4c9beb258c6ff01

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

changed files
M gomponents_test.gogomponents_test.go
@@ -183,3 +183,16 @@ t.FailNow()
} }) } + +func TestMap(t *testing.T) { + t.Run("maps slices to nodes", func(t *testing.T) { + items := []string{"hat", "partyhat", "turtlehat"} + lis := g.Map(len(items), func(i int) g.Node { + return g.El("li", g.Text(items[i])) + }) + + list := g.El("ul", lis...) + + assert.Equal(t, `<ul><li>hat</li><li>partyhat</li><li>turtlehat</li></ul>`, list) + }) +}