From b12942fb32082729e862eb53b8c3962534561e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20W=C3=BCstenberg?= Date: Thu, 3 Nov 2022 11:32:16 +0100 Subject: [PATCH] Delete low-value helper components (#115) I'd rather reserve the package for components that have proven repeatedly useful, like `Classes` and `HTML5`. --- components/attributes.go | 37 ------------------ components/attributes_test.go | 39 ------------------- components/{documents.go => components.go} | 31 +++++++++++++++ .../{documents_test.go => components_test.go} | 30 ++++++++++++++ components/elements.go | 18 --------- components/elements_test.go | 30 -------------- 6 files changed, 61 insertions(+), 124 deletions(-) delete mode 100644 components/attributes.go delete mode 100644 components/attributes_test.go rename components/{documents.go => components.go} (56%) rename components/{documents_test.go => components_test.go} (60%) delete mode 100644 components/elements.go delete mode 100644 components/elements_test.go diff --git a/components/attributes.go b/components/attributes.go deleted file mode 100644 index dc7ef9b..0000000 --- a/components/attributes.go +++ /dev/null @@ -1,37 +0,0 @@ -package components - -import ( - "io" - "sort" - "strings" - - g "github.com/maragudk/gomponents" - "github.com/maragudk/gomponents/html" -) - -// Classes is a map of strings to booleans, which Renders to an attribute with name "class". -// The attribute value is a sorted, space-separated string of all the map keys, -// for which the corresponding map value is true. -type Classes map[string]bool - -func (c Classes) Render(w io.Writer) error { - var included []string - for c, include := range c { - if include { - included = append(included, c) - } - } - sort.Strings(included) - return html.Class(strings.Join(included, " ")).Render(w) -} - -func (c Classes) Type() g.NodeType { - return g.AttributeType -} - -// String satisfies fmt.Stringer. -func (c Classes) String() string { - var b strings.Builder - _ = c.Render(&b) - return b.String() -} diff --git a/components/attributes_test.go b/components/attributes_test.go deleted file mode 100644 index dcfbad2..0000000 --- a/components/attributes_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package components_test - -import ( - "os" - "testing" - - g "github.com/maragudk/gomponents" - c "github.com/maragudk/gomponents/components" - "github.com/maragudk/gomponents/internal/assert" -) - -func TestClasses(t *testing.T) { - t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) { - assert.Equal(t, ` class="boheme-hat hat partyhat"`, c.Classes{ - "boheme-hat": true, - "hat": true, - "partyhat": true, - "turtlehat": false, - }) - }) - - t.Run("renders as attribute in an element", func(t *testing.T) { - e := g.El("div", c.Classes{"hat": true}) - assert.Equal(t, `
`, e) - }) - - t.Run("also works with fmt", func(t *testing.T) { - a := c.Classes{"hat": true} - if a.String() != ` class="hat"` { - t.FailNow() - } - }) -} - -func ExampleClasses() { - e := g.El("div", c.Classes{"party-hat": true, "boring-hat": false}) - _ = e.Render(os.Stdout) - // Output:
-} diff --git a/components/documents.go b/components/components.go similarity index 56% rename from components/documents.go rename to components/components.go index c7d8f8d..379d18d 100644 --- a/components/documents.go +++ b/components/components.go @@ -2,6 +2,10 @@ package components import ( + "io" + "sort" + "strings" + g "github.com/maragudk/gomponents" . "github.com/maragudk/gomponents/html" ) @@ -31,3 +35,30 @@ func HTML5(p HTML5Props) g.Node { ), ) } + +// Classes is a map of strings to booleans, which Renders to an attribute with name "class". +// The attribute value is a sorted, space-separated string of all the map keys, +// for which the corresponding map value is true. +type Classes map[string]bool + +func (c Classes) Render(w io.Writer) error { + var included []string + for c, include := range c { + if include { + included = append(included, c) + } + } + sort.Strings(included) + return Class(strings.Join(included, " ")).Render(w) +} + +func (c Classes) Type() g.NodeType { + return g.AttributeType +} + +// String satisfies fmt.Stringer. +func (c Classes) String() string { + var b strings.Builder + _ = c.Render(&b) + return b.String() +} diff --git a/components/documents_test.go b/components/components_test.go similarity index 60% rename from components/documents_test.go rename to components/components_test.go index 21261fa..6420cab 100644 --- a/components/documents_test.go +++ b/components/components_test.go @@ -1,6 +1,7 @@ package components_test import ( + "os" "testing" g "github.com/maragudk/gomponents" @@ -30,3 +31,32 @@ func TestHTML5(t *testing.T) { assert.Equal(t, `Hat`, e) }) } + +func TestClasses(t *testing.T) { + t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) { + assert.Equal(t, ` class="boheme-hat hat partyhat"`, Classes{ + "boheme-hat": true, + "hat": true, + "partyhat": true, + "turtlehat": false, + }) + }) + + t.Run("renders as attribute in an element", func(t *testing.T) { + e := g.El("div", Classes{"hat": true}) + assert.Equal(t, `
`, e) + }) + + t.Run("also works with fmt", func(t *testing.T) { + a := Classes{"hat": true} + if a.String() != ` class="hat"` { + t.FailNow() + } + }) +} + +func ExampleClasses() { + e := g.El("div", Classes{"party-hat": true, "boring-hat": false}) + _ = e.Render(os.Stdout) + // Output:
+} diff --git a/components/elements.go b/components/elements.go deleted file mode 100644 index c9e1427..0000000 --- a/components/elements.go +++ /dev/null @@ -1,18 +0,0 @@ -package components - -import ( - g "github.com/maragudk/gomponents" - . "github.com/maragudk/gomponents/html" -) - -func InputHidden(name, value string, children ...g.Node) g.Node { - return Input(Type("hidden"), Name(name), Value(value), g.Group(children)) -} - -func LinkStylesheet(href string, children ...g.Node) g.Node { - return Link(Rel("stylesheet"), Href(href), g.Group(children)) -} - -func LinkPreload(href, as string, children ...g.Node) g.Node { - return Link(Rel("preload"), Href(href), As(as), g.Group(children)) -} diff --git a/components/elements_test.go b/components/elements_test.go deleted file mode 100644 index 7afa7db..0000000 --- a/components/elements_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package components_test - -import ( - "testing" - - g "github.com/maragudk/gomponents" - c "github.com/maragudk/gomponents/components" - "github.com/maragudk/gomponents/internal/assert" -) - -func TestInputHidden(t *testing.T) { - t.Run("returns an input element with type hidden, and the given name and value", func(t *testing.T) { - n := c.InputHidden("id", "partyhat", g.Attr("class", "hat")) - assert.Equal(t, ``, n) - }) -} - -func TestLinkStylesheet(t *testing.T) { - t.Run("returns a link element with rel stylesheet and the given href", func(t *testing.T) { - n := c.LinkStylesheet("style.css", g.Attr("media", "print")) - assert.Equal(t, ``, n) - }) -} - -func TestLinkPreload(t *testing.T) { - t.Run("returns a link element with rel preload and the given href and as", func(t *testing.T) { - n := c.LinkPreload("party.woff2", "font", g.Attr("type", "font/woff2")) - assert.Equal(t, ``, n) - }) -}