Skip to content

Commit

Permalink
added DataAttr and DataAttrs functions
Browse files Browse the repository at this point in the history
  • Loading branch information
whisk committed Nov 17, 2023
1 parent ecbd947 commit 8f79a77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions attrs/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package attrs

import "strings"

// DataAttr returns the name for a data attribute
func DataAttr(name string) string {
var builder strings.Builder
builder.WriteString(DataPrefix)
builder.WriteString(name)
return builder.String()
}

// DataAttrs returns Props of data attributes build from given name-value pairs
func DataAttrs(pairs map[string]string) Props {
dataProps := Props{}
for k, v := range pairs {
dataProps[DataAttr(k)] = v
}
return dataProps
}
19 changes: 19 additions & 0 deletions attrs/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package attrs

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDataAttr(t *testing.T) {
actual := DataAttr("foobar")
expected := "data-foobar"
assert.Equal(t, expected, actual)
}

func TestDataAttrs(t *testing.T) {
actual := DataAttrs(map[string]string{"theme": "cupcake", "foobar": "baz"})
expected := Props{"data-theme": "cupcake", "data-foobar": "baz"}
assert.Equal(t, expected, actual)
}

0 comments on commit 8f79a77

Please sign in to comment.