github.com/coveo/gotemplate@v2.7.7+incompatible/collections/string_array.go (about) 1 package collections 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 "unicode" 8 ) 9 10 // StringArray is an implementation of a array of class String objects. 11 type StringArray []String 12 13 // Strings convert array of String objects to an array of regular go string. 14 func (as StringArray) Strings() []string { return ToStrings(as) } 15 16 // Str is just an abbreviation of Strings. 17 func (as StringArray) Str() []string { return as.Strings() } 18 19 // Title applies the corresponding String method on each string in the array. 20 func (as StringArray) Title() StringArray { return as.apply(String.Title) } 21 22 // ToTitle applies the corresponding String method on each string in the array. 23 func (as StringArray) ToTitle() StringArray { return as.apply(String.ToTitle) } 24 25 // ToLower applies the corresponding String method on each string in the array. 26 func (as StringArray) ToLower() StringArray { return as.apply(String.ToLower) } 27 28 // ToUpper applies the corresponding String method on each string in the array. 29 func (as StringArray) ToUpper() StringArray { return as.apply(String.ToUpper) } 30 31 // Trim applies the corresponding String method on each string in the array. 32 func (as StringArray) Trim(cutset string) StringArray { return as.applyStr(String.Trim, cutset) } 33 34 // TrimFunc applies the corresponding String method on each string in the array. 35 func (as StringArray) TrimFunc(f func(rune) bool) StringArray { return as.applyRF(String.TrimFunc, f) } 36 37 // TrimLeft applies the corresponding String method on each string in the array. 38 func (as StringArray) TrimLeft(cutset string) StringArray { return as.applyStr(String.TrimLeft, cutset) } 39 40 // TrimLeftFunc applies the corresponding String method on each string in the array. 41 func (as StringArray) TrimLeftFunc(f func(rune) bool) StringArray { 42 return as.applyRF(String.TrimLeftFunc, f) 43 } 44 45 // TrimPrefix applies the corresponding String method on each string in the array. 46 func (as StringArray) TrimPrefix(prefix string) StringArray { 47 return as.applyStr(String.TrimPrefix, prefix) 48 } 49 50 // TrimRight applies the corresponding String method on each string in the array. 51 func (as StringArray) TrimRight(cutset string) StringArray { 52 return as.applyStr(String.TrimRight, cutset) 53 } 54 55 // TrimRightFunc applies the corresponding String method on each string in the array. 56 func (as StringArray) TrimRightFunc(f func(rune) bool) StringArray { 57 return as.applyRF(String.TrimRightFunc, f) 58 } 59 60 // TrimSpace applies the corresponding String method on each string in the array. 61 func (as StringArray) TrimSpace() StringArray { return as.apply(String.TrimSpace) } 62 63 // TrimSuffix applies the corresponding String method on each string in the array. 64 func (as StringArray) TrimSuffix(suffix string) StringArray { 65 return as.applyStr(String.TrimSuffix, suffix) 66 } 67 68 // ToLowerSpecial applies the corresponding String method on each string in the array. 69 func (as StringArray) ToLowerSpecial(c unicode.SpecialCase) StringArray { 70 return as.applySC(String.ToLowerSpecial, c) 71 } 72 73 // ToTitleSpecial applies the corresponding String method on each string in the array. 74 func (as StringArray) ToTitleSpecial(c unicode.SpecialCase) StringArray { 75 return as.applySC(String.ToTitleSpecial, c) 76 } 77 78 // ToUpperSpecial applies the corresponding String method on each string in the array. 79 func (as StringArray) ToUpperSpecial(c unicode.SpecialCase) StringArray { 80 return as.applySC(String.ToUpperSpecial, c) 81 } 82 83 // Center applies the corresponding String method on each string in the array. 84 func (as StringArray) Center(width int) StringArray { return as.applyInt(String.Center, width) } 85 86 // Wrap applies the corresponding String method on each string in the array. 87 func (as StringArray) Wrap(width int) StringArray { return as.applyInt(String.Wrap, width) } 88 89 // Indent applies the corresponding String method on each string in the array. 90 func (as StringArray) Indent(indent string) StringArray { return as.applyStr(String.Indent, indent) } 91 92 // IndentN applies the corresponding String method on each string in the array. 93 func (as StringArray) IndentN(indent int) StringArray { return as.applyInt(String.IndentN, indent) } 94 95 // UnIndent applies the corresponding String method on each string in the array. 96 func (as StringArray) UnIndent() StringArray { return as.apply(String.UnIndent) } 97 98 // Join joins all lines in the array using sep as the join string. 99 func (as StringArray) Join(sep interface{}) String { 100 return String(strings.Join(as.Strings(), fmt.Sprint(sep))) 101 } 102 103 // Sorted apply a sort on the array. 104 func (as StringArray) Sorted() StringArray { 105 sorted := as.Strings() 106 sort.Strings(sorted) 107 return stringArray(sorted) 108 } 109 110 // Utility functions to apply String methods on each element of an array 111 112 func (as StringArray) applyRF(f func(String, func(rune) bool) String, fr func(rune) bool) (result StringArray) { 113 return as.apply(func(s String) String { return f(s, fr) }) 114 } 115 116 func (as StringArray) applySC(f func(String, unicode.SpecialCase) String, c unicode.SpecialCase) (result StringArray) { 117 return as.apply(func(s String) String { return f(s, c) }) 118 } 119 120 func (as StringArray) applyStr(f func(String, string) String, a string) (result StringArray) { 121 return as.apply(func(s String) String { return f(s, a) }) 122 } 123 124 func (as StringArray) applyInt(f func(String, int) String, a int) (result StringArray) { 125 return as.apply(func(s String) String { return f(s, a) }) 126 } 127 128 func (as StringArray) apply(f func(s String) String) (result StringArray) { 129 result = make(StringArray, len(as)) 130 for i := range result { 131 result[i] = f(as[i]) 132 } 133 return 134 } 135 136 func stringArray(a []string) (result StringArray) { 137 result = make(StringArray, len(a)) 138 for i := range result { 139 result[i] = String(a[i]) 140 } 141 return 142 }