File tree 6 files changed +76
-38
lines changed
6 files changed +76
-38
lines changed Original file line number Diff line number Diff line change 9
9
\_/__/
10
10
```
11
11
12
- # Underscore.go [ ![ GoDoc] ( https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg )] ( https://godoc.org/github.com/ahl5esoft/golang-underscore ) [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore )] ( https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore ) ![ Version] ( https://img.shields.io/badge/version-2.4.0 -green.svg )
12
+ # Underscore.go [ ![ GoDoc] ( https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg )] ( https://godoc.org/github.com/ahl5esoft/golang-underscore ) [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore )] ( https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore ) ![ Version] ( https://img.shields.io/badge/version-2.4.1 -green.svg )
13
13
like <a href =" http://underscorejs.org/ " >underscore.js</a > and C# LINQ, but for Go
14
14
15
15
## Installation
Original file line number Diff line number Diff line change @@ -14,6 +14,26 @@ func Benchmark_Any(b *testing.B) {
14
14
}
15
15
}
16
16
17
+ func Test_Any (t * testing.T ) {
18
+ t .Run ("source is empty slice" , func (t * testing.T ) {
19
+ arr := make ([]testModel , 0 )
20
+ res := Chain (arr ).Any (func (r testModel , _ int ) bool {
21
+ return r .ID == 0
22
+ })
23
+ assert .False (t , res )
24
+ })
25
+
26
+ t .Run ("source is nil" , func (t * testing.T ) {
27
+ var arr []testModel
28
+ assert .Nil (t , arr )
29
+
30
+ res := Chain (arr ).Any (func (r testModel , _ int ) bool {
31
+ return r .ID == 0
32
+ })
33
+ assert .False (t , res )
34
+ })
35
+ }
36
+
17
37
func Test_Any_False (t * testing.T ) {
18
38
ok := Chain ([]testModel {
19
39
{ID : 1 , Name : "one" },
Original file line number Diff line number Diff line change 1
1
package underscore
2
2
3
- import "reflect"
3
+ import (
4
+ "reflect"
5
+ )
4
6
5
7
// Chain is 创建枚举器
6
8
func Chain (src interface {}) IEnumerable {
@@ -63,18 +65,20 @@ func chainFromValue(value reflect.Value) IEnumerable {
63
65
value .Len (),
64
66
)
65
67
default :
66
- if iterator , ok := value .Interface ().(IEnumerator ); ok {
67
- return enumerable {
68
- Enumerator : func () IEnumerator {
69
- return iterator
70
- },
68
+ if value .IsValid () {
69
+ if iterator , ok := value .Interface ().(IEnumerator ); ok {
70
+ return enumerable {
71
+ Enumerator : func () IEnumerator {
72
+ return iterator
73
+ },
74
+ }
71
75
}
72
- }
73
76
74
- if value .Kind () == reflect .Ptr {
75
- return chainFromValue (
76
- value .Elem (),
77
- )
77
+ if value .Kind () == reflect .Ptr {
78
+ return chainFromValue (
79
+ value .Elem (),
80
+ )
81
+ }
78
82
}
79
83
80
84
return enumerable {
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ func Test_Group(t *testing.T) {
53
53
}).Value (resValue )
54
54
assert .EqualValues (
55
55
t ,
56
- resValue .Interface (),
56
+ resValue .Elem (). Interface (),
57
57
map [string ][]int {
58
58
"odd" : {1 , 3 , 5 },
59
59
"even" : {2 , 4 },
Original file line number Diff line number Diff line change 1
1
package underscore
2
2
3
3
import (
4
- "fmt"
5
4
"reflect"
6
5
"strconv"
7
6
"testing"
@@ -51,9 +50,6 @@ func Test_Map(t *testing.T) {
51
50
resValue .Elem ().Interface (),
52
51
[]int {0 , 1 },
53
52
)
54
- fmt .Println (resValue )
55
-
56
- assert .True (t , false )
57
53
})
58
54
}
59
55
Original file line number Diff line number Diff line change @@ -7,27 +7,45 @@ import (
7
7
)
8
8
9
9
func Test_Order (t * testing.T ) {
10
- arr := []testModel {
11
- {ID : 2 , Name : "two" },
12
- {ID : 1 , Name : "one" },
13
- {ID : 3 , Name : "three" },
14
- }
15
- var res []int
16
- Chain (arr ).Order (func (n testModel , _ int ) int {
17
- return n .ID
18
- }).Map (func (r testModel , _ int ) int {
19
- return r .ID
20
- }).Value (& res )
21
- assert .Len (
22
- t ,
23
- res ,
24
- len (arr ),
25
- )
26
- assert .EqualValues (
27
- t ,
28
- res ,
29
- []int {1 , 2 , 3 },
30
- )
10
+ t .Run ("ok" , func (t * testing.T ) {
11
+ arr := []testModel {
12
+ {ID : 2 , Name : "two" },
13
+ {ID : 1 , Name : "one" },
14
+ {ID : 3 , Name : "three" },
15
+ }
16
+ var res []int
17
+ Chain (arr ).Order (func (n testModel , _ int ) int {
18
+ return n .ID
19
+ }).Map (func (r testModel , _ int ) int {
20
+ return r .ID
21
+ }).Value (& res )
22
+ assert .Len (
23
+ t ,
24
+ res ,
25
+ len (arr ),
26
+ )
27
+ assert .EqualValues (
28
+ t ,
29
+ res ,
30
+ []int {1 , 2 , 3 },
31
+ )
32
+ })
33
+
34
+ t .Run ("chain" , func (t * testing.T ) {
35
+ arr := []testModel {
36
+ {ID : 2 , Name : "two" },
37
+ {ID : 1 , Name : "one" },
38
+ {ID : 3 , Name : "three" },
39
+ }
40
+ var res []int
41
+ Chain (arr ).Where (func (r testModel , _ int ) bool {
42
+ return r .ID > 5
43
+ }).Order (func (r testModel , _ int ) int {
44
+ return r .ID
45
+ }).Map (func (r testModel , _ int ) int {
46
+ return r .ID
47
+ }).Value (& res )
48
+ })
31
49
}
32
50
33
51
func Test_OrderBy (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments