Skip to content

Commit 6ba3144

Browse files
author
ahl5esoft
committed
增加IEnumerable、IEnumerator
All、Any、Chain2、Find、First、Range2、Value支持IEnumerable
1 parent 1f91366 commit 6ba3144

27 files changed

+663
-318
lines changed

README.md

Lines changed: 71 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
\_/__/
1010
```
1111

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-1.0.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-1.1.0-green.svg)
1313
like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
1414

1515
## Installation
@@ -19,13 +19,7 @@ like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
1919
$ go get -u github.com/ahl5esoft/golang-underscore
2020

2121
## Lack
22-
* Performance optimization
23-
* Benchmark
24-
* Compatible with `.NET LINQ`
25-
* more...
26-
27-
## Suggest
28-
always using `chain`
22+
* IQuery性能差,将来会逐步用IEnumerable替代
2923

3024
## Documentation
3125

@@ -57,17 +51,15 @@ like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
5751
* [`Sort`](#sort), [`SortBy`](#sortBy)
5852
* [`Take`](#take)
5953
* [`Uniq`](#uniq), [`UniqBy`](#uniqBy)
60-
* [`Value`](#value)
6154
* [`Values`](#values)
6255
* [`Where`](#where), [`WhereBy`](#whereBy)
6356

6457
<a name="all" />
6558

66-
### All(source, predicate)
59+
### All(predicate) bool
6760

6861
__Arguments__
6962

70-
* `source` - array or map
7163
* `predicate` - func(element, index or key) bool
7264

7365
__Return__
@@ -77,59 +69,47 @@ __Return__
7769
__Examples__
7870

7971
```go
80-
src := []testModel{
72+
ok := Chain2([]testModel{
8173
{ID: 1, Name: "one"},
8274
{ID: 1, Name: "two"},
8375
{ID: 1, Name: "three"},
84-
}
85-
ok := All(src, func(r testModel, _ int) bool {
86-
return r.Id == 1
76+
}).All(func(r testModel, _ int) bool {
77+
return r.ID == 1
8778
})
8879
// ok == true
8980
```
9081

9182
<a name="allBy" />
9283

93-
### AllBy(source, properties)
84+
### AllBy(properties) bool
9485

9586
__Arguments__
9687

97-
* `source` - array or map
9888
* `properties` - map[string]interface{}
9989

10090
__Return__
10191

102-
* bool
92+
* bool - all the values that pass a truth test `predicate`
10393

10494
__Examples__
10595

10696
```go
107-
src := []testModel{
97+
ok := Chain2([]testModel{
10898
{ID: 1, Name: "one"},
109-
{ID: 1, Name: "two"},
110-
{ID: 1, Name: "three"},
111-
}
112-
ok := AllBy(src, nil)
113-
// ok == true
114-
115-
ok = AllBy(src, map[string]interface{}{
116-
"name": "a",
117-
})
118-
// ok == false
119-
120-
ok = AllBy(src, map[string]interface{}{
121-
"id": 1,
99+
{ID: 2, Name: "one"},
100+
{ID: 3, Name: "one"},
101+
}).AllBy(map[string]interface{}{
102+
"name": "one",
122103
})
123104
// ok == true
124105
```
125106

126107
<a name="any" />
127108

128-
### Any(source, predicate)
109+
### Any(predicate) bool
129110

130111
__Arguments__
131112

132-
* `source` - array or map
133113
* `predicate` - func(element or value, index or key) bool
134114

135115
__Return__
@@ -139,24 +119,22 @@ __Return__
139119
__Examples__
140120

141121
```go
142-
src := []testModel{
122+
ok := Chain2([]testModel{
143123
{ID: 1, Name: "one"},
144124
{ID: 2, Name: "two"},
145125
{ID: 3, Name: "three"},
146-
}
147-
ok := Any(src, func(r testModel, _ int) bool {
148-
return r.Id == 0
126+
}).Any(func(r testModel, _ int) bool {
127+
return r.ID == 0
149128
})
150129
// ok == false
151130
```
152131

153132
<a name="anyBy" />
154133

155-
### AnyBy(source, properties)
134+
### AnyBy(properties) bool
156135

157136
__Arguments__
158137

159-
* `source` - array or map
160138
* `properties` - map[string]interface{}
161139

162140
__Return__
@@ -166,19 +144,12 @@ __Return__
166144
__Examples__
167145

168146
```go
169-
src := []testModel{
147+
ok := Chain2([]testModel{
170148
{ID: 1, Name: "one"},
171149
{ID: 2, Name: "two"},
172150
{ID: 3, Name: "three"},
173-
}
174-
ok := AnyBy(src, map[string]interface{}{
175-
"Id": 0,
176-
})
177-
// ok == false
178-
179-
ok = AnyBy(src, map[string]interface{}{
180-
"id": src[0].Id,
181-
"name": src[0].Name,
151+
}).AnyBy(map[string]interface{}{
152+
"name": "two",
182153
})
183154
// ok == true
184155
```
@@ -272,66 +243,56 @@ Each(src, func (r testModel, i int) {
272243

273244
<a name="find" />
274245

275-
### Find(source, predicate)
246+
### Find(predicate) IQuery
247+
### Find(predicate) IEnumerable
276248

277249
__Arguments__
278250

279-
* `source` - array or map
280251
* `predicate` - func(element or value, index or key) bool
281252

282-
__Return__
283-
284-
* interface{} -- `source` elem
285-
286253
__Examples__
287254

288255
```go
289-
src := []testModel{
290-
{ID: 1, Name: "one"},
291-
{ID: 2, Name: "two"},
292-
{ID: 3, Name: "three"},
293-
}
294-
var item testModel
295-
Chain(src).Find(func(r testModel, _ int) bool {
296-
return r.ID == 1
297-
}).Value(&item)
256+
var dst int
257+
Chain2([]int{1, 2, 3}).Find(func(r, _ int) bool {
258+
return r == 2
259+
}).Value(&dst)
260+
// dst == 2
298261
// or
299-
item := Find(src, func(r testModel, _ int) bool {
300-
return r.ID == 1
301-
})
302-
// item == arr[0]
262+
var dst int
263+
Chain2([][]int{
264+
[]int{1, 3, 5, 7},
265+
[]int{2, 4, 6, 8},
266+
}).Find(func(r []int, _ int) bool {
267+
return r[0]%2 == 0
268+
}).Find(func(r, _ int) bool {
269+
return r > 6
270+
}).Value(&dst)
271+
// dst == 8
303272
```
304273

305274
<a name="findBy" />
306275

307-
### FindBy(source, properties)
276+
### FindBy(properties) IQuery
277+
### FindBy(properties) IEnumerable
308278

309279
__Arguments__
310280

311-
* `source` - array or map
312281
* `properties` - map[string]interface{}
313282

314-
__Return__
315-
316-
* interface{} - `source` elem
317-
318283
__Examples__
319284

320285
```go
321-
arr := []testModel{
286+
src := []testModel{
322287
{ID: 1, Name: "one"},
323288
{ID: 2, Name: "two"},
324289
{ID: 3, Name: "three"},
325290
}
326-
var item testModel
327-
Chain(arr).FindBy(map[string]interface{}{
328-
"id": 2,
329-
}).Value(&item)
330-
// or
331-
item := FindBy(arr, map[string]interface{}{
291+
var dst testModel
292+
Chain2(src).FindBy(map[string]interface{}{
332293
"id": 2,
333-
})
334-
// item == arr[1]
294+
}).Value(&dst)
295+
// dst == src[1]
335296
```
336297

337298
<a name="findIndex" />
@@ -390,28 +351,26 @@ i := FindIndexBy(arr, map[string]interface{}{
390351

391352
<a name="first" />
392353

393-
### First(source)
354+
### First() IQuery
355+
### First() IEnumerable
394356

395357
__Arguments__
396358

397-
* `source` - array or map
398-
399-
__Return__
400-
401-
* interface{}
359+
* `predicate` - func(element or value, index or key) bool
402360

403361
__Examples__
404362

405363
```go
406-
arr := []int{ 1, 2, 3 }
407-
var res int
408-
Chain(arr).First().Value(&res)
364+
var dst int
365+
Chain2([]int{1, 2, 3}).First().Value(&dst)
366+
// dst == 1
409367
// or
410-
res := First(arr).(int)
411-
// res = 1
412-
413-
res := First(nil)
414-
// res == nil
368+
var dst int
369+
Chain2([][]int{
370+
[]int{1, 3, 5, 7},
371+
[]int{2, 4, 6, 8},
372+
}).First().First().Value(&dst)
373+
// dst == 1
415374
```
416375

417376
<a name="group" />
@@ -872,39 +831,36 @@ nameRV, err := getNameRV(item)
872831

873832
<a name="range" />
874833

875-
### Range(start, stop, step)
834+
### Range(start, stop, step) IQuery
835+
### Range(start, stop, step) IEnumerable
876836

877837
__Arguments__
878838

879839
* `start` - int
880840
* `stop` - int
881841
* `step` - int
882842

883-
__Return__
884-
885-
* IQuery - a wrapped object, wrapped objects until value is called
886-
887843
__Examples__
888844

889845
```go
890846
var res []int
891-
Range(0, 0, 1).Value(&res)
847+
Range2(0, 0, 1).Value(&res)
892848
// res = []
893849

894850
var res []int
895-
Range(0, 10, 0).Value(&res)
896-
// res = []
851+
Range2(0, 10, 0).Value(&res)
852+
// panic
897853

898854
var res []int
899-
Range(10, 0, 1).Value(&res)
900-
// res = []
855+
Range2(4, 0, -1).Value(&res)
856+
// res = [4 3 2 1]
901857

902858
var res []int
903-
Range(0, 2, 1).Value(&res)
859+
Range2(0, 2, 1).Value(&res)
904860
// res = [0 1]
905861

906862
var res []int
907-
Range(0, 3, 2).Value(&res)
863+
Range2(0, 3, 2).Value(&res)
908864
// res = [0 2]
909865
```
910866

@@ -1224,25 +1180,6 @@ res := UniqBy(arr, "Name").([]testModel)
12241180
// res = [{{0} 1 one}]
12251181
```
12261182

1227-
<a name="value" />
1228-
1229-
### Value(result)
1230-
1231-
__Examples__
1232-
1233-
```go
1234-
arr := []int{1, 2, 1, 4, 1, 3}
1235-
var res map[string][]int
1236-
Chain(arr).Uniq(nil).Group(func(n, _ int) string {
1237-
if n%2 == 0 {
1238-
return "even"
1239-
}
1240-
1241-
return "old"
1242-
}).Value(&res)
1243-
// res = map[old:[1 3] even:[2 4]]
1244-
```
1245-
12461183
<a name="values" />
12471184

12481185
### Values(source)
@@ -1336,6 +1273,11 @@ res := WhereBy(src, map[string]interface{}{
13361273
```
13371274

13381275
## Release Notes
1276+
~~~
1277+
v1.1.0 (2018-06-02)
1278+
* 增加IEnumerable、IEnumerator
1279+
* All、Any、Chain2、Find、First、Range2、Value支持IEnumerable
1280+
~~~
13391281

13401282
~~~
13411283
v1.0.0 (2018-04-23)

0 commit comments

Comments
 (0)