Custom helper for coords attribute

This commit is contained in:
Brandon Dyck 2019-05-29 22:41:04 -06:00
parent 4aacd6e722
commit a5306fcee4
5 changed files with 63 additions and 12 deletions

View File

@ -1,6 +1,6 @@
# Changelog # Changelog
## [unversioned] ## [0.0.5] - 2019-05-29
### Added ### Added
@ -10,8 +10,9 @@
### Changed ### Changed
- Formatted code identifiers in Changelog - Formatted code identifiers in Changelog
- Attributes `accept`, `accept-charset`, `class`, `for`, `headers`, `ping`, - `Accept`, `AcceptCharset`, `Class`, `For`, `Headers`, `Ping`, `Rel`, `Srcset`,
`rel`, `srcset`, and `sandbox` have variadic string parameters. and `Sandbox` attribute helper functions have variadic `string` parameters.
- `Coords` attribute helper function has variadic `float32` parameter.
## [0.0.4] - 2019-05-27 ## [0.0.4] - 2019-05-27

19
attribute/coords.go Normal file
View File

@ -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, ","),
}
}

40
attribute/coords_test.go Normal file
View File

@ -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")
})
}

View File

@ -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 // Crossorigin creates a "crossorigin" attribute
func Crossorigin(value string) hatmill.Attrib { func Crossorigin(value string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{

View File

@ -19,7 +19,6 @@
{"name": "contenteditable", "type": "explicit bool"}, {"name": "contenteditable", "type": "explicit bool"},
{"name": "contextmenu", "type": "string"}, {"name": "contextmenu", "type": "string"},
{"name": "controls", "type": "bool"}, {"name": "controls", "type": "bool"},
{"name": "coords", "type": "string"},
{"name": "crossorigin", "type": "string"}, {"name": "crossorigin", "type": "string"},
{"name": "datetime", "type": "string"}, {"name": "datetime", "type": "string"},
{"name": "decoding", "type": "string"}, {"name": "decoding", "type": "string"},