photoprism/pkg/txt/list.go
Michael Mayer 52ab802731 Pkg: Apply "golangci-lint" recommendations to txt/... packages #5330
Signed-off-by: Michael Mayer <michael@photoprism.app>
2025-11-21 16:16:42 +01:00

36 lines
656 B
Go

package txt
// JoinAnd formats a slice of strings using commas and a localized "and" before the final element.
// Examples:
//
// []string{} => ""
// []string{"a"} => "a"
// []string{"a","b"} => "a and b"
// []string{"a","b","c"} => "a, b, and c"
func JoinAnd(values []string) string {
length := len(values)
switch length {
case 0:
return ""
case 1:
return values[0]
case 2:
return values[0] + " and " + values[1]
}
// length >= 3
result := ""
for i := 0; i < length; i++ {
switch i {
case 0:
result = values[i]
case length - 1:
result += ", and " + values[i]
default:
result += ", " + values[i]
}
}
return result
}