mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: validate that parameter names are unique (#7882)
This commit is contained in:
19
coderd/util/strings/strings.go
Normal file
19
coderd/util/strings/strings.go
Normal file
@ -0,0 +1,19 @@
|
||||
package strings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JoinWithConjunction joins a slice of strings with commas except for the last
|
||||
// two which are joined with "and".
|
||||
func JoinWithConjunction(s []string) string {
|
||||
last := len(s) - 1
|
||||
if last == 0 {
|
||||
return s[last]
|
||||
}
|
||||
return fmt.Sprintf("%s and %s",
|
||||
strings.Join(s[:last], ", "),
|
||||
s[last],
|
||||
)
|
||||
}
|
16
coderd/util/strings/strings_test.go
Normal file
16
coderd/util/strings/strings_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package strings_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/coderd/util/strings"
|
||||
)
|
||||
|
||||
func TestJoinWithConjunction(t *testing.T) {
|
||||
t.Parallel()
|
||||
require.Equal(t, "foo", strings.JoinWithConjunction([]string{"foo"}))
|
||||
require.Equal(t, "foo and bar", strings.JoinWithConjunction([]string{"foo", "bar"}))
|
||||
require.Equal(t, "foo, bar and baz", strings.JoinWithConjunction([]string{"foo", "bar", "baz"}))
|
||||
}
|
Reference in New Issue
Block a user