scripting system, hue fixes and such
This commit is contained in:
@@ -6,6 +6,45 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CompactMatch(ids []string) string {
|
||||
if len(ids) == 1 {
|
||||
return ids[0]
|
||||
}
|
||||
|
||||
shortestPrefix := ids[0]
|
||||
for _, curr := range ids[1:] {
|
||||
minLength := len(shortestPrefix)
|
||||
if len(curr) < minLength {
|
||||
minLength = len(curr)
|
||||
}
|
||||
|
||||
if len(shortestPrefix) > minLength {
|
||||
shortestPrefix = shortestPrefix[:minLength]
|
||||
}
|
||||
|
||||
for i := 0; i < minLength; i++ {
|
||||
if curr[i] != shortestPrefix[i] {
|
||||
shortestPrefix = curr[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString(shortestPrefix)
|
||||
sb.WriteByte('{')
|
||||
for i, curr := range ids {
|
||||
if i != 0 {
|
||||
sb.WriteByte(',')
|
||||
}
|
||||
|
||||
sb.WriteString(curr[len(shortestPrefix):])
|
||||
}
|
||||
sb.WriteByte('}')
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func CompactIDList(ids []string) []string {
|
||||
ids = append(ids[:0:0], ids...)
|
||||
sort.Strings(ids)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package formattools
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompactMatch(t *testing.T) {
|
||||
table := []struct {
|
||||
O string
|
||||
I []string
|
||||
}{
|
||||
{
|
||||
"nanoleaf:172.16.119.33:{abcd,dead,beef,0000}",
|
||||
[]string{
|
||||
"nanoleaf:172.16.119.33:abcd", "nanoleaf:172.16.119.33:dead",
|
||||
"nanoleaf:172.16.119.33:beef", "nanoleaf:172.16.119.33:0000",
|
||||
},
|
||||
},
|
||||
{
|
||||
"nanoleaf:172.16.{119.33:abcd,22.33:dead}",
|
||||
[]string{
|
||||
"nanoleaf:172.16.119.33:abcd", "nanoleaf:172.16.22.33:dead",
|
||||
},
|
||||
},
|
||||
{
|
||||
"{nanoleaf:172.16.119.33:abcd,hue:172.16.119.34:81bd3cc3-cf22-4401-a5cb-a4bf18687270}",
|
||||
[]string{
|
||||
"nanoleaf:172.16.119.33:abcd", "hue:172.16.119.34:81bd3cc3-cf22-4401-a5cb-a4bf18687270",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, row := range table {
|
||||
t.Run(row.O, func(t *testing.T) {
|
||||
assert.Equal(t, row.O, CompactMatch(row.I))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user