-
Notifications
You must be signed in to change notification settings - Fork 11
/
string_array.go
144 lines (110 loc) · 5.54 KB
/
string_array.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package collections
import (
"fmt"
"sort"
"strings"
"unicode"
)
// StringArray is an implementation of a array of class String objects.
type StringArray []String
// Strings convert array of String objects to an array of regular go string.
func (as StringArray) Strings() []string { return ToStrings(as) }
// Str is just an abbreviation of Strings.
func (as StringArray) Str() []string { return as.Strings() }
// Title applies the corresponding String method on each string in the array.
func (as StringArray) Title() StringArray { return as.apply(String.Title) }
// ToTitle applies the corresponding String method on each string in the array.
func (as StringArray) ToTitle() StringArray { return as.apply(String.ToTitle) }
// ToLower applies the corresponding String method on each string in the array.
func (as StringArray) ToLower() StringArray { return as.apply(String.ToLower) }
// ToUpper applies the corresponding String method on each string in the array.
func (as StringArray) ToUpper() StringArray { return as.apply(String.ToUpper) }
// Trim applies the corresponding String method on each string in the array.
func (as StringArray) Trim(cutset string) StringArray { return as.applyStr(String.Trim, cutset) }
// TrimFunc applies the corresponding String method on each string in the array.
func (as StringArray) TrimFunc(f func(rune) bool) StringArray { return as.applyRF(String.TrimFunc, f) }
// TrimLeft applies the corresponding String method on each string in the array.
func (as StringArray) TrimLeft(cutset string) StringArray {
return as.applyStr(String.TrimLeft, cutset)
}
// TrimLeftFunc applies the corresponding String method on each string in the array.
func (as StringArray) TrimLeftFunc(f func(rune) bool) StringArray {
return as.applyRF(String.TrimLeftFunc, f)
}
// TrimPrefix applies the corresponding String method on each string in the array.
func (as StringArray) TrimPrefix(prefix string) StringArray {
return as.applyStr(String.TrimPrefix, prefix)
}
// TrimRight applies the corresponding String method on each string in the array.
func (as StringArray) TrimRight(cutset string) StringArray {
return as.applyStr(String.TrimRight, cutset)
}
// TrimRightFunc applies the corresponding String method on each string in the array.
func (as StringArray) TrimRightFunc(f func(rune) bool) StringArray {
return as.applyRF(String.TrimRightFunc, f)
}
// TrimSpace applies the corresponding String method on each string in the array.
func (as StringArray) TrimSpace() StringArray { return as.apply(String.TrimSpace) }
// TrimSuffix applies the corresponding String method on each string in the array.
func (as StringArray) TrimSuffix(suffix string) StringArray {
return as.applyStr(String.TrimSuffix, suffix)
}
// ToLowerSpecial applies the corresponding String method on each string in the array.
func (as StringArray) ToLowerSpecial(c unicode.SpecialCase) StringArray {
return as.applySC(String.ToLowerSpecial, c)
}
// ToTitleSpecial applies the corresponding String method on each string in the array.
func (as StringArray) ToTitleSpecial(c unicode.SpecialCase) StringArray {
return as.applySC(String.ToTitleSpecial, c)
}
// ToUpperSpecial applies the corresponding String method on each string in the array.
func (as StringArray) ToUpperSpecial(c unicode.SpecialCase) StringArray {
return as.applySC(String.ToUpperSpecial, c)
}
// Center applies the corresponding String method on each string in the array.
func (as StringArray) Center(width int) StringArray { return as.applyInt(String.Center, width) }
// Wrap applies the corresponding String method on each string in the array.
func (as StringArray) Wrap(width int) StringArray { return as.applyInt(String.Wrap, width) }
// Indent applies the corresponding String method on each string in the array.
func (as StringArray) Indent(indent string) StringArray { return as.applyStr(String.Indent, indent) }
// IndentN applies the corresponding String method on each string in the array.
func (as StringArray) IndentN(indent int) StringArray { return as.applyInt(String.IndentN, indent) }
// UnIndent applies the corresponding String method on each string in the array.
func (as StringArray) UnIndent() StringArray { return as.apply(String.UnIndent) }
// Join joins all lines in the array using sep as the join string.
func (as StringArray) Join(sep interface{}) String {
return String(strings.Join(as.Strings(), fmt.Sprint(sep)))
}
// Sorted apply a sort on the array.
func (as StringArray) Sorted() StringArray {
sorted := as.Strings()
sort.Strings(sorted)
return stringArray(sorted)
}
// Utility functions to apply String methods on each element of an array
func (as StringArray) applyRF(f func(String, func(rune) bool) String, fr func(rune) bool) (result StringArray) {
return as.apply(func(s String) String { return f(s, fr) })
}
func (as StringArray) applySC(f func(String, unicode.SpecialCase) String, c unicode.SpecialCase) (result StringArray) {
return as.apply(func(s String) String { return f(s, c) })
}
func (as StringArray) applyStr(f func(String, string) String, a string) (result StringArray) {
return as.apply(func(s String) String { return f(s, a) })
}
func (as StringArray) applyInt(f func(String, int) String, a int) (result StringArray) {
return as.apply(func(s String) String { return f(s, a) })
}
func (as StringArray) apply(f func(s String) String) (result StringArray) {
result = make(StringArray, len(as))
for i := range result {
result[i] = f(as[i])
}
return
}
func stringArray(a []string) (result StringArray) {
result = make(StringArray, len(a))
for i := range result {
result[i] = String(a[i])
}
return
}