Repository: maragudk/gomponents
Pull request #40 (2020-11-02 09:44:57)

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

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=h1) Report
> Merging [#40](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=desc) into [master](https://codecov.io/gh/maragudk/gomponents/commit/6c8f0c235287edf7252fe239d4c9beb258c6ff01?el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/maragudk/gomponents/pull/40/graphs/tree.svg?width=650&height=150&src=pr&token=JYN032TYAC)](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #40   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            8         8           
  Lines          236       239    +3     
=========================================
+ Hits           236       239    +3     
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [gomponents.go](https://codecov.io/gh/maragudk/gomponents/pull/40/diff?src=pr&el=tree#diff-Z29tcG9uZW50cy5nbw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=footer). Last update [6c8f0c2...d2fbbbb](https://codecov.io/gh/maragudk/gomponents/pull/40?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/examples/simple/simple.go b/examples/simple/simple.go
index 3802f43..dd6dd58 100644
--- a/examples/simple/simple.go
+++ b/examples/simple/simple.go
@@ -61,11 +61,11 @@ func navbar(props navbarProps) g.Node {
 		{"/foo", "Foo"},
 		{"/bar", "Bar"},
 	}
-	var lis []g.Node
-	for _, item := range items {
-		lis = append(lis, el.Li(el.A(item.path,
-			attr.Classes(map[string]bool{"is-active": props.path == item.path}),
-			g.Text(item.text))))
-	}
+	lis := g.Map(len(items), func(i int) g.Node {
+		item := items[i]
+		return el.Li(
+			el.A(item.path, attr.Classes(map[string]bool{"is-active": props.path == item.path}), g.Text(item.text)),
+		)
+	})
 	return el.Ul(attr.Class("nav"), g.Group(lis))
 }
diff --git a/gomponents.go b/gomponents.go
index 178f0a2..02e52ca 100644
--- a/gomponents.go
+++ b/gomponents.go
@@ -217,3 +217,20 @@ func (g group) Render(io.Writer) error {
 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
+}


Test patch:
diff --git a/gomponents_test.go b/gomponents_test.go
index 3028723..0c5dc68 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -183,3 +183,16 @@ func TestGroup(t *testing.T) {
 		}
 	})
 }
+
+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)
+	})
+}