-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DataAttr and DataAttrs functions
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |