/
list.go
40 lines (36 loc) · 943 Bytes
/
list.go
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
31
32
33
34
35
36
37
38
39
40
package utils
import (
"fmt"
"github.com/coveo/gotemplate/collections"
)
// MergeLists return a single list from all supplied lists
func MergeLists(lists ...collections.IGenericList) collections.IGenericList {
switch len(lists) {
case 0:
return nil
case 1:
return lists[0]
}
result := lists[0].Create(0, lists[0].Len()*len(lists))
for _, list := range lists {
result = result.Append(list.AsArray()...)
}
return result
}
// FormatList returns an array of string where format as been applied on every element of the supplied array
func FormatList(format string, args ...interface{}) collections.IGenericList {
var list collections.IGenericList
switch len(args) {
case 1:
if l, err := collections.TryAsList(args[0]); err == nil {
list = l
}
default:
list = collections.AsList(args)
}
result := list.Clone()
for i, value := range list.AsArray() {
result.Set(i, fmt.Sprintf(format, value))
}
return result
}