diff options
Diffstat (limited to 'echo/tplfuncs/humanize.go')
-rw-r--r-- | echo/tplfuncs/humanize.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/echo/tplfuncs/humanize.go b/echo/tplfuncs/humanize.go new file mode 100644 index 0000000..4fc47a3 --- /dev/null +++ b/echo/tplfuncs/humanize.go | |||
@@ -0,0 +1,23 @@ | |||
1 | package tplfuncs | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "strings" | ||
6 | ) | ||
7 | |||
8 | // JoinEnglish renders a list of strings with commas between them and if | ||
9 | // there are three or more words will include the word "and" before the | ||
10 | // final word. | ||
11 | func JoinEnglish(words []string) string { | ||
12 | switch len(words) { | ||
13 | case 0: | ||
14 | return "" | ||
15 | case 1: | ||
16 | return words[0] | ||
17 | case 2: | ||
18 | return fmt.Sprintf("%s and %s", words[0], words[1]) | ||
19 | default: | ||
20 | base := strings.Join(words[:len(words)-1], ", ") | ||
21 | return fmt.Sprintf("%s, and %s", base, words[len(words)-1]) | ||
22 | } | ||
23 | } | ||