-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from warriors-vn/dev-feature
feat: add the func includes
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package collection | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/warriors-vn/go-dash/constants" | ||
) | ||
|
||
// includes checks if the input array contains the specified search value. | ||
// It takes an array-like data structure, a search value, and an optional starting index. | ||
// If the starting index is provided, the search starts from that index. | ||
// The function returns true if the search is found, false otherwise, or an error if any occurs. | ||
func includes(array interface{}, search interface{}, fromIndex ...int) (interface{}, error) { | ||
arrValue, searchValue := reflect.ValueOf(array), reflect.ValueOf(search) | ||
|
||
if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { | ||
return false, constants.ErrNotSlice | ||
} | ||
|
||
from := 0 | ||
if fromIndex != nil && fromIndex[0] > 0 { | ||
from = fromIndex[0] | ||
} | ||
|
||
if from >= arrValue.Len() { | ||
return false, nil | ||
} | ||
|
||
for i := from; i < arrValue.Len(); i++ { | ||
if reflect.DeepEqual(arrValue.Index(i).Interface(), searchValue.Interface()) { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package collection | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/warriors-vn/go-dash/constants" | ||
) | ||
|
||
func Test_includes_valid_int(t *testing.T) { | ||
result, err := includes([]int{1, 2, 3, 4, 5}, 3) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_int64(t *testing.T) { | ||
result, err := includes([]int64{1, 2, 3, 4, 5}, int64(5)) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_float64(t *testing.T) { | ||
result, err := includes([]float64{1.1, 2.2, 3.3, 4.4, 5.5}, 5.5, 2) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_string(t *testing.T) { | ||
result, err := includes([]string{"1.1", "2.2", "3.3", "4.4", "5.5"}, "5.5", 3) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_interface(t *testing.T) { | ||
result, err := includes([]interface{}{"1.1", true, 3.3, 4, false}, true) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_not_include(t *testing.T) { | ||
result, err := includes([]interface{}{"1.1", true, 3.3, false}, 3) | ||
|
||
assert.Equal(t, false, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_bool(t *testing.T) { | ||
result, err := includes([]bool{true, true, false}, true, 1) | ||
|
||
assert.Equal(t, true, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_valid_from_great_than_array_length(t *testing.T) { | ||
result, err := includes([]bool{true, true, false}, true, 10) | ||
|
||
assert.Equal(t, false, result) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_includes_invalid_array_not_slice(t *testing.T) { | ||
result, err := includes(true, true) | ||
|
||
assert.Equal(t, false, result) | ||
assert.Equal(t, constants.ErrNotSlice, err) | ||
} |