diff --git a/README.md b/README.md
index 390d2ca..deba278 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@
\_/__/
```
-# 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)
+# 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)
like underscore.js, but for Go
## Installation
@@ -19,6 +19,7 @@ like underscore.js, but for Go
$ go get -u github.com/ahl5esoft/golang-underscore
## Lack
+* OrderBy、ThenBy
* IQuery性能差,将来会逐步用IEnumerable替代
## Documentation
@@ -29,6 +30,7 @@ like underscore.js, but for Go
* [`Any`](#any), [`AnyBy`](#anyBy)
* [`AsParallel`](#asParallel)
* [`Chain`](#chain)
+* [`Count`](#count)
* [`Distinct`](#distinct), [`DistinctBy`](#distinctBy)
* [`Each`](#each)
* [`Filter`](#where), [`FilterBy`](#whereBy)
@@ -50,7 +52,7 @@ like underscore.js, but for Go
* [`Reject`](#reject), [`RejectBy`](#rejectBy)
* [`Reverse`](#reverse), [`ReverseBy`](#reverseBy)
* [`Select`](#select), [`SelectBy`](#selectBy)
-* [`Size`](#size)
+* [`Size`](#count)
* [`Skip`](#skip)
* [`Sort`](#sort), [`SortBy`](#sortBy)
* [`Take`](#take)
@@ -227,6 +229,21 @@ Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
// len(res) == 2 && ok == true
```
+
+
+### Count() int
+
+__Examples__
+
+```go
+src := []string{"a", "b", "c"}
+dst := Chain2(src).Count()
+// dst = 3
+```
+
+__Same__
+* `Size`
+
### Distinct(selector) IEnumerable
@@ -997,30 +1014,6 @@ __Same__
* `MapBy`
-
-
-### Size(source)
-
-__Arguments__
-
-* `source` - array or map
-
-__Return__
-
-* int
-
-__Examples__
-
-```go
-dict := map[string]int{
- "a": 1,
- "b": 2,
- "c": 3,
-}
-l := Size(dict)
-// l = 3
-```
-
### Skip(count) IEnumerable
@@ -1199,6 +1192,12 @@ __Same__
* `FilterBy`
## Release Notes
+~~~
+v1.6.0 (2019-06-21)
+* IEnumerable增加Count、Size
+* 删除FindLastIndex
+~~~
+
~~~
v1.5.0 (2019-06-18)
* 增加Chain Benchmark
diff --git a/count.go b/count.go
new file mode 100644
index 0000000..f1b9b1e
--- /dev/null
+++ b/count.go
@@ -0,0 +1,11 @@
+package underscore
+
+func (m enumerable) Count() int {
+ iterator := m.GetEnumerator()
+ count := 0
+ for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() {
+ count++
+ }
+
+ return count
+}
diff --git a/count_test.go b/count_test.go
new file mode 100644
index 0000000..bdced3a
--- /dev/null
+++ b/count_test.go
@@ -0,0 +1,11 @@
+package underscore
+
+import "testing"
+
+func Test_Count(t *testing.T) {
+ src := []string{"a", "b", "c"}
+ dst := Chain2(src).Count()
+ if dst != len(src) {
+ t.Error("wrong")
+ }
+}
diff --git a/find-last-index.go b/find-last-index.go
deleted file mode 100644
index 4d14750..0000000
--- a/find-last-index.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package underscore
-
-// FindLastIndex gets the last index of the argument
-func FindLastIndex(source interface{}) int {
- if !IsArray(source) {
- return -1
- }
-
- return Size(source) - 1
-}
-
-func (m *query) FindLastIndex() IQuery {
- m.Source = FindLastIndex(m.Source)
- return m
-}
diff --git a/find-last-index_test.go b/find-last-index_test.go
deleted file mode 100644
index db823f7..0000000
--- a/find-last-index_test.go
+++ /dev/null
@@ -1,50 +0,0 @@
-package underscore
-
-import (
- "testing"
-)
-
-func Test_FindLastIndex(t *testing.T) {
- arr := []testModel{
- {ID: 1, Name: "one"},
- {ID: 2, Name: "two"},
- {ID: 3, Name: "three"},
- }
- i := FindLastIndex(arr)
- if i != 2 {
- t.Error("wrong")
- }
-}
-
-func Test_FindLastIndex_EmptyArray(t *testing.T) {
- if FindLastIndex([]int{}) != -1 {
- t.Error("err")
- }
-}
-
-func Test_FindLastIndex_NotArray(t *testing.T) {
- if FindLastIndex(nil) != -1 {
- t.Fatal("err")
- }
-
- dic := map[string]int{
- "a": 1,
- "b": 2,
- }
- if FindLastIndex(dic) != -1 {
- t.Error("err")
- }
-}
-
-func Test_Chain_FindLastIndex(t *testing.T) {
- arr := []testModel{
- {ID: 1, Name: "one"},
- {ID: 2, Name: "two"},
- {ID: 3, Name: "three"},
- }
- var index int
- Chain(arr).FindLastIndex().Value(&index)
- if index != 2 {
- t.Error("wrong")
- }
-}
diff --git a/i-query.go b/i-query.go
index b7ef96b..d9e5dcc 100644
--- a/i-query.go
+++ b/i-query.go
@@ -12,7 +12,6 @@ type IQuery interface {
FindBy(map[string]interface{}) IQuery
FindIndex(interface{}) int
FindIndexBy(map[string]interface{}) int
- FindLastIndex() IQuery
First() IQuery
Group(interface{}) IQuery
GroupBy(string) IQuery
diff --git a/object.go b/object.go
index 2d1d6e6..bc4ac60 100644
--- a/object.go
+++ b/object.go
@@ -33,7 +33,7 @@ func (m *query) Object() IQuery {
func objectAsParallel(source interface{}) interface{} {
first := First(source)
- if first == nil || Size(first) != 2 {
+ if first == nil || Chain(first).Size() != 2 {
return nil
}
diff --git a/range.go b/range.go
index cfcd0af..c766e37 100644
--- a/range.go
+++ b/range.go
@@ -41,18 +41,19 @@ func Range2(start, stop, step int) IEnumerable {
return enumerable{
Enumerator: func() IEnumerator {
+ current := start
index := 0
return &enumerator{
MoveNextFunc: func() (valueRV reflect.Value, keyRV reflect.Value, ok bool) {
if step > 0 {
- ok = start < stop
+ ok = current < stop
} else {
- ok = start > stop
+ ok = current > stop
}
if ok {
- valueRV = reflect.ValueOf(start)
+ valueRV = reflect.ValueOf(current)
keyRV = reflect.ValueOf(index)
- start += step
+ current += step
index++
}
diff --git a/range_test.go b/range_test.go
index 261ea83..6c77c8b 100644
--- a/range_test.go
+++ b/range_test.go
@@ -2,6 +2,24 @@ package underscore
import "testing"
+func Test_Range(t *testing.T) {
+ q := Range2(0, 100, 1)
+
+ odd := q.Where(func(r, _ int) bool {
+ return r%2 == 1
+ }).Count()
+ if odd != 50 {
+ t.Fatal(odd)
+ }
+
+ even := q.Where(func(r, _ int) bool {
+ return r%2 == 0
+ }).Count()
+ if even != 50 {
+ t.Fatal(even)
+ }
+}
+
func Test_Range_StepEq0(t *testing.T) {
defer func() {
if rv := recover(); rv == nil {
diff --git a/size.go b/size.go
index 3f9d40b..353e73b 100644
--- a/size.go
+++ b/size.go
@@ -1,11 +1,10 @@
package underscore
-// Size is 数组或字典的长度
-func Size(source interface{}) int {
- length, _ := parseSource(source)
+func (m *query) Size() int {
+ length, _ := parseSource(m.Source)
return length
}
-func (m *query) Size() int {
- return Size(m.Source)
+func (m enumerable) Size() int {
+ return m.Count()
}
diff --git a/size_test.go b/size_test.go
index a89be3d..0bb1500 100644
--- a/size_test.go
+++ b/size_test.go
@@ -1,24 +1,11 @@
package underscore
-import (
- "testing"
-)
+import "testing"
func Test_Size(t *testing.T) {
- dict := map[string]int{
- "a": 1,
- "b": 2,
- "c": 3,
- }
- if Size(dict) != len(dict) {
- t.Error("wrong")
- }
-}
-
-func Test_Chain_Size(t *testing.T) {
- arr := []string{"a", "b", "c"}
- size := Chain(arr).Size()
- if size != len(arr) {
+ src := []string{"a", "b", "c"}
+ size := Chain2(src).Size()
+ if size != len(src) {
t.Error("wrong")
}
}