Skip to content

Commit 6499e7e

Browse files
committed
Added includes with test and updated doc
1 parent ad16cd7 commit 6499e7e

File tree

5 files changed

+101
-31
lines changed

5 files changed

+101
-31
lines changed

README.md

+20-13
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,30 @@ users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
3232
filteredUsers := js.Filter(users, func(user User, index int) bool {
3333
return user.Id == 10
3434
})
35-
// filteredUsers
36-
{{Id: 10, Name: "Go"}}
35+
// {{Id: 10, Name: "Go"}}
3736
3837
```
3938

4039
### Map
4140

4241
```
43-
mapedUsers := js.Map(users, func(user User, index int) Foo {
42+
js.Map(users, func(user User, index int) Foo {
4443
return Foo{
4544
Bar: fmt.Sprintf("%d %s", user.Id, user.Name),
4645
}
4746
})
48-
// mapedUsers
49-
{{Bar: "10 Go"}, {Bar: "11 Lang"}}
47+
// {{Bar: "10 Go"}, {Bar: "11 Lang"}}
5048
5149
```
5250

5351
### Reduce
5452
```
5553
numbers := []int{10, 11, 12, 13}
56-
sum := js.Reduce(numbers, func(s int, n int, i int) int {
54+
js.Reduce(numbers, func(s int, n int, i int) int {
5755
return s + n
5856
}, 0)
5957
60-
// sum
61-
46
58+
// 46
6259
```
6360

6461
### Every
@@ -68,12 +65,10 @@ users := []User{{Id: 12, Name: "Go"}, {Id: 14, Name: "Go"}}
6865
resultTrue := Every(users, func(user User, _ int) bool {
6966
return user.Name == "Go"
7067
})
71-
// resultTrue
72-
true
68+
// true
7369
74-
resultFalse := Every([]string{"hey", "hi"}, "hi")
75-
// resultFalse
76-
false
70+
Every([]string{"hey", "hi"}, "hi")
71+
// false
7772
```
7873

7974
### Foreach
@@ -86,3 +81,15 @@ Foreach(slices, func(ln string, index int) {
8681
})
8782
```
8883

84+
### Includes
85+
86+
```
87+
languages := []User{{Id: 12, Name: "Go"}, {Id: 14, Name: "Lang"}, {Id: 15, Name: "Typescript"}}
88+
89+
Includes(languages, func(ln User, _ int) bool { return ln.Name == "Go" })
90+
//true
91+
Includes(languages, func(ln User, _ int) bool { return ln.Name == "MySql" })
92+
//false
93+
94+
```
95+

filter_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ func TestFilterWithStructSlice(t *testing.T) {
2020
func TestFilterWithStringSlice(t *testing.T) {
2121
var slices = []string{"Go", "Typescript", "Nodejs"}
2222

23-
ln := Filter(slices, func(ln string, index int) bool {
24-
return ln == "Go"
25-
})
23+
ln := Filter(slices, "Go")
2624

2725
expected := "Go"
2826

find_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func TestFindWithStructSlice(t *testing.T) {
1919
func TestFindWithStringSlice(t *testing.T) {
2020
var slices = []string{"Go", "Typescript", "Nodejs"}
2121

22-
ln := Find(slices, func(ln string, index int) bool {
23-
return ln == "Go"
24-
})
22+
ln := Find(slices, "Go")
2523

2624
expected := "Go"
2725

include_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package js_array_method
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIncludeWithoutCallback(t *testing.T) {
8+
9+
languages := []string{"go", "PHP", "MySql", "TypeScript"}
10+
11+
if !Includes(languages, "TypeScript") {
12+
t.Errorf("Expected %s received %s", "true", "false")
13+
}
14+
15+
if Includes(languages, "Hack") {
16+
t.Errorf("Expected %s received %s", "false", "true")
17+
}
18+
}
19+
20+
func TestIncludeWithCallback(t *testing.T) {
21+
22+
languages := []User{{Id: 12, Name: "Go"}, {Id: 14, Name: "Lang"}, {Id: 15, Name: "Typescript"}}
23+
24+
if !Includes(languages, func(ln User, _ int) bool { return ln.Name == "Go" }) {
25+
t.Errorf("Expected %s received %s", "true", "false")
26+
}
27+
28+
if Includes(languages, func(ln User, _ int) bool { return ln.Name == "MySql" }) {
29+
t.Errorf("Expected %s received %s", "false", "true")
30+
}
31+
}

js_array_method.go

+48-12
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ func Map[T any, U any](input []T, callback func(element T, index int) U) []U {
1717
return output
1818
}
1919

20-
func Filter[T any](input []T, callback func(element T, index int) bool) []T {
20+
// conditions can be callback or a value
21+
// callback has two parameter one is element and another is index
22+
// callback must return true or false
23+
func Filter[T any](input []T, conditions interface{}) []T {
2124
var output []T
25+
conditionType := getType(conditions)
2226

2327
for index, value := range input {
24-
if callback(value, index) {
28+
if conditionType == "func" && callCallback(conditions, value, index) {
2529
output = append(output, value)
30+
continue
31+
} else if conditionType != "func" && compareValue(conditions, value) {
32+
output = append(output, value)
33+
continue
2634
}
2735
}
2836

@@ -39,10 +47,17 @@ func Reduce[T any, U any](input []T, callback func(pre U, current T, index int)
3947
return output
4048
}
4149

42-
func Find[T any](input []T, callback func(element T, index int) bool) T {
50+
// conditions can be callback or a value
51+
// callback has two parameter one is element and another is index
52+
// callback must return true or false
53+
func Find[T any](input []T, conditions interface{}) T {
4354
var item T
55+
conditionType := getType(conditions)
4456
for index, value := range input {
45-
if callback(value, index) {
57+
if conditionType == "func" && callCallback(conditions, value, index) {
58+
item = value
59+
break
60+
} else if conditionType != "func" && compareValue(conditions, value) {
4661
item = value
4762
break
4863
}
@@ -57,21 +72,31 @@ func Foreach[T any](input []T, callback func(element T, index int)) {
5772
}
5873
}
5974

75+
func callCallback[T any](conditions interface{}, value T, index int) bool {
76+
res := reflect.ValueOf(conditions).Call([]reflect.Value{reflect.ValueOf(value), reflect.ValueOf(index)})
77+
78+
return res[0].Bool()
79+
}
80+
81+
func compareValue[T any](conditions interface{}, value T) bool {
82+
res := reflect.ValueOf(conditions).String()
83+
return res == fmt.Sprintf("%v", value)
84+
}
85+
86+
func getType(conditions interface{}) string {
87+
return reflect.TypeOf(conditions).Kind().String()
88+
}
89+
6090
func calculateEveryElement[T any](input []T, conditions interface{}) []bool {
6191
output := []bool{}
6292

63-
conditionType := reflect.TypeOf(conditions).Kind().String()
64-
93+
conditionType := getType(conditions)
6594
for index, value := range input {
6695
result := false
6796
if conditionType == "func" {
68-
res := reflect.ValueOf(conditions).Call([]reflect.Value{reflect.ValueOf(value), reflect.ValueOf(index)})
69-
result = res[0].Bool()
97+
result = callCallback(conditions, value, index)
7098
} else {
71-
res := reflect.ValueOf(conditions).String()
72-
if res == fmt.Sprintf("%v", value) {
73-
result = true
74-
}
99+
result = compareValue(conditions, value)
75100
}
76101
if result {
77102
output = append(output, true)
@@ -96,3 +121,14 @@ func Some[T any](input []T, conditions interface{}) any {
96121
output := calculateEveryElement(input, conditions)
97122
return len(output) > 0
98123
}
124+
125+
func Includes[T any](input []T, conditions interface{}) bool {
126+
result := Find(input, conditions)
127+
128+
if getType(result) == "struct" && reflect.ValueOf(result).IsZero() {
129+
return false
130+
} else if getType(result) != "struct" && fmt.Sprintf("%v", result) == "" {
131+
return false
132+
}
133+
return true
134+
}

0 commit comments

Comments
 (0)