github.com/coveo/gotemplate@v2.7.7+incompatible/utils/list.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/coveo/gotemplate/collections"
     7  )
     8  
     9  // MergeLists return a single list from all supplied lists
    10  func MergeLists(lists ...collections.IGenericList) collections.IGenericList {
    11  	switch len(lists) {
    12  	case 0:
    13  		return nil
    14  	case 1:
    15  		return lists[0]
    16  	}
    17  	result := lists[0].Create(0, lists[0].Len()*len(lists))
    18  	for _, list := range lists {
    19  		result = result.Append(list.AsArray()...)
    20  	}
    21  	return result
    22  }
    23  
    24  // FormatList returns an array of string where format as been applied on every element of the supplied array
    25  func FormatList(format string, args ...interface{}) collections.IGenericList {
    26  	var list collections.IGenericList
    27  	switch len(args) {
    28  	case 1:
    29  		if l, err := collections.TryAsList(args[0]); err == nil {
    30  			list = l
    31  		}
    32  	default:
    33  		list = collections.AsList(args)
    34  	}
    35  	result := list.Clone()
    36  	for i, value := range list.AsArray() {
    37  		result.Set(i, fmt.Sprintf(format, value))
    38  	}
    39  	return result
    40  }