Skip to content

Commit 3d005de

Browse files
committed
feat: v2.0.1 Sort/SortBy/Order/OrderBy
1 parent 5d6a96a commit 3d005de

30 files changed

+445
-362
lines changed

README.md

+59-159
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-2.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-2.0.1-green.svg)
1313
like <a href="http://underscorejs.org/">underscore.js</a> and C# LINQ, but for Go
1414

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

2121
## Lack
22-
* ExceptExceptBy、Order、OrderBy、ThenBy
22+
* Except/ExceptBy/Reject/RejectBy/Reverse/ReverseBy/ThenBy
2323

2424
## Documentation
2525

@@ -44,16 +44,15 @@ like <a href="http://underscorejs.org/">underscore.js</a> and C# LINQ, but for G
4444
* [`Map`](#select), [`MapBy`](#selectBy)
4545
* [`MapMany`](#selectMany), [`MapManyBy`](#selectManyBy)
4646
* [`Object`](#object)
47+
* [`Order`](#order), [`OrderBy`](#orderBy)
4748
* [`Property`](#property), [`PropertyRV`](#propertyRV)
4849
* [`Range`](#range)
4950
* [`Reduce`](#aggregate)
50-
* [`Reject`](#reject), [`RejectBy`](#rejectBy)
51-
* [`Reverse`](#reverse), [`ReverseBy`](#reverseBy)
5251
* [`Select`](#select), [`SelectBy`](#selectBy)
5352
* [`SelectMany`](#selectMany), [`SelectManyBy`](#selectManyBy)
5453
* [`Size`](#count)
5554
* [`Skip`](#skip)
56-
* [`Sort`](#sort), [`SortBy`](#sortBy)
55+
* [`Sort`](#order), [`SortBy`](#orderBy)
5756
* [`Take`](#take)
5857
* [`Uniq`](#distinct), [`UniqBy`](#distinctBy)
5958
* [`Values`](#values)
@@ -622,6 +621,59 @@ Chain(src).Object().Value(&dst)
622621
// dst = map[a:1 b:2]
623622
```
624623

624+
<a name="order" />
625+
626+
### Order(selector) IEnumerable
627+
628+
__Arguments__
629+
630+
* `selector` - func(element, key or index) anyType
631+
632+
633+
__Examples__
634+
635+
```go
636+
arr := []testModel{
637+
{ID: 2, Name: "two"},
638+
{ID: 1, Name: "one"},
639+
{ID: 3, Name: "three"},
640+
}
641+
var res []testModel
642+
Chain(arr).Order(func(n testModel, _ int) int {
643+
return n.ID
644+
}).Value(&res)
645+
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]
646+
```
647+
648+
__Same__
649+
650+
* `Sort`
651+
652+
<a name="orderBy" />
653+
654+
### OrderBy(fieldName) IEnumerable
655+
656+
__Arguments__
657+
658+
* `fieldName` - string
659+
660+
__Examples__
661+
662+
```go
663+
arr := []testModel{
664+
{ID: 2, Name: "two"},
665+
{ID: 1, Name: "one"},
666+
{ID: 3, Name: "three"},
667+
}
668+
var res []testModel
669+
Chain(arr).OrderBy("id").Value(&res)
670+
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]
671+
```
672+
673+
__Same__
674+
675+
* `SortBy`
676+
625677
<a name="property" />
626678

627679
### Property(name)
@@ -708,107 +760,6 @@ Range2(0, 3, 2).Value(&res)
708760
// res = [0 2]
709761
```
710762

711-
<a name="reject" />
712-
713-
### Reject(predicate) IEnumerable
714-
715-
__Arguments__
716-
717-
* `predicate` - func(element or value, index or key) bool
718-
719-
__Examples__
720-
721-
```go
722-
arr := []int{1, 2, 3, 4}
723-
var res []int
724-
Chain(arr).Reject(func(n, i int) bool {
725-
return n%2 == 0
726-
}).Value(&res)
727-
// or
728-
res := Reject(arr, func(n, i int) bool {
729-
return n%2 == 0
730-
}).([]int)
731-
// res = [1 3]
732-
```
733-
734-
<a name="rejectBy" />
735-
736-
### RejectBy(properties) IEnumerable
737-
738-
__Arguments__
739-
740-
* `properties` - map[string]interface{}
741-
742-
__Examples__
743-
744-
```go
745-
arr := []testModel{
746-
{ID: 1, Name: "one"},
747-
{ID: 2, Name: "two"},
748-
{ID: 3, Name: "three"},
749-
}
750-
var res []testModel
751-
Chain(arr).RejectBy(map[string]interface{}{
752-
"Id": 1,
753-
}).Value(&res)
754-
// or
755-
res := RejectBy(arr, map[string]interface{}{
756-
"Id": 1,
757-
}).([]testModel)
758-
// res = [{{0} 2 two} {{0} 3 three}]
759-
```
760-
761-
<a name="reverse" />
762-
763-
### Reverse(selector) IEnumerable
764-
765-
__Arguments__
766-
767-
* `selector` - func(element, key or index) anyType
768-
769-
770-
__Examples__
771-
772-
```go
773-
arr := []testModel{
774-
{ID: 2, Name: "two"},
775-
{ID: 1, Name: "one"},
776-
{ID: 3, Name: "three"},
777-
}
778-
var res []testModel
779-
Chain(arr).Reverse(func(n testModel, _ int) int {
780-
return n.ID
781-
}).Value(&res)
782-
// or
783-
res := Reverse(arr, func(n testModel, _ int) int {
784-
return n.ID
785-
}).([]testModel)
786-
// res = [{{0} 3 three} {{0} 2 two} {{0} 1 one}]
787-
```
788-
789-
<a name="reverseBy" />
790-
791-
### ReverseBy(fieldName) IEnumerable
792-
793-
__Arguments__
794-
795-
* `fieldName` - string
796-
797-
__Examples__
798-
799-
```go
800-
arr := []testModel{
801-
{ID: 2, Name: "two"},
802-
{ID: 1, Name: "one"},
803-
{ID: 3, Name: "three"},
804-
}
805-
var res []testModel
806-
Chain(arr).ReverseBy("id").Value(&res)
807-
// or
808-
res := ReverseBy(arr, "id").([]testModel)
809-
// res = [{{0} 3 three} {{0} 2 two} {{0} 1 one}]
810-
```
811-
812763
<a name="select" />
813764

814765
### Select(selector) IEnumerable
@@ -881,9 +832,9 @@ __Same__
881832

882833
* `MapMany`
883834

884-
<a name="mapManyBy" />
835+
<a name="selectManyBy" />
885836

886-
### MapManyBy(property) IEnumerable
837+
### SelectManyBy(property) IEnumerable
887838

888839
__Arguments__
889840

@@ -922,57 +873,6 @@ Chain(src).Skip(2).Value(&dst)
922873
// dst = [3]
923874
```
924875

925-
<a name="sort" />
926-
927-
### Sort(selector) IEnumerable
928-
929-
__Arguments__
930-
931-
* `selector` - func(element, key or index) anyType
932-
933-
934-
__Examples__
935-
936-
```go
937-
arr := []testModel{
938-
{ID: 2, Name: "two"},
939-
{ID: 1, Name: "one"},
940-
{ID: 3, Name: "three"},
941-
}
942-
var res []testModel
943-
Chain(arr).Sort(func(n testModel, _ int) int {
944-
return n.ID
945-
}).Value(&res)
946-
// or
947-
res := Sort(arr, func(n testModel, _ int) int {
948-
return n.ID
949-
}).([]testModel)
950-
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]
951-
```
952-
953-
<a name="sortBy" />
954-
955-
### SortBy(fieldName) IEnumerable
956-
957-
__Arguments__
958-
959-
* `fieldName` - string
960-
961-
__Examples__
962-
963-
```go
964-
arr := []testModel{
965-
{ID: 2, Name: "two"},
966-
{ID: 1, Name: "one"},
967-
{ID: 3, Name: "three"},
968-
}
969-
var res []testModel
970-
Chain(arr).SortBy("id").Value(&res)
971-
// or
972-
res := SortBy(arr, "id").([]testModel)
973-
// res = [{{0} 1 one} {{0} 2 two} {{0} 3 three}]
974-
```
975-
976876
<a name="take" />
977877

978878
### Take(count) IEnumerable

aggregate.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package underscore
33
import "reflect"
44

55
func (m enumerable) Aggregate(memo interface{}, fn interface{}) IEnumerable {
6-
fnRV := reflect.ValueOf(fn)
6+
fnValue := reflect.ValueOf(fn)
77
iterator := m.GetEnumerator()
8-
memoRV := reflect.ValueOf(memo)
8+
memoValue := reflect.ValueOf(memo)
99
for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() {
10-
memoRV = fnRV.Call([]reflect.Value{
11-
memoRV,
12-
iterator.GetValue(),
13-
iterator.GetKey(),
14-
})[0]
10+
memoValue = fnValue.Call(
11+
[]reflect.Value{
12+
memoValue,
13+
iterator.GetValue(),
14+
iterator.GetKey(),
15+
},
16+
)[0]
1517
}
16-
return chainFromRV(memoRV)
18+
return chainFromValue(memoValue)
1719
}

aggregate_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package underscore
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func Benchmark_Aggregate(b *testing.B) {
@@ -32,7 +34,9 @@ func Test_Aggregate(t *testing.T) {
3234
memo = append(memo, n+10)
3335
return memo
3436
}).Value(&dst)
35-
if !(len(dst) == 4 && dst[0] == 1 && dst[1] == 11 && dst[2] == 2 && dst[3] == 12) {
36-
t.Error(dst)
37-
}
37+
assert.EqualValues(
38+
t,
39+
dst,
40+
[]int{1, 11, 2, 12},
41+
)
3842
}

all.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import "reflect"
44

55
func (m enumerable) All(predicate interface{}) bool {
66
iterator := m.GetEnumerator()
7-
predicateRV := reflect.ValueOf(predicate)
7+
predicateValue := reflect.ValueOf(predicate)
88
for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() {
9-
returnRVs := predicateRV.Call([]reflect.Value{
10-
iterator.GetValue(),
11-
iterator.GetKey(),
12-
})
9+
returnRVs := predicateValue.Call(
10+
[]reflect.Value{
11+
iterator.GetValue(),
12+
iterator.GetKey(),
13+
},
14+
)
1315
if !returnRVs[0].Bool() {
1416
return false
1517
}

all_test.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package underscore
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
48

59
func Benchmark_All(b *testing.B) {
610
for n := 0; n < b.N; n++ {
@@ -18,9 +22,7 @@ func Test_All_False(t *testing.T) {
1822
}).All(func(r testModel, _ int) bool {
1923
return r.Name == "one"
2024
})
21-
if ok {
22-
t.Error("wrong")
23-
}
25+
assert.False(t, ok)
2426
}
2527

2628
func Test_All_True(t *testing.T) {
@@ -31,9 +33,7 @@ func Test_All_True(t *testing.T) {
3133
}).All(func(r testModel, _ int) bool {
3234
return r.ID == 1
3335
})
34-
if !ok {
35-
t.Error("wrong")
36-
}
36+
assert.True(t, ok)
3737
}
3838

3939
func Test_AllBy_False(t *testing.T) {
@@ -44,9 +44,7 @@ func Test_AllBy_False(t *testing.T) {
4444
}).AllBy(map[string]interface{}{
4545
"Name": "a",
4646
})
47-
if ok {
48-
t.Error("wrong")
49-
}
47+
assert.False(t, ok)
5048
}
5149

5250
func Test_AllBy_True(t *testing.T) {
@@ -57,7 +55,5 @@ func Test_AllBy_True(t *testing.T) {
5755
}).AllBy(map[string]interface{}{
5856
"name": "one",
5957
})
60-
if !ok {
61-
t.Error("wrong")
62-
}
58+
assert.True(t, ok)
6359
}

0 commit comments

Comments
 (0)