File: helper.go

package info (click to toggle)
golang-github-joomcode-errorx 1.2.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 280 kB
  • sloc: makefile: 2
file content (30 lines) | stat: -rw-r--r-- 561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package errorx

import "strings"

func joinStringsIfNonEmpty(delimiter string, parts ...string) string {
	switch len(parts) {
	case 0:
		return ""
	case 1:
		return parts[0]
	case 2:
		if len(parts[0]) == 0 {
			return parts[1]
		} else if len(parts[1]) == 0 {
			return parts[0]
		} else {
			return parts[0] + delimiter + parts[1]
		}
	default:
		filteredParts := make([]string, 0, len(parts))
		for _, part := range parts {
			if len(part) > 0 {
				filteredParts = append(filteredParts, part)
			}
		}

		return strings.Join(filteredParts, delimiter)
	}
}