MCPcopy Create free account
hub / github.com/algolia/cli / SliceToReadableString

Function SliceToReadableString

pkg/utils/utils.go:94–112  ·  view source on GitHub ↗

Convert slice of string to a readable string eg: ["one", "two", "three"] -> "one, two and three"

(str []string)

Source from the content-addressed store, hash-verified

92// Convert slice of string to a readable string
93// eg: ["one", "two", "three"] -> "one, two and three"
94func SliceToReadableString(str []string) string {
95 if len(str) == 0 {
96 return ""
97 }
98 if len(str) == 1 {
99 return str[0]
100 }
101 if len(str) == 2 {
102 return fmt.Sprintf("%s and %s", str[0], str[1])
103 }
104 readableStr := ""
105 if len(str) > 2 {
106 return fmt.Sprintf("%s%s",
107 strings.Join(str[:len(str)-1], ", "),
108 fmt.Sprintf(" and %s", str[len(str)-1]))
109 }
110
111 return readableStr
112}

Callers 6

runStringSliceCompletionFunction · 0.92
runClearCmdFunction · 0.92
GetConfirmMessageFunction · 0.92
runExportCmdFunction · 0.92

Calls

no outgoing calls

Tested by 1