Skip to content

Commit

Permalink
Merge pull request #4 from spekary/has_word_prefix
Browse files Browse the repository at this point in the history
Adding HasWordPrefix
  • Loading branch information
spekary authored Dec 10, 2023
2 parents 48c68f3 + 6fe2ef3 commit f92bdbe
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.dll
*.so
*.dylib
.idea/
.DS_Store

# Test binary, built with `go test -c`
*.test
Expand Down
25 changes: 19 additions & 6 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Attributer interface {
// and then convert it to a string to get the attributes embeddable in an HTML tag.
//
// To create new attributes, the easiest is to do this:
// a := Attributes{"id":"theId", "class":"myClass"}
//
// a := Attributes{"id":"theId", "class":"myClass"}
type Attributes map[string]string

// NewAttributes creates a new Attributes collection.
Expand Down Expand Up @@ -350,7 +351,8 @@ func (a Attributes) Merge(aIn Attributes) Attributes {
// OverrideString merges an attribute string into the attributes. Conflicts are won by the string.
//
// It takes an attribute string of the form
// a="b" c="d"
//
// a="b" c="d"
func (a Attributes) OverrideString(s string) Attributes {
if s == "" {
return a
Expand All @@ -364,7 +366,8 @@ func (a Attributes) OverrideString(s string) Attributes {
// Conflicts are won by the string, but styles and classes merge.
//
// It takes an attribute string of the form
// a="b" c="d"
//
// a="b" c="d"
func (a Attributes) MergeString(s string) Attributes {
if s == "" {
return a
Expand Down Expand Up @@ -473,6 +476,15 @@ func (a Attributes) RemoveClassesWithPrefix(v string) bool {
return false
}

// HasClassWithPrefix returns true if the attribute has a class with the given prefix.
func (a Attributes) HasClassWithPrefix(prefix string) bool {
if a.Has("class") {
class := a.Get("class")
return HasWordWithPrefix(class, prefix)
}
return false
}

// AddValuesChanged adds the given space separated values to the end of the values in the
// given attribute, removing duplicates and returning true if the attribute was changed at all.
// An example of a place to use this is the aria-labelledby attribute, which can take multiple
Expand Down Expand Up @@ -556,6 +568,7 @@ func (a Attributes) HasClass(c string) bool {
// <div id='test1' data-test-case="my test"></div>
//
// You would get that value in javascript by doing:
//
// g$('test1').data('testCase');
//
// Conversion to special html data-* name formatting is handled here automatically. So if you SetData('testCase') here,
Expand Down Expand Up @@ -772,9 +785,9 @@ func getAttributesFromTemplate(s string) Attributes {
/*
type AttributeCreator map[string]string
func (c AttributeCreator) Create() Attributes {
return Attributes(c)
}
func (c AttributeCreator) Create() Attributes {
return Attributes(c)
}
*/
var templateMatcher *regexp.Regexp

Expand Down
7 changes: 7 additions & 0 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ func ExampleAttributes_RemoveClassesWithPrefix() {
// Output: class="that"
}

func ExampleAttributes_HasClassWithPrefix() {
a := Attributes{"class": "col-2 that"}
found := a.HasClassWithPrefix("col-")
fmt.Println(found)
// Output: true
}

func ExampleAttributes_AddValues() {
a := Attributes{"abc": "123"}
a.AddValues("abc", "456")
Expand Down
12 changes: 12 additions & 0 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ func RemoveClassesWithPrefix(class string, prefix string) string {

return ret
}

// HasWordWithPrefix returns true if the given string has a word in it with the given prefix.
func HasWordWithPrefix(class string, prefix string) bool {
classes := strings.Fields(class)

for _, s := range classes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
28 changes: 28 additions & 0 deletions class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func ExampleRemoveClassesWithPrefix() {
// Output: col4-other
}

func ExampleHasClassWithPrefix() {
exists := HasWordWithPrefix("col-6 col-brk col4-other", "col4-")
fmt.Println(exists)
// Output: true
}

func TestMergeWords1(t *testing.T) {
tests := []struct {
name string
Expand All @@ -53,3 +59,25 @@ func TestMergeWords1(t *testing.T) {
})
}
}

func TestHasClassWithPrefix(t *testing.T) {
tests := []struct {
name string
class string
prefix string
want bool
}{
{"True - one", "a-b c-d", "a-", true},
{"True - two", "a-b a-c c-d", "a-", true},
{"False - none", "", "a-", false},
{"False - one", "b-c", "a-", false},
{"False - two", "b-c c-d", "a-", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HasWordWithPrefix(tt.class, tt.prefix); got != tt.want {
t.Errorf("HasWordWithPrefix() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f92bdbe

Please sign in to comment.