Skip to content

Commit 39ddf00

Browse files
author
ahl5esoft
committed
IEnumerable增加Count、Size
删除FindLastIndex
1 parent cbbca4b commit 39ddf00

11 files changed

+79
-119
lines changed

README.md

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

1515
## Installation
@@ -19,6 +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+
* OrderBy、ThenBy
2223
* IQuery性能差,将来会逐步用IEnumerable替代
2324

2425
## Documentation
@@ -29,6 +30,7 @@ like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
2930
* [`Any`](#any), [`AnyBy`](#anyBy)
3031
* [`AsParallel`](#asParallel)
3132
* [`Chain`](#chain)
33+
* [`Count`](#count)
3234
* [`Distinct`](#distinct), [`DistinctBy`](#distinctBy)
3335
* [`Each`](#each)
3436
* [`Filter`](#where), [`FilterBy`](#whereBy)
@@ -50,7 +52,7 @@ like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
5052
* [`Reject`](#reject), [`RejectBy`](#rejectBy)
5153
* [`Reverse`](#reverse), [`ReverseBy`](#reverseBy)
5254
* [`Select`](#select), [`SelectBy`](#selectBy)
53-
* [`Size`](#size)
55+
* [`Size`](#count)
5456
* [`Skip`](#skip)
5557
* [`Sort`](#sort), [`SortBy`](#sortBy)
5658
* [`Take`](#take)
@@ -227,6 +229,21 @@ Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
227229
// len(res) == 2 && ok == true
228230
```
229231

232+
<a name="count" />
233+
234+
### Count() int
235+
236+
__Examples__
237+
238+
```go
239+
src := []string{"a", "b", "c"}
240+
dst := Chain2(src).Count()
241+
// dst = 3
242+
```
243+
244+
__Same__
245+
* `Size`
246+
230247
<a name="distinct" />
231248

232249
### Distinct(selector) IEnumerable
@@ -997,30 +1014,6 @@ __Same__
9971014

9981015
* `MapBy`
9991016

1000-
<a name="size" />
1001-
1002-
### Size(source)
1003-
1004-
__Arguments__
1005-
1006-
* `source` - array or map
1007-
1008-
__Return__
1009-
1010-
* int
1011-
1012-
__Examples__
1013-
1014-
```go
1015-
dict := map[string]int{
1016-
"a": 1,
1017-
"b": 2,
1018-
"c": 3,
1019-
}
1020-
l := Size(dict)
1021-
// l = 3
1022-
```
1023-
10241017
<a name="skip" />
10251018

10261019
### Skip(count) IEnumerable
@@ -1199,6 +1192,12 @@ __Same__
11991192
* `FilterBy`
12001193

12011194
## Release Notes
1195+
~~~
1196+
v1.6.0 (2019-06-21)
1197+
* IEnumerable增加Count、Size
1198+
* 删除FindLastIndex
1199+
~~~
1200+
12021201
~~~
12031202
v1.5.0 (2019-06-18)
12041203
* 增加Chain Benchmark

count.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package underscore
2+
3+
func (m enumerable) Count() int {
4+
iterator := m.GetEnumerator()
5+
count := 0
6+
for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() {
7+
count++
8+
}
9+
10+
return count
11+
}

count_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package underscore
2+
3+
import "testing"
4+
5+
func Test_Count(t *testing.T) {
6+
src := []string{"a", "b", "c"}
7+
dst := Chain2(src).Count()
8+
if dst != len(src) {
9+
t.Error("wrong")
10+
}
11+
}

find-last-index.go

-15
This file was deleted.

find-last-index_test.go

-50
This file was deleted.

i-query.go

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type IQuery interface {
1212
FindBy(map[string]interface{}) IQuery
1313
FindIndex(interface{}) int
1414
FindIndexBy(map[string]interface{}) int
15-
FindLastIndex() IQuery
1615
First() IQuery
1716
Group(interface{}) IQuery
1817
GroupBy(string) IQuery

object.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (m *query) Object() IQuery {
3333

3434
func objectAsParallel(source interface{}) interface{} {
3535
first := First(source)
36-
if first == nil || Size(first) != 2 {
36+
if first == nil || Chain(first).Size() != 2 {
3737
return nil
3838
}
3939

range.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ func Range2(start, stop, step int) IEnumerable {
4141

4242
return enumerable{
4343
Enumerator: func() IEnumerator {
44+
current := start
4445
index := 0
4546
return &enumerator{
4647
MoveNextFunc: func() (valueRV reflect.Value, keyRV reflect.Value, ok bool) {
4748
if step > 0 {
48-
ok = start < stop
49+
ok = current < stop
4950
} else {
50-
ok = start > stop
51+
ok = current > stop
5152
}
5253
if ok {
53-
valueRV = reflect.ValueOf(start)
54+
valueRV = reflect.ValueOf(current)
5455
keyRV = reflect.ValueOf(index)
55-
start += step
56+
current += step
5657
index++
5758
}
5859

range_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ package underscore
22

33
import "testing"
44

5+
func Test_Range(t *testing.T) {
6+
q := Range2(0, 100, 1)
7+
8+
odd := q.Where(func(r, _ int) bool {
9+
return r%2 == 1
10+
}).Count()
11+
if odd != 50 {
12+
t.Fatal(odd)
13+
}
14+
15+
even := q.Where(func(r, _ int) bool {
16+
return r%2 == 0
17+
}).Count()
18+
if even != 50 {
19+
t.Fatal(even)
20+
}
21+
}
22+
523
func Test_Range_StepEq0(t *testing.T) {
624
defer func() {
725
if rv := recover(); rv == nil {

size.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package underscore
22

3-
// Size is 数组或字典的长度
4-
func Size(source interface{}) int {
5-
length, _ := parseSource(source)
3+
func (m *query) Size() int {
4+
length, _ := parseSource(m.Source)
65
return length
76
}
87

9-
func (m *query) Size() int {
10-
return Size(m.Source)
8+
func (m enumerable) Size() int {
9+
return m.Count()
1110
}

size_test.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
package underscore
22

3-
import (
4-
"testing"
5-
)
3+
import "testing"
64

75
func Test_Size(t *testing.T) {
8-
dict := map[string]int{
9-
"a": 1,
10-
"b": 2,
11-
"c": 3,
12-
}
13-
if Size(dict) != len(dict) {
14-
t.Error("wrong")
15-
}
16-
}
17-
18-
func Test_Chain_Size(t *testing.T) {
19-
arr := []string{"a", "b", "c"}
20-
size := Chain(arr).Size()
21-
if size != len(arr) {
6+
src := []string{"a", "b", "c"}
7+
size := Chain2(src).Size()
8+
if size != len(src) {
229
t.Error("wrong")
2310
}
2411
}

0 commit comments

Comments
 (0)