3 --------------------------------------------------
6 // {4} gets the value as a {1}, returns the optionalDefault
7 // value or a system default object if the value is the wrong type.
8 func (v *Value) {4}(optionalDefault ...{1}) {1} {
9 if s, ok := v.data.({1}); ok {
12 if len(optionalDefault) == 1 {
13 return optionalDefault[0]
18 // Must{4} gets the value as a {1}.
20 // Panics if the object is not a {1}.
21 func (v *Value) Must{4}() {1} {
25 // {4}Slice gets the value as a []{1}, returns the optionalDefault
26 // value or nil if the value is not a []{1}.
27 func (v *Value) {4}Slice(optionalDefault ...[]{1}) []{1} {
28 if s, ok := v.data.([]{1}); ok {
31 if len(optionalDefault) == 1 {
32 return optionalDefault[0]
37 // Must{4}Slice gets the value as a []{1}.
39 // Panics if the object is not a []{1}.
40 func (v *Value) Must{4}Slice() []{1} {
44 // Is{4} gets whether the object contained is a {1} or not.
45 func (v *Value) Is{4}() bool {
50 // Is{4}Slice gets whether the object contained is a []{1} or not.
51 func (v *Value) Is{4}Slice() bool {
52 _, ok := v.data.([]{1})
56 // Each{4} calls the specified callback for each object
59 // Panics if the object is the wrong type.
60 func (v *Value) Each{4}(callback func(int, {1}) bool) *Value {
62 for index, val := range v.Must{4}Slice() {
63 carryon := callback(index, val)
73 // Where{4} uses the specified decider function to select items
74 // from the []{1}. The object contained in the result will contain
75 // only the selected items.
76 func (v *Value) Where{4}(decider func(int, {1}) bool) *Value {
80 v.Each{4}(func(index int, val {1}) bool {
81 shouldSelect := decider(index, val)
82 if shouldSelect == false {
83 selected = append(selected, val)
88 return &Value{data:selected}
92 // Group{4} uses the specified grouper function to group the items
93 // keyed by the return of the grouper. The object contained in the
94 // result will contain a map[string][]{1}.
95 func (v *Value) Group{4}(grouper func(int, {1}) string) *Value {
97 groups := make(map[string][]{1})
99 v.Each{4}(func(index int, val {1}) bool {
100 group := grouper(index, val)
101 if _, ok := groups[group]; !ok {
102 groups[group] = make([]{1}, 0)
104 groups[group] = append(groups[group], val)
108 return &Value{data:groups}
112 // Replace{4} uses the specified function to replace each {1}s
113 // by iterating each item. The data in the returned result will be a
114 // []{1} containing the replaced items.
115 func (v *Value) Replace{4}(replacer func(int, {1}) {1}) *Value {
117 arr := v.Must{4}Slice()
118 replaced := make([]{1}, len(arr))
120 v.Each{4}(func(index int, val {1}) bool {
121 replaced[index] = replacer(index, val)
125 return &Value{data:replaced}
129 // Collect{4} uses the specified collector function to collect a value
130 // for each of the {1}s in the slice. The data returned will be a
132 func (v *Value) Collect{4}(collector func(int, {1}) interface{}) *Value {
134 arr := v.Must{4}Slice()
135 collected := make([]interface{}, len(arr))
137 v.Each{4}(func(index int, val {1}) bool {
138 collected[index] = collector(index, val)
142 return &Value{data:collected}
145 // ************************************************************
147 // ************************************************************
149 func Test{4}(t *testing.T) {
152 m := map[string]interface{}{"value": val, "nothing": nil}
153 assert.Equal(t, val, New(m).Get("value").{4}())
154 assert.Equal(t, val, New(m).Get("value").Must{4}())
155 assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}())
156 assert.Equal(t, val, New(m).Get("nothing").{4}({2}))
158 assert.Panics(t, func() {
159 New(m).Get("age").Must{4}()
164 func Test{4}Slice(t *testing.T) {
167 m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil}
168 assert.Equal(t, val, New(m).Get("value").{4}Slice()[0])
169 assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0])
170 assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice())
171 assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0])
173 assert.Panics(t, func() {
174 New(m).Get("nothing").Must{4}Slice()
179 func TestIs{4}(t *testing.T) {
183 v = &Value{data: {1}({2})}
184 assert.True(t, v.Is{4}())
186 v = &Value{data: []{1}{ {1}({2}) }}
187 assert.True(t, v.Is{4}Slice())
191 func TestEach{4}(t *testing.T) {
193 v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
195 replacedVals := make([]{1}, 0)
196 assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool {
199 replacedVals = append(replacedVals, val)
210 assert.Equal(t, count, 3)
211 assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0])
212 assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1])
213 assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2])
217 func TestWhere{4}(t *testing.T) {
219 v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
221 selected := v.Where{4}(func(i int, val {1}) bool {
225 assert.Equal(t, 3, len(selected))
229 func TestGroup{4}(t *testing.T) {
231 v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
233 grouped := v.Group{4}(func(i int, val {1}) string {
234 return fmt.Sprintf("%v", i%2==0)
235 }).data.(map[string][]{1})
237 assert.Equal(t, 2, len(grouped))
238 assert.Equal(t, 3, len(grouped["true"]))
239 assert.Equal(t, 3, len(grouped["false"]))
243 func TestReplace{4}(t *testing.T) {
245 v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
247 rawArr := v.Must{4}Slice()
249 replaced := v.Replace{4}(func(index int, val {1}) {1} {
250 if index < len(rawArr)-1 {
251 return rawArr[index+1]
256 replacedArr := replaced.Must{4}Slice()
257 if assert.Equal(t, 6, len(replacedArr)) {
258 assert.Equal(t, replacedArr[0], rawArr[1])
259 assert.Equal(t, replacedArr[1], rawArr[2])
260 assert.Equal(t, replacedArr[2], rawArr[3])
261 assert.Equal(t, replacedArr[3], rawArr[4])
262 assert.Equal(t, replacedArr[4], rawArr[5])
263 assert.Equal(t, replacedArr[5], rawArr[0])
268 func TestCollect{4}(t *testing.T) {
270 v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
272 collected := v.Collect{4}(func(index int, val {1}) interface{} {
276 collectedArr := collected.MustInterSlice()
277 if assert.Equal(t, 6, len(collectedArr)) {
278 assert.Equal(t, collectedArr[0], 0)
279 assert.Equal(t, collectedArr[1], 1)
280 assert.Equal(t, collectedArr[2], 2)
281 assert.Equal(t, collectedArr[3], 3)
282 assert.Equal(t, collectedArr[4], 4)
283 assert.Equal(t, collectedArr[5], 5)