11package js_array_method
22
33import (
4- "fmt"
5- "reflect"
4+ "errors"
65)
76
7+ func Foreach [T any ](input []T , callback func (element T , index int )) {
8+ for index , value := range input {
9+ callback (value , index )
10+ }
11+ }
12+
813func Map [T any , U any ](input []T , callback func (element T , index int ) U ) []U {
914 var output []U
1015
@@ -50,60 +55,36 @@ func Reduce[T any, U any](input []T, callback func(pre U, current T, index int)
5055// conditions can be callback or a value
5156// callback has two parameter one is element and another is index
5257// callback must return true or false
53- func Find [T any ](input []T , conditions interface {}) T {
58+ func Find [T any ](input []T , conditions interface {}) ( T , error ) {
5459 var item T
55- conditionType := getType (conditions )
56- for index , value := range input {
57- if conditionType == "func" && callCallback (conditions , value , index ) {
58- item = value
59- break
60- } else if conditionType != "func" && compareValue (conditions , value ) {
61- item = value
62- break
63- }
64- }
60+ index := FindIndex (input , conditions )
6561
66- return item
67- }
68-
69- func Foreach [T any ](input []T , callback func (element T , index int )) {
70- for index , value := range input {
71- callback (value , index )
62+ if index < 0 {
63+ return item , errors .New ("nothing found" )
7264 }
73- }
7465
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 )})
66+ item = input [index ]
7767
78- return res [ 0 ]. Bool ()
68+ return item , nil
7969}
8070
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-
90- func calculateEveryElement [T any ](input []T , conditions interface {}) []bool {
91- output := []bool {}
92-
71+ // conditions can be callback or a value
72+ // callback has two parameter one is element and another is index
73+ // callback must return true or false
74+ func FindIndex [T any ](input []T , conditions interface {}) int {
75+ var itemIndex int = - 1
9376 conditionType := getType (conditions )
9477 for index , value := range input {
95- result := false
96- if conditionType == "func" {
97- result = callCallback (conditions , value , index )
98- } else {
99- result = compareValue (conditions , value )
100- }
101- if result {
102- output = append (output , true )
78+ if conditionType == "func" && callCallback (conditions , value , index ) {
79+ itemIndex = index
80+ break
81+ } else if conditionType != "func" && compareValue (conditions , value ) {
82+ itemIndex = index
83+ break
10384 }
10485 }
10586
106- return output
87+ return itemIndex
10788}
10889
10990// conditions can be callback or a value
@@ -126,14 +107,8 @@ func Some[T any](input []T, conditions interface{}) any {
126107// callback has two parameter one is element and another is index
127108// callback must return true or false
128109func Includes [T any ](input []T , conditions interface {}) bool {
129- result := Find (input , conditions )
130-
131- if getType (result ) == "struct" && reflect .ValueOf (result ).IsZero () {
132- return false
133- } else if getType (result ) != "struct" && fmt .Sprintf ("%v" , result ) == "" {
134- return false
135- }
136- return true
110+ result := FindIndex (input , conditions )
111+ return result >= 0
137112}
138113
139114func Reverse [T any ](input []T ) []T {
0 commit comments