Skip to content

Commit

Permalink
Merge pull request #115 from warriors-vn/dev-feature
Browse files Browse the repository at this point in the history
chore: public all function
  • Loading branch information
nguyenvantuan2391996 authored Aug 7, 2023
2 parents 9abc651 + a251eda commit a935233
Show file tree
Hide file tree
Showing 160 changed files with 812 additions and 812 deletions.
10 changes: 5 additions & 5 deletions array/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/warriors-vn/go-dash/constants"
)

// chunk divides a given array into smaller chunks of the specified size.
// The function accepts an array of any type (interface{}) and a size for each chunk.
// It returns a slice of slices, where each inner slice contains elements from the original array.
// The last chunk may contain fewer elements if the length of the array is not divisible by the chunk size.
func chunk(array interface{}, size int) (interface{}, error) {
// Chunk divides a given array into smaller chunks of the specified size.
// The function accepts an array of any type (interface{}) and a size for each Chunk.
// It returns a Slice of slices, where each inner Slice contains elements from the original array.
// The Last Chunk may contain fewer elements if the length of the array is not divisible by the Chunk size.
func Chunk(array interface{}, size int) (interface{}, error) {
arrValue := reflect.ValueOf(array)

if size < 0 {
Expand Down
24 changes: 12 additions & 12 deletions array/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,84 @@ import (
)

func Test_chunk_valid_int(t *testing.T) {
result, err := chunk([]int{1, 2, 3}, 1)
result, err := Chunk([]int{1, 2, 3}, 1)

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

func Test_chunk_valid_int32(t *testing.T) {
result, err := chunk([]int32{1, 2, 3}, 2)
result, err := Chunk([]int32{1, 2, 3}, 2)

assert.Equal(t, [][]int32{{1, 2}, {3}}, result)
assert.Nil(t, err)
}

func Test_chunk_valid_int64(t *testing.T) {
result, err := chunk([]int64{1, 2, 3}, 2)
result, err := Chunk([]int64{1, 2, 3}, 2)

assert.Equal(t, [][]int64{{1, 2}, {3}}, result)
assert.Nil(t, err)
}

func Test_chunk_valid_float32(t *testing.T) {
result, err := chunk([]float32{1.1, 2.2, 3.3}, 2)
result, err := Chunk([]float32{1.1, 2.2, 3.3}, 2)

assert.Equal(t, [][]float32{{1.1, 2.2}, {3.3}}, result)
assert.Nil(t, err)
}

func Test_chunk_valid_float64(t *testing.T) {
result, err := chunk([]float64{1.1, 2.2, 3.3}, 2)
result, err := Chunk([]float64{1.1, 2.2, 3.3}, 2)

assert.Equal(t, [][]float64{{1.1, 2.2}, {3.3}}, result)
assert.Nil(t, err)
}

func Test_chunk_valid_string(t *testing.T) {
result, err := chunk([]string{"a", "b", "c", "d"}, 3)
result, err := Chunk([]string{"a", "b", "c", "d"}, 3)

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

func Test_chunk_valid_size_great_than_array_length(t *testing.T) {
result, err := chunk([]int{1, 2, 3}, 9)
result, err := Chunk([]int{1, 2, 3}, 9)

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

func Test_chunk_invalid_array_empty(t *testing.T) {
result, err := chunk([]int{}, 1)
result, err := Chunk([]int{}, 1)

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

func Test_chunk_invalid_size_zero(t *testing.T) {
result, err := chunk([]int{1, 2, 3}, 0)
result, err := Chunk([]int{1, 2, 3}, 0)

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

func Test_chunk_invalid_array_not_slice(t *testing.T) {
result, err := chunk(true, 1)
result, err := Chunk(true, 1)

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

func Test_chunk_invalid_array_interface(t *testing.T) {
result, err := chunk([]interface{}{1, "2", true}, 1)
result, err := Chunk([]interface{}{1, "2", true}, 1)

assert.Equal(t, [][]interface{}{{1}, {"2"}, {true}}, result)
assert.Nil(t, err)
}

func Test_chunk_invalid_size_less_than_zero(t *testing.T) {
result, err := chunk([]int{1, 2, 3}, -1)
result, err := Chunk([]int{1, 2, 3}, -1)

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrParamLessThanZero, err)
Expand Down
4 changes: 2 additions & 2 deletions array/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/warriors-vn/go-dash/constants"
)

// concat concatenates two arrays into a single array.
// Concat concatenates two arrays into a single array.
// The function accepts two arrays of any type (interface{}) and returns a single concatenated array.
func concat(array, extend interface{}) (interface{}, error) {
func Concat(array, extend interface{}) (interface{}, error) {
arrValue, extendValue := reflect.ValueOf(array), reflect.ValueOf(extend)

if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) ||
Expand Down
38 changes: 19 additions & 19 deletions array/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,133 +8,133 @@ import (
)

func Test_concat_valid_int(t *testing.T) {
result, err := concat([]int{1, 2, 3}, []int{4, 5, 6, 7, 8})
result, err := Concat([]int{1, 2, 3}, []int{4, 5, 6, 7, 8})

assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8}, result)
assert.Nil(t, err)
}

func Test_concat_valid_int32(t *testing.T) {
result, err := concat([]int32{1, 2, 3}, []int32{4, 5, 6, 7, 8})
result, err := Concat([]int32{1, 2, 3}, []int32{4, 5, 6, 7, 8})

assert.Equal(t, []int32{1, 2, 3, 4, 5, 6, 7, 8}, result)
assert.Nil(t, err)
}

func Test_concat_valid_int64(t *testing.T) {
result, err := concat([]int64{1, 2, 3}, []int64{4, 5, 6, 7, 8})
result, err := Concat([]int64{1, 2, 3}, []int64{4, 5, 6, 7, 8})

assert.Equal(t, []int64{1, 2, 3, 4, 5, 6, 7, 8}, result)
assert.Nil(t, err)
}

func Test_concat_valid_float32(t *testing.T) {
result, err := concat([]float32{1.1, 2.2, 3.3}, []float32{4.4, 5.5, 6.6, 7.7, 8.8})
result, err := Concat([]float32{1.1, 2.2, 3.3}, []float32{4.4, 5.5, 6.6, 7.7, 8.8})

assert.Equal(t, []float32{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8}, result)
assert.Nil(t, err)
}

func Test_concat_valid_float64(t *testing.T) {
result, err := concat([]float64{1.1, 2.2, 3.3}, []float64{4.4, 5.5, 6.6, 7.7, 8.8})
result, err := Concat([]float64{1.1, 2.2, 3.3}, []float64{4.4, 5.5, 6.6, 7.7, 8.8})

assert.Equal(t, []float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8}, result)
assert.Nil(t, err)
}

func Test_concat_valid_string(t *testing.T) {
result, err := concat([]string{"a", "b"}, []string{"f", "e", "d"})
result, err := Concat([]string{"a", "b"}, []string{"f", "e", "d"})

assert.Equal(t, []string{"a", "b", "f", "e", "d"}, result)
assert.Nil(t, err)
}

func Test_concat_valid_interface(t *testing.T) {
result, err := concat([]interface{}{1, "2", true}, []interface{}{true, false})
result, err := Concat([]interface{}{1, "2", true}, []interface{}{true, false})

assert.Equal(t, []interface{}{1, "2", true, true, false}, result)
assert.Nil(t, err)
}

func Test_concat_valid_bool(t *testing.T) {
result, err := concat([]bool{true}, []bool{false})
result, err := Concat([]bool{true}, []bool{false})

assert.Equal(t, []bool{true, false}, result)
assert.Nil(t, err)
}

func Test_concat_valid_incompatible(t *testing.T) {
result, err := concat([]interface{}{1, "2", true}, []float64{1.1, 2.2})
result, err := Concat([]interface{}{1, "2", true}, []float64{1.1, 2.2})

assert.Equal(t, []interface{}{1, "2", true, 1.1, 2.2}, result)
assert.Nil(t, err)
}

func Test_concat_invalid_array_empty(t *testing.T) {
result, err := concat([]int{}, []int{1})
result, err := Concat([]int{}, []int{1})

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

func Test_concat_invalid_extend_empty(t *testing.T) {
result, err := concat([]int{1}, []int{})
result, err := Concat([]int{1}, []int{})

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

func Test_concat_invalid_array_not_slice(t *testing.T) {
result, err := concat(true, []int{1})
result, err := Concat(true, []int{1})

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

func Test_concat_invalid_extend_not_slice(t *testing.T) {
result, err := concat([]int{1}, true)
result, err := Concat([]int{1}, true)

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

func Test_concat_invalid_int_array_extend_incompatible(t *testing.T) {
result, err := concat([]int{1}, []float64{1.1})
result, err := Concat([]int{1}, []float64{1.1})

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

func Test_concat_invalid_int32_array_extend_incompatible(t *testing.T) {
result, err := concat([]int32{1}, []float64{1.1})
result, err := Concat([]int32{1}, []float64{1.1})

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

func Test_concat_invalid_int64_array_extend_incompatible(t *testing.T) {
result, err := concat([]int64{1}, []float64{1.1})
result, err := Concat([]int64{1}, []float64{1.1})

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

func Test_concat_invalid_float32_array_extend_incompatible(t *testing.T) {
result, err := concat([]float32{1}, []float64{1.1})
result, err := Concat([]float32{1}, []float64{1.1})

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

func Test_concat_invalid_float64_array_extend_incompatible(t *testing.T) {
result, err := concat([]float64{1.2}, []int{1})
result, err := Concat([]float64{1.2}, []int{1})

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

func Test_concat_invalid_string_array_extend_incompatible(t *testing.T) {
result, err := concat([]string{"1"}, []int{1})
result, err := Concat([]string{"1"}, []int{1})

assert.Equal(t, nil, result)
assert.Equal(t, constants.ErrIncompatible, err)
Expand Down
6 changes: 3 additions & 3 deletions array/difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/warriors-vn/go-dash/constants"
)

// difference returns a new slice that contains the elements from the input array that are not present in the exclude slices.
// Difference returns a new Slice that contains the elements from the input array that are not present in the exclude slices.
// It takes an array-like data structure and one or more exclude slices.
// The function returns the new slice of different elements and an error if any occurs.
func difference(array interface{}, exclude ...interface{}) (interface{}, error) {
// The function returns the new Slice of different elements and an error if any occurs.
func Difference(array interface{}, exclude ...interface{}) (interface{}, error) {
arrValue := reflect.ValueOf(array)

if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array {
Expand Down
Loading

0 comments on commit a935233

Please sign in to comment.