From a5306fcee40ad231261b2aa0a0667f60b0fa2f51 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 29 May 2019 22:41:04 -0600 Subject: [PATCH] Custom helper for coords attribute --- CHANGELOG.md | 7 ++++--- attribute/coords.go | 19 +++++++++++++++++++ attribute/coords_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ attribute/generated.go | 8 -------- defs.json | 1 - 5 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 attribute/coords.go create mode 100644 attribute/coords_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 01b7620..b60c0b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [unversioned] +## [0.0.5] - 2019-05-29 ### Added @@ -10,8 +10,9 @@ ### Changed - Formatted code identifiers in Changelog -- Attributes `accept`, `accept-charset`, `class`, `for`, `headers`, `ping`, -`rel`, `srcset`, and `sandbox` have variadic string parameters. +- `Accept`, `AcceptCharset`, `Class`, `For`, `Headers`, `Ping`, `Rel`, `Srcset`, +and `Sandbox` attribute helper functions have variadic `string` parameters. +- `Coords` attribute helper function has variadic `float32` parameter. ## [0.0.4] - 2019-05-27 diff --git a/attribute/coords.go b/attribute/coords.go new file mode 100644 index 0000000..ddec182 --- /dev/null +++ b/attribute/coords.go @@ -0,0 +1,19 @@ +package attribute + +import ( + "strconv" + "strings" + + "gitlab.codemonkeysoftware.net/b/hatmill" +) + +func Coords(values ...float32) hatmill.Attrib { + s := make([]string, 0, len(values)) + for _, value := range values { + s = append(s, strconv.FormatFloat(float64(value), 'G', -1, 32)) + } + return hatmill.Attrib{ + Key: "coords", + Value: strings.Join(s, ","), + } +} diff --git a/attribute/coords_test.go b/attribute/coords_test.go new file mode 100644 index 0000000..d38b7cf --- /dev/null +++ b/attribute/coords_test.go @@ -0,0 +1,40 @@ +package attribute_test + +import ( + "testing" + + "gitlab.codemonkeysoftware.net/b/hatmill/attribute" +) + +func TestCoordsNumberFormat(t *testing.T) { + for _, testcase := range []struct { + name string + f float32 + value string + }{ + {name: "zero", f: 0, value: "0"}, + {name: "integral", f: 45, value: "45"}, + {name: "positive fractional, small positive exponent", f: 12.55, value: "12.55"}, + {name: "positive fractional, large positive exponent", f: 1.55E+12, value: "1.55E+12"}, + {name: "positive fractional, large negative exponent", f: 1.55E-12, value: "1.55E-12"}, + {name: "negative fractional, small positive exponent", f: -12.55, value: "-12.55"}, + {name: "negative fractional, large positive exponent", f: -1.55E+12, value: "-1.55E+12"}, + {name: "negative fractional, large negative exponent", f: -1.55E-12, value: "-1.55E-12"}, + } { + t.Run(testcase.name, func(t *testing.T) { + testAttribValue(t, attribute.Coords(testcase.f), testcase.value) + }) + } +} + +func TestCoordsCardinality(t *testing.T) { + t.Run("zero", func(t *testing.T) { + testAttribValue(t, attribute.Coords(), "") + }) + t.Run("one", func(t *testing.T) { + testAttribValue(t, attribute.Coords(5), "5") + }) + t.Run("many", func(t *testing.T) { + testAttribValue(t, attribute.Coords(5, 4, 3, 2, 1), "5,4,3,2,1") + }) +} diff --git a/attribute/generated.go b/attribute/generated.go index 1bbe738..bbe02e8 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -154,14 +154,6 @@ func Controls() hatmill.Attrib { } } -// Coords creates a "coords" attribute -func Coords(value string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "coords", - Value: value, - } -} - // Crossorigin creates a "crossorigin" attribute func Crossorigin(value string) hatmill.Attrib { return hatmill.Attrib{ diff --git a/defs.json b/defs.json index 815f7f3..f7df4c0 100644 --- a/defs.json +++ b/defs.json @@ -19,7 +19,6 @@ {"name": "contenteditable", "type": "explicit bool"}, {"name": "contextmenu", "type": "string"}, {"name": "controls", "type": "bool"}, - {"name": "coords", "type": "string"}, {"name": "crossorigin", "type": "string"}, {"name": "datetime", "type": "string"}, {"name": "decoding", "type": "string"},