From 6aea6d0965f9030d598eb87f5bfca36b638035f6 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 19 Mar 2025 01:26:01 +0100 Subject: add MapWithIndex and MapMap helpers --- gomponents.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gomponents.go b/gomponents.go index ad9c2cd..37267d4 100644 --- a/gomponents.go +++ b/gomponents.go @@ -257,6 +257,26 @@ func Map[T any](ts []T, cb func(T) Node) Group { return nodes } +// Map a slice of anything to a [Group] (which is just a slice of [Node]-s). +func MapWithIndex[T any](ts []T, cb func(int, T) Node) Group { + nodes := make([]Node, 0, len(ts)) + for k, t := range ts { + nodes = append(nodes, cb(k, t)) + } + + return nodes +} + +// Map a map of anything to a [Group] (which is just a slice of [Node]-s). +func MapMap[K comparable, T any](ts map[K]T, cb func(K, T) Node) Group { + nodes := make([]Node, 0, len(ts)) + for k, t := range ts { + nodes = append(nodes, cb(k, t)) + } + + return nodes +} + // Group a slice of [Node]-s into one Node, while still being usable like a regular slice of [Node]-s. // A [Group] can render directly, but if any of the direct children are [AttributeType], they will be ignored, // to not produce invalid HTML. -- cgit 1.4.1