aboutsummaryrefslogtreecommitdiff
path: root/echo/tplfuncs/humanize.go
blob: 4fc47a3693c40461208586292355da94250552e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package tplfuncs

import (
	"fmt"
	"strings"
)

// JoinEnglish renders a list of strings with commas between them and if
// there are three or more words will include the word "and" before the
// final word.
func JoinEnglish(words []string) string {
	switch len(words) {
	case 0:
		return ""
	case 1:
		return words[0]
	case 2:
		return fmt.Sprintf("%s and %s", words[0], words[1])
	default:
		base := strings.Join(words[:len(words)-1], ", ")
		return fmt.Sprintf("%s, and %s", base, words[len(words)-1])
	}
}