Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the func remove #88

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions array/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package array

import (
"reflect"

"github.com/warriors-vn/go-dash/constants"
)

// remove elements from the input array based on a given predicate function.
// It takes an array-like data structure and a predicate function that determines
// whether an element should be removed.
// The function returns the modified array and an error if any occurs.
func remove(array interface{}, predicate interface{}) (interface{}, error) {
arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate)

if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array {
return nil, constants.ErrNotSlice
}

if predicateValue.Kind() != reflect.Func {
return nil, constants.ErrNotFunction
}

numParams := predicateValue.Type().NumIn()
if numParams != 1 {
return nil, constants.ErrNotSupport
}

kind, result := predicateValue.Type().In(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0)
for i := 0; i < arrValue.Len(); i++ {
element := arrValue.Index(i)
if element.Kind() != kind {
return nil, constants.ErrIncompatible
}

res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())})
if len(res) > 0 && res[0].Interface().(bool) {
result = reflect.Append(result, element)
}
}

return result.Interface(), nil
}
60 changes: 60 additions & 0 deletions array/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package array

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/warriors-vn/go-dash/constants"
)

func Test_remove_valid_int(t *testing.T) {
result, err := remove([]int{1, 2, 3, 4}, func(n int) bool {
return n%2 == 0
})

assert.Equal(t, []int{2, 4}, result)
assert.Nil(t, err)
}

func Test_remove_valid_string(t *testing.T) {
result, err := remove([]string{"a", "b", "c"}, func(n string) bool {
return n > "a"
})

assert.Equal(t, []string{"b", "c"}, result)
assert.Nil(t, err)
}

func Test_remove_valid_string_string_two(t *testing.T) {
result, err := remove([]string{"a", "b", "c"}, func(n int) bool {
return n%2 == 0
})

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrIncompatible, err)
}

func Test_remove_invalid_array_not_slice(t *testing.T) {
result, err := remove(true, func(n int) bool {
return n%2 == 0
})

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrNotSlice, err)
}

func Test_remove_invalid_predicate_not_func(t *testing.T) {
result, err := remove([]string{"a", "b", "c"}, true)

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrNotFunction, err)
}

func Test_remove_invalid_param_predicate_limit(t *testing.T) {
result, err := remove([]string{"a", "b", "c"}, func(n string, m int) bool {
return n == "a" || m%2 == 0
})

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrNotSupport, err)
}
1 change: 1 addition & 0 deletions constants/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ var (
ErrFieldNotFound = errors.New("field not found")
ErrIncompatible = errors.New("incompatible field types")
ErrParamLessThanZero = errors.New("param is less than zero")
ErrNotFunction = errors.New("input is not a function")
)
Loading