Skip to content

Commit

Permalink
IEnumerable增加Count、Size
Browse files Browse the repository at this point in the history
删除FindLastIndex
  • Loading branch information
ahl5esoft committed Jun 21, 2019
1 parent cbbca4b commit 39ddf00
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 119 deletions.
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://underscorejs.org/">underscore.js</a>, but for Go

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

## Lack
* OrderBy、ThenBy
* IQuery性能差,将来会逐步用IEnumerable替代

## Documentation
Expand All @@ -29,6 +30,7 @@ like <a href="http://underscorejs.org/">underscore.js</a>, but for Go
* [`Any`](#any), [`AnyBy`](#anyBy)
* [`AsParallel`](#asParallel)
* [`Chain`](#chain)
* [`Count`](#count)
* [`Distinct`](#distinct), [`DistinctBy`](#distinctBy)
* [`Each`](#each)
* [`Filter`](#where), [`FilterBy`](#whereBy)
Expand All @@ -50,7 +52,7 @@ like <a href="http://underscorejs.org/">underscore.js</a>, 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)
Expand Down Expand Up @@ -227,6 +229,21 @@ Chain([]int{1, 2, 1, 4, 1, 3}).Uniq(nil).Group(func(n, _ int) string {
// len(res) == 2 && ok == true
```

<a name="count" />

### Count() int

__Examples__

```go
src := []string{"a", "b", "c"}
dst := Chain2(src).Count()
// dst = 3
```

__Same__
* `Size`

<a name="distinct" />

### Distinct(selector) IEnumerable
Expand Down Expand Up @@ -997,30 +1014,6 @@ __Same__

* `MapBy`

<a name="size" />

### 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
```

<a name="skip" />

### Skip(count) IEnumerable
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions count.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions count_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
15 changes: 0 additions & 15 deletions find-last-index.go

This file was deleted.

50 changes: 0 additions & 50 deletions find-last-index_test.go

This file was deleted.

1 change: 0 additions & 1 deletion i-query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}

Expand Down
18 changes: 18 additions & 0 deletions range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions size.go
Original file line number Diff line number Diff line change
@@ -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()
}
21 changes: 4 additions & 17 deletions size_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 39ddf00

Please sign in to comment.