Skip to content

Commit 8899566

Browse files
committed
Added map filter reduce foreach find with test and doc
0 parents  commit 8899566

8 files changed

+270
-0
lines changed

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# go-js-array-methods
2+
3+
This package is to implement javacript array methods which is missing in golang
4+
5+
## Installation
6+
7+
```
8+
go get github.com/dev-parvej/go-js-array-methods
9+
10+
```
11+
12+
## Usage
13+
14+
```
15+
import js "github.com/dev-parvej/go-js-array-methods"
16+
17+
type User struct {
18+
Id int
19+
Name string
20+
}
21+
22+
type Foo struct {
23+
Bar string
24+
}
25+
26+
users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
27+
28+
```
29+
30+
### Filter
31+
32+
```
33+
filteredUsers := js.Filter(users, func(user User, index int) bool {
34+
return user.Id == 10
35+
})
36+
37+
```
38+
39+
### Map
40+
41+
```
42+
mapedUsers := js.Map(users, func(user User, index int) Foo {
43+
return Foo{
44+
Bar: fmt.Sprintf("%d %s", user.Id, user.Name),
45+
}
46+
})
47+
48+
```
49+
50+
### Reduce
51+
```
52+
numbers := []int{10, 11, 12, 13}
53+
sum := js.Reduce(numbers, func(s int, n int, i int) int {
54+
return s + n
55+
}, 0)
56+
57+
```
58+

filter_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package js_array_method
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFilterWithStructSlice(t *testing.T) {
8+
users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
9+
10+
mapedUsers := Filter(users, func(user User, index int) bool {
11+
return user.Id == 10
12+
})
13+
expected := 10
14+
15+
if mapedUsers[0].Id != 10 {
16+
t.Errorf("Expected %d received %d", expected, mapedUsers[0].Id)
17+
}
18+
}
19+
20+
func TestFilterWithStringSlice(t *testing.T) {
21+
var slices = []string{"Go", "Typescript", "Nodejs"}
22+
23+
ln := Filter(slices, func(ln string, index int) bool {
24+
return ln == "Go"
25+
})
26+
27+
expected := "Go"
28+
29+
if ln[0] != expected {
30+
t.Errorf("Expected %s received %s", expected, ln[0])
31+
}
32+
}

find_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package js_array_method
2+
3+
import "testing"
4+
5+
func TestFindWithStructSlice(t *testing.T) {
6+
users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
7+
8+
user := Find(users, func(user User, index int) bool {
9+
return user.Id == 10
10+
})
11+
12+
expected := "Go"
13+
14+
if user.Name != expected {
15+
t.Errorf("Expected %s received %s", expected, user.Name)
16+
}
17+
}
18+
19+
func TestFindWithStringSlice(t *testing.T) {
20+
var slices = []string{"Go", "Typescript", "Nodejs"}
21+
22+
ln := Find(slices, func(ln string, index int) bool {
23+
return ln == "Go"
24+
})
25+
26+
expected := "Go"
27+
28+
if ln != expected {
29+
t.Errorf("Expected %s received %s", expected, ln)
30+
}
31+
}

forech_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package js_array_method
2+
3+
import "testing"
4+
5+
func TestForeachWithStructSlice(t *testing.T) {
6+
users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
7+
8+
Foreach(users, func(user User, index int) {
9+
if index == 0 && user.Name != "Go" {
10+
t.Errorf("Expected %s received %s", "Go", user.Name)
11+
}
12+
})
13+
14+
}
15+
16+
func TestForeachWithStringSlice(t *testing.T) {
17+
var slices = []string{"Go", "Typescript", "Nodejs"}
18+
19+
Foreach(slices, func(ln string, index int) {
20+
if index == 0 && ln != "Go" {
21+
t.Errorf("Expected %s received %s", "Go", ln)
22+
}
23+
})
24+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/dev-parvej/js_array_method
2+
3+
go 1.19

js_array_method.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package js_array_method
2+
3+
func Map[T any, U any](input []T, callback func(element T, index int) U) []U {
4+
var output []U
5+
6+
for index, value := range input {
7+
result := callback(value, index)
8+
9+
output = append(output, result)
10+
}
11+
12+
return output
13+
}
14+
15+
func Filter[T any](input []T, callback func(element T, index int) bool) []T {
16+
var output []T
17+
18+
for index, value := range input {
19+
if callback(value, index) {
20+
output = append(output, value)
21+
}
22+
}
23+
24+
return output
25+
}
26+
27+
func Reduce[T any, U any](input []T, callback func(pre U, current T, index int) U, initialValue U) U {
28+
output := initialValue
29+
30+
for index, value := range input {
31+
output = callback(output, value, index)
32+
}
33+
34+
return output
35+
}
36+
37+
func Find[T any](input []T, callback func(element T, index int) bool) T {
38+
var item T
39+
for index, value := range input {
40+
if callback(value, index) {
41+
item = value
42+
break
43+
}
44+
}
45+
46+
return item
47+
}
48+
49+
func Foreach[T any](input []T, callback func(element T, index int)) {
50+
for index, value := range input {
51+
callback(value, index)
52+
}
53+
}

map_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package js_array_method
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type User struct {
9+
Id int
10+
Name string
11+
}
12+
13+
type Foo struct {
14+
Bar string
15+
}
16+
17+
func TestMapWithStructSlice(t *testing.T) {
18+
users := []User{{Id: 10, Name: "Go"}, {Id: 11, Name: "Lang"}}
19+
20+
mapedUsers := Map(users, func(user User, index int) Foo {
21+
return Foo{
22+
Bar: fmt.Sprintf("%d %s", user.Id, user.Name),
23+
}
24+
})
25+
26+
expected := "10 Go"
27+
if mapedUsers[0].Bar != expected {
28+
t.Errorf("Expected %s received %s", expected, mapedUsers[0].Bar)
29+
}
30+
expected = "11 Lang"
31+
if mapedUsers[1].Bar != expected {
32+
t.Errorf("Expected %s received %s", expected, mapedUsers[1].Bar)
33+
}
34+
}
35+
36+
func TestMapWithStringSlice(t *testing.T) {
37+
var slices = []string{"Go", "Typescript", "Nodejs"}
38+
39+
mappedSlice := Map(slices, func(ln string, index int) string {
40+
return fmt.Sprintf("%d/%s", index, ln)
41+
})
42+
43+
expected := "0/Go"
44+
45+
if mappedSlice[0] != expected {
46+
t.Errorf("Expected %s received %s", expected, mappedSlice[0])
47+
}
48+
49+
expected = "1/Typescript"
50+
51+
if mappedSlice[1] != expected {
52+
t.Errorf("Expected %s received %s", expected, mappedSlice[1])
53+
}
54+
}

reduce_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package js_array_method
2+
3+
import "testing"
4+
5+
func TestReduce(t *testing.T) {
6+
numbers := []int{10, 11, 12, 13}
7+
8+
sum := Reduce(numbers, func(s int, n int, i int) int {
9+
return s + n
10+
}, 0)
11+
12+
if sum != 46 {
13+
t.Errorf("Expected %d received %d", 46, sum)
14+
}
15+
}

0 commit comments

Comments
 (0)