├── .gitignore ├── constants ├── pattern.go └── error.go ├── date ├── now_test.go └── now.go ├── string ├── replace_test.go ├── lower_first.go ├── upper_first.go ├── escape.go ├── capitalize.go ├── unescape.go ├── repeat.go ├── replace.go ├── parse_int.go ├── escape_test.go ├── lower_first_test.go ├── split.go ├── repeat_test.go ├── upper_case.go ├── upper_first_test.go ├── unescape_test.go ├── kebab_case.go ├── lower_case.go ├── upper_case_test.go ├── capitalize_test.go ├── camel_case_test.go ├── split_test.go ├── starts_with.go ├── snake_case_test.go ├── ends_with.go ├── snake_case.go ├── pad_end_test.go ├── camel_case.go ├── pad_start_test.go ├── kebab_case_test.go ├── lower_case_test.go ├── pad_end.go ├── pad_test.go ├── pad_start.go ├── parse_int_test.go ├── pad.go ├── ends_with_test.go └── starts_with_test.go ├── go.mod ├── lang ├── isString.go ├── isArray_test.go ├── eq.go ├── isArray.go ├── isString_test.go ├── isNumber.go ├── isNumber_test.go ├── toString_test.go ├── toString.go ├── castArray.go ├── lt.go ├── lte.go ├── eq_test.go ├── castArray_test.go ├── lt_test.go └── lte_test.go ├── go_math ├── divide.go ├── mean.go ├── round.go ├── divide_test.go ├── ceil.go ├── floor.go ├── ceil_test.go ├── floor_test.go ├── round_test.go ├── mean_test.go ├── sum.go ├── add_test.go ├── subtract.go ├── subtract_test.go ├── add.go ├── multiply.go ├── multiply_test.go ├── sum_test.go ├── max.go ├── min.go ├── min_test.go └── max_test.go ├── array ├── head.go ├── last.go ├── nth.go ├── head_test.go ├── last_test.go ├── reverse.go ├── join.go ├── find_index.go ├── isContains.go ├── tail.go ├── initial.go ├── lastIndexOf.go ├── sortedUniq_test.go ├── slice.go ├── drop.go ├── indexOf.go ├── pullAt.go ├── take.go ├── without.go ├── takeRight.go ├── sortedUniq.go ├── dropRight.go ├── indexOf_test.go ├── pullAll.go ├── concat.go ├── intersection.go ├── chunk.go ├── fill.go ├── pullAt_test.go ├── difference.go ├── union.go ├── reverse_test.go ├── lastIndexOf_test.go ├── union_test.go ├── remove.go ├── take_test.go ├── tail_test.go ├── takeRight_test.go ├── xor_test.go ├── without_test.go ├── join_test.go ├── isContains_test.go ├── pullAll_test.go ├── nth_test.go ├── find_index_test.go ├── xor.go ├── initial_test.go ├── intersection_test.go ├── slice_test.go ├── chunk_test.go ├── remove_test.go ├── drop_test.go ├── difference_test.go └── dropRight_test.go ├── number ├── random.go ├── random_test.go ├── clamp_test.go ├── clamp.go ├── inRange.go └── inRange_test.go ├── collection ├── sample.go ├── sample_test.go ├── sampleSize.go ├── includes.go ├── sampleSize_test.go ├── find.go ├── findLast.go ├── filter.go ├── every.go ├── reject.go ├── map.go ├── includes_test.go ├── partition.go ├── find_test.go ├── every_test.go ├── reject_test.go ├── filter_test.go ├── findLast_test.go ├── partition_test.go └── map_test.go ├── go.sum ├── LICENSE └── .github └── workflows └── workflows-be.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | .idea -------------------------------------------------------------------------------- /constants/pattern.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | const ( 4 | PatternNumberAndAlphabet = "[0-9a-zA-Z]+" 5 | ) 6 | -------------------------------------------------------------------------------- /date/now_test.go: -------------------------------------------------------------------------------- 1 | package date 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_now(t *testing.T) { 9 | result := Now() 10 | fmt.Println(result) 11 | } 12 | -------------------------------------------------------------------------------- /date/now.go: -------------------------------------------------------------------------------- 1 | package date 2 | 3 | import "time" 4 | 5 | // Now returns the current timestamp in Unix time (seconds since January 1, 1970 UTC). 6 | // It returns the timestamp as an int64 value. 7 | func Now() int64 { 8 | return time.Now().UTC().Unix() 9 | } 10 | -------------------------------------------------------------------------------- /string/replace_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_replace_valid(t *testing.T) { 10 | result := Replace("Hi Fred", "Fred", "Barney") 11 | 12 | assert.Equal(t, "Hi Barney", result) 13 | } 14 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/warriors-vn/go-dash 2 | 3 | go 1.18 4 | 5 | require github.com/stretchr/testify v1.8.4 6 | 7 | require ( 8 | github.com/davecgh/go-spew v1.1.1 // indirect 9 | github.com/pmezard/go-difflib v1.0.0 // indirect 10 | gopkg.in/yaml.v3 v3.0.1 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /string/lower_first.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // LowerFirst converts the first letter of a string to lowercase while leaving the rest unchanged. 6 | func LowerFirst(s string) string { 7 | if len(s) == 0 { 8 | return s 9 | } 10 | 11 | return strings.ToLower(s[:1]) + s[1:] 12 | } 13 | -------------------------------------------------------------------------------- /string/upper_first.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // UpperFirst converts the first letter of a string to uppercase while leaving the rest unchanged. 6 | func UpperFirst(s string) string { 7 | if len(s) == 0 { 8 | return s 9 | } 10 | 11 | return strings.ToUpper(s[:1]) + s[1:] 12 | } 13 | -------------------------------------------------------------------------------- /string/escape.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "html" 4 | 5 | // Escape converts special characters in a string to their corresponding HTML entities. 6 | // It returns the escaped string where special characters are replaced by their entities. 7 | func Escape(s string) string { 8 | return html.EscapeString(s) 9 | } 10 | -------------------------------------------------------------------------------- /string/capitalize.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // Capitalize the first letter of a string and leaves the rest unchanged. 6 | func Capitalize(s string) string { 7 | if len(s) == 0 { 8 | return s 9 | } 10 | 11 | lower := strings.ToLower(s) 12 | 13 | return strings.ToUpper(lower[:1]) + lower[1:] 14 | } 15 | -------------------------------------------------------------------------------- /lang/isString.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import "reflect" 4 | 5 | // IsString checks if a given value is a string type. 6 | // The function accepts a value of any type (interface{}) and returns true if it is a string, false otherwise. 7 | func IsString(value interface{}) bool { 8 | return reflect.TypeOf(value).Kind() == reflect.String 9 | } 10 | -------------------------------------------------------------------------------- /go_math/divide.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import "math" 4 | 5 | // Divide calculates the division of the given 'dividend' by the 'divisor'. 6 | // It returns the result of the division as a float64. 7 | func Divide(dividend, divisor float64) float64 { 8 | if divisor == 0.0 { 9 | return math.Inf(1) 10 | } 11 | 12 | result := dividend / divisor 13 | return result 14 | } 15 | -------------------------------------------------------------------------------- /lang/isArray_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_isArray_valid(t *testing.T) { 10 | result := IsArray([]int{1, 2, 3}) 11 | 12 | assert.Equal(t, true, result) 13 | } 14 | 15 | func Test_isArray_invalid(t *testing.T) { 16 | result := IsArray(123) 17 | 18 | assert.Equal(t, false, result) 19 | } 20 | -------------------------------------------------------------------------------- /string/unescape.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "html" 5 | ) 6 | 7 | // Unescape a string that contains Escape sequences and returns the unescaped string. 8 | // The input string should be surrounded by double quotes to properly Unescape it. 9 | // If un-escaping fails, the original input string is returned. 10 | func Unescape(s string) string { 11 | return html.UnescapeString(s) 12 | } 13 | -------------------------------------------------------------------------------- /go_math/mean.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | // Mean calculates the Mean (average) value of a slice of float64 numbers. 4 | // It returns the Mean value as a float64. 5 | func Mean(array []float64) float64 { 6 | if len(array) == 0 { 7 | return float64(0) 8 | } 9 | 10 | sumArr := float64(0) 11 | for _, value := range array { 12 | sumArr += value 13 | } 14 | 15 | return sumArr / float64(len(array)) 16 | } 17 | -------------------------------------------------------------------------------- /string/repeat.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // Repeat the given string 's' a specified number of 'times'. 6 | // It returns the concatenated string where 's' is repeated 'times' times. 7 | // If 'times' is zero or negative, an empty string is returned. 8 | func Repeat(s string, times int) string { 9 | if times <= 0 { 10 | return "" 11 | } 12 | 13 | return strings.Repeat(s, times) 14 | } 15 | -------------------------------------------------------------------------------- /string/replace.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // Replace occurrences of a specified 'pattern' in the string 's' with the 'replacement' string. 6 | // It returns the modified string where all occurrences of 'pattern' are replaced with 'replacement'. 7 | func Replace(s, pattern, replacement string) string { 8 | modified := strings.Replace(s, pattern, replacement, -1) 9 | return modified 10 | } 11 | -------------------------------------------------------------------------------- /go_math/round.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import "math" 4 | 5 | // Round rounds a float64 number to the specified precision. 6 | // If precision is not provided, it defaults to 0 (no decimal places). 7 | func Round(num float64, precision ...int) float64 { 8 | pre := 0 9 | if precision != nil { 10 | pre = precision[0] 11 | } 12 | scale := math.Pow(10, float64(pre)) 13 | 14 | return math.Round(num*scale) / scale 15 | } 16 | -------------------------------------------------------------------------------- /string/parse_int.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | // ParseInt parses an integer from the given string 's'. 9 | // It returns the parsed integer value and an error if parsing fails. 10 | func ParseInt(s string) (int, error) { 11 | parsedInt, err := strconv.Atoi(strings.Replace(s, " ", "", -1)) 12 | if err != nil { 13 | return 0, err 14 | } 15 | return parsedInt, nil 16 | } 17 | -------------------------------------------------------------------------------- /lang/eq.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import "reflect" 4 | 5 | // Eq checks if two values are equal using a deep comparison. 6 | // The function accepts two values of any type (interface{}) and returns true if they are equal, false otherwise. 7 | // It performs a deep comparison for slices and maps, and uses regular comparison for other types. 8 | func Eq(value, other interface{}) bool { 9 | return reflect.DeepEqual(value, other) 10 | } 11 | -------------------------------------------------------------------------------- /go_math/divide_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "math" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_divide_valid(t *testing.T) { 11 | result := Divide(float64(6), float64(4)) 12 | 13 | assert.Equal(t, 1.5, result) 14 | } 15 | 16 | func Test_divide_divisor_zero(t *testing.T) { 17 | result := Divide(float64(6), float64(0)) 18 | 19 | assert.Equal(t, math.Inf(1), result) 20 | } 21 | -------------------------------------------------------------------------------- /string/escape_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_escape_valid_one(t *testing.T) { 10 | result := Escape("fred, barney, & pebbles") 11 | 12 | assert.Equal(t, "fred, barney, & pebbles", result) 13 | } 14 | 15 | func Test_escape_valid_two(t *testing.T) { 16 | result := Escape("Hello ") 17 | 18 | assert.Equal(t, "Hello <World>", result) 19 | } 20 | -------------------------------------------------------------------------------- /lang/isArray.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "reflect" 5 | ) 6 | 7 | // IsArray checks if a given input is an array (slice). 8 | // The function accepts a value of any type (interface{}) and returns true if it is an array, false otherwise. 9 | func IsArray(input interface{}) bool { 10 | inputValue := reflect.ValueOf(input) 11 | 12 | if inputValue.Kind() == reflect.Slice || inputValue.Kind() == reflect.Array { 13 | return true 14 | } 15 | 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /go_math/ceil.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import "math" 4 | 5 | // Ceil rounds the given floating-point 'num' up to the specified 'precision'. 6 | // The 'precision' parameter determines the number of decimal places to Round to. 7 | // The function returns the rounded number as a float64. 8 | func Ceil(num float64, precision ...int) float64 { 9 | pre := 0 10 | if precision != nil { 11 | pre = precision[0] 12 | } 13 | scale := math.Pow(10, float64(pre)) 14 | 15 | return math.Ceil(num*scale) / scale 16 | } 17 | -------------------------------------------------------------------------------- /go_math/floor.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import "math" 4 | 5 | // Floor rounds the given floating-point 'num' down to the specified 'precision'. 6 | // The 'precision' parameter determines the number of decimal places to Round to. 7 | // The function returns the rounded number as a float64. 8 | func Floor(num float64, precision ...int) float64 { 9 | pre := 0 10 | if precision != nil { 11 | pre = precision[0] 12 | } 13 | scale := math.Pow(10, float64(pre)) 14 | 15 | return math.Floor(num*scale) / scale 16 | } 17 | -------------------------------------------------------------------------------- /lang/isString_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_isString_valid(t *testing.T) { 10 | result := IsString("hihi") 11 | 12 | assert.Equal(t, true, result) 13 | } 14 | 15 | func Test_isString_invalid_one(t *testing.T) { 16 | result := IsString(1) 17 | 18 | assert.Equal(t, false, result) 19 | } 20 | 21 | func Test_isString_invalid_two(t *testing.T) { 22 | result := IsString(true) 23 | 24 | assert.Equal(t, false, result) 25 | } 26 | -------------------------------------------------------------------------------- /string/lower_first_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_lowerFirst_valid_one(t *testing.T) { 10 | result := LowerFirst("Fred") 11 | assert.Equal(t, "fred", result) 12 | } 13 | 14 | func Test_lowerFirst_valid_two(t *testing.T) { 15 | result := LowerFirst("FRED") 16 | assert.Equal(t, "fRED", result) 17 | } 18 | 19 | func Test_lowerFirst_invalid(t *testing.T) { 20 | result := LowerFirst("") 21 | assert.Equal(t, "", result) 22 | } 23 | -------------------------------------------------------------------------------- /string/split.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "strings" 4 | 5 | // Split a string 's' into substrings using the specified 'separator'. 6 | // It returns a slice of substrings. The 'limit' parameter controls the maximum 7 | // number of substrings to return. If 'limit' is negative, there is no limit. 8 | func Split(s, separator string, limit int) []string { 9 | substrings := strings.Split(s, separator) 10 | if limit > len(substrings) { 11 | limit = len(substrings) 12 | } 13 | 14 | return substrings[0:limit] 15 | } 16 | -------------------------------------------------------------------------------- /go_math/ceil_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_ceil_valid_one(t *testing.T) { 10 | result := Ceil(4.006) 11 | 12 | assert.Equal(t, float64(5), result) 13 | } 14 | 15 | func Test_ceil_valid_two(t *testing.T) { 16 | result := Ceil(6.004, 2) 17 | 18 | assert.Equal(t, 6.01, result) 19 | } 20 | 21 | func Test_ceil_valid_three(t *testing.T) { 22 | result := Ceil(6040, -2) 23 | 24 | assert.Equal(t, float64(6100), result) 25 | } 26 | -------------------------------------------------------------------------------- /string/repeat_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_repeat_valid_one(t *testing.T) { 10 | result := Repeat("*", 3) 11 | 12 | assert.Equal(t, "***", result) 13 | } 14 | 15 | func Test_repeat_valid_two(t *testing.T) { 16 | result := Repeat("abc", 2) 17 | 18 | assert.Equal(t, "abcabc", result) 19 | } 20 | 21 | func Test_repeat_valid_three(t *testing.T) { 22 | result := Repeat("abc", 0) 23 | 24 | assert.Equal(t, "", result) 25 | } 26 | -------------------------------------------------------------------------------- /go_math/floor_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_floor_valid_one(t *testing.T) { 10 | result := Floor(4.006) 11 | 12 | assert.Equal(t, float64(4), result) 13 | } 14 | 15 | func Test_floor_valid_two(t *testing.T) { 16 | result := Floor(0.046, 2) 17 | 18 | assert.Equal(t, 0.04, result) 19 | } 20 | 21 | func Test_floor_valid_three(t *testing.T) { 22 | result := Floor(4060, -2) 23 | 24 | assert.Equal(t, float64(4000), result) 25 | } 26 | -------------------------------------------------------------------------------- /go_math/round_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_round_valid_one(t *testing.T) { 10 | result := Round(4.006) 11 | 12 | assert.Equal(t, float64(4), result) 13 | } 14 | 15 | func Test_round_valid_two(t *testing.T) { 16 | result := Round(4.006, 2) 17 | 18 | assert.Equal(t, 4.01, result) 19 | } 20 | 21 | func Test_round_valid_three(t *testing.T) { 22 | result := Round(4060, -2) 23 | 24 | assert.Equal(t, float64(4100), result) 25 | } 26 | -------------------------------------------------------------------------------- /lang/isNumber.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | // IsNumber checks if a given value is a numeric type (integer or floating-point). 4 | // The function accepts a value of any type (interface{}) and returns true if it is a numeric type, false otherwise. 5 | func IsNumber(value interface{}) bool { 6 | switch value.(type) { 7 | case int, int8, int16, int32, int64: 8 | return true 9 | case uint, uint8, uint16, uint32, uint64: 10 | return true 11 | case float32, float64: 12 | return true 13 | default: 14 | return false 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /string/upper_case.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "regexp" 5 | "strings" 6 | 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | // UpperCase converts all characters in the input string 's' to uppercase. 11 | // It returns the modified string with all characters in uppercase. 12 | func UpperCase(s string) (string, error) { 13 | regex, _ := regexp.Compile(constants.PatternNumberAndAlphabet) 14 | 15 | matches := regex.FindAllString(strings.ToUpper(s), -1) 16 | 17 | return strings.Join(matches, " "), nil 18 | } 19 | -------------------------------------------------------------------------------- /array/head.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | ) 6 | 7 | // Head returns the first element of an array (Slice). 8 | // The function accepts an array (Slice) and returns the first element of the array. 9 | // If the array is empty or not a Slice, the function returns nil. 10 | func Head(array interface{}) interface{} { 11 | arrValue := reflect.ValueOf(array) 12 | 13 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || arrValue.Len() == 0 { 14 | return nil 15 | } 16 | 17 | return arrValue.Index(0).Interface() 18 | } 19 | -------------------------------------------------------------------------------- /array/last.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import "reflect" 4 | 5 | // Last returns the Last element of an array (Slice). 6 | // The function accepts an array (Slice) and returns its Last element. 7 | // If the input is not a Slice or if the array is empty, the function returns nil. 8 | func Last(array interface{}) interface{} { 9 | arrValue := reflect.ValueOf(array) 10 | 11 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || arrValue.Len() == 0 { 12 | return nil 13 | } 14 | 15 | return arrValue.Index(arrValue.Len() - 1).Interface() 16 | } 17 | -------------------------------------------------------------------------------- /go_math/mean_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_mean_valid_one(t *testing.T) { 10 | result := Mean([]float64{3.5, 7.2, 2.8, 9.1, 5.4}) 11 | 12 | assert.Equal(t, 5.6, result) 13 | } 14 | 15 | func Test_mean_valid_two(t *testing.T) { 16 | result := Mean([]float64{4, 2, 8, 6}) 17 | 18 | assert.Equal(t, float64(5), result) 19 | } 20 | 21 | func Test_mean_invalid_empty_array(t *testing.T) { 22 | result := Mean([]float64{}) 23 | 24 | assert.Equal(t, float64(0), result) 25 | } 26 | -------------------------------------------------------------------------------- /string/upper_first_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_upperFirst(t *testing.T) { 10 | 11 | } 12 | 13 | func Test_upperFirst_valid_one(t *testing.T) { 14 | result := UpperFirst("fred") 15 | assert.Equal(t, "Fred", result) 16 | } 17 | 18 | func Test_upperFirst_valid_two(t *testing.T) { 19 | result := UpperFirst("FRED") 20 | assert.Equal(t, "FRED", result) 21 | } 22 | 23 | func Test_upperFirst_invalid(t *testing.T) { 24 | result := UpperFirst("") 25 | assert.Equal(t, "", result) 26 | } 27 | -------------------------------------------------------------------------------- /number/random.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "math/rand" 5 | "time" 6 | ) 7 | 8 | // Random generates a Random float64 number within the specified range. 9 | // The function accepts an upper bound (upper) and an optional lower bound (lower). 10 | // It returns a Random float64 number between lower (inclusive) and upper (exclusive). 11 | func Random(upper float64, lower ...float64) float64 { 12 | lowerBound := float64(0) 13 | if lower != nil { 14 | lowerBound = lower[0] 15 | } 16 | 17 | rand.Seed(time.Now().UnixNano()) 18 | 19 | return rand.Float64()*(upper-lowerBound) + lowerBound 20 | } 21 | -------------------------------------------------------------------------------- /string/unescape_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_unescape_valid_one(t *testing.T) { 10 | result := Unescape("fred, barney, & pebbles") 11 | 12 | assert.Equal(t, "fred, barney, & pebbles", result) 13 | } 14 | 15 | func Test_unescape_valid_two(t *testing.T) { 16 | result := Unescape("fred < barney") 17 | 18 | assert.Equal(t, "fred < barney", result) 19 | } 20 | 21 | func Test_unescape_valid_three(t *testing.T) { 22 | result := Unescape("fred > barney") 23 | 24 | assert.Equal(t, "fred > barney", result) 25 | } 26 | -------------------------------------------------------------------------------- /constants/error.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | var ( 8 | ErrNotFound = errors.New("not found") 9 | ErrNotSupport = errors.New("not support") 10 | ErrOutOfRange = errors.New("len out of range") 11 | ErrNotSlice = errors.New("input is not a slice") 12 | ErrEmptyList = errors.New("empty list") 13 | ErrFieldNotFound = errors.New("field not found") 14 | ErrIncompatible = errors.New("incompatible field types") 15 | ErrParamLessThanZero = errors.New("param is less than zero") 16 | ErrNotFunction = errors.New("input is not a function") 17 | ) 18 | -------------------------------------------------------------------------------- /string/kebab_case.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "regexp" 5 | "strings" 6 | 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | // KebabCase converts a string to kebab-case format, where words are separated by hyphens. 11 | // It removes spaces, punctuation, and converts letters to lowercase. 12 | // Returns the kebab-case string and an error if the input string is empty. 13 | func KebabCase(s string) (string, error) { 14 | regex, _ := regexp.Compile(constants.PatternNumberAndAlphabet) 15 | 16 | matches := regex.FindAllString(strings.ToLower(s), -1) 17 | 18 | return strings.Join(matches, "-"), nil 19 | } 20 | -------------------------------------------------------------------------------- /string/lower_case.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "regexp" 5 | "strings" 6 | 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | // LowerCase converts a string to kebab-case format, where words are separated by hyphens. 11 | // It removes spaces, punctuation, and converts letters to lowercase. 12 | // Returns the kebab-case string and an error if the input string is empty. 13 | func LowerCase(s string) (string, error) { 14 | regex, _ := regexp.Compile(constants.PatternNumberAndAlphabet) 15 | 16 | matches := regex.FindAllString(strings.ToLower(s), -1) 17 | 18 | return strings.Join(matches, " "), nil 19 | } 20 | -------------------------------------------------------------------------------- /lang/isNumber_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_isNumber_valid_one(t *testing.T) { 10 | result := IsNumber(1) 11 | 12 | assert.Equal(t, true, result) 13 | } 14 | 15 | func Test_isNumber_valid_two(t *testing.T) { 16 | result := IsNumber(uint(1)) 17 | 18 | assert.Equal(t, true, result) 19 | } 20 | 21 | func Test_isNumber_valid_three(t *testing.T) { 22 | result := IsNumber(1.1) 23 | 24 | assert.Equal(t, true, result) 25 | } 26 | 27 | func Test_isNumber_invalid(t *testing.T) { 28 | result := IsNumber(true) 29 | 30 | assert.Equal(t, false, result) 31 | } 32 | -------------------------------------------------------------------------------- /number/random_test.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_random_valid_one(t *testing.T) { 11 | result := Random(float64(5)) 12 | 13 | if result >= float64(0) && result <= float64(5) { 14 | assert.Nil(t, nil) 15 | } 16 | 17 | assert.Error(t, errors.New("the func Random has a error")) 18 | } 19 | 20 | func Test_random_valid_two(t *testing.T) { 21 | result := Random(float64(10), 6.9) 22 | 23 | if result >= 6.9 && result <= float64(10) { 24 | assert.Nil(t, nil) 25 | } 26 | 27 | assert.Error(t, errors.New("the func Random has a error")) 28 | } 29 | -------------------------------------------------------------------------------- /string/upper_case_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_upperCase_valid_one(t *testing.T) { 10 | result, err := UpperCase("--foo-bar") 11 | 12 | assert.Equal(t, "FOO BAR", result) 13 | assert.Nil(t, err) 14 | } 15 | 16 | func Test_upperCase_valid_two(t *testing.T) { 17 | result, err := UpperCase("fooBar") 18 | 19 | assert.Equal(t, "FOOBAR", result) 20 | assert.Nil(t, err) 21 | } 22 | 23 | func Test_upperCase_valid_three(t *testing.T) { 24 | result, err := UpperCase("__foo_bar__") 25 | 26 | assert.Equal(t, "FOO BAR", result) 27 | assert.Nil(t, err) 28 | } 29 | -------------------------------------------------------------------------------- /lang/toString_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_toString_valid_one(t *testing.T) { 10 | result := ToString(123) 11 | 12 | assert.Equal(t, "123", result) 13 | } 14 | 15 | func Test_toString_valid_two(t *testing.T) { 16 | result := ToString(-1) 17 | 18 | assert.Equal(t, "-1", result) 19 | } 20 | 21 | func Test_toString_valid_three(t *testing.T) { 22 | result := ToString([]int{1, 2, 3}) 23 | 24 | assert.Equal(t, "1,2,3", result) 25 | } 26 | 27 | func Test_toString_valid_four(t *testing.T) { 28 | result := ToString(true) 29 | 30 | assert.Equal(t, "true", result) 31 | } 32 | -------------------------------------------------------------------------------- /string/capitalize_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_capitalize_valid_one(t *testing.T) { 10 | result := Capitalize("FRED") 11 | assert.Equal(t, "Fred", result) 12 | } 13 | 14 | func Test_capitalize_valid_two(t *testing.T) { 15 | result := Capitalize("fRED") 16 | assert.Equal(t, "Fred", result) 17 | } 18 | 19 | func Test_capitalize_valid_three(t *testing.T) { 20 | result := Capitalize("FReD 123") 21 | assert.Equal(t, "Fred 123", result) 22 | } 23 | 24 | func Test_capitalize_invalid(t *testing.T) { 25 | result := Capitalize("") 26 | assert.Equal(t, "", result) 27 | } 28 | -------------------------------------------------------------------------------- /string/camel_case_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_camelCase_valid_one(t *testing.T) { 10 | result := CamelCase("Foo Bar") 11 | assert.Equal(t, "fooBar", result) 12 | } 13 | 14 | func Test_camelCase_valid_two(t *testing.T) { 15 | result := CamelCase("--foo-bar--") 16 | assert.Equal(t, "fooBar", result) 17 | } 18 | 19 | func Test_camelCase_valid_three(t *testing.T) { 20 | result := CamelCase("__FOO_BAR__") 21 | assert.Equal(t, "fooBar", result) 22 | } 23 | 24 | func Test_camelCase_valid_four(t *testing.T) { 25 | result := CamelCase("@a98@#$,.23237@#$hello-world") 26 | assert.Equal(t, "a9823237HelloWorld", result) 27 | } 28 | -------------------------------------------------------------------------------- /string/split_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_split_valid_one(t *testing.T) { 10 | result := Split("a-b-c", "-", 2) 11 | 12 | assert.Equal(t, []string{"a", "b"}, result) 13 | } 14 | 15 | func Test_split_valid_two(t *testing.T) { 16 | result := Split("a-b-c", "-", 4) 17 | 18 | assert.Equal(t, []string{"a", "b", "c"}, result) 19 | } 20 | 21 | func Test_split_valid_three(t *testing.T) { 22 | result := Split("a-b-c", "abc", 2) 23 | 24 | assert.Equal(t, []string{"a-b-c"}, result) 25 | } 26 | 27 | func Test_split_valid_four(t *testing.T) { 28 | result := Split("a-b-c", "c", 2) 29 | 30 | assert.Equal(t, []string{"a-b-", ""}, result) 31 | } 32 | -------------------------------------------------------------------------------- /string/starts_with.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "github.com/warriors-vn/go-dash/constants" 4 | 5 | // StartsWith checks if the string 'str' starts with the target string 'target' optionally 6 | // from the specified 'position' in 'str'. 7 | // It returns true if 'str' starts with 'target', and false otherwise. 8 | // If 'position' is provided, the comparison starts from that position in 'str'. 9 | func StartsWith(str, target string, position ...int) (bool, error) { 10 | pos := 1 11 | if position != nil { 12 | pos = position[0] 13 | } 14 | 15 | if pos > len(str) { 16 | return false, constants.ErrOutOfRange 17 | } 18 | 19 | if string(str[pos-1]) != target { 20 | return false, nil 21 | } 22 | 23 | return true, nil 24 | } 25 | -------------------------------------------------------------------------------- /string/snake_case_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_snakeCase_valid_one(t *testing.T) { 10 | result := SnakeCase("Foo Bar") 11 | 12 | assert.Equal(t, "foo_bar", result) 13 | } 14 | 15 | func Test_snakeCase_valid_two(t *testing.T) { 16 | result := SnakeCase("fooBar") 17 | 18 | assert.Equal(t, "foobar", result) 19 | } 20 | 21 | func Test_snakeCase_valid_three(t *testing.T) { 22 | result := SnakeCase("--FOO-BAR--") 23 | 24 | assert.Equal(t, "foo_bar", result) 25 | } 26 | 27 | func Test_snakeCase_valid_four(t *testing.T) { 28 | result := SnakeCase("@a98@#$,.23237@#$hello-world") 29 | 30 | assert.Equal(t, "a98_23237_hello_world", result) 31 | } 32 | -------------------------------------------------------------------------------- /lang/toString.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | // ToString converts a value to its string representation. 9 | // The function accepts a value of any type (interface{}) and returns its string representation. 10 | func ToString(value interface{}) string { 11 | valueOf := reflect.ValueOf(value) 12 | 13 | switch valueOf.Kind() { 14 | case reflect.Slice, reflect.Array: 15 | result := "" 16 | for i := 0; i < valueOf.Len(); i++ { 17 | if i == valueOf.Len()-1 { 18 | result += fmt.Sprintf("%v", valueOf.Index(i).Interface()) 19 | } else { 20 | result += fmt.Sprintf("%v,", valueOf.Index(i).Interface()) 21 | } 22 | } 23 | 24 | return result 25 | } 26 | return fmt.Sprintf("%v", value) 27 | } 28 | -------------------------------------------------------------------------------- /string/ends_with.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "github.com/warriors-vn/go-dash/constants" 4 | 5 | // EndsWith checks if the string 'str' ends with the target string 'target' optionally 6 | // up to a specified 'position' in 'str'. 7 | // It returns true if 'str' ends with 'target', and false otherwise. 8 | // If 'position' is provided, only the characters up to that position in 'str' are considered. 9 | func EndsWith(str, target string, position ...int) (bool, error) { 10 | pos := len(str) 11 | if position != nil { 12 | pos = position[0] 13 | } 14 | 15 | if pos > len(str) { 16 | return false, constants.ErrOutOfRange 17 | } 18 | 19 | if string(str[pos-1]) != target { 20 | return false, nil 21 | } 22 | 23 | return true, nil 24 | } 25 | -------------------------------------------------------------------------------- /string/snake_case.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import "unicode" 4 | 5 | // SnakeCase converts a string to snake_case format. 6 | // It replaces spaces and punctuation with underscores and converts letters to lowercase. 7 | func SnakeCase(s string) string { 8 | nextToSnakeCase, result := false, "" 9 | 10 | for _, char := range s { 11 | if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9') { 12 | if nextToSnakeCase { 13 | result += "_" + string(unicode.ToLower(char)) 14 | nextToSnakeCase = false 15 | } else { 16 | result += string(unicode.ToLower(char)) 17 | } 18 | } else { 19 | if len(result) > 0 { 20 | nextToSnakeCase = true 21 | } 22 | } 23 | } 24 | 25 | return result 26 | } 27 | -------------------------------------------------------------------------------- /collection/sample.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "math/rand" 5 | "reflect" 6 | "time" 7 | 8 | "github.com/warriors-vn/go-dash/constants" 9 | ) 10 | 11 | // Sample randomly selects and returns a single element from the input array. 12 | // It takes an array-like data structure and returns a randomly chosen element, or an error if any occurs. 13 | func Sample(array interface{}) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | rand.Seed(time.Now().UnixNano()) 21 | min := 0 22 | max := arrValue.Len() - 1 23 | 24 | return arrValue.Index(rand.Intn(max-min+1) + min).Interface(), nil 25 | } 26 | -------------------------------------------------------------------------------- /array/nth.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Nth returns the element at the specified index from the array (Slice). 10 | // The index is 1-based, so the first element is at index 1. 11 | func Nth(array interface{}, number int) (interface{}, error) { 12 | arrValue := reflect.ValueOf(array) 13 | 14 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 15 | return nil, constants.ErrNotSlice 16 | } 17 | 18 | if number >= arrValue.Len() { 19 | return nil, nil 20 | } 21 | 22 | if number < 0 { 23 | if number+arrValue.Len() < 0 { 24 | return nil, nil 25 | } 26 | number = number + arrValue.Len() 27 | } 28 | 29 | return arrValue.Index(number).Interface(), nil 30 | } 31 | -------------------------------------------------------------------------------- /string/pad_end_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_padEnd_valid_one(t *testing.T) { 10 | result := PadEnd("abc", 6) 11 | assert.Equal(t, "abc ", result) 12 | } 13 | 14 | func Test_padEnd_valid_two(t *testing.T) { 15 | result := PadEnd("abc", 6, "_-") 16 | assert.Equal(t, "abc_-_", result) 17 | } 18 | 19 | func Test_padEnd_valid_three(t *testing.T) { 20 | result := PadEnd("abc", 3) 21 | assert.Equal(t, "abc", result) 22 | } 23 | 24 | func Test_padEnd_invalid_length(t *testing.T) { 25 | result := PadEnd("abc", 2) 26 | assert.Equal(t, "abc", result) 27 | } 28 | 29 | func Test_padEnd_char_empty(t *testing.T) { 30 | result := PadEnd("abc", 6, "") 31 | assert.Equal(t, "abc", result) 32 | } 33 | -------------------------------------------------------------------------------- /array/head_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_head_empty_array(t *testing.T) { 10 | result := Head([]int{}) 11 | 12 | assert.Equal(t, nil, result) 13 | } 14 | 15 | func Test_head_array_not_slice(t *testing.T) { 16 | result := Head(true) 17 | 18 | assert.Equal(t, nil, result) 19 | } 20 | 21 | func Test_head_valid_one(t *testing.T) { 22 | result := Head([]bool{true, false, true}) 23 | 24 | assert.Equal(t, true, result) 25 | } 26 | 27 | func Test_head_valid_two(t *testing.T) { 28 | result := Head([]int{1, 2, 3}) 29 | 30 | assert.Equal(t, 1, result) 31 | } 32 | 33 | func Test_head_valid_three(t *testing.T) { 34 | result := Head([]string{"a", "b", "c"}) 35 | 36 | assert.Equal(t, "a", result) 37 | } 38 | -------------------------------------------------------------------------------- /array/last_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_last_empty_array(t *testing.T) { 10 | result := Last([]int{}) 11 | 12 | assert.Equal(t, nil, result) 13 | } 14 | 15 | func Test_last_array_not_slice(t *testing.T) { 16 | result := Last(true) 17 | 18 | assert.Equal(t, nil, result) 19 | } 20 | 21 | func Test_last_valid_one(t *testing.T) { 22 | result := Last([]bool{true, false, false}) 23 | 24 | assert.Equal(t, false, result) 25 | } 26 | 27 | func Test_last_valid_two(t *testing.T) { 28 | result := Last([]int{1, 2, 3}) 29 | 30 | assert.Equal(t, 3, result) 31 | } 32 | 33 | func Test_last_valid_three(t *testing.T) { 34 | result := Last([]string{"a", "b", "c"}) 35 | 36 | assert.Equal(t, "c", result) 37 | } 38 | -------------------------------------------------------------------------------- /string/camel_case.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "unicode" 5 | ) 6 | 7 | // CamelCase converts a string to CamelCase format. 8 | // It capitalizes the first letter of each word (except the first word), 9 | // removes spaces and punctuation, and lower cases the rest of the letters. 10 | func CamelCase(s string) string { 11 | nextToUpper, result := false, "" 12 | 13 | for _, char := range s { 14 | if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9') { 15 | if nextToUpper { 16 | result += string(unicode.ToUpper(char)) 17 | nextToUpper = false 18 | } else { 19 | result += string(unicode.ToLower(char)) 20 | } 21 | } else { 22 | if len(result) > 0 { 23 | nextToUpper = true 24 | } 25 | } 26 | } 27 | 28 | return result 29 | } 30 | -------------------------------------------------------------------------------- /string/pad_start_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_padStart_valid_one(t *testing.T) { 10 | result := PadStart("abc", 6) 11 | assert.Equal(t, " abc", result) 12 | } 13 | 14 | func Test_padStart_valid_two(t *testing.T) { 15 | result := PadStart("abc", 6, "_-") 16 | assert.Equal(t, "_-_abc", result) 17 | } 18 | 19 | func Test_padStart_valid_three(t *testing.T) { 20 | result := PadStart("abc", 3) 21 | assert.Equal(t, "abc", result) 22 | } 23 | 24 | func Test_padStart_invalid_length(t *testing.T) { 25 | result := PadStart("abc", 2) 26 | assert.Equal(t, "abc", result) 27 | } 28 | 29 | func Test_padStart_char_empty(t *testing.T) { 30 | result := PadStart("abc", 6, "") 31 | assert.Equal(t, "abc", result) 32 | } 33 | -------------------------------------------------------------------------------- /array/reverse.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Reverse reverses the elements in the input array. 10 | // It takes an array-like data structure and returns the modified array with reversed elements. 11 | // The function returns the modified array and an error if any occurs. 12 | func Reverse(array interface{}) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 20 | for i := arrValue.Len() - 1; i >= 0; i-- { 21 | result = reflect.Append(result, arrValue.Index(i)) 22 | } 23 | 24 | return result.Interface(), nil 25 | } 26 | -------------------------------------------------------------------------------- /collection/sample_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_sample_valid_int(t *testing.T) { 11 | result, err := Sample([]int{1, 2, 3}) 12 | 13 | assert.Nil(t, err) 14 | if result == 1 || result == 2 || result == 3 { 15 | assert.Nil(t, nil) 16 | } 17 | } 18 | 19 | func Test_sample_valid_string(t *testing.T) { 20 | result, err := Sample([]string{"1", "2", "3"}) 21 | 22 | assert.Nil(t, err) 23 | if result == "1" || result == "2" || result == "3" { 24 | assert.Nil(t, nil) 25 | } 26 | } 27 | 28 | func Test_sample_invalid_array_not_slice(t *testing.T) { 29 | result, err := Sample(true) 30 | 31 | assert.Equal(t, nil, result) 32 | assert.Equal(t, constants.ErrNotSlice, err) 33 | } 34 | -------------------------------------------------------------------------------- /array/join.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | // Join takes an array (Slice) of elements and a separator string, 11 | // and returns a single string by joining the elements with the separator. 12 | func Join(array interface{}, separator string) (string, error) { 13 | arrValue, result := reflect.ValueOf(array), "" 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return result, constants.ErrNotSlice 17 | } 18 | 19 | for i := 0; i < arrValue.Len(); i++ { 20 | if i == arrValue.Len()-1 { 21 | result += fmt.Sprintf("%v", arrValue.Index(i).Interface()) 22 | } else { 23 | result += fmt.Sprintf("%v%v", arrValue.Index(i).Interface(), separator) 24 | } 25 | } 26 | 27 | return result, nil 28 | } 29 | -------------------------------------------------------------------------------- /array/find_index.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | ) 6 | 7 | // FindIndex uses reflection to find the index of the 'target' value in the 'array'. 8 | // It returns the index of the first occurrence of 'target' and a boolean indicating if the target was found. 9 | // If 'target' is not found in 'array', the function returns -1 and false. 10 | func FindIndex(array, target interface{}) (int, bool) { 11 | arrValue := reflect.ValueOf(array) 12 | targetValue := reflect.ValueOf(target) 13 | 14 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 15 | return -1, false 16 | } 17 | 18 | for i := 0; i < arrValue.Len(); i++ { 19 | elem := arrValue.Index(i) 20 | if reflect.DeepEqual(elem.Interface(), targetValue.Interface()) { 21 | return i, true 22 | } 23 | } 24 | 25 | return -1, false 26 | } 27 | -------------------------------------------------------------------------------- /string/kebab_case_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_kebabCase_valid_one(t *testing.T) { 10 | result, err := KebabCase("Foo Bar") 11 | 12 | assert.Equal(t, "foo-bar", result) 13 | assert.Nil(t, err) 14 | } 15 | 16 | func Test_kebabCase_valid_two(t *testing.T) { 17 | result, err := KebabCase("__FOO_BAR__") 18 | 19 | assert.Equal(t, "foo-bar", result) 20 | assert.Nil(t, err) 21 | } 22 | 23 | func Test_kebabCase_valid_three(t *testing.T) { 24 | result, err := KebabCase("fooBar") 25 | 26 | assert.Equal(t, "foobar", result) 27 | assert.Nil(t, err) 28 | } 29 | 30 | func Test_kebabCase_valid_four(t *testing.T) { 31 | result, err := KebabCase(")*)_%&^sa_FOO_BAR__") 32 | 33 | assert.Equal(t, "sa-foo-bar", result) 34 | assert.Nil(t, err) 35 | } 36 | -------------------------------------------------------------------------------- /string/lower_case_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_lowerCase_valid_one(t *testing.T) { 10 | result, err := LowerCase("Foo Bar") 11 | 12 | assert.Equal(t, "foo bar", result) 13 | assert.Nil(t, err) 14 | } 15 | 16 | func Test_lowerCase_valid_two(t *testing.T) { 17 | result, err := LowerCase("__FOO_BAR__") 18 | 19 | assert.Equal(t, "foo bar", result) 20 | assert.Nil(t, err) 21 | } 22 | 23 | func Test_lowerCase_valid_three(t *testing.T) { 24 | result, err := LowerCase("fooBar") 25 | 26 | assert.Equal(t, "foobar", result) 27 | assert.Nil(t, err) 28 | } 29 | 30 | func Test_lowerCase_valid_four(t *testing.T) { 31 | result, err := LowerCase(")*)_%&^sa_FOO_BAR__") 32 | 33 | assert.Equal(t, "sa foo bar", result) 34 | assert.Nil(t, err) 35 | } 36 | -------------------------------------------------------------------------------- /array/isContains.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // IsContainsArray checks if a given element is present in the array. 10 | // The function accepts an array (Slice) and an element of any type (interface{}) and returns true if the element is found in the array, false otherwise. 11 | // If the array or element is of an unsupported type, the function returns an error. 12 | func IsContainsArray(array, element interface{}) (bool, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return false, constants.ErrNotSlice 17 | } 18 | 19 | for i := 0; i < arrValue.Len(); i++ { 20 | if reflect.DeepEqual(element, arrValue.Index(i).Interface()) { 21 | return true, nil 22 | } 23 | } 24 | 25 | return false, nil 26 | } 27 | -------------------------------------------------------------------------------- /string/pad_end.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | // PadEnd pads a string 's' with a specified 'length' by adding 'char' at the end. 4 | // If 'char' is not provided, it defaults to a space character. 5 | // The resulting padded string will have a total length of at least 'length'. 6 | // If the original string length is greater than or equal to 'length', the original string is returned unchanged. 7 | func PadEnd(s string, length int, char ...string) string { 8 | if len(s) >= length || (char != nil && char[0] == "") { 9 | return s 10 | } 11 | 12 | paddingChar := " " 13 | if char != nil { 14 | paddingChar = char[0] 15 | } 16 | 17 | padLen, rightPadding, cur := length-len(s), "", 0 18 | for index := 0; index < padLen; index++ { 19 | rightPadding += string(paddingChar[cur]) 20 | 21 | cur++ 22 | if cur >= len(paddingChar) { 23 | cur = 0 24 | } 25 | } 26 | 27 | return s + rightPadding 28 | } 29 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 6 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 7 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 8 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 9 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 10 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 11 | -------------------------------------------------------------------------------- /string/pad_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_pad_valid_no_char(t *testing.T) { 10 | result := Pad("abc", 8) 11 | assert.Equal(t, " abc ", result) 12 | } 13 | 14 | func Test_pad_valid_one(t *testing.T) { 15 | result := Pad("abc", 8, "_-") 16 | assert.Equal(t, "_-abc_-_", result) 17 | } 18 | 19 | func Test_pad_valid_two(t *testing.T) { 20 | result := Pad("Hello", 6, "*") 21 | assert.Equal(t, "Hello*", result) 22 | } 23 | 24 | func Test_pad_valid_three(t *testing.T) { 25 | result := Pad("Hello", 10, "*") 26 | assert.Equal(t, "**Hello***", result) 27 | } 28 | 29 | func Test_pad_invalid_length(t *testing.T) { 30 | result := Pad("abc", 2) 31 | assert.Equal(t, "abc", result) 32 | } 33 | 34 | func Test_pad_char_empty(t *testing.T) { 35 | result := Pad("abc", 6, "") 36 | assert.Equal(t, "abc", result) 37 | } 38 | -------------------------------------------------------------------------------- /array/tail.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Tail returns a new Slice containing all elements of the input array except the first one. 10 | // It takes an array-like data structure as input and returns a new Slice excluding the first element. 11 | // The function returns the new Slice and an error if any occurs. 12 | func Tail(array interface{}) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if arrValue.Len() == 0 { 20 | return array, nil 21 | } 22 | 23 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 24 | for i := 1; i < arrValue.Len(); i++ { 25 | result = reflect.Append(result, arrValue.Index(i)) 26 | } 27 | 28 | return result.Interface(), nil 29 | } 30 | -------------------------------------------------------------------------------- /string/pad_start.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | // PadStart pads a string 's' with a specified 'length' by adding 'char' at the beginning. 4 | // If 'char' is not provided, it defaults to a space character. 5 | // The resulting padded string will have a total length of at least 'length'. 6 | // If the original string length is greater than or equal to 'length', the original string is returned unchanged. 7 | func PadStart(s string, length int, char ...string) string { 8 | if len(s) >= length || (char != nil && char[0] == "") { 9 | return s 10 | } 11 | 12 | paddingChar := " " 13 | if char != nil { 14 | paddingChar = char[0] 15 | } 16 | 17 | padLen, leftPadding, cur := length-len(s), "", 0 18 | for index := 0; index < padLen; index++ { 19 | leftPadding += string(paddingChar[cur]) 20 | 21 | cur++ 22 | if cur >= len(paddingChar) { 23 | cur = 0 24 | } 25 | } 26 | 27 | return leftPadding + s 28 | } 29 | -------------------------------------------------------------------------------- /array/initial.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Initial returns a new array (Slice) containing all elements of the input array except the Last one. 10 | // The function accepts an array (Slice) and returns a new array with all elements except the Last one. 11 | // If the input is not a Slice or if the array is empty, the function returns an error. 12 | func Initial(array interface{}) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if arrValue.Len() == 0 { 20 | return array, nil 21 | } 22 | 23 | result := reflect.MakeSlice(arrValue.Type(), arrValue.Len()-1, arrValue.Len()-1) 24 | 25 | for i := 0; i < arrValue.Len()-1; i++ { 26 | result.Index(i).Set(arrValue.Index(i)) 27 | } 28 | 29 | return result.Interface(), nil 30 | } 31 | -------------------------------------------------------------------------------- /collection/sampleSize.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "math/rand" 5 | "reflect" 6 | "time" 7 | 8 | "github.com/warriors-vn/go-dash/constants" 9 | ) 10 | 11 | // SampleSize randomly selects and returns a specified number of elements from the input array. 12 | // It takes an array-like data structure and the number of elements to Sample. 13 | // The function returns a slice of randomly chosen elements, or an error if any occurs. 14 | func SampleSize(array interface{}, number int) (interface{}, error) { 15 | arrValue := reflect.ValueOf(array) 16 | 17 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 18 | return nil, constants.ErrNotSlice 19 | } 20 | 21 | result, min, max := reflect.MakeSlice(arrValue.Type(), 0, 0), 0, arrValue.Len()-1 22 | for i := 0; i < number; i++ { 23 | rand.Seed(time.Now().UnixNano()) 24 | result = reflect.Append(result, arrValue.Index(rand.Intn(max-min+1)+min)) 25 | } 26 | 27 | return result.Interface(), nil 28 | } 29 | -------------------------------------------------------------------------------- /array/lastIndexOf.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // LastIndexOf searches for the Last occurrence of the given inputSearch value 10 | // within the array (Slice) and returns its index. The search starts from the end 11 | // of the array or from the specified fromIndex if provided. 12 | func LastIndexOf(array, inputSearch interface{}, fromIndex ...int) (int, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return -1, constants.ErrNotSlice 17 | } 18 | 19 | from := arrValue.Len() - 1 20 | if fromIndex != nil && fromIndex[0] >= 0 { 21 | from = fromIndex[0] 22 | } 23 | 24 | if from >= arrValue.Len() { 25 | from = arrValue.Len() - 1 26 | } 27 | 28 | for i := from; i >= 0; i-- { 29 | if reflect.DeepEqual(arrValue.Index(i).Interface(), inputSearch) { 30 | return i, nil 31 | } 32 | } 33 | 34 | return -1, nil 35 | } 36 | -------------------------------------------------------------------------------- /lang/castArray.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // CastArray converts a value to an array if it is not already an array. 10 | // The function accepts a value of any type (interface{}) and returns an array containing the value. 11 | // If the value is already an array, it is returned as-is. 12 | func CastArray(value interface{}) (interface{}, error) { 13 | switch reflect.ValueOf(value).Kind() { 14 | case reflect.Int: 15 | return []int{value.(int)}, nil 16 | case reflect.Int32: 17 | return []int32{value.(int32)}, nil 18 | case reflect.Int64: 19 | return []int64{value.(int64)}, nil 20 | case reflect.Float32: 21 | return []float32{value.(float32)}, nil 22 | case reflect.Float64: 23 | return []float64{value.(float64)}, nil 24 | case reflect.String: 25 | return []string{value.(string)}, nil 26 | case reflect.Bool: 27 | return []bool{value.(bool)}, nil 28 | } 29 | 30 | return nil, constants.ErrNotSupport 31 | } 32 | -------------------------------------------------------------------------------- /array/sortedUniq_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_sortedUniq_valid_int(t *testing.T) { 11 | result, err := SortedUniq([]int{1, 1, 2, 3, 4, 4, 5}) 12 | 13 | assert.Equal(t, []int{1, 2, 3, 4, 5}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_sortedUniq_valid_int64(t *testing.T) { 18 | result, err := SortedUniq([]int64{1, 1, 2, 3, 4, 4, 5, 1, 2}) 19 | 20 | assert.Equal(t, []int64{1, 2, 3, 4, 5}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_sortedUniq_valid_string(t *testing.T) { 25 | result, err := SortedUniq([]string{"1", "1", "2", "3", "4", "4", "5"}) 26 | 27 | assert.Equal(t, []string{"1", "2", "3", "4", "5"}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_sortedUniq_invalid_array_not_slice(t *testing.T) { 32 | result, err := SortedUniq(true) 33 | 34 | assert.Equal(t, nil, result) 35 | assert.Equal(t, constants.ErrNotSlice, err) 36 | } 37 | -------------------------------------------------------------------------------- /string/parse_int_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_parseInt_valid_one(t *testing.T) { 12 | result, err := ParseInt("08") 13 | 14 | assert.Equal(t, 8, result) 15 | assert.Nil(t, err) 16 | } 17 | 18 | func Test_parseInt_valid_two(t *testing.T) { 19 | result, err := ParseInt(" -42") 20 | 21 | assert.Equal(t, -42, result) 22 | assert.Nil(t, err) 23 | } 24 | 25 | func Test_parseInt_invalid(t *testing.T) { 26 | result, err := ParseInt("abc") 27 | 28 | assert.Equal(t, 0, result) 29 | assert.Error(t, err) 30 | } 31 | 32 | func Test_parseInt_invalid_out_of_range_one(t *testing.T) { 33 | result, err := ParseInt(fmt.Sprintf("%v0", math.MaxInt)) 34 | 35 | assert.Equal(t, 0, result) 36 | assert.Error(t, err) 37 | } 38 | 39 | func Test_parseInt_invalid_out_of_range_two(t *testing.T) { 40 | result, err := ParseInt(fmt.Sprintf("%v0", math.MinInt)) 41 | 42 | assert.Equal(t, 0, result) 43 | assert.Error(t, err) 44 | } 45 | -------------------------------------------------------------------------------- /go_math/sum.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Sum calculates the Sum of elements in a slice of numeric type. 10 | // It returns the Sum as a float64. 11 | func Sum(array interface{}) (float64, error) { 12 | arrValue := reflect.ValueOf(array) 13 | 14 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 15 | return float64(0), constants.ErrNotSlice 16 | } 17 | 18 | sumArr := float64(0) 19 | for i := 0; i < arrValue.Len(); i++ { 20 | elem := arrValue.Index(i) 21 | switch elem.Kind() { 22 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 23 | sumArr += float64(elem.Int()) 24 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 25 | sumArr += float64(elem.Uint()) 26 | case reflect.Float32, reflect.Float64: 27 | sumArr += elem.Float() 28 | default: 29 | return float64(0), constants.ErrNotSupport 30 | } 31 | } 32 | 33 | return sumArr, nil 34 | } 35 | -------------------------------------------------------------------------------- /go_math/add_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_add_valid_int(t *testing.T) { 10 | result := Add(1, 9) 11 | 12 | assert.Equal(t, 10, result) 13 | } 14 | 15 | func Test_add_valid_int32(t *testing.T) { 16 | result := Add(int32(1), int32(9)) 17 | 18 | assert.Equal(t, int32(10), result) 19 | } 20 | 21 | func Test_add_valid_int64(t *testing.T) { 22 | result := Add(int64(1), int64(9)) 23 | 24 | assert.Equal(t, int64(10), result) 25 | } 26 | 27 | func Test_add_valid_float32(t *testing.T) { 28 | result := Add(float32(1.5), float32(9)) 29 | 30 | assert.Equal(t, float32(10.5), result) 31 | } 32 | 33 | func Test_add_valid_float64(t *testing.T) { 34 | result := Add(1.5, 9.5) 35 | 36 | assert.Equal(t, float64(11), result) 37 | } 38 | 39 | func Test_add_invalid_float32(t *testing.T) { 40 | result := Add(float32(1.5), 9) 41 | 42 | assert.Equal(t, 0, result) 43 | } 44 | 45 | func Test_add_invalid_type(t *testing.T) { 46 | result := Add(true, false) 47 | 48 | assert.Equal(t, 0, result) 49 | } 50 | -------------------------------------------------------------------------------- /array/slice.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Slice extracts a sub-Slice from the input array, starting from the 'start' index (inclusive) 10 | // up to the 'end' index (exclusive). 11 | // It takes an array-like data structure, the start index, and the end index as arguments. 12 | // The function returns the sub-Slice and an error if any occurs. 13 | func Slice(array interface{}, start, end int) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if start < 0 || end < 0 { 21 | return nil, constants.ErrParamLessThanZero 22 | } 23 | 24 | if start >= end || start >= arrValue.Len() || end > arrValue.Len() { 25 | return nil, constants.ErrOutOfRange 26 | } 27 | 28 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 29 | for i := start; i < end; i++ { 30 | result = reflect.Append(result, arrValue.Index(i)) 31 | } 32 | 33 | return result.Interface(), nil 34 | } 35 | -------------------------------------------------------------------------------- /string/pad.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | // Pad a string 's' with a specified 'length' by adding 'chars' on both sides. 4 | // If 'char' is not provided, it defaults to a space character. 5 | // The resulting padded string will have a total length of at least 'length'. 6 | // If the original string length is greater than or equal to 'length', the original string is returned unchanged. 7 | func Pad(s string, length int, char ...string) string { 8 | if len(s) >= length || (char != nil && char[0] == "") { 9 | return s 10 | } 11 | 12 | paddingChar := " " 13 | if char != nil { 14 | paddingChar = char[0] 15 | } 16 | 17 | padLen, leftPadding, rightPadding, cur, nextCur := length-len(s), "", "", 0, false 18 | for index := 0; index < padLen; index++ { 19 | if index%2 == 0 { 20 | rightPadding += string(paddingChar[cur]) 21 | } else { 22 | leftPadding += string(paddingChar[cur]) 23 | nextCur = true 24 | } 25 | 26 | if nextCur { 27 | cur++ 28 | nextCur = false 29 | } 30 | 31 | if cur >= len(paddingChar) { 32 | cur = 0 33 | } 34 | } 35 | 36 | return leftPadding + s + rightPadding 37 | } 38 | -------------------------------------------------------------------------------- /array/drop.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Drop returns a new array with `n` elements dropped from the beginning. 10 | // The function accepts an array of any type (interface{}) and an optional number `num` of elements to Drop. 11 | // It returns a new array with the specified number of elements removed from the beginning. 12 | func Drop(array interface{}, num ...int) (interface{}, error) { 13 | arrValue, n := reflect.ValueOf(array), 0 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if num != nil { 20 | n = num[0] 21 | } else { 22 | return array, nil 23 | } 24 | 25 | if n == 0 || arrValue.Len() == 0 { 26 | return array, nil 27 | } 28 | 29 | if n < 0 { 30 | return nil, constants.ErrParamLessThanZero 31 | } 32 | 33 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 34 | for i := n; i < arrValue.Len(); i++ { 35 | result = reflect.Append(result, arrValue.Index(i)) 36 | } 37 | 38 | return result.Interface(), nil 39 | } 40 | -------------------------------------------------------------------------------- /string/ends_with_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_endsWith_valid_not_position(t *testing.T) { 11 | isEndWith, err := EndsWith("abc", "c") 12 | 13 | assert.Equal(t, true, isEndWith) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_endsWith_valid_position(t *testing.T) { 18 | isEndWith, err := EndsWith("abcdef", "c", 3) 19 | 20 | assert.Equal(t, true, isEndWith) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_endsWith_invalid_not_position(t *testing.T) { 25 | isEndWith, err := EndsWith("abc", "d") 26 | 27 | assert.Equal(t, false, isEndWith) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_endsWith_invalid_position(t *testing.T) { 32 | isEndWith, err := EndsWith("abcdef", "c", 2) 33 | 34 | assert.Equal(t, false, isEndWith) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_endsWith_out_of_range(t *testing.T) { 39 | isEndWith, err := EndsWith("abc", "c", 9) 40 | 41 | assert.Equal(t, false, isEndWith) 42 | assert.Equal(t, constants.ErrOutOfRange, err) 43 | } 44 | -------------------------------------------------------------------------------- /array/indexOf.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import "reflect" 4 | 5 | // IndexOf finds the index of the first occurrence of a value in an array (Slice). 6 | // The function accepts an array (Slice), a value to search for, and an optional fromIndex. 7 | // It returns the index of the first occurrence of the value in the array. 8 | // If the value is not found, the function returns -1. 9 | func IndexOf(array, input interface{}, fromIndex ...int) interface{} { 10 | arrValue, inputValue := reflect.ValueOf(array), reflect.ValueOf(input) 11 | 12 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || arrValue.Len() == 0 { 13 | return -1 14 | } 15 | 16 | from := 0 17 | if fromIndex != nil { 18 | from = fromIndex[0] 19 | } 20 | 21 | if from < 0 { 22 | from = 0 23 | } 24 | 25 | if from >= arrValue.Len() { 26 | return -1 27 | } 28 | 29 | for i := from; i < arrValue.Len(); i++ { 30 | if arrValue.Index(0).Kind() != inputValue.Kind() { 31 | return -1 32 | } 33 | 34 | if reflect.DeepEqual(arrValue.Index(i).Interface(), input) { 35 | return i 36 | } 37 | } 38 | 39 | return -1 40 | } 41 | -------------------------------------------------------------------------------- /array/pullAt.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // PullAt removes elements from the input array at specified indexes and returns the modified array. 10 | // It takes an array-like data structure and a list of indexes to Remove. 11 | // The function returns the modified array and an error if any occurs. 12 | func PullAt(array interface{}, indexes []int) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | mapIndexesValue := make(map[int]int) 20 | for _, idx := range indexes { 21 | mapIndexesValue[idx]++ 22 | } 23 | 24 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 25 | for i := 0; i < arrValue.Len(); i++ { 26 | element := arrValue.Index(i) 27 | if data, ok := mapIndexesValue[i]; ok && data > 0 { 28 | mapIndexesValue[i] = 0 29 | for j := 0; j < data; j++ { 30 | result = reflect.Append(result, element) 31 | } 32 | } 33 | } 34 | 35 | return result.Interface(), nil 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 warriors-vn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/workflows-be.yml: -------------------------------------------------------------------------------- 1 | name: prepare-before-merge 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v3 16 | - uses: actions/setup-go@v3 17 | with: 18 | go-version: '1.18' 19 | - name: go-lint 20 | uses: golangci/golangci-lint-action@v3 21 | with: 22 | version: v1.46.2 23 | 24 | - name: unit-test 25 | run: go test -coverprofile=coverage.out ./... 26 | 27 | - name: coverage 28 | run: | 29 | SUM_COVERAGE=$(go test ./... --cover | awk '{if ($1 != "?") print $5; else print "0.0";}' | sed 's/\%//g' | awk '{s+=$1} END {printf "%.2f\n", s}') 30 | SUM_TEST=$(go test ./... --cover | wc -l) 31 | COVER_AVERAGE=$(echo "$SUM_COVERAGE/$SUM_TEST" | bc -l) 32 | echo $COVER_AVERAGE 33 | 34 | - name: upload coverage reports to Codecov 35 | uses: codecov/codecov-action@v3 36 | env: 37 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} -------------------------------------------------------------------------------- /array/take.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Take returns a new Slice containing the first 'number' elements from the input array. 10 | // It takes an array-like data structure and an optional number of elements to Take. 11 | // If 'number' is not provided or is greater than the length of the array, all elements are taken. 12 | // The function returns the new Slice and an error if any occurs. 13 | func Take(array interface{}, number ...int) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if arrValue.Len() == 0 { 21 | return array, nil 22 | } 23 | 24 | n := 1 25 | if number != nil && number[0] >= 0 { 26 | n = number[0] 27 | } 28 | 29 | if n >= arrValue.Len() { 30 | n = arrValue.Len() 31 | } 32 | 33 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 34 | for i := 0; i < n; i++ { 35 | result = reflect.Append(result, arrValue.Index(i)) 36 | } 37 | 38 | return result.Interface(), nil 39 | } 40 | -------------------------------------------------------------------------------- /array/without.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Without returns a new Slice that excludes specified values from the input array. 10 | // It takes an array-like data structure and a variable number of values to be excluded. 11 | // The function returns the new Slice Without the specified values and an error if any occurs. 12 | func Without(array interface{}, values ...interface{}) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if values == nil || arrValue.Len() == 0 { 20 | return array, nil 21 | } 22 | 23 | mapValue := make(map[interface{}]bool) 24 | for _, v := range values { 25 | mapValue[v] = true 26 | } 27 | 28 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 29 | for i := 0; i < arrValue.Len(); i++ { 30 | element := arrValue.Index(i) 31 | if !mapValue[element.Interface()] { 32 | result = reflect.Append(result, element) 33 | } 34 | } 35 | 36 | return result.Interface(), nil 37 | } 38 | -------------------------------------------------------------------------------- /string/starts_with_test.go: -------------------------------------------------------------------------------- 1 | package string 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_startsWith_valid_not_position(t *testing.T) { 11 | isStartWith, err := StartsWith("abc", "a") 12 | 13 | assert.Equal(t, true, isStartWith) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_startsWith_valid_position(t *testing.T) { 18 | isStartWith, err := StartsWith("abcdef", "c", 3) 19 | 20 | assert.Equal(t, true, isStartWith) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_startsWith_invalid_not_position(t *testing.T) { 25 | isStartWith, err := StartsWith("abc", "b") 26 | 27 | assert.Equal(t, false, isStartWith) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_startsWith_invalid_position(t *testing.T) { 32 | isStartWith, err := StartsWith("abcdef", "c", 2) 33 | 34 | assert.Equal(t, false, isStartWith) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_startsWith_out_of_range(t *testing.T) { 39 | isStartWith, err := StartsWith("abc", "a", 9) 40 | 41 | assert.Equal(t, false, isStartWith) 42 | assert.Equal(t, constants.ErrOutOfRange, err) 43 | } 44 | -------------------------------------------------------------------------------- /go_math/subtract.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import "reflect" 4 | 5 | // Subtract performs subtraction on two values of various numeric types. 6 | // It returns the result as an interface{} to handle different numeric types. 7 | func Subtract(minuend, subtrahend interface{}) interface{} { 8 | typeOfMinuend, typeOfSubtrahend := reflect.TypeOf(minuend), reflect.TypeOf(subtrahend) 9 | 10 | kind := typeOfMinuend.Kind() 11 | if typeOfMinuend.Kind() != typeOfSubtrahend.Kind() { 12 | return 0 13 | } 14 | 15 | switch kind { 16 | case reflect.Int: 17 | if result, ok := subtrahend.(int); ok { 18 | return minuend.(int) - result 19 | } 20 | case reflect.Int32: 21 | if result, ok := subtrahend.(int32); ok { 22 | return minuend.(int32) - result 23 | } 24 | case reflect.Int64: 25 | if result, ok := subtrahend.(int64); ok { 26 | return minuend.(int64) - result 27 | } 28 | case reflect.Float32: 29 | if result, ok := subtrahend.(float32); ok { 30 | return minuend.(float32) - result 31 | } 32 | case reflect.Float64: 33 | if result, ok := subtrahend.(float64); ok { 34 | return minuend.(float64) - result 35 | } 36 | } 37 | 38 | return 0 39 | } 40 | -------------------------------------------------------------------------------- /go_math/subtract_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_subtract_valid_int(t *testing.T) { 10 | result := Subtract(10, 1) 11 | 12 | assert.Equal(t, 9, result) 13 | } 14 | 15 | func Test_subtract_valid_int32(t *testing.T) { 16 | result := Subtract(int32(10), int32(10)) 17 | 18 | assert.Equal(t, int32(0), result) 19 | } 20 | 21 | func Test_subtract_valid_int64(t *testing.T) { 22 | result := Subtract(int64(1), int64(10)) 23 | 24 | assert.Equal(t, int64(-9), result) 25 | } 26 | 27 | func Test_subtract_valid_float32(t *testing.T) { 28 | result := Subtract(float32(10), float32(1)) 29 | 30 | assert.Equal(t, float32(9), result) 31 | } 32 | 33 | func Test_subtract_valid_float64(t *testing.T) { 34 | result := Subtract(float64(1), float64(10)) 35 | 36 | assert.Equal(t, float64(-9), result) 37 | } 38 | 39 | func Test_subtract_invalid_float32(t *testing.T) { 40 | result := Subtract(float32(1.5), 9) 41 | 42 | assert.Equal(t, 0, result) 43 | } 44 | 45 | func Test_subtract_invalid_type(t *testing.T) { 46 | result := Subtract(true, false) 47 | 48 | assert.Equal(t, 0, result) 49 | } 50 | -------------------------------------------------------------------------------- /collection/includes.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Includes checks if the input array contains the specified search value. 10 | // It takes an array-like data structure, a search value, and an optional starting index. 11 | // If the starting index is provided, the search starts from that index. 12 | // The function returns true if the search is found, false otherwise, or an error if any occurs. 13 | func Includes(array interface{}, search interface{}, fromIndex ...int) (interface{}, error) { 14 | arrValue, searchValue := reflect.ValueOf(array), reflect.ValueOf(search) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return false, constants.ErrNotSlice 18 | } 19 | 20 | from := 0 21 | if fromIndex != nil && fromIndex[0] > 0 { 22 | from = fromIndex[0] 23 | } 24 | 25 | if from >= arrValue.Len() { 26 | return false, nil 27 | } 28 | 29 | for i := from; i < arrValue.Len(); i++ { 30 | if reflect.DeepEqual(arrValue.Index(i).Interface(), searchValue.Interface()) { 31 | return true, nil 32 | } 33 | } 34 | 35 | return false, nil 36 | } 37 | -------------------------------------------------------------------------------- /array/takeRight.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // TakeRight returns a new Slice containing the Last 'number' elements from the input array. 10 | // It takes an array-like data structure and an optional number of elements to Take. 11 | // If 'number' is not provided or is greater than the length of the array, all elements are taken. 12 | // The function returns the new Slice and an error if any occurs. 13 | func TakeRight(array interface{}, number ...int) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if arrValue.Len() == 0 { 21 | return array, nil 22 | } 23 | 24 | n := 1 25 | if number != nil && number[0] >= 0 { 26 | n = number[0] 27 | } 28 | 29 | if n >= arrValue.Len() { 30 | n = arrValue.Len() 31 | } 32 | 33 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 34 | for i := arrValue.Len() - n; i < arrValue.Len(); i++ { 35 | result = reflect.Append(result, arrValue.Index(i)) 36 | } 37 | 38 | return result.Interface(), nil 39 | } 40 | -------------------------------------------------------------------------------- /array/sortedUniq.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // SortedUniq returns a new Slice with only unique elements from the sorted input Slice. 10 | // It takes an array-like data structure as input, assumes that the input Slice is sorted, 11 | // and returns a new Slice containing only unique elements. 12 | // The function returns the new Slice and an error if any occurs. 13 | func SortedUniq(array interface{}) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | mapArrValue := make(map[interface{}]int) 21 | for i := 0; i < arrValue.Len(); i++ { 22 | mapArrValue[arrValue.Index(i).Interface()]++ 23 | } 24 | 25 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 26 | for i := 0; i < arrValue.Len(); i++ { 27 | element := arrValue.Index(i) 28 | if data, ok := mapArrValue[element.Interface()]; ok && data > 0 { 29 | mapArrValue[element.Interface()] = 0 30 | result = reflect.Append(result, element) 31 | } 32 | } 33 | 34 | return result.Interface(), nil 35 | } 36 | -------------------------------------------------------------------------------- /go_math/add.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "reflect" 5 | ) 6 | 7 | // Add performs a mathematical operation on the provided 'augend' and 'addend'. 8 | // The function supports addition of integer and floating-point values. 9 | // It returns the result of the mathematical operation as an interface{} value. 10 | func Add(augend, addend interface{}) interface{} { 11 | typeOfAugend, typeOfAddend := reflect.TypeOf(augend), reflect.TypeOf(addend) 12 | 13 | kind := typeOfAugend.Kind() 14 | if typeOfAugend.Kind() != typeOfAddend.Kind() { 15 | return 0 16 | } 17 | 18 | switch kind { 19 | case reflect.Int: 20 | if result, ok := addend.(int); ok { 21 | return augend.(int) + result 22 | } 23 | case reflect.Int32: 24 | if result, ok := addend.(int32); ok { 25 | return augend.(int32) + result 26 | } 27 | case reflect.Int64: 28 | if result, ok := addend.(int64); ok { 29 | return augend.(int64) + result 30 | } 31 | case reflect.Float32: 32 | if result, ok := addend.(float32); ok { 33 | return augend.(float32) + result 34 | } 35 | case reflect.Float64: 36 | if result, ok := addend.(float64); ok { 37 | return augend.(float64) + result 38 | } 39 | } 40 | 41 | return 0 42 | } 43 | -------------------------------------------------------------------------------- /array/dropRight.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // DropRight returns a new array with `num` elements dropped from the end. 10 | // The function accepts an array of any type (interface{}) and an optional number `num` of elements to Drop. 11 | // It returns a new array with the specified number of elements removed from the end. 12 | func DropRight(array interface{}, num ...int) (interface{}, error) { 13 | arrValue, n := reflect.ValueOf(array), 0 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if num != nil { 20 | n = num[0] 21 | } else { 22 | return array, nil 23 | } 24 | 25 | if n == 0 { 26 | return array, nil 27 | } 28 | 29 | if n < 0 { 30 | return nil, constants.ErrParamLessThanZero 31 | } 32 | 33 | if arrValue.Len() == 0 { 34 | return array, nil 35 | } 36 | 37 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 38 | if n >= arrValue.Len() { 39 | return result.Interface(), nil 40 | } 41 | 42 | for i := 0; i < arrValue.Len()-n; i++ { 43 | result = reflect.Append(result, arrValue.Index(i)) 44 | } 45 | 46 | return result.Interface(), nil 47 | } 48 | -------------------------------------------------------------------------------- /array/indexOf_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_indexOf_valid_empty_array(t *testing.T) { 10 | result := IndexOf([]int{}, 1) 11 | 12 | assert.Equal(t, -1, result) 13 | } 14 | 15 | func Test_indexOf_valid_array_not_slice(t *testing.T) { 16 | result := IndexOf(true, 1) 17 | 18 | assert.Equal(t, -1, result) 19 | } 20 | 21 | func Test_indexOf_valid_from_great_than_array_length(t *testing.T) { 22 | result := IndexOf([]int64{1, 2, 3}, 1, 6) 23 | 24 | assert.Equal(t, -1, result) 25 | } 26 | 27 | func Test_indexOf_valid_from_less_than_zero(t *testing.T) { 28 | result := IndexOf([]float32{1.1, 2.2, 3.3}, float32(2.2), -6) 29 | 30 | assert.Equal(t, 1, result) 31 | } 32 | 33 | func Test_indexOf_valid_incompatible(t *testing.T) { 34 | result := IndexOf([]interface{}{1.1, "2.2", 3.3}, 2.2, 0) 35 | 36 | assert.Equal(t, -1, result) 37 | } 38 | 39 | func Test_indexOf_valid_not_found(t *testing.T) { 40 | result := IndexOf([]int32{1, 2, 3}, int32(4)) 41 | 42 | assert.Equal(t, -1, result) 43 | } 44 | 45 | func Test_indexOf_valid_string(t *testing.T) { 46 | result := IndexOf([]string{"1", "2", "b", "3", "c", "b"}, "b", 2) 47 | 48 | assert.Equal(t, 2, result) 49 | } 50 | -------------------------------------------------------------------------------- /array/pullAll.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // PullAll removes all occurrences of specified elements from the array (Slice). 10 | func PullAll(array, removes interface{}) (interface{}, error) { 11 | arrValue, removesValue := reflect.ValueOf(array), reflect.ValueOf(removes) 12 | 13 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || 14 | (removesValue.Kind() != reflect.Slice && removesValue.Kind() != reflect.Array) { 15 | return nil, constants.ErrNotSlice 16 | } 17 | 18 | mapArrValue := make(map[interface{}]int) 19 | for i := 0; i < arrValue.Len(); i++ { 20 | mapArrValue[arrValue.Index(i).Interface()]++ 21 | } 22 | 23 | for i := 0; i < removesValue.Len(); i++ { 24 | element := removesValue.Index(i) 25 | if _, ok := mapArrValue[element.Interface()]; ok { 26 | mapArrValue[element.Interface()] = 0 27 | } 28 | } 29 | 30 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 31 | for i := 0; i < arrValue.Len(); i++ { 32 | element := arrValue.Index(i) 33 | if data, ok := mapArrValue[element.Interface()]; ok && data > 0 { 34 | result = reflect.Append(result, element) 35 | } 36 | } 37 | 38 | return result.Interface(), nil 39 | } 40 | -------------------------------------------------------------------------------- /lang/lt.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Lt checks if the first value is less than the second value. 10 | // The function accepts two values of any comparable type (int, float, string, etc.) and returns true if the first value is less than the second value, false otherwise. 11 | // If the values are not comparable, the function returns false. 12 | func Lt(value, other interface{}) (bool, error) { 13 | valueOf, otherOfValue := reflect.ValueOf(value), reflect.ValueOf(other) 14 | 15 | if valueOf.Kind() != otherOfValue.Kind() { 16 | return false, constants.ErrIncompatible 17 | } 18 | 19 | switch valueOf.Kind() { 20 | case reflect.Int: 21 | return valueOf.Interface().(int) < otherOfValue.Interface().(int), nil 22 | case reflect.Int32: 23 | return valueOf.Interface().(int32) < otherOfValue.Interface().(int32), nil 24 | case reflect.Int64: 25 | return valueOf.Interface().(int64) < otherOfValue.Interface().(int64), nil 26 | case reflect.Float32: 27 | return valueOf.Interface().(float32) < otherOfValue.Interface().(float32), nil 28 | case reflect.Float64: 29 | return valueOf.Interface().(float64) < otherOfValue.Interface().(float64), nil 30 | } 31 | 32 | return false, constants.ErrNotSupport 33 | } 34 | -------------------------------------------------------------------------------- /array/concat.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Concat concatenates two arrays into a single array. 10 | // The function accepts two arrays of any type (interface{}) and returns a single concatenated array. 11 | func Concat(array, extend interface{}) (interface{}, error) { 12 | arrValue, extendValue := reflect.ValueOf(array), reflect.ValueOf(extend) 13 | 14 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || 15 | (extendValue.Kind() != reflect.Slice && extendValue.Kind() != reflect.Array) { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if arrValue.Len() == 0 { 20 | return extend, nil 21 | } 22 | 23 | if extendValue.Len() == 0 { 24 | return array, nil 25 | } 26 | 27 | result, kind := reflect.MakeSlice(arrValue.Type(), 0, 0), arrValue.Index(0).Kind() 28 | for i := 0; i < arrValue.Len(); i++ { 29 | result = reflect.Append(result, arrValue.Index(i)) 30 | } 31 | 32 | for i := 0; i < extendValue.Len(); i++ { 33 | element := extendValue.Index(i) 34 | if kind != element.Kind() && kind != reflect.Interface { 35 | return nil, constants.ErrIncompatible 36 | } 37 | result = reflect.Append(result, element) 38 | } 39 | 40 | return result.Interface(), nil 41 | } 42 | -------------------------------------------------------------------------------- /lang/lte.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Lte checks if the first value is less than or equal to the second value. 10 | // The function accepts two values of any comparable type (int, float, string, etc.) and returns true if the first value is less than or equal to the second value, false otherwise. 11 | // If the values are not comparable, the function returns an error. 12 | func Lte(value, other interface{}) (bool, error) { 13 | valueOf, otherOfValue := reflect.ValueOf(value), reflect.ValueOf(other) 14 | 15 | if valueOf.Kind() != otherOfValue.Kind() { 16 | return false, constants.ErrIncompatible 17 | } 18 | 19 | switch valueOf.Kind() { 20 | case reflect.Int: 21 | return valueOf.Interface().(int) <= otherOfValue.Interface().(int), nil 22 | case reflect.Int32: 23 | return valueOf.Interface().(int32) <= otherOfValue.Interface().(int32), nil 24 | case reflect.Int64: 25 | return valueOf.Interface().(int64) <= otherOfValue.Interface().(int64), nil 26 | case reflect.Float32: 27 | return valueOf.Interface().(float32) <= otherOfValue.Interface().(float32), nil 28 | case reflect.Float64: 29 | return valueOf.Interface().(float64) <= otherOfValue.Interface().(float64), nil 30 | } 31 | 32 | return false, constants.ErrNotSupport 33 | } 34 | -------------------------------------------------------------------------------- /array/intersection.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Intersection returns a new array (Slice) containing unique elements that are present in both input arrays. 10 | // The function accepts two arrays (slices) and returns a new array with the Intersection of unique elements. 11 | // If either of the inputs is not a Slice, the function returns an error. 12 | func Intersection(array, other interface{}) (interface{}, error) { 13 | arrValue, otherValue := reflect.ValueOf(array), reflect.ValueOf(other) 14 | 15 | if (arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array) || (otherValue.Kind() != reflect.Slice && otherValue.Kind() != reflect.Array) { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | if arrValue.Len() == 0 { 20 | return array, nil 21 | } 22 | 23 | if otherValue.Len() == 0 { 24 | return other, nil 25 | } 26 | 27 | mapOtherValue := make(map[interface{}]int) 28 | for i := 0; i < otherValue.Len(); i++ { 29 | mapOtherValue[otherValue.Index(i).Interface()]++ 30 | } 31 | 32 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 33 | for i := 0; i < arrValue.Len(); i++ { 34 | element := arrValue.Index(i) 35 | if data, ok := mapOtherValue[element.Interface()]; ok && data != 0 { 36 | mapOtherValue[element.Interface()] = 0 37 | result = reflect.Append(result, element) 38 | } 39 | } 40 | 41 | return result.Interface(), nil 42 | } 43 | -------------------------------------------------------------------------------- /go_math/multiply.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Multiply calculates the product of the multiplier and subtrahend. 10 | // The function accepts two arguments, multiplier and subtrahend, both of type interface{}. 11 | // It returns the result of the multiplication as an interface{} and an error if any issue occurs. 12 | func Multiply(multiplier, subtrahend interface{}) (interface{}, error) { 13 | typeOfMultiplier, typeOfSubtrahend := reflect.TypeOf(multiplier), reflect.TypeOf(subtrahend) 14 | 15 | kind := typeOfMultiplier.Kind() 16 | if typeOfMultiplier.Kind() != typeOfSubtrahend.Kind() { 17 | return 0, constants.ErrIncompatible 18 | } 19 | 20 | switch kind { 21 | case reflect.Int: 22 | if result, ok := subtrahend.(int); ok { 23 | return multiplier.(int) * result, nil 24 | } 25 | case reflect.Int32: 26 | if result, ok := subtrahend.(int32); ok { 27 | return multiplier.(int32) * result, nil 28 | } 29 | case reflect.Int64: 30 | if result, ok := subtrahend.(int64); ok { 31 | return multiplier.(int64) * result, nil 32 | } 33 | case reflect.Float32: 34 | if result, ok := subtrahend.(float32); ok { 35 | return multiplier.(float32) * result, nil 36 | } 37 | case reflect.Float64: 38 | if result, ok := subtrahend.(float64); ok { 39 | return multiplier.(float64) * result, nil 40 | } 41 | } 42 | 43 | return 0, constants.ErrNotSupport 44 | } 45 | -------------------------------------------------------------------------------- /array/chunk.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Chunk divides a given array into smaller chunks of the specified size. 10 | // The function accepts an array of any type (interface{}) and a size for each Chunk. 11 | // It returns a Slice of slices, where each inner Slice contains elements from the original array. 12 | // The Last Chunk may contain fewer elements if the length of the array is not divisible by the Chunk size. 13 | func Chunk(array interface{}, size int) (interface{}, error) { 14 | arrValue := reflect.ValueOf(array) 15 | 16 | if size < 0 { 17 | return nil, constants.ErrParamLessThanZero 18 | } 19 | 20 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 21 | return nil, constants.ErrNotSlice 22 | } 23 | 24 | if arrValue.Len() == 0 || size == 0 { 25 | return nil, constants.ErrEmptyList 26 | } 27 | 28 | chunkPart := reflect.MakeSlice(arrValue.Type(), 0, 0) 29 | result := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(array)), 0, 0) 30 | for i := 0; i < arrValue.Len(); i++ { 31 | chunkPart = reflect.Append(chunkPart, arrValue.Index(i)) 32 | if chunkPart.Len() == size { 33 | result = reflect.Append(result, chunkPart) 34 | chunkPart = reflect.MakeSlice(arrValue.Type(), 0, 0) 35 | } 36 | } 37 | 38 | if chunkPart.Len() > 0 { 39 | result = reflect.Append(result, chunkPart) 40 | } 41 | 42 | return result.Interface(), nil 43 | } 44 | -------------------------------------------------------------------------------- /array/fill.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Fill elements of an array (Slice) with a specified value within the specified range. 10 | // The function accepts an array (Slice), a value to Fill with, and optional start and end indices. 11 | // It returns the modified array with filled values. 12 | // If the input is not a Slice or if the start or end indices are out of bounds, the function returns an error. 13 | func Fill(array, input interface{}, start, end int) (interface{}, error) { 14 | arrValue, inputValue := reflect.ValueOf(array), reflect.ValueOf(input) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if start >= end || arrValue.Len() == 0 { 21 | return array, nil 22 | } 23 | 24 | if start < 0 { 25 | start = 0 26 | } 27 | 28 | if end < 0 { 29 | end = 1 30 | } 31 | 32 | if end > arrValue.Len() { 33 | end = arrValue.Len() 34 | } 35 | 36 | kind, result := arrValue.Index(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0) 37 | for i := 0; i < arrValue.Len(); i++ { 38 | if kind != inputValue.Kind() && kind != reflect.Interface { 39 | return nil, constants.ErrIncompatible 40 | } 41 | 42 | if start <= i && i < end { 43 | result = reflect.Append(result, inputValue) 44 | } else { 45 | result = reflect.Append(result, arrValue.Index(i)) 46 | } 47 | } 48 | 49 | return result.Interface(), nil 50 | } 51 | -------------------------------------------------------------------------------- /collection/sampleSize_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | "github.com/warriors-vn/go-dash/constants" 9 | ) 10 | 11 | func Test_sampleSize_valid_int(t *testing.T) { 12 | result, err := SampleSize([]int{1, 2, 3, 4, 5, 6}, 3) 13 | 14 | assert.Equal(t, 3, reflect.ValueOf(result).Len()) 15 | assert.Nil(t, err) 16 | } 17 | 18 | func Test_sampleSize_valid_int64(t *testing.T) { 19 | result, err := SampleSize([]int64{1, 2, 3, 4, 5, 6}, 2) 20 | 21 | assert.Equal(t, 2, reflect.ValueOf(result).Len()) 22 | assert.Nil(t, err) 23 | } 24 | 25 | func Test_sampleSize_valid_float64(t *testing.T) { 26 | result, err := SampleSize([]float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6}, 9) 27 | 28 | assert.Equal(t, 9, reflect.ValueOf(result).Len()) 29 | assert.Nil(t, err) 30 | } 31 | 32 | func Test_sampleSize_valid_string(t *testing.T) { 33 | result, err := SampleSize([]string{"1.1", "2.2", "3.3", "4.4", "5.5", "6.6"}, 8) 34 | 35 | assert.Equal(t, 8, reflect.ValueOf(result).Len()) 36 | assert.Nil(t, err) 37 | } 38 | 39 | func Test_sampleSize_valid_interface(t *testing.T) { 40 | result, err := SampleSize([]interface{}{"1.1", 2.2, true, false, 5, 6.6}, 6) 41 | 42 | assert.Equal(t, 6, reflect.ValueOf(result).Len()) 43 | assert.Nil(t, err) 44 | } 45 | 46 | func Test_sampleSize_invalid_array_not_slice(t *testing.T) { 47 | result, err := SampleSize(true, 1) 48 | 49 | assert.Equal(t, nil, result) 50 | assert.Equal(t, constants.ErrNotSlice, err) 51 | } 52 | -------------------------------------------------------------------------------- /array/pullAt_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_pullAt_valid_int(t *testing.T) { 11 | result, err := PullAt([]int{1, 2, 3}, []int{-2}) 12 | 13 | assert.Equal(t, []int{}, result) 14 | assert.Equal(t, nil, err) 15 | } 16 | 17 | func Test_pullAt_valid_float64(t *testing.T) { 18 | result, err := PullAt([]float64{1.1, 2.2, 3.3}, []int{0, 1, 2}) 19 | 20 | assert.Equal(t, []float64{1.1, 2.2, 3.3}, result) 21 | assert.Equal(t, nil, err) 22 | } 23 | 24 | func Test_pullAt_valid_bool(t *testing.T) { 25 | result, err := PullAt([]bool{true, true, false, true, false}, []int{0, 1, 3, 4}) 26 | 27 | assert.Equal(t, []bool{true, true, true, false}, result) 28 | assert.Equal(t, nil, err) 29 | } 30 | 31 | func Test_pullAt_valid_interface(t *testing.T) { 32 | result, err := PullAt([]interface{}{"true", 1, false, true, 1.1, "false"}, []int{0, 1, 3, 4, 5}) 33 | 34 | assert.Equal(t, []interface{}{"true", 1, true, 1.1, "false"}, result) 35 | assert.Equal(t, nil, err) 36 | } 37 | 38 | func Test_pullAt_valid_string(t *testing.T) { 39 | result, err := PullAt([]string{"a", "b", "c", "d"}, []int{1, 3}) 40 | 41 | assert.Equal(t, []string{"b", "d"}, result) 42 | assert.Equal(t, nil, err) 43 | } 44 | 45 | func Test_pullAt_invalid_array_not_slice(t *testing.T) { 46 | result, err := PullAt(true, []int{1, 2}) 47 | 48 | assert.Equal(t, nil, result) 49 | assert.Equal(t, constants.ErrNotSlice, err) 50 | } 51 | -------------------------------------------------------------------------------- /go_math/multiply_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_multiply(t *testing.T) { 11 | 12 | } 13 | 14 | func Test_multiply_valid_int(t *testing.T) { 15 | result, err := Multiply(10, 2) 16 | 17 | assert.Equal(t, 20, result) 18 | assert.Nil(t, err) 19 | } 20 | 21 | func Test_multiply_valid_int32(t *testing.T) { 22 | result, err := Multiply(int32(10), int32(10)) 23 | 24 | assert.Equal(t, int32(100), result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_multiply_valid_int64(t *testing.T) { 29 | result, err := Multiply(int64(-1), int64(10)) 30 | 31 | assert.Equal(t, int64(-10), result) 32 | assert.Nil(t, err) 33 | } 34 | 35 | func Test_multiply_valid_float32(t *testing.T) { 36 | result, err := Multiply(float32(10), float32(0.5)) 37 | 38 | assert.Equal(t, float32(5), result) 39 | assert.Nil(t, err) 40 | } 41 | 42 | func Test_multiply_valid_float64(t *testing.T) { 43 | result, err := Multiply(float64(1), 10.5) 44 | 45 | assert.Equal(t, 10.5, result) 46 | assert.Nil(t, err) 47 | } 48 | 49 | func Test_multiply_invalid_float32(t *testing.T) { 50 | result, err := Multiply(float32(1.5), 9) 51 | 52 | assert.Equal(t, 0, result) 53 | assert.Equal(t, constants.ErrIncompatible, err) 54 | } 55 | 56 | func Test_multiply_invalid_type(t *testing.T) { 57 | result, err := Multiply(true, false) 58 | 59 | assert.Equal(t, 0, result) 60 | assert.Equal(t, constants.ErrNotSupport, err) 61 | } 62 | -------------------------------------------------------------------------------- /collection/find.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Find returns the first element from the input array that satisfies the predicate. 10 | // It takes an array-like data structure and a predicate function that determines whether an element is a match. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns the found element and a boolean indicating whether a match was found, or an error if any occurs. 13 | func Find(array interface{}, predicate interface{}) (interface{}, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, constants.ErrNotSupport 27 | } 28 | 29 | kind := predicateValue.Type().In(0).Kind() 30 | for i := 0; i < arrValue.Len(); i++ { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return nil, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() == reflect.Bool && res[0].Interface().(bool) { 38 | return element.Interface(), nil 39 | } 40 | } 41 | 42 | return nil, constants.ErrNotFound 43 | } 44 | -------------------------------------------------------------------------------- /array/difference.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Difference returns a new Slice that contains the elements from the input array that are not present in the exclude slices. 10 | // It takes an array-like data structure and one or more exclude slices. 11 | // The function returns the new Slice of different elements and an error if any occurs. 12 | func Difference(array interface{}, exclude ...interface{}) (interface{}, error) { 13 | arrValue := reflect.ValueOf(array) 14 | 15 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 16 | return nil, constants.ErrNotSlice 17 | } 18 | 19 | var excludeArr interface{} 20 | if exclude == nil { 21 | return array, nil 22 | } else { 23 | excludeArr = exclude[0] 24 | } 25 | 26 | excludeValue := reflect.ValueOf(excludeArr) 27 | if excludeValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 28 | return nil, constants.ErrNotSlice 29 | } 30 | 31 | if excludeValue.Len() == 0 || arrValue.Len() == 0 { 32 | return array, nil 33 | } 34 | 35 | mapExcludeValue := make(map[interface{}]int) 36 | for i := 0; i < excludeValue.Len(); i++ { 37 | mapExcludeValue[excludeValue.Index(i).Interface()]++ 38 | } 39 | 40 | result := reflect.MakeSlice(arrValue.Type(), 0, 0) 41 | for i := 0; i < arrValue.Len(); i++ { 42 | element := arrValue.Index(i) 43 | if _, ok := mapExcludeValue[element.Interface()]; !ok { 44 | result = reflect.Append(result, element) 45 | } 46 | } 47 | 48 | return result.Interface(), nil 49 | } 50 | -------------------------------------------------------------------------------- /array/union.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Union returns a new Slice that contains the unique elements from multiple input arrays. 10 | // It takes variadic arguments representing array-like data structures and returns a new Slice 11 | // containing unique elements from all input arrays combined. 12 | // The function returns the new Slice and an error if any occurs. 13 | func Union(arrays ...interface{}) (interface{}, error) { 14 | if arrays == nil { 15 | return nil, constants.ErrNotSupport 16 | } 17 | 18 | arrFirstValueOf := reflect.ValueOf(arrays[0]) 19 | if arrFirstValueOf.Kind() != reflect.Slice && arrFirstValueOf.Kind() != reflect.Array { 20 | return nil, constants.ErrNotSlice 21 | } 22 | 23 | if arrFirstValueOf.Len() == 0 { 24 | return nil, constants.ErrNotSupport 25 | } 26 | 27 | mapValue, result, firstKind := make(map[interface{}]bool), reflect.MakeSlice(arrFirstValueOf.Type(), 0, 0), arrFirstValueOf.Index(0).Kind() 28 | for i := 0; i < len(arrays); i++ { 29 | arr := reflect.ValueOf(arrays[i]) 30 | if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array { 31 | continue 32 | } 33 | 34 | for j := 0; j < arr.Len(); j++ { 35 | element := arr.Index(j) 36 | if !mapValue[element.Interface()] { 37 | mapValue[element.Interface()] = true 38 | if firstKind != element.Kind() { 39 | return nil, constants.ErrIncompatible 40 | } 41 | result = reflect.Append(result, element) 42 | } 43 | } 44 | } 45 | 46 | return result.Interface(), nil 47 | } 48 | -------------------------------------------------------------------------------- /collection/findLast.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // FindLast returns the last element from the input array that satisfies the predicate. 10 | // It takes an array-like data structure and a predicate function that determines whether an element is a match. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns the found element and a boolean indicating whether a match was found, or an error if any occurs. 13 | func FindLast(array interface{}, predicate interface{}) (interface{}, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, constants.ErrNotSupport 27 | } 28 | 29 | kind := predicateValue.Type().In(0).Kind() 30 | for i := arrValue.Len() - 1; i >= 0; i-- { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return nil, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() == reflect.Bool && res[0].Interface().(bool) { 38 | return element.Interface(), nil 39 | } 40 | } 41 | 42 | return nil, constants.ErrNotFound 43 | } 44 | -------------------------------------------------------------------------------- /collection/filter.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Filter returns a new slice containing elements from the input array that satisfy the predicate. 10 | // It takes an array-like data structure and a predicate function that determines whether an element should be included. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns the new filtered slice and an error if any occurs. 13 | func Filter(array interface{}, predicate interface{}) (interface{}, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, constants.ErrNotSupport 27 | } 28 | 29 | kind, result := predicateValue.Type().In(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0) 30 | for i := 0; i < arrValue.Len(); i++ { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return nil, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() == reflect.Bool && res[0].Interface().(bool) { 38 | result = reflect.Append(result, element) 39 | } 40 | } 41 | 42 | return result.Interface(), nil 43 | } 44 | -------------------------------------------------------------------------------- /array/reverse_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_reverse_valid_int(t *testing.T) { 11 | result, err := Reverse([]int{1, 2, 3}) 12 | 13 | assert.Equal(t, []int{3, 2, 1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_reverse_valid_int64(t *testing.T) { 18 | result, err := Reverse([]int64{1, 2, 3}) 19 | 20 | assert.Equal(t, []int64{3, 2, 1}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_reverse_valid_float64(t *testing.T) { 25 | result, err := Reverse([]float64{1.1, 2.2, 3.3}) 26 | 27 | assert.Equal(t, []float64{3.3, 2.2, 1.1}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_reverse_valid_string(t *testing.T) { 32 | result, err := Reverse([]string{"1.1", "2.2", "3.3"}) 33 | 34 | assert.Equal(t, []string{"3.3", "2.2", "1.1"}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_reverse_valid_bool(t *testing.T) { 39 | result, err := Reverse([]bool{true, true, false, true, false}) 40 | 41 | assert.Equal(t, []bool{false, true, false, true, true}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_reverse_valid_interface(t *testing.T) { 46 | result, err := Reverse([]interface{}{"true", true, 1, 2.2, false}) 47 | 48 | assert.Equal(t, []interface{}{false, 2.2, 1, true, "true"}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_reverse_invalid_array_not_slice(t *testing.T) { 53 | result, err := Reverse(true) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotSlice, err) 57 | } 58 | -------------------------------------------------------------------------------- /array/lastIndexOf_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_lastIndexOf_valid_int(t *testing.T) { 11 | result, err := LastIndexOf([]int{1, 2, 1, 2}, 2) 12 | 13 | assert.Equal(t, 3, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_lastIndexOf_valid_int32(t *testing.T) { 18 | result, err := LastIndexOf([]int{1, 2, 1, 2}, 2, 2) 19 | 20 | assert.Equal(t, 1, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_lastIndexOf_valid_int64(t *testing.T) { 25 | result, err := LastIndexOf([]int64{2, 2, 1, 2}, int64(2), 0) 26 | 27 | assert.Equal(t, 0, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_lastIndexOf_valid_float32_from_great_than_array_length(t *testing.T) { 32 | result, err := LastIndexOf([]float32{1.1, 2.1, 1.1, 2.1}, float32(2.1), 9) 33 | 34 | assert.Equal(t, 3, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_lastIndexOf_valid_float64_not_found_search(t *testing.T) { 39 | result, err := LastIndexOf([]float64{1.1, 2.2, 1.1, 2.2}, 2.3, 4) 40 | 41 | assert.Equal(t, -1, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_lastIndexOf_valid_interface(t *testing.T) { 46 | result, err := LastIndexOf([]interface{}{"1.1", 2.2, true, false, true, int64(123)}, true) 47 | 48 | assert.Equal(t, 4, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_lastIndexOf_invalid_array_not_slice(t *testing.T) { 53 | result, err := LastIndexOf(true, 1) 54 | 55 | assert.Equal(t, -1, result) 56 | assert.Equal(t, constants.ErrNotSlice, err) 57 | } 58 | -------------------------------------------------------------------------------- /array/union_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_union_valid_arrays_int(t *testing.T) { 11 | result, err := Union([]int{2}, []int{1, 2}) 12 | 13 | assert.Equal(t, []int{2, 1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_union_valid_arrays_int64(t *testing.T) { 18 | result, err := Union([]int64{2}, []int64{1, 3}, 5) 19 | 20 | assert.Equal(t, []int64{2, 1, 3}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_union_valid_arrays_interface(t *testing.T) { 25 | result, err := Union([]interface{}{2}, []int64{1, 3}, 5, []bool{true, false}) 26 | 27 | assert.Equal(t, nil, result) 28 | assert.Equal(t, constants.ErrIncompatible, err) 29 | } 30 | 31 | func Test_union_invalid_arrays_nil(t *testing.T) { 32 | result, err := Union() 33 | 34 | assert.Equal(t, nil, result) 35 | assert.Equal(t, constants.ErrNotSupport, err) 36 | } 37 | 38 | func Test_union_invalid_arrays_first_not_slice(t *testing.T) { 39 | result, err := Union(true) 40 | 41 | assert.Equal(t, nil, result) 42 | assert.Equal(t, constants.ErrNotSlice, err) 43 | } 44 | 45 | func Test_union_invalid_arrays_first_empty(t *testing.T) { 46 | result, err := Union([]int32{}) 47 | 48 | assert.Equal(t, nil, result) 49 | assert.Equal(t, constants.ErrNotSupport, err) 50 | } 51 | 52 | func Test_union_invalid_arrays_first_not_interface(t *testing.T) { 53 | result, err := Union([]int32{1, 2, 3}, []bool{true}) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrIncompatible, err) 57 | } 58 | -------------------------------------------------------------------------------- /collection/every.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Every check if all elements in the input array satisfy the predicate function. 10 | // It takes an array-like data structure and a predicate function that determines the condition. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns true if all elements satisfy the condition, false otherwise, or an error if any occurs. 13 | func Every(array, predicate interface{}) (bool, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return false, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return false, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return false, constants.ErrNotSupport 27 | } 28 | 29 | kind := predicateValue.Type().In(0).Kind() 30 | for i := 0; i < arrValue.Len(); i++ { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return false, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() != reflect.Bool { 38 | return false, constants.ErrNotSupport 39 | } 40 | 41 | if len(res) > 0 && res[0].Kind() == reflect.Bool && !res[0].Interface().(bool) { 42 | return false, nil 43 | } 44 | } 45 | 46 | return true, nil 47 | } 48 | -------------------------------------------------------------------------------- /lang/eq_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_eq_valid_one(t *testing.T) { 10 | result := Eq(1, 1) 11 | 12 | assert.Equal(t, true, result) 13 | } 14 | 15 | func Test_eq_valid_two(t *testing.T) { 16 | result := Eq(1, 2) 17 | 18 | assert.Equal(t, false, result) 19 | } 20 | 21 | func Test_eq_valid_three(t *testing.T) { 22 | result := Eq(1.1, 1.1) 23 | 24 | assert.Equal(t, true, result) 25 | } 26 | 27 | func Test_eq_valid_four(t *testing.T) { 28 | result := Eq(true, true) 29 | 30 | assert.Equal(t, true, result) 31 | } 32 | 33 | func Test_eq_valid_five(t *testing.T) { 34 | result := Eq("true", "true") 35 | 36 | assert.Equal(t, true, result) 37 | } 38 | 39 | func Test_eq_valid_six(t *testing.T) { 40 | type User struct { 41 | Name string 42 | Age int 43 | } 44 | 45 | result := Eq(User{Name: "Vegeta", Age: 26}, User{Name: "Vegeta", Age: 26}) 46 | 47 | assert.Equal(t, true, result) 48 | } 49 | 50 | func Test_eq_valid_seven(t *testing.T) { 51 | type User struct { 52 | Name string 53 | Age int 54 | } 55 | 56 | result := Eq(&User{Name: "Vegeta", Age: 26}, &User{Name: "Vegeta", Age: 26}) 57 | 58 | assert.Equal(t, true, result) 59 | } 60 | 61 | func Test_eq_valid_eight(t *testing.T) { 62 | type User struct { 63 | Name string 64 | Age int 65 | } 66 | 67 | result := Eq(&User{Name: "Vegeta", Age: 27}, &User{Name: "Vegeta", Age: 26}) 68 | 69 | assert.Equal(t, false, result) 70 | } 71 | 72 | func Test_eq_invalid_type(t *testing.T) { 73 | result := Eq("true", true) 74 | 75 | assert.Equal(t, false, result) 76 | } 77 | -------------------------------------------------------------------------------- /collection/reject.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Reject returns a new slice containing elements from the input array that do not satisfy the predicate. 10 | // It takes an array-like data structure and a predicate function that determines whether an element should be excluded. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns the new slice without the excluded elements and an error if any occurs. 13 | func Reject(array interface{}, predicate interface{}) (interface{}, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, constants.ErrNotSupport 27 | } 28 | 29 | kind, result := predicateValue.Type().In(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0) 30 | for i := 0; i < arrValue.Len(); i++ { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return nil, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() == reflect.Bool && !res[0].Interface().(bool) { 38 | result = reflect.Append(result, element) 39 | } 40 | } 41 | 42 | return result.Interface(), nil 43 | } 44 | -------------------------------------------------------------------------------- /collection/map.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // GoDashMap applies the iteratee function to each element in the input array and returns a new slice of results. 10 | // It takes an array-like data structure and an iteratee function that operates on each element. 11 | // The iteratee function should have the signature func(elementType) mappedElementType. 12 | // The function returns a new slice of mapped elements and an error if any occurs. 13 | func GoDashMap(array, iteratee interface{}) (interface{}, error) { 14 | arrValue, iterateeValue := reflect.ValueOf(array), reflect.ValueOf(iteratee) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, constants.ErrNotSlice 18 | } 19 | 20 | if iterateeValue.Kind() != reflect.Func { 21 | return nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := iterateeValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, constants.ErrNotSupport 27 | } 28 | 29 | kind, result := iterateeValue.Type().In(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0) 30 | if kind == reflect.Interface { 31 | return nil, constants.ErrNotSupport 32 | } 33 | for i := 0; i < arrValue.Len(); i++ { 34 | element := arrValue.Index(i) 35 | if element.Kind() != kind { 36 | return nil, constants.ErrIncompatible 37 | } 38 | 39 | res := iterateeValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 40 | if len(res) > 0 && res[0].Kind() == kind { 41 | result = reflect.Append(result, res[0]) 42 | } 43 | } 44 | 45 | return result.Interface(), nil 46 | } 47 | -------------------------------------------------------------------------------- /lang/castArray_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_castArray_valid_int(t *testing.T) { 11 | result, err := CastArray(123) 12 | 13 | assert.Equal(t, []int{123}, result) 14 | assert.Equal(t, nil, err) 15 | } 16 | 17 | func Test_castArray_valid_int32(t *testing.T) { 18 | result, err := CastArray(int32(123)) 19 | 20 | assert.Equal(t, []int32{123}, result) 21 | assert.Equal(t, nil, err) 22 | } 23 | 24 | func Test_castArray_valid_int64(t *testing.T) { 25 | result, err := CastArray(int64(123)) 26 | 27 | assert.Equal(t, []int64{123}, result) 28 | assert.Equal(t, nil, err) 29 | } 30 | 31 | func Test_castArray_valid_float32(t *testing.T) { 32 | result, err := CastArray(float32(1.23)) 33 | 34 | assert.Equal(t, []float32{1.23}, result) 35 | assert.Equal(t, nil, err) 36 | } 37 | 38 | func Test_castArray_valid_float64(t *testing.T) { 39 | result, err := CastArray(1.23) 40 | 41 | assert.Equal(t, []float64{1.23}, result) 42 | assert.Equal(t, nil, err) 43 | } 44 | 45 | func Test_castArray_valid_string(t *testing.T) { 46 | result, err := CastArray("1.23") 47 | 48 | assert.Equal(t, []string{"1.23"}, result) 49 | assert.Equal(t, nil, err) 50 | } 51 | 52 | func Test_castArray_valid_bool(t *testing.T) { 53 | result, err := CastArray(true) 54 | 55 | assert.Equal(t, []bool{true}, result) 56 | assert.Equal(t, nil, err) 57 | } 58 | 59 | func Test_castArray_invalid_not_support(t *testing.T) { 60 | result, err := CastArray(uint(123)) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSupport, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/remove.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Remove elements from the input array based on a given predicate function. 10 | // It takes an array-like data structure and a predicate function that determines 11 | // whether an element should be removed. 12 | // The function returns the modified array and an error if any occurs. 13 | func Remove(array interface{}, predicate interface{}) (interface{}, interface{}, error) { 14 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 15 | 16 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 17 | return nil, nil, constants.ErrNotSlice 18 | } 19 | 20 | if predicateValue.Kind() != reflect.Func { 21 | return nil, nil, constants.ErrNotFunction 22 | } 23 | 24 | numParams := predicateValue.Type().NumIn() 25 | if numParams != 1 { 26 | return nil, nil, constants.ErrNotSupport 27 | } 28 | 29 | kind, result, old := predicateValue.Type().In(0).Kind(), reflect.MakeSlice(arrValue.Type(), 0, 0), reflect.MakeSlice(arrValue.Type(), 0, 0) 30 | for i := 0; i < arrValue.Len(); i++ { 31 | element := arrValue.Index(i) 32 | if element.Kind() != kind { 33 | return nil, nil, constants.ErrIncompatible 34 | } 35 | 36 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 37 | if len(res) > 0 && res[0].Kind() == reflect.Bool { 38 | if res[0].Interface().(bool) { 39 | result = reflect.Append(result, element) 40 | } else { 41 | old = reflect.Append(old, element) 42 | } 43 | } 44 | } 45 | 46 | return result.Interface(), old.Interface(), nil 47 | } 48 | -------------------------------------------------------------------------------- /array/take_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_take_valid_array_empty(t *testing.T) { 11 | result, err := Take([]int{}, 69) 12 | 13 | assert.Equal(t, []int{}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_take_valid_int(t *testing.T) { 18 | result, err := Take([]int{1, 2, 3}) 19 | 20 | assert.Equal(t, []int{1}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_take_valid_int64(t *testing.T) { 25 | result, err := Take([]int64{1, 2, 3}, 2) 26 | 27 | assert.Equal(t, []int64{1, 2}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_take_valid_float64(t *testing.T) { 32 | result, err := Take([]float64{1, 2, 3}, 5) 33 | 34 | assert.Equal(t, []float64{1, 2, 3}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_take_valid_string(t *testing.T) { 39 | result, err := Take([]string{"1", "2", "3"}, 0) 40 | 41 | assert.Equal(t, []string{}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_take_valid_bool(t *testing.T) { 46 | result, err := Take([]bool{true, true, true, false, true, false}, 4) 47 | 48 | assert.Equal(t, []bool{true, true, true, false}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_take_valid_interface(t *testing.T) { 53 | result, err := Take([]interface{}{"true", true, 1, 2.2, true, false}, 4) 54 | 55 | assert.Equal(t, []interface{}{"true", true, 1, 2.2}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_take_invalid_array_not_slice(t *testing.T) { 60 | result, err := Take(true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSlice, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/tail_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_tail_valid_int(t *testing.T) { 11 | result, err := Tail([]int{1, 2, 3, 3, 2, 1}) 12 | 13 | assert.Equal(t, []int{2, 3, 3, 2, 1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_tail_valid_int64(t *testing.T) { 18 | result, err := Tail([]int64{1, 2, 3, 3, 2, 1}) 19 | 20 | assert.Equal(t, []int64{2, 3, 3, 2, 1}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_tail_valid_float64(t *testing.T) { 25 | result, err := Tail([]float64{1.1, 2.2, 3.3, 3.3, 2.2, 1.1}) 26 | 27 | assert.Equal(t, []float64{2.2, 3.3, 3.3, 2.2, 1.1}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_tail_valid_string(t *testing.T) { 32 | result, err := Tail([]string{"1.1", "2.2", "3.3", "3.3", "2.2", "1.1"}) 33 | 34 | assert.Equal(t, []string{"2.2", "3.3", "3.3", "2.2", "1.1"}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_tail_valid_bool(t *testing.T) { 39 | result, err := Tail([]bool{false, true, true}) 40 | 41 | assert.Equal(t, []bool{true, true}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_tail_valid_interface(t *testing.T) { 46 | result, err := Tail([]interface{}{false, "true", true, 1, 2.2}) 47 | 48 | assert.Equal(t, []interface{}{"true", true, 1, 2.2}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_tail_valid_array_empty(t *testing.T) { 53 | result, err := Tail([]bool{}) 54 | 55 | assert.Equal(t, []bool{}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_tail_invalid_array_not_slice(t *testing.T) { 60 | result, err := Tail(true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSlice, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/takeRight_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_takeRight_valid_int(t *testing.T) { 11 | result, err := TakeRight([]int{1, 2, 3}) 12 | 13 | assert.Equal(t, []int{3}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_takeRight_valid_int64(t *testing.T) { 18 | result, err := TakeRight([]int64{1, 2, 3}, 2) 19 | 20 | assert.Equal(t, []int64{2, 3}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_takeRight_valid_float64(t *testing.T) { 25 | result, err := TakeRight([]float64{1.1, 2.2, 3.3}, 5) 26 | 27 | assert.Equal(t, []float64{1.1, 2.2, 3.3}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_takeRight_valid_string(t *testing.T) { 32 | result, err := TakeRight([]string{"1.1", "2.2", "3.3"}, 0) 33 | 34 | assert.Equal(t, []string{}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_takeRight_valid_bool(t *testing.T) { 39 | result, err := TakeRight([]bool{true, true, false, true, false}, 3) 40 | 41 | assert.Equal(t, []bool{false, true, false}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_takeRight_valid_interface(t *testing.T) { 46 | result, err := TakeRight([]interface{}{true, "true", false, "true", 1.1}, 3) 47 | 48 | assert.Equal(t, []interface{}{false, "true", 1.1}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_takeRight_valid_array_empty(t *testing.T) { 53 | result, err := TakeRight([]int{}) 54 | 55 | assert.Equal(t, []int{}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_takeRight_invalid_array_not_slice(t *testing.T) { 60 | result, err := TakeRight(true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSlice, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/xor_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_xor_valid_int(t *testing.T) { 11 | result, err := Xor([]int{4, 4, 2, 1}, []int{2, 3, 4}) 12 | 13 | assert.Equal(t, []int{1, 3}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_xor_valid_int64(t *testing.T) { 18 | result, err := Xor([]int64{2, 1}, []int64{2, 3}, []int64{2}) 19 | 20 | assert.Equal(t, []int64{1, 3}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_xor_valid_float64(t *testing.T) { 25 | result, err := Xor([]float64{2, 1}, 3, []float64{2, 3}, []float64{2}) 26 | 27 | assert.Equal(t, []float64{1, 3}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_xor_valid_string(t *testing.T) { 32 | result, err := Xor([]string{"2", "1"}, 3, []string{"2", "3"}, []string{"2"}) 33 | 34 | assert.Equal(t, []string{"1", "3"}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_xor_invalid_arrays_nil(t *testing.T) { 39 | result, err := Xor() 40 | 41 | assert.Equal(t, nil, result) 42 | assert.Equal(t, constants.ErrNotSupport, err) 43 | } 44 | 45 | func Test_xor_invalid_first_array_not_slice(t *testing.T) { 46 | result, err := Xor(true, []int{1, 2, 3}) 47 | 48 | assert.Equal(t, nil, result) 49 | assert.Equal(t, constants.ErrNotSlice, err) 50 | } 51 | 52 | func Test_xor_invalid_first_array_empty(t *testing.T) { 53 | result, err := Xor([]int{}, []int{1, 2, 3}) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotSupport, err) 57 | } 58 | 59 | func Test_xor_invalid_incompatible(t *testing.T) { 60 | result, err := Xor([]int{1, 2, 3}, []float64{1, 2, 3}) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrIncompatible, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/without_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_without_valid_array_empty(t *testing.T) { 11 | result, err := Without([]int{}, 1, 2) 12 | 13 | assert.Equal(t, []int{}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_without_valid_values_nil(t *testing.T) { 18 | result, err := Without([]int32{1, 2, 3}) 19 | 20 | assert.Equal(t, []int32{1, 2, 3}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_without_valid_values_int(t *testing.T) { 25 | result, err := Without([]int{2, 1, 2, 3}, 1, 2) 26 | 27 | assert.Equal(t, []int{3}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_without_valid_values_int64(t *testing.T) { 32 | result, err := Without([]int64{2, 1, 2, 3}, int64(1), int64(2)) 33 | 34 | assert.Equal(t, []int64{3}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_without_valid_values_string(t *testing.T) { 39 | result, err := Without([]string{"2", "1", "2", "3"}, "1", 2) 40 | 41 | assert.Equal(t, []string{"2", "2", "3"}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_without_valid_values_bool(t *testing.T) { 46 | result, err := Without([]bool{true, true, false, true, false, false}, false) 47 | 48 | assert.Equal(t, []bool{true, true, true}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_without_valid_values_interface(t *testing.T) { 53 | result, err := Without([]interface{}{"true", true, false, 1, 2.2, 3.3}, 2.2, false) 54 | 55 | assert.Equal(t, []interface{}{"true", true, 1, 3.3}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_without_invalid_array_not_slice(t *testing.T) { 60 | result, err := Without(true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSlice, err) 64 | } 65 | -------------------------------------------------------------------------------- /array/join_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_join_valid_int(t *testing.T) { 11 | result, err := Join([]int{1, 2, 3}, "-") 12 | 13 | assert.Equal(t, "1-2-3", result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_join_valid_int32(t *testing.T) { 18 | result, err := Join([]int32{1, 2, 3}, "-") 19 | 20 | assert.Equal(t, "1-2-3", result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_join_valid_int64(t *testing.T) { 25 | result, err := Join([]int64{1, 2, 3}, "-") 26 | 27 | assert.Equal(t, "1-2-3", result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_join_valid_float32(t *testing.T) { 32 | result, err := Join([]float32{1.1, 2.2, 3.3}, "-") 33 | 34 | assert.Equal(t, "1.1-2.2-3.3", result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_join_valid_float64(t *testing.T) { 39 | result, err := Join([]float64{1.1, 2.2, 3.3}, "-") 40 | 41 | assert.Equal(t, "1.1-2.2-3.3", result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_join_valid_bool(t *testing.T) { 46 | result, err := Join([]bool{true, false}, "-") 47 | 48 | assert.Equal(t, "true-false", result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_join_valid_string(t *testing.T) { 53 | result, err := Join([]string{"true", "false"}, "-") 54 | 55 | assert.Equal(t, "true-false", result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_join_valid_interface(t *testing.T) { 60 | result, err := Join([]interface{}{"1", false, 1.1, 2.2, 2}, "-") 61 | 62 | assert.Equal(t, "1-false-1.1-2.2-2", result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_join_invalid_array_not_slice(t *testing.T) { 67 | result, err := Join(true, "-") 68 | 69 | assert.Equal(t, "", result) 70 | assert.Equal(t, constants.ErrNotSlice, err) 71 | } 72 | -------------------------------------------------------------------------------- /array/isContains_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_isContainsArray_valid_one(t *testing.T) { 11 | result, err := IsContainsArray([]int{1, 2, 3}, 1) 12 | 13 | assert.Equal(t, true, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_isContainsArray_valid_two(t *testing.T) { 18 | result, err := IsContainsArray([]string{"1", "2", "3"}, "1") 19 | 20 | assert.Equal(t, true, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_isContainsArray_valid_three(t *testing.T) { 25 | result, err := IsContainsArray([]string{"1", "2", "3"}, "9") 26 | 27 | assert.Equal(t, false, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_isContainsArray_valid_four(t *testing.T) { 32 | result, err := IsContainsArray([]bool{true, true, false}, true) 33 | 34 | assert.Equal(t, true, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_isContainsArray_valid_five(t *testing.T) { 39 | type User struct { 40 | Name string 41 | Age int 42 | } 43 | result, err := IsContainsArray([]*User{{Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}, {Name: "GoTen", Age: 9}}, &User{Name: "Trunk", Age: 10}) 44 | 45 | assert.Equal(t, true, result) 46 | assert.Nil(t, err) 47 | } 48 | 49 | func Test_isContainsArray_valid_six(t *testing.T) { 50 | type User struct { 51 | Name string 52 | Age int 53 | } 54 | result, err := IsContainsArray([]*User{{Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}, {Name: "GoTen", Age: 9}}, &User{Name: "Kakalot", Age: 26}) 55 | 56 | assert.Equal(t, false, result) 57 | assert.Nil(t, err) 58 | } 59 | 60 | func Test_isContainsArray_invalid_array_not_slice(t *testing.T) { 61 | result, err := IsContainsArray(true, 123) 62 | 63 | assert.Equal(t, false, result) 64 | assert.Equal(t, constants.ErrNotSlice, err) 65 | } 66 | -------------------------------------------------------------------------------- /array/pullAll_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_pullAll_valid_int(t *testing.T) { 11 | result, err := PullAll([]int{1, 2, 3, 4, 3}, []int{2}) 12 | 13 | assert.Equal(t, []int{1, 3, 4, 3}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_pullAll_valid_bool(t *testing.T) { 18 | result, err := PullAll([]bool{true, true, false, true, false}, []bool{false}) 19 | 20 | assert.Equal(t, []bool{true, true, true}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_pullAll_valid_interface(t *testing.T) { 25 | result, err := PullAll([]interface{}{"true", 1, 1.1, true, false}, []bool{false}) 26 | 27 | assert.Equal(t, []interface{}{"true", 1, 1.1, true}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_pullAll_valid_interface_two(t *testing.T) { 32 | result, err := PullAll([]string{"a", "b", "c", "a", "b", "c"}, []interface{}{false}) 33 | 34 | assert.Equal(t, []string{"a", "b", "c", "a", "b", "c"}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_pullAll_valid_string(t *testing.T) { 39 | result, err := PullAll([]string{"a", "b", "c", "a", "b", "c"}, []string{"a", "c"}) 40 | 41 | assert.Equal(t, []string{"b", "b"}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_pullAll_valid_string_two(t *testing.T) { 46 | result, err := PullAll([]string{"a", "b", "c", "a", "b", "c"}, []string{}) 47 | 48 | assert.Equal(t, []string{"a", "b", "c", "a", "b", "c"}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_pullAll_invalid_array_not_slice(t *testing.T) { 53 | result, err := PullAll(true, []int{1, 2, 3}) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotSlice, err) 57 | } 58 | 59 | func Test_pullAll_invalid_remmoves_not_slice(t *testing.T) { 60 | result, err := PullAll([]int{1, 2, 3}, true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotSlice, err) 64 | } 65 | -------------------------------------------------------------------------------- /collection/includes_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_includes_valid_int(t *testing.T) { 11 | result, err := Includes([]int{1, 2, 3, 4, 5}, 3) 12 | 13 | assert.Equal(t, true, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_includes_valid_int64(t *testing.T) { 18 | result, err := Includes([]int64{1, 2, 3, 4, 5}, int64(5)) 19 | 20 | assert.Equal(t, true, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_includes_valid_float64(t *testing.T) { 25 | result, err := Includes([]float64{1.1, 2.2, 3.3, 4.4, 5.5}, 5.5, 2) 26 | 27 | assert.Equal(t, true, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_includes_valid_string(t *testing.T) { 32 | result, err := Includes([]string{"1.1", "2.2", "3.3", "4.4", "5.5"}, "5.5", 3) 33 | 34 | assert.Equal(t, true, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_includes_valid_interface(t *testing.T) { 39 | result, err := Includes([]interface{}{"1.1", true, 3.3, 4, false}, true) 40 | 41 | assert.Equal(t, true, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_includes_valid_not_include(t *testing.T) { 46 | result, err := Includes([]interface{}{"1.1", true, 3.3, false}, 3) 47 | 48 | assert.Equal(t, false, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_includes_valid_bool(t *testing.T) { 53 | result, err := Includes([]bool{true, true, false}, true, 1) 54 | 55 | assert.Equal(t, true, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_includes_valid_from_great_than_array_length(t *testing.T) { 60 | result, err := Includes([]bool{true, true, false}, true, 10) 61 | 62 | assert.Equal(t, false, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_includes_invalid_array_not_slice(t *testing.T) { 67 | result, err := Includes(true, true) 68 | 69 | assert.Equal(t, false, result) 70 | assert.Equal(t, constants.ErrNotSlice, err) 71 | } 72 | -------------------------------------------------------------------------------- /array/nth_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_nth_valid_n_great_than_array_length(t *testing.T) { 11 | result, err := Nth([]int{1, 2, 3}, 4) 12 | 13 | assert.Equal(t, nil, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_nth_valid_n_less_than_zero_one(t *testing.T) { 18 | result, err := Nth([]int32{1, 2, 3}, -3) 19 | 20 | assert.Equal(t, int32(1), result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_nth_valid_n_less_than_zero_two(t *testing.T) { 25 | result, err := Nth([]int32{1, 2, 3}, -4) 26 | 27 | assert.Equal(t, nil, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_nth_valid_int64(t *testing.T) { 32 | result, err := Nth([]int64{1, 2, 3}, 2) 33 | 34 | assert.Equal(t, int64(3), result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_nth_valid_float32(t *testing.T) { 39 | result, err := Nth([]float32{1, 2, 3}, 1) 40 | 41 | assert.Equal(t, float32(2), result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_nth_valid_float64(t *testing.T) { 46 | result, err := Nth([]float64{1.1, 2.2, 3.3}, 2) 47 | 48 | assert.Equal(t, 3.3, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_nth_valid_bool(t *testing.T) { 53 | result, err := Nth([]bool{true, true, true, false, true}, 2) 54 | 55 | assert.Equal(t, true, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_nth_valid_string(t *testing.T) { 60 | result, err := Nth([]string{"1.1", "2.2", "3.3"}, 2) 61 | 62 | assert.Equal(t, "3.3", result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_nth_valid_interface(t *testing.T) { 67 | result, err := Nth([]interface{}{"1.1", true, 2.2, float32(3.3)}, 2) 68 | 69 | assert.Equal(t, 2.2, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_nth_invalid_array_not_slice(t *testing.T) { 74 | result, err := Nth(true, 1) 75 | 76 | assert.Equal(t, nil, result) 77 | assert.Equal(t, constants.ErrNotSlice, err) 78 | } 79 | -------------------------------------------------------------------------------- /array/find_index_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | type User struct { 10 | Name string 11 | Age int 12 | } 13 | 14 | func Test_findIndex_int32(t *testing.T) { 15 | array := []int32{1, 2, 3, 8, 7, 6, 5, 9} 16 | target := int32(6) 17 | 18 | index, found := FindIndex(array, target) 19 | 20 | assert.Equal(t, 5, index) 21 | assert.Equal(t, true, found) 22 | } 23 | 24 | func Test_findIndex_int64(t *testing.T) { 25 | array := []int64{1, 2, 3, 8, 7, 6, 5, 9} 26 | target := int64(8) 27 | 28 | index, found := FindIndex(array, target) 29 | 30 | assert.Equal(t, 3, index) 31 | assert.Equal(t, true, found) 32 | } 33 | 34 | func Test_findIndex_float32(t *testing.T) { 35 | array := []float32{1, 2, 3, 8, 7, 6, 5, 9} 36 | target := float32(3) 37 | 38 | index, found := FindIndex(array, target) 39 | 40 | assert.Equal(t, 2, index) 41 | assert.Equal(t, true, found) 42 | } 43 | 44 | func Test_findIndex_string(t *testing.T) { 45 | array := []string{"a", "b", "c"} 46 | target := "b" 47 | 48 | index, found := FindIndex(array, target) 49 | 50 | assert.Equal(t, 1, index) 51 | assert.Equal(t, true, found) 52 | } 53 | 54 | func Test_findIndex_struct(t *testing.T) { 55 | array := []*User{ 56 | { 57 | Name: "warriors", 58 | Age: 20, 59 | }, 60 | { 61 | Name: "god", 62 | Age: 10, 63 | }, 64 | { 65 | Name: "vegeta", 66 | Age: 15, 67 | }, 68 | } 69 | target := &User{ 70 | Name: "vegeta", 71 | Age: 15, 72 | } 73 | 74 | index, found := FindIndex(array, target) 75 | 76 | assert.Equal(t, 2, index) 77 | assert.Equal(t, true, found) 78 | } 79 | 80 | func Test_findIndex_invalid_array(t *testing.T) { 81 | array := int32(9) 82 | target := int32(6) 83 | 84 | index, found := FindIndex(array, target) 85 | 86 | assert.Equal(t, -1, index) 87 | assert.Equal(t, false, found) 88 | } 89 | 90 | func Test_findIndex_not_found(t *testing.T) { 91 | array := []int32{1, 2, 3, 8, 7, 6, 5, 9} 92 | target := int32(10) 93 | 94 | index, found := FindIndex(array, target) 95 | 96 | assert.Equal(t, -1, index) 97 | assert.Equal(t, false, found) 98 | } 99 | -------------------------------------------------------------------------------- /array/xor.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Xor returns a new Slice that contains the elements that appear in an odd number of input arrays. 10 | // It takes variadic arguments representing array-like data structures and returns a new Slice 11 | // containing elements that are present in an odd number of input arrays. 12 | // The function returns the new Slice and an error if any occurs. 13 | func Xor(arrays ...interface{}) (interface{}, error) { 14 | if arrays == nil { 15 | return nil, constants.ErrNotSupport 16 | } 17 | 18 | arrFirstValueOf := reflect.ValueOf(arrays[0]) 19 | if arrFirstValueOf.Kind() != reflect.Slice && arrFirstValueOf.Kind() != reflect.Array { 20 | return nil, constants.ErrNotSlice 21 | } 22 | 23 | if arrFirstValueOf.Len() == 0 { 24 | return nil, constants.ErrNotSupport 25 | } 26 | 27 | arrMapValue := make([]map[interface{}]bool, 0) 28 | for i := 0; i < len(arrays); i++ { 29 | arr := reflect.ValueOf(arrays[i]) 30 | if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array { 31 | continue 32 | } 33 | 34 | mapValue := make(map[interface{}]bool) 35 | for j := 0; j < arr.Len(); j++ { 36 | mapValue[arr.Index(j).Interface()] = true 37 | } 38 | 39 | arrMapValue = append(arrMapValue, mapValue) 40 | } 41 | 42 | result, firstKind := reflect.MakeSlice(arrFirstValueOf.Type(), 0, 0), arrFirstValueOf.Index(0).Kind() 43 | for i := 0; i < len(arrays); i++ { 44 | arr := reflect.ValueOf(arrays[i]) 45 | if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array { 46 | continue 47 | } 48 | 49 | for j := 0; j < arr.Len(); j++ { 50 | element, isSetTrue := arr.Index(j), false 51 | for _, mapValue := range arrMapValue { 52 | if isSetTrue { 53 | mapValue[element.Interface()] = true 54 | } 55 | 56 | if !mapValue[element.Interface()] && !isSetTrue { 57 | if firstKind != element.Kind() { 58 | return nil, constants.ErrIncompatible 59 | } 60 | isSetTrue = true 61 | result = reflect.Append(result, element) 62 | } 63 | } 64 | } 65 | } 66 | 67 | return result.Interface(), nil 68 | } 69 | -------------------------------------------------------------------------------- /array/initial_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_initial_valid_empty_array(t *testing.T) { 11 | result, err := Initial([]int{}) 12 | 13 | assert.Equal(t, []int{}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_initial_valid_int(t *testing.T) { 18 | result, err := Initial([]int{1, 2, 3}) 19 | 20 | assert.Equal(t, []int{1, 2}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_initial_valid_int32(t *testing.T) { 25 | result, err := Initial([]int32{1, 2, 3, 4, 5, 6}) 26 | 27 | assert.Equal(t, []int32{1, 2, 3, 4, 5}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_initial_valid_int64(t *testing.T) { 32 | result, err := Initial([]int64{1, 2, 3, 4, 5, 6}) 33 | 34 | assert.Equal(t, []int64{1, 2, 3, 4, 5}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_initial_valid_float32(t *testing.T) { 39 | result, err := Initial([]float32{1, 2, 3, 4, 5, 6}) 40 | 41 | assert.Equal(t, []float32{1, 2, 3, 4, 5}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_initial_valid_float64(t *testing.T) { 46 | result, err := Initial([]float64{1, 2, 3, 4, 5, 6}) 47 | 48 | assert.Equal(t, []float64{1, 2, 3, 4, 5}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_initial_valid_string(t *testing.T) { 53 | result, err := Initial([]string{"a", "b", "c"}) 54 | 55 | assert.Equal(t, []string{"a", "b"}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_initial_valid_bool(t *testing.T) { 60 | result, err := Initial([]bool{true, true, false, true}) 61 | 62 | assert.Equal(t, []bool{true, true, false}, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_initial_valid_interface(t *testing.T) { 67 | result, err := Initial([]interface{}{"true", 1, 1.1, true, int32(123), false, true}) 68 | 69 | assert.Equal(t, []interface{}{"true", 1, 1.1, true, int32(123), false}, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_initial_invalid_not_slice(t *testing.T) { 74 | result, err := Initial(true) 75 | 76 | assert.Equal(t, nil, result) 77 | assert.Equal(t, constants.ErrNotSlice, err) 78 | } 79 | -------------------------------------------------------------------------------- /collection/partition.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // Partition separates the elements of the input array into two slices based on the predicate function. 10 | // It takes an array-like data structure and a predicate function that determines the Partition. 11 | // The predicate function should have the signature func(elementType) bool. 12 | // The function returns two new slices: one containing elements that satisfy the predicate, 13 | // and the other containing elements that do not satisfy the predicate. 14 | // An error is returned if any occurs. 15 | func Partition(array, predicate interface{}) (interface{}, error) { 16 | arrValue, predicateValue := reflect.ValueOf(array), reflect.ValueOf(predicate) 17 | 18 | if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array { 19 | return nil, constants.ErrNotSlice 20 | } 21 | 22 | if predicateValue.Kind() != reflect.Func { 23 | return nil, constants.ErrNotFunction 24 | } 25 | 26 | numParams := predicateValue.Type().NumIn() 27 | if numParams != 1 { 28 | return nil, constants.ErrNotSupport 29 | } 30 | 31 | kind, result := predicateValue.Type().In(0).Kind(), reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(array)), 0, 0) 32 | partitionOne, partitionTwo := reflect.MakeSlice(arrValue.Type(), 0, 0), reflect.MakeSlice(arrValue.Type(), 0, 0) 33 | if kind == reflect.Interface { 34 | return nil, constants.ErrNotSupport 35 | } 36 | 37 | for i := 0; i < arrValue.Len(); i++ { 38 | element := arrValue.Index(i) 39 | if element.Kind() != kind { 40 | return nil, constants.ErrIncompatible 41 | } 42 | 43 | res := predicateValue.Call([]reflect.Value{reflect.ValueOf(element.Interface())}) 44 | if len(res) > 0 && res[0].Kind() == reflect.Bool { 45 | if res[0].Interface().(bool) { 46 | partitionOne = reflect.Append(partitionOne, element) 47 | } else { 48 | partitionTwo = reflect.Append(partitionTwo, element) 49 | } 50 | 51 | } 52 | } 53 | 54 | if partitionOne.Len() > 0 || partitionTwo.Len() > 0 { 55 | result = reflect.Append(result, partitionOne, partitionTwo) 56 | } 57 | return result.Interface(), nil 58 | } 59 | -------------------------------------------------------------------------------- /lang/lt_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_lt_valid_int_one(t *testing.T) { 11 | result, err := Lt(1, 2) 12 | 13 | assert.Equal(t, true, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_lt_valid_int_two(t *testing.T) { 18 | result, err := Lt(2, 2) 19 | 20 | assert.Equal(t, false, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_lt_valid_int32_one(t *testing.T) { 25 | result, err := Lt(int32(1), int32(2)) 26 | 27 | assert.Equal(t, true, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_lt_valid_int32_two(t *testing.T) { 32 | result, err := Lt(int32(2), int32(2)) 33 | 34 | assert.Equal(t, false, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_lt_valid_int64_one(t *testing.T) { 39 | result, err := Lt(int64(1), int64(2)) 40 | 41 | assert.Equal(t, true, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_lt_valid_int64_two(t *testing.T) { 46 | result, err := Lt(int64(2), int64(2)) 47 | 48 | assert.Equal(t, false, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_lt_valid_float32_one(t *testing.T) { 53 | result, err := Lt(float32(1.1), float32(2.2)) 54 | 55 | assert.Equal(t, true, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_lt_valid_float32_two(t *testing.T) { 60 | result, err := Lt(float32(2.2), float32(2.2)) 61 | 62 | assert.Equal(t, false, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_lt_valid_float64_one(t *testing.T) { 67 | result, err := Lt(1.1, 2.2) 68 | 69 | assert.Equal(t, true, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_lt_valid_float64_two(t *testing.T) { 74 | result, err := Lt(2.2, 2.2) 75 | 76 | assert.Equal(t, false, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_lt_invalid_incompatible(t *testing.T) { 81 | result, err := Lt(1, true) 82 | 83 | assert.Equal(t, false, result) 84 | assert.Equal(t, constants.ErrIncompatible, err) 85 | } 86 | 87 | func Test_lt_invalid_not_found(t *testing.T) { 88 | result, err := Lt(true, true) 89 | 90 | assert.Equal(t, false, result) 91 | assert.Equal(t, constants.ErrNotSupport, err) 92 | } 93 | -------------------------------------------------------------------------------- /collection/find_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_find_valid_int(t *testing.T) { 11 | result, err := Find([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, 2, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_find_valid_string(t *testing.T) { 20 | result, err := Find([]string{"a", "b", "c"}, func(n string) bool { 21 | return n > "a" 22 | }) 23 | 24 | assert.Equal(t, "b", result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_find_valid_struct(t *testing.T) { 29 | type User struct { 30 | Name string 31 | Age int 32 | } 33 | result, err := Find([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 34 | return n.Age%2 == 0 35 | }) 36 | 37 | assert.Equal(t, &User{Name: "Kakalot", Age: 26}, result) 38 | assert.Nil(t, err) 39 | } 40 | 41 | func Test_find_invalid_string_string(t *testing.T) { 42 | result, err := Find([]string{"a", "b", "c"}, func(n int) bool { 43 | return n%2 == 0 44 | }) 45 | 46 | assert.Equal(t, nil, result) 47 | assert.Equal(t, constants.ErrIncompatible, err) 48 | } 49 | 50 | func Test_find_invalid_response_func_not_bool(t *testing.T) { 51 | result, err := Find([]int{1, 2, 3, 4}, func(n int) int { 52 | return n 53 | }) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotFound, err) 57 | } 58 | 59 | func Test_find_invalid_array_not_slice(t *testing.T) { 60 | result, err := Find(true, func(n int) bool { 61 | return n%2 == 0 62 | }) 63 | 64 | assert.Equal(t, nil, result) 65 | assert.Equal(t, constants.ErrNotSlice, err) 66 | } 67 | 68 | func Test_find_invalid_predicate_not_func(t *testing.T) { 69 | result, err := Find([]string{"a", "b", "c"}, true) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotFunction, err) 73 | } 74 | 75 | func Test_find_invalid_param_predicate_limit(t *testing.T) { 76 | result, err := Find([]string{"a", "b", "c"}, func(n string, m int) bool { 77 | return n == "a" || m%2 == 0 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | -------------------------------------------------------------------------------- /collection/every_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_every_valid_int(t *testing.T) { 11 | result, err := Every([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, false, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_every_valid_response_func_not_bool(t *testing.T) { 20 | result, err := Every([]int{1, 2, 3, 4}, func(n int) int { 21 | return n 22 | }) 23 | 24 | assert.Equal(t, false, result) 25 | assert.Equal(t, constants.ErrNotSupport, err) 26 | } 27 | 28 | func Test_every_valid_string(t *testing.T) { 29 | result, err := Every([]string{"d", "b", "c"}, func(n string) bool { 30 | return n > "a" 31 | }) 32 | 33 | assert.Equal(t, true, result) 34 | assert.Nil(t, err) 35 | } 36 | 37 | func Test_every_valid_string_string_two(t *testing.T) { 38 | result, err := Every([]string{"a", "b", "c"}, func(n int) bool { 39 | return n%2 == 0 40 | }) 41 | 42 | assert.Equal(t, false, result) 43 | assert.Equal(t, constants.ErrIncompatible, err) 44 | } 45 | 46 | func Test_every_valid_struct(t *testing.T) { 47 | type User struct { 48 | Name string 49 | Age int 50 | } 51 | result, err := Every([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 52 | return n.Age%2 == 0 53 | }) 54 | 55 | assert.Equal(t, false, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_every_invalid_array_not_slice(t *testing.T) { 60 | result, err := Every(true, func(n int) bool { 61 | return n%2 == 0 62 | }) 63 | 64 | assert.Equal(t, false, result) 65 | assert.Equal(t, constants.ErrNotSlice, err) 66 | } 67 | 68 | func Test_every_invalid_predicate_not_func(t *testing.T) { 69 | result, err := Every([]string{"a", "b", "c"}, true) 70 | 71 | assert.Equal(t, false, result) 72 | assert.Equal(t, constants.ErrNotFunction, err) 73 | } 74 | 75 | func Test_every_invalid_param_predicate_limit(t *testing.T) { 76 | result, err := Every([]string{"a", "b", "c"}, func(n string, m int) bool { 77 | return n == "a" || m%2 == 0 78 | }) 79 | 80 | assert.Equal(t, false, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | -------------------------------------------------------------------------------- /collection/reject_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_reject_valid_int(t *testing.T) { 11 | result, err := Reject([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, []int{1, 3}, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_reject_valid_response_func_not_bool(t *testing.T) { 20 | result, err := Reject([]int{1, 2, 3, 4}, func(n int) int { 21 | return n 22 | }) 23 | 24 | assert.Equal(t, []int{}, result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_reject_valid_string(t *testing.T) { 29 | result, err := Reject([]string{"a", "b", "c"}, func(n string) bool { 30 | return n > "a" 31 | }) 32 | 33 | assert.Equal(t, []string{"a"}, result) 34 | assert.Nil(t, err) 35 | } 36 | 37 | func Test_reject_valid_string_string_two(t *testing.T) { 38 | result, err := Reject([]string{"a", "b", "c"}, func(n int) bool { 39 | return n%2 == 0 40 | }) 41 | 42 | assert.Equal(t, nil, result) 43 | assert.Equal(t, constants.ErrIncompatible, err) 44 | } 45 | 46 | func Test_reject_valid_struct(t *testing.T) { 47 | type User struct { 48 | Name string 49 | Age int 50 | } 51 | result, err := Reject([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 52 | return n.Age%2 == 0 53 | }) 54 | 55 | assert.Equal(t, []*User{{Name: "Vegeta", Age: 27}}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_reject_invalid_array_not_slice(t *testing.T) { 60 | result, err := Reject(true, func(n int) bool { 61 | return n%2 == 0 62 | }) 63 | 64 | assert.Equal(t, nil, result) 65 | assert.Equal(t, constants.ErrNotSlice, err) 66 | } 67 | 68 | func Test_reject_invalid_predicate_not_func(t *testing.T) { 69 | result, err := Reject([]string{"a", "b", "c"}, true) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotFunction, err) 73 | } 74 | 75 | func Test_reject_invalid_param_predicate_limit(t *testing.T) { 76 | result, err := Reject([]string{"a", "b", "c"}, func(n string, m int) bool { 77 | return n == "a" || m%2 == 0 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | -------------------------------------------------------------------------------- /collection/filter_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_filter_valid_int(t *testing.T) { 11 | result, err := Filter([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, []int{2, 4}, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_filter_valid_response_func_not_bool(t *testing.T) { 20 | result, err := Filter([]int{1, 2, 3, 4}, func(n int) int { 21 | return n 22 | }) 23 | 24 | assert.Equal(t, []int{}, result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_filter_valid_string(t *testing.T) { 29 | result, err := Filter([]string{"a", "b", "c"}, func(n string) bool { 30 | return n > "a" 31 | }) 32 | 33 | assert.Equal(t, []string{"b", "c"}, result) 34 | assert.Nil(t, err) 35 | } 36 | 37 | func Test_filter_valid_string_string_two(t *testing.T) { 38 | result, err := Filter([]string{"a", "b", "c"}, func(n int) bool { 39 | return n%2 == 0 40 | }) 41 | 42 | assert.Equal(t, nil, result) 43 | assert.Equal(t, constants.ErrIncompatible, err) 44 | } 45 | 46 | func Test_filter_valid_struct(t *testing.T) { 47 | type User struct { 48 | Name string 49 | Age int 50 | } 51 | result, err := Filter([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 52 | return n.Age%2 == 0 53 | }) 54 | 55 | assert.Equal(t, []*User{{Name: "Kakalot", Age: 26}, {Name: "Trunk", Age: 10}}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_filter_invalid_array_not_slice(t *testing.T) { 60 | result, err := Filter(true, func(n int) bool { 61 | return n%2 == 0 62 | }) 63 | 64 | assert.Equal(t, nil, result) 65 | assert.Equal(t, constants.ErrNotSlice, err) 66 | } 67 | 68 | func Test_filter_invalid_predicate_not_func(t *testing.T) { 69 | result, err := Filter([]string{"a", "b", "c"}, true) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotFunction, err) 73 | } 74 | 75 | func Test_filter_invalid_param_predicate_limit(t *testing.T) { 76 | result, err := Filter([]string{"a", "b", "c"}, func(n string, m int) bool { 77 | return n == "a" || m%2 == 0 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | -------------------------------------------------------------------------------- /collection/findLast_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_findLastLast_valid_int(t *testing.T) { 11 | result, err := FindLast([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, 4, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_findLastLast_valid_string(t *testing.T) { 20 | result, err := FindLast([]string{"a", "b", "c"}, func(n string) bool { 21 | return n > "a" 22 | }) 23 | 24 | assert.Equal(t, "c", result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_findLastLast_valid_struct(t *testing.T) { 29 | type User struct { 30 | Name string 31 | Age int 32 | } 33 | result, err := FindLast([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 34 | return n.Age%2 == 0 35 | }) 36 | 37 | assert.Equal(t, &User{Name: "Trunk", Age: 10}, result) 38 | assert.Nil(t, err) 39 | } 40 | 41 | func Test_findLastLast_invalid_string_string(t *testing.T) { 42 | result, err := FindLast([]string{"a", "b", "c"}, func(n int) bool { 43 | return n%2 == 0 44 | }) 45 | 46 | assert.Equal(t, nil, result) 47 | assert.Equal(t, constants.ErrIncompatible, err) 48 | } 49 | 50 | func Test_findLastLast_invalid_response_func_not_bool(t *testing.T) { 51 | result, err := FindLast([]int{1, 2, 3, 4}, func(n int) int { 52 | return n 53 | }) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotFound, err) 57 | } 58 | 59 | func Test_findLastLast_invalid_array_not_slice(t *testing.T) { 60 | result, err := FindLast(true, func(n int) bool { 61 | return n%2 == 0 62 | }) 63 | 64 | assert.Equal(t, nil, result) 65 | assert.Equal(t, constants.ErrNotSlice, err) 66 | } 67 | 68 | func Test_findLastLast_invalid_predicate_not_func(t *testing.T) { 69 | result, err := FindLast([]string{"a", "b", "c"}, true) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotFunction, err) 73 | } 74 | 75 | func Test_findLastLast_invalid_param_predicate_limit(t *testing.T) { 76 | result, err := FindLast([]string{"a", "b", "c"}, func(n string, m int) bool { 77 | return n == "a" || m%2 == 0 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | -------------------------------------------------------------------------------- /number/clamp_test.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_clamp_valid_int_one(t *testing.T) { 11 | result, err := Clamp(-10, -5, 5) 12 | 13 | assert.Equal(t, -5, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_clamp_valid_int_two(t *testing.T) { 18 | result, err := Clamp(10, -5, 5) 19 | 20 | assert.Equal(t, 5, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_clamp_valid_int32_one(t *testing.T) { 25 | result, err := Clamp(int32(-10), int32(-5), int32(5)) 26 | 27 | assert.Equal(t, int32(-5), result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_clamp_valid_int32_two(t *testing.T) { 32 | result, err := Clamp(int32(10), int32(-5), int32(5)) 33 | 34 | assert.Equal(t, int32(5), result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_clamp_valid_int64_one(t *testing.T) { 39 | result, err := Clamp(int64(-10), int64(-5), int64(5)) 40 | 41 | assert.Equal(t, int64(-5), result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_clamp_valid_int64_two(t *testing.T) { 46 | result, err := Clamp(int64(10), int64(-5), int64(5)) 47 | 48 | assert.Equal(t, int64(5), result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_clamp_valid_float32_one(t *testing.T) { 53 | result, err := Clamp(float32(-10), float32(-5), float32(5)) 54 | 55 | assert.Equal(t, float32(-5), result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_clamp_valid_float32_two(t *testing.T) { 60 | result, err := Clamp(float32(10), float32(-5), float32(5)) 61 | 62 | assert.Equal(t, float32(5), result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_clamp_valid_float64_one(t *testing.T) { 67 | result, err := Clamp(float64(-10), float64(-5), float64(5)) 68 | 69 | assert.Equal(t, float64(-5), result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_clamp_valid_float64_two(t *testing.T) { 74 | result, err := Clamp(float64(10), float64(-5), float64(5)) 75 | 76 | assert.Equal(t, float64(5), result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_clamp_invalid_incompatible(t *testing.T) { 81 | result, err := Clamp(1, 2, true) 82 | 83 | assert.Equal(t, nil, result) 84 | assert.Equal(t, constants.ErrIncompatible, err) 85 | } 86 | 87 | func Test_clamp_invalid_not_support(t *testing.T) { 88 | result, err := Clamp(false, true, true) 89 | 90 | assert.Equal(t, nil, result) 91 | assert.Equal(t, constants.ErrNotSupport, err) 92 | } 93 | -------------------------------------------------------------------------------- /number/clamp.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "reflect" 5 | "sort" 6 | 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | // Clamp a given number to be within the specified range [lower, upper]. 11 | // The function accepts three arguments: number, lower bound (lower), and upper bound (upper), 12 | // all of type interface{}. It returns the clamped value within the specified range and an error if any issue occurs. 13 | func Clamp(number, lower, upper interface{}) (interface{}, error) { 14 | typeOfNumber, typeOfLower, typeOfUpper := reflect.TypeOf(number), reflect.TypeOf(lower), reflect.TypeOf(upper) 15 | 16 | if !(typeOfNumber.Kind() == typeOfLower.Kind() && typeOfNumber.Kind() == typeOfUpper.Kind()) { 17 | return nil, constants.ErrIncompatible 18 | } 19 | 20 | switch typeOfNumber.Kind() { 21 | case reflect.Int: 22 | resultNumber, _ := number.(int) 23 | resultLower, _ := lower.(int) 24 | resultUpper, _ := upper.(int) 25 | 26 | arr := []int{resultNumber, resultLower, resultUpper} 27 | sort.Slice(arr, func(i, j int) bool { 28 | return arr[i] > arr[j] 29 | }) 30 | 31 | return arr[1], nil 32 | case reflect.Int32: 33 | resultNumber, _ := number.(int32) 34 | resultLower, _ := lower.(int32) 35 | resultUpper, _ := upper.(int32) 36 | 37 | arr := []int32{resultNumber, resultLower, resultUpper} 38 | sort.Slice(arr, func(i, j int) bool { 39 | return arr[i] > arr[j] 40 | }) 41 | 42 | return arr[1], nil 43 | case reflect.Int64: 44 | resultNumber, _ := number.(int64) 45 | resultLower, _ := lower.(int64) 46 | resultUpper, _ := upper.(int64) 47 | 48 | arr := []int64{resultNumber, resultLower, resultUpper} 49 | sort.Slice(arr, func(i, j int) bool { 50 | return arr[i] > arr[j] 51 | }) 52 | 53 | return arr[1], nil 54 | case reflect.Float32: 55 | resultNumber, _ := number.(float32) 56 | resultLower, _ := lower.(float32) 57 | resultUpper, _ := upper.(float32) 58 | 59 | arr := []float32{resultNumber, resultLower, resultUpper} 60 | sort.Slice(arr, func(i, j int) bool { 61 | return arr[i] > arr[j] 62 | }) 63 | 64 | return arr[1], nil 65 | case reflect.Float64: 66 | resultNumber, _ := number.(float64) 67 | resultLower, _ := lower.(float64) 68 | resultUpper, _ := upper.(float64) 69 | 70 | arr := []float64{resultNumber, resultLower, resultUpper} 71 | sort.Slice(arr, func(i, j int) bool { 72 | return arr[i] > arr[j] 73 | }) 74 | 75 | return arr[1], nil 76 | } 77 | 78 | return nil, constants.ErrNotSupport 79 | } 80 | -------------------------------------------------------------------------------- /array/intersection_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_intersection_valid_int(t *testing.T) { 11 | result, err := Intersection([]int{1, 2, 3}, []int{1}) 12 | 13 | assert.Equal(t, []int{1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_intersection_valid_int32(t *testing.T) { 18 | result, err := Intersection([]int32{1, 2, 3}, []int32{1, 3}) 19 | 20 | assert.Equal(t, []int32{1, 3}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_intersection_valid_int64(t *testing.T) { 25 | result, err := Intersection([]int64{1, 2, 3}, []int64{1, 3}) 26 | 27 | assert.Equal(t, []int64{1, 3}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_intersection_valid_float32(t *testing.T) { 32 | result, err := Intersection([]float32{1.1, 2.2, 3.3}, []float32{1.1, 3.3}) 33 | 34 | assert.Equal(t, []float32{1.1, 3.3}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_intersection_valid_float64(t *testing.T) { 39 | result, err := Intersection([]float64{1.1, 2.2, 3.3}, []float64{1.1, 3.3}) 40 | 41 | assert.Equal(t, []float64{1.1, 3.3}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_intersection_valid_string(t *testing.T) { 46 | result, err := Intersection([]string{"1.1", "2.2", "3.3"}, []string{"1.1", "3.3"}) 47 | 48 | assert.Equal(t, []string{"1.1", "3.3"}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_intersection_valid_bool(t *testing.T) { 53 | result, err := Intersection([]bool{true, false}, []bool{true}) 54 | 55 | assert.Equal(t, []bool{true}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_intersection_valid_array_empty(t *testing.T) { 60 | result, err := Intersection([]int32{}, []int32{1, 2, 3}) 61 | 62 | assert.Equal(t, []int32{}, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_intersection_valid_other_empty(t *testing.T) { 67 | result, err := Intersection([]int32{1, 2, 3}, []int32{}) 68 | 69 | assert.Equal(t, []int32{}, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_intersection_invalid_array_not_slice(t *testing.T) { 74 | result, err := Intersection(true, []int{1, 2, 3}) 75 | 76 | assert.Equal(t, nil, result) 77 | assert.Equal(t, constants.ErrNotSlice, err) 78 | } 79 | 80 | func Test_intersection_invalid_other_not_slice(t *testing.T) { 81 | result, err := Intersection([]int{1, 2, 3}, true) 82 | 83 | assert.Equal(t, nil, result) 84 | assert.Equal(t, constants.ErrNotSlice, err) 85 | } 86 | -------------------------------------------------------------------------------- /array/slice_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_slice_valid_int(t *testing.T) { 11 | result, err := Slice([]int{1, 2, 3, 4, 5, 6}, 1, 3) 12 | 13 | assert.Equal(t, []int{2, 3}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_slice_valid_float64(t *testing.T) { 18 | result, err := Slice([]float64{1, 2, 3, 4, 5, 6}, 1, 4) 19 | 20 | assert.Equal(t, []float64{2, 3, 4}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_slice_valid_string(t *testing.T) { 25 | result, err := Slice([]string{"1", "2", "3", "4", "5", "6"}, 1, 4) 26 | 27 | assert.Equal(t, []string{"2", "3", "4"}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_slice_valid_bool(t *testing.T) { 32 | result, err := Slice([]bool{true, true, true, false, true, false, true}, 1, 4) 33 | 34 | assert.Equal(t, []bool{true, true, false}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_slice_valid_interface(t *testing.T) { 39 | result, err := Slice([]interface{}{true, "true", true, 1.1, 2, false, true}, 1, 5) 40 | 41 | assert.Equal(t, []interface{}{"true", true, 1.1, 2}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_slice_invalid_array_not_slice(t *testing.T) { 46 | result, err := Slice(true, 0, 1) 47 | 48 | assert.Equal(t, nil, result) 49 | assert.Equal(t, constants.ErrNotSlice, err) 50 | } 51 | 52 | func Test_slice_invalid_start_less_than_zero(t *testing.T) { 53 | result, err := Slice([]int{1, 2, 3}, -1, 1) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrParamLessThanZero, err) 57 | } 58 | 59 | func Test_slice_invalid_end_less_than_zero(t *testing.T) { 60 | result, err := Slice([]int32{1, 2, 3}, 1, -1) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrParamLessThanZero, err) 64 | } 65 | 66 | func Test_slice_invalid_end_less_than_start(t *testing.T) { 67 | result, err := Slice([]int32{1, 2, 3}, 1, 0) 68 | 69 | assert.Equal(t, nil, result) 70 | assert.Equal(t, constants.ErrOutOfRange, err) 71 | } 72 | 73 | func Test_slice_invalid_array_length_less_than_start(t *testing.T) { 74 | result, err := Slice([]float32{1, 2, 3}, 6, 8) 75 | 76 | assert.Equal(t, nil, result) 77 | assert.Equal(t, constants.ErrOutOfRange, err) 78 | } 79 | 80 | func Test_slice_invalid_array_length_less_than_end(t *testing.T) { 81 | result, err := Slice([]float64{1, 2, 3}, 1, 8) 82 | 83 | assert.Equal(t, nil, result) 84 | assert.Equal(t, constants.ErrOutOfRange, err) 85 | } 86 | -------------------------------------------------------------------------------- /array/chunk_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_chunk_valid_int(t *testing.T) { 11 | result, err := Chunk([]int{1, 2, 3}, 1) 12 | 13 | assert.Equal(t, [][]int{{1}, {2}, {3}}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_chunk_valid_int32(t *testing.T) { 18 | result, err := Chunk([]int32{1, 2, 3}, 2) 19 | 20 | assert.Equal(t, [][]int32{{1, 2}, {3}}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_chunk_valid_int64(t *testing.T) { 25 | result, err := Chunk([]int64{1, 2, 3}, 2) 26 | 27 | assert.Equal(t, [][]int64{{1, 2}, {3}}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_chunk_valid_float32(t *testing.T) { 32 | result, err := Chunk([]float32{1.1, 2.2, 3.3}, 2) 33 | 34 | assert.Equal(t, [][]float32{{1.1, 2.2}, {3.3}}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_chunk_valid_float64(t *testing.T) { 39 | result, err := Chunk([]float64{1.1, 2.2, 3.3}, 2) 40 | 41 | assert.Equal(t, [][]float64{{1.1, 2.2}, {3.3}}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_chunk_valid_string(t *testing.T) { 46 | result, err := Chunk([]string{"a", "b", "c", "d"}, 3) 47 | 48 | assert.Equal(t, [][]string{{"a", "b", "c"}, {"d"}}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_chunk_valid_size_great_than_array_length(t *testing.T) { 53 | result, err := Chunk([]int{1, 2, 3}, 9) 54 | 55 | assert.Equal(t, [][]int{{1, 2, 3}}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_chunk_invalid_array_empty(t *testing.T) { 60 | result, err := Chunk([]int{}, 1) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrEmptyList, err) 64 | } 65 | 66 | func Test_chunk_invalid_size_zero(t *testing.T) { 67 | result, err := Chunk([]int{1, 2, 3}, 0) 68 | 69 | assert.Equal(t, nil, result) 70 | assert.Equal(t, constants.ErrEmptyList, err) 71 | } 72 | 73 | func Test_chunk_invalid_array_not_slice(t *testing.T) { 74 | result, err := Chunk(true, 1) 75 | 76 | assert.Equal(t, nil, result) 77 | assert.Equal(t, constants.ErrNotSlice, err) 78 | } 79 | 80 | func Test_chunk_invalid_array_interface(t *testing.T) { 81 | result, err := Chunk([]interface{}{1, "2", true}, 1) 82 | 83 | assert.Equal(t, [][]interface{}{{1}, {"2"}, {true}}, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_chunk_invalid_size_less_than_zero(t *testing.T) { 88 | result, err := Chunk([]int{1, 2, 3}, -1) 89 | 90 | assert.Equal(t, nil, result) 91 | assert.Equal(t, constants.ErrParamLessThanZero, err) 92 | } 93 | -------------------------------------------------------------------------------- /array/remove_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_remove_valid_int(t *testing.T) { 11 | result, old, err := Remove([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, []int{2, 4}, result) 16 | assert.Equal(t, []int{1, 3}, old) 17 | assert.Nil(t, err) 18 | } 19 | 20 | func Test_remove_valid_response_func_not_bool(t *testing.T) { 21 | result, old, err := Remove([]int{1, 2, 3, 4}, func(n int) int { 22 | return n 23 | }) 24 | 25 | assert.Equal(t, []int{}, result) 26 | assert.Equal(t, []int{}, old) 27 | assert.Nil(t, err) 28 | } 29 | 30 | func Test_remove_valid_string(t *testing.T) { 31 | result, old, err := Remove([]string{"a", "b", "c"}, func(n string) bool { 32 | return n > "a" 33 | }) 34 | 35 | assert.Equal(t, []string{"b", "c"}, result) 36 | assert.Equal(t, []string{"a"}, old) 37 | assert.Nil(t, err) 38 | } 39 | 40 | func Test_remove_valid_string_string_two(t *testing.T) { 41 | result, old, err := Remove([]string{"a", "b", "c"}, func(n int) bool { 42 | return n%2 == 0 43 | }) 44 | 45 | assert.Equal(t, nil, result) 46 | assert.Equal(t, nil, old) 47 | assert.Equal(t, constants.ErrIncompatible, err) 48 | } 49 | 50 | func Test_remove_valid_struct(t *testing.T) { 51 | type User struct { 52 | Name string 53 | Age int 54 | } 55 | result, old, err := Remove([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 56 | return n.Age%2 == 0 57 | }) 58 | 59 | assert.Equal(t, []*User{{Name: "Kakalot", Age: 26}, {Name: "Trunk", Age: 10}}, result) 60 | assert.Equal(t, []*User{{Name: "Vegeta", Age: 27}}, old) 61 | assert.Nil(t, err) 62 | } 63 | 64 | func Test_remove_invalid_array_not_slice(t *testing.T) { 65 | result, old, err := Remove(true, func(n int) bool { 66 | return n%2 == 0 67 | }) 68 | 69 | assert.Equal(t, nil, result) 70 | assert.Equal(t, nil, old) 71 | assert.Equal(t, constants.ErrNotSlice, err) 72 | } 73 | 74 | func Test_remove_invalid_predicate_not_func(t *testing.T) { 75 | result, old, err := Remove([]string{"a", "b", "c"}, true) 76 | 77 | assert.Equal(t, nil, result) 78 | assert.Equal(t, nil, old) 79 | assert.Equal(t, constants.ErrNotFunction, err) 80 | } 81 | 82 | func Test_remove_invalid_param_predicate_limit(t *testing.T) { 83 | result, old, err := Remove([]string{"a", "b", "c"}, func(n string, m int) bool { 84 | return n == "a" || m%2 == 0 85 | }) 86 | 87 | assert.Equal(t, nil, result) 88 | assert.Equal(t, nil, old) 89 | assert.Equal(t, constants.ErrNotSupport, err) 90 | } 91 | -------------------------------------------------------------------------------- /collection/partition_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_partition_valid_int(t *testing.T) { 11 | result, err := Partition([]int{1, 2, 3, 4}, func(n int) bool { 12 | return n%2 == 0 13 | }) 14 | 15 | assert.Equal(t, [][]int{{2, 4}, {1, 3}}, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_partition_valid_response_func_invalid(t *testing.T) { 20 | result, err := Partition([]int{1, 2, 3, 4}, func(n int) int { 21 | return n 22 | }) 23 | 24 | assert.Equal(t, [][]int{}, result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_partition_valid_string(t *testing.T) { 29 | result, err := Partition([]string{"a", "b", "c"}, func(n string) bool { 30 | return n > "a" 31 | }) 32 | 33 | assert.Equal(t, [][]string{{"b", "c"}, {"a"}}, result) 34 | assert.Nil(t, err) 35 | } 36 | 37 | func Test_partition_valid_struct(t *testing.T) { 38 | type User struct { 39 | Name string 40 | Age int 41 | } 42 | result, err := Partition([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) bool { 43 | return n.Age%2 == 0 44 | }) 45 | 46 | assert.Equal(t, [][]*User{{{Name: "Kakalot", Age: 26}, {Name: "Trunk", Age: 10}}, {{Name: "Vegeta", Age: 27}}}, result) 47 | assert.Nil(t, err) 48 | } 49 | 50 | func Test_partition_invalid_array_not_slice(t *testing.T) { 51 | result, err := Partition(true, func(n int) int { 52 | return n * 2 53 | }) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotSlice, err) 57 | } 58 | 59 | func Test_partition_invalid_predicate_not_func(t *testing.T) { 60 | result, err := Partition([]string{"a", "b", "c"}, true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotFunction, err) 64 | } 65 | 66 | func Test_partition_invalid_param_predicate_limit(t *testing.T) { 67 | result, err := Partition([]string{"a", "b", "c"}, func(n string, m int) bool { 68 | return n == "a" || m%2 == 0 69 | }) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotSupport, err) 73 | } 74 | 75 | func Test_partition_invalid_interface(t *testing.T) { 76 | result, err := Partition([]interface{}{"a", "b", "c"}, func(n interface{}) interface{} { 77 | return n 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | 84 | func Test_partition_invalid_incompatible(t *testing.T) { 85 | result, err := Partition([]string{"a", "b", "c"}, func(n int) int { 86 | return n * n 87 | }) 88 | 89 | assert.Equal(t, nil, result) 90 | assert.Equal(t, constants.ErrIncompatible, err) 91 | } 92 | -------------------------------------------------------------------------------- /collection/map_test.go: -------------------------------------------------------------------------------- 1 | package collection 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_goDashMap_valid_int(t *testing.T) { 11 | result, err := GoDashMap([]int{1, 2, 3, 4}, func(n int) int { 12 | return n * n 13 | }) 14 | 15 | assert.Equal(t, []int{1, 4, 9, 16}, result) 16 | assert.Nil(t, err) 17 | } 18 | 19 | func Test_goDashMap_valid_response_func_invalid(t *testing.T) { 20 | result, err := GoDashMap([]int{1, 2, 3, 4}, func(n int) bool { 21 | return n%2 == 0 22 | }) 23 | 24 | assert.Equal(t, []int{}, result) 25 | assert.Nil(t, err) 26 | } 27 | 28 | func Test_goDashMap_valid_string(t *testing.T) { 29 | result, err := GoDashMap([]string{"a", "b", "c"}, func(n string) string { 30 | return n + n 31 | }) 32 | 33 | assert.Equal(t, []string{"aa", "bb", "cc"}, result) 34 | assert.Nil(t, err) 35 | } 36 | 37 | func Test_goDashMap_valid_struct(t *testing.T) { 38 | type User struct { 39 | Name string 40 | Age int 41 | } 42 | result, err := GoDashMap([]*User{{Name: "Kakalot", Age: 26}, {Name: "Vegeta", Age: 27}, {Name: "Trunk", Age: 10}}, func(n *User) *User { 43 | return &User{Name: n.Name, Age: n.Age * 2} 44 | }) 45 | 46 | assert.Equal(t, []*User{{Name: "Kakalot", Age: 52}, {Name: "Vegeta", Age: 54}, {Name: "Trunk", Age: 20}}, result) 47 | assert.Nil(t, err) 48 | } 49 | 50 | func Test_goDashMap_invalid_array_not_slice(t *testing.T) { 51 | result, err := GoDashMap(true, func(n int) int { 52 | return n * 2 53 | }) 54 | 55 | assert.Equal(t, nil, result) 56 | assert.Equal(t, constants.ErrNotSlice, err) 57 | } 58 | 59 | func Test_goDashMap_invalid_predicate_not_func(t *testing.T) { 60 | result, err := GoDashMap([]string{"a", "b", "c"}, true) 61 | 62 | assert.Equal(t, nil, result) 63 | assert.Equal(t, constants.ErrNotFunction, err) 64 | } 65 | 66 | func Test_goDashMap_invalid_param_predicate_limit(t *testing.T) { 67 | result, err := GoDashMap([]string{"a", "b", "c"}, func(n string, m int) bool { 68 | return n == "a" || m%2 == 0 69 | }) 70 | 71 | assert.Equal(t, nil, result) 72 | assert.Equal(t, constants.ErrNotSupport, err) 73 | } 74 | 75 | func Test_goDashMap_invalid_interface(t *testing.T) { 76 | result, err := GoDashMap([]interface{}{"a", "b", "c"}, func(n interface{}) interface{} { 77 | return n 78 | }) 79 | 80 | assert.Equal(t, nil, result) 81 | assert.Equal(t, constants.ErrNotSupport, err) 82 | } 83 | 84 | func Test_goDashMap_invalid_incompatible(t *testing.T) { 85 | result, err := GoDashMap([]string{"a", "b", "c"}, func(n int) int { 86 | return n * n 87 | }) 88 | 89 | assert.Equal(t, nil, result) 90 | assert.Equal(t, constants.ErrIncompatible, err) 91 | } 92 | -------------------------------------------------------------------------------- /go_math/sum_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_sum_valid_int(t *testing.T) { 11 | result, err := Sum([]int{1, 2, 3}) 12 | 13 | assert.Equal(t, float64(6), result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_sum_valid_int8(t *testing.T) { 18 | result, err := Sum([]int8{1, 2, 3}) 19 | 20 | assert.Equal(t, float64(6), result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_sum_valid_int16(t *testing.T) { 25 | result, err := Sum([]int16{1, 2, 3}) 26 | 27 | assert.Equal(t, float64(6), result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_sum_valid_int32(t *testing.T) { 32 | result, err := Sum([]int32{1, 2, 3}) 33 | 34 | assert.Equal(t, float64(6), result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_sum_valid_int64(t *testing.T) { 39 | result, err := Sum([]int64{1, 2, 3}) 40 | 41 | assert.Equal(t, float64(6), result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_sum_valid_uint(t *testing.T) { 46 | result, err := Sum([]uint{1, 2, 3}) 47 | 48 | assert.Equal(t, float64(6), result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_sum_valid_uint8(t *testing.T) { 53 | result, err := Sum([]uint8{1, 2, 3}) 54 | 55 | assert.Equal(t, float64(6), result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_sum_valid_uint16(t *testing.T) { 60 | result, err := Sum([]uint16{1, 2, 3}) 61 | 62 | assert.Equal(t, float64(6), result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_sum_valid_uint32(t *testing.T) { 67 | result, err := Sum([]uint32{1, 2, 3}) 68 | 69 | assert.Equal(t, float64(6), result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_sum_valid_uint64(t *testing.T) { 74 | result, err := Sum([]uint64{1, 2, 3}) 75 | 76 | assert.Equal(t, float64(6), result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_sum_valid_float32(t *testing.T) { 81 | result, err := Sum([]float32{1, 2, 3}) 82 | 83 | assert.Equal(t, float64(6), result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_sum_valid_float64(t *testing.T) { 88 | result, err := Sum([]float64{1, 2, 3}) 89 | 90 | assert.Equal(t, float64(6), result) 91 | assert.Nil(t, err) 92 | } 93 | 94 | func Test_sum_invalid_arr_not_slice(t *testing.T) { 95 | result, err := Sum("123") 96 | 97 | assert.Equal(t, float64(0), result) 98 | assert.Equal(t, constants.ErrNotSlice, err) 99 | } 100 | 101 | func Test_sum_invalid_type_not_support(t *testing.T) { 102 | result, err := Sum([]string{"1", "2"}) 103 | 104 | assert.Equal(t, float64(0), result) 105 | assert.Equal(t, constants.ErrNotSupport, err) 106 | } 107 | -------------------------------------------------------------------------------- /array/drop_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_drop_valid_int_one(t *testing.T) { 11 | result, err := Drop([]int{1, 2, 3}, 2) 12 | 13 | assert.Equal(t, []int{3}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_drop_valid_int_two(t *testing.T) { 18 | result, err := Drop([]int{1, 2, 3}, 5) 19 | 20 | assert.Equal(t, []int{}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_drop_valid_int32(t *testing.T) { 25 | result, err := Drop([]int32{1, 2, 3}, 2) 26 | 27 | assert.Equal(t, []int32{3}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_drop_valid_int64(t *testing.T) { 32 | result, err := Drop([]int64{1, 2, 3}, 2) 33 | 34 | assert.Equal(t, []int64{3}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_drop_valid_float32(t *testing.T) { 39 | result, err := Drop([]float32{1, 2, 3}, 2) 40 | 41 | assert.Equal(t, []float32{3}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_drop_valid_float64(t *testing.T) { 46 | result, err := Drop([]float64{1, 2, 3}, 2) 47 | 48 | assert.Equal(t, []float64{3}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_drop_valid_string(t *testing.T) { 53 | result, err := Drop([]string{"1", "2", "3"}, 2) 54 | 55 | assert.Equal(t, []string{"3"}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_drop_valid_zero_drop(t *testing.T) { 60 | result, err := Drop([]int{1, 2, 3}, 0) 61 | 62 | assert.Equal(t, []int{1, 2, 3}, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_drop_valid_empty_drop(t *testing.T) { 67 | result, err := Drop([]int{1, 2, 3}) 68 | 69 | assert.Equal(t, []int{1, 2, 3}, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_drop_valid_empty_array(t *testing.T) { 74 | result, err := Drop([]int{}, 1) 75 | 76 | assert.Equal(t, []int{}, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_drop_valid_array_interface(t *testing.T) { 81 | result, err := Drop([]interface{}{1, "2", true}, 2) 82 | 83 | assert.Equal(t, []interface{}{true}, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_drop_invalid_array_not_slice(t *testing.T) { 88 | result, err := Drop(true, 2) 89 | 90 | assert.Equal(t, nil, result) 91 | assert.Equal(t, constants.ErrNotSlice, err) 92 | } 93 | 94 | func Test_drop_invalid_array_not_support(t *testing.T) { 95 | result, err := Drop([]bool{true, false, true}, 2) 96 | 97 | assert.Equal(t, []bool{true}, result) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_drop_invalid_size_drop_less_than_zero(t *testing.T) { 102 | result, err := Drop([]int{1, 2, 3}, -2) 103 | 104 | assert.Equal(t, nil, result) 105 | assert.Equal(t, constants.ErrParamLessThanZero, err) 106 | } 107 | -------------------------------------------------------------------------------- /number/inRange.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // InRange checks if a given number is within the specified range [start, end). 10 | // The function accepts three arguments: number, start, and end, all of type interface{}. 11 | // It returns a boolean indicating whether the number is within the range and an error if any issue occurs. 12 | func InRange(number, start interface{}, end ...interface{}) (bool, error) { 13 | typeOfNumber, typeOfStart := reflect.TypeOf(number), reflect.TypeOf(start) 14 | 15 | var endRange interface{} 16 | if end != nil { 17 | endRange = end[0] 18 | if !(typeOfNumber.Kind() == typeOfStart.Kind() && typeOfNumber.Kind() == reflect.TypeOf(endRange).Kind()) { 19 | return false, constants.ErrIncompatible 20 | } 21 | } else { 22 | endRange, start = start, endRange 23 | if !(typeOfNumber.Kind() == reflect.TypeOf(endRange).Kind()) { 24 | return false, constants.ErrIncompatible 25 | } 26 | } 27 | 28 | switch typeOfNumber.Kind() { 29 | case reflect.Int: 30 | if start == nil { 31 | start = 0 32 | } 33 | 34 | if resultStart, ok := start.(int); ok && number.(int) <= resultStart { 35 | return false, nil 36 | } 37 | 38 | if resultEnd, ok := endRange.(int); ok && number.(int) >= resultEnd { 39 | return false, nil 40 | } 41 | 42 | return true, nil 43 | case reflect.Int32: 44 | if start == nil { 45 | start = int32(0) 46 | } 47 | 48 | if resultStart, ok := start.(int32); ok && number.(int32) <= resultStart { 49 | return false, nil 50 | } 51 | 52 | if resultEnd, ok := endRange.(int32); ok && number.(int32) >= resultEnd { 53 | return false, nil 54 | } 55 | 56 | return true, nil 57 | case reflect.Int64: 58 | if start == nil { 59 | start = int64(0) 60 | } 61 | 62 | if resultStart, ok := start.(int64); ok && number.(int64) <= resultStart { 63 | return false, nil 64 | } 65 | 66 | if resultEnd, ok := endRange.(int64); ok && number.(int64) >= resultEnd { 67 | return false, nil 68 | } 69 | 70 | return true, nil 71 | case reflect.Float32: 72 | if start == nil { 73 | start = float32(0) 74 | } 75 | 76 | if resultStart, ok := start.(float32); ok && number.(float32) <= resultStart { 77 | return false, nil 78 | } 79 | 80 | if resultEnd, ok := endRange.(float32); ok && number.(float32) >= resultEnd { 81 | return false, nil 82 | } 83 | 84 | return true, nil 85 | case reflect.Float64: 86 | if start == nil { 87 | start = float64(0) 88 | } 89 | 90 | if resultStart, ok := start.(float64); ok && number.(float64) <= resultStart { 91 | return false, nil 92 | } 93 | 94 | if resultEnd, ok := endRange.(float64); ok && number.(float64) >= resultEnd { 95 | return false, nil 96 | } 97 | 98 | return true, nil 99 | } 100 | 101 | return false, constants.ErrNotSupport 102 | } 103 | -------------------------------------------------------------------------------- /go_math/max.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // MaxInt finds and returns the maximum value from the given slice of integers 'i'. 10 | func MaxInt(i []int) int { 11 | if len(i) == 0 { 12 | return 0 13 | } 14 | 15 | max := i[0] 16 | for _, value := range i { 17 | if value > max { 18 | max = value 19 | } 20 | } 21 | 22 | return max 23 | } 24 | 25 | // MaxInt32 finds and returns the maximum value from the given slice of int32 values 'i'. 26 | func MaxInt32(i []int32) int32 { 27 | if len(i) == 0 { 28 | return 0 29 | } 30 | 31 | max := i[0] 32 | 33 | for _, value := range i { 34 | if value > max { 35 | max = value 36 | } 37 | } 38 | 39 | return max 40 | } 41 | 42 | // MaxInt64 finds and returns the maximum value from the given slice of int64 values 'i'. 43 | func MaxInt64(i []int64) int64 { 44 | if len(i) == 0 { 45 | return 0 46 | } 47 | 48 | max := i[0] 49 | 50 | for _, value := range i { 51 | if value > max { 52 | max = value 53 | } 54 | } 55 | 56 | return max 57 | } 58 | 59 | // MaxFloat32 finds and returns the maximum value from the given slice of float32 values 'i'. 60 | func MaxFloat32(i []float32) float32 { 61 | if len(i) == 0 { 62 | return 0 63 | } 64 | 65 | max := i[0] 66 | 67 | for _, value := range i { 68 | if value > max { 69 | max = value 70 | } 71 | } 72 | 73 | return max 74 | } 75 | 76 | // MaxFloat64 finds and returns the maximum value from the given slice of float64 values 'i'. 77 | func MaxFloat64(i []float64) float64 { 78 | if len(i) == 0 { 79 | return 0 80 | } 81 | 82 | max := i[0] 83 | 84 | for _, value := range i { 85 | if value > max { 86 | max = value 87 | } 88 | } 89 | 90 | return max 91 | } 92 | 93 | // MaxField finds and returns the maximum value of a specified field in a list of structs. 94 | // The 'list' parameter is the slice of structs, and 'fieldName' specifies the field to compare. 95 | // The function returns the maximum field value as an interface{} and an error if any issue occurs. 96 | func MaxField(list interface{}, fieldName string) (interface{}, error) { 97 | value := reflect.ValueOf(list) 98 | if value.Kind() != reflect.Slice { 99 | return nil, constants.ErrNotSlice 100 | } 101 | 102 | if value.Len() == 0 { 103 | return nil, constants.ErrEmptyList 104 | } 105 | 106 | fieldFirst := value.Index(0).FieldByName(fieldName) 107 | if !fieldFirst.IsValid() { 108 | return nil, constants.ErrFieldNotFound 109 | } 110 | 111 | maxValue := fieldFirst.Interface() 112 | 113 | for i := 1; i < value.Len(); i++ { 114 | elem := value.Index(i) 115 | field := elem.FieldByName(fieldName) 116 | 117 | if field.Interface() == nil { 118 | continue 119 | } 120 | 121 | if reflect.ValueOf(maxValue).Type() != field.Type() { 122 | return nil, constants.ErrIncompatible 123 | } 124 | 125 | if field.Interface().(int) > maxValue.(int) { 126 | maxValue = field.Interface() 127 | } 128 | } 129 | 130 | return maxValue, nil 131 | } 132 | -------------------------------------------------------------------------------- /go_math/min.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "reflect" 5 | 6 | "github.com/warriors-vn/go-dash/constants" 7 | ) 8 | 9 | // MinInt finds and returns the minimum value from the given slice of integers 'i'. 10 | func MinInt(i []int) int { 11 | if len(i) == 0 { 12 | return 0 13 | } 14 | 15 | min := i[0] 16 | for _, value := range i { 17 | if value < min { 18 | min = value 19 | } 20 | } 21 | 22 | return min 23 | } 24 | 25 | // MinInt32 finds and returns the minimum value from the given slice of int32 values 'i'. 26 | func MinInt32(i []int32) int32 { 27 | if len(i) == 0 { 28 | return 0 29 | } 30 | 31 | min := i[0] 32 | 33 | for _, value := range i { 34 | if value < min { 35 | min = value 36 | } 37 | } 38 | 39 | return min 40 | } 41 | 42 | // MinInt64 finds and returns the minimum value from the given slice of int64 values 'i'. 43 | func MinInt64(i []int64) int64 { 44 | if len(i) == 0 { 45 | return 0 46 | } 47 | 48 | min := i[0] 49 | 50 | for _, value := range i { 51 | if value < min { 52 | min = value 53 | } 54 | } 55 | 56 | return min 57 | } 58 | 59 | // MinFloat32 finds and returns the minimum value from the given slice of float32 values 'i'. 60 | func MinFloat32(i []float32) float32 { 61 | if len(i) == 0 { 62 | return 0 63 | } 64 | 65 | min := i[0] 66 | 67 | for _, value := range i { 68 | if value < min { 69 | min = value 70 | } 71 | } 72 | 73 | return min 74 | } 75 | 76 | // MinFloat64 finds and returns the minimum value from the given slice of float64 values 'i'. 77 | func MinFloat64(i []float64) float64 { 78 | if len(i) == 0 { 79 | return 0 80 | } 81 | 82 | min := i[0] 83 | 84 | for _, value := range i { 85 | if value < min { 86 | min = value 87 | } 88 | } 89 | 90 | return min 91 | } 92 | 93 | // MinField finds and returns the minimum value of a specified field in a list of structs. 94 | // The 'list' parameter is the slice of structs, and 'fieldName' specifies the field to compare. 95 | // The function returns the minimum field value as an interface{} and an error if any issue occurs. 96 | func MinField(list interface{}, fieldName string) (interface{}, error) { 97 | value := reflect.ValueOf(list) 98 | if value.Kind() != reflect.Slice { 99 | return nil, constants.ErrNotSlice 100 | } 101 | 102 | if value.Len() == 0 { 103 | return nil, constants.ErrEmptyList 104 | } 105 | 106 | fieldFirst := value.Index(0).FieldByName(fieldName) 107 | if !fieldFirst.IsValid() { 108 | return nil, constants.ErrFieldNotFound 109 | } 110 | 111 | minValue := fieldFirst.Interface() 112 | 113 | for i := 1; i < value.Len(); i++ { 114 | elem := value.Index(i) 115 | field := elem.FieldByName(fieldName) 116 | 117 | if field.Interface() == nil { 118 | continue 119 | } 120 | 121 | if reflect.ValueOf(minValue).Type() != field.Type() { 122 | return nil, constants.ErrIncompatible 123 | } 124 | 125 | if field.Interface().(int) < minValue.(int) { 126 | minValue = field.Interface() 127 | } 128 | } 129 | 130 | return minValue, nil 131 | } 132 | -------------------------------------------------------------------------------- /lang/lte_test.go: -------------------------------------------------------------------------------- 1 | package lang 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_lte_valid_int_one(t *testing.T) { 11 | result, err := Lte(1, 2) 12 | 13 | assert.Equal(t, true, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_lte_valid_int_two(t *testing.T) { 18 | result, err := Lte(2, 2) 19 | 20 | assert.Equal(t, true, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_lte_valid_int_three(t *testing.T) { 25 | result, err := Lte(3, 2) 26 | 27 | assert.Equal(t, false, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_lte_valid_int32_one(t *testing.T) { 32 | result, err := Lte(int32(1), int32(2)) 33 | 34 | assert.Equal(t, true, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_lte_valid_int32_two(t *testing.T) { 39 | result, err := Lte(int32(2), int32(2)) 40 | 41 | assert.Equal(t, true, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_lte_valid_int32_three(t *testing.T) { 46 | result, err := Lte(int32(3), int32(2)) 47 | 48 | assert.Equal(t, false, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_lte_valid_int64_one(t *testing.T) { 53 | result, err := Lte(int64(1), int64(2)) 54 | 55 | assert.Equal(t, true, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_lte_valid_int64_two(t *testing.T) { 60 | result, err := Lte(int64(2), int64(2)) 61 | 62 | assert.Equal(t, true, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_lte_valid_int64_three(t *testing.T) { 67 | result, err := Lte(int64(3), int64(2)) 68 | 69 | assert.Equal(t, false, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_lte_valid_float32_one(t *testing.T) { 74 | result, err := Lte(float32(1.1), float32(2.2)) 75 | 76 | assert.Equal(t, true, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_lte_valid_float32_two(t *testing.T) { 81 | result, err := Lte(float32(2.2), float32(2.2)) 82 | 83 | assert.Equal(t, true, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_lte_valid_float32_three(t *testing.T) { 88 | result, err := Lte(float32(3.2), float32(2.2)) 89 | 90 | assert.Equal(t, false, result) 91 | assert.Nil(t, err) 92 | } 93 | 94 | func Test_lte_valid_float64_one(t *testing.T) { 95 | result, err := Lte(1.1, 2.2) 96 | 97 | assert.Equal(t, true, result) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_lte_valid_float64_two(t *testing.T) { 102 | result, err := Lte(2.2, 2.2) 103 | 104 | assert.Equal(t, true, result) 105 | assert.Nil(t, err) 106 | } 107 | 108 | func Test_lte_valid_float64_three(t *testing.T) { 109 | result, err := Lte(3.2, 2.2) 110 | 111 | assert.Equal(t, false, result) 112 | assert.Nil(t, err) 113 | } 114 | 115 | func Test_lte_invalid_incompatible(t *testing.T) { 116 | result, err := Lte(1, true) 117 | 118 | assert.Equal(t, false, result) 119 | assert.Equal(t, constants.ErrIncompatible, err) 120 | } 121 | 122 | func Test_lte_invalid_not_found(t *testing.T) { 123 | result, err := Lte(true, true) 124 | 125 | assert.Equal(t, false, result) 126 | assert.Equal(t, constants.ErrNotSupport, err) 127 | } 128 | -------------------------------------------------------------------------------- /array/difference_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_difference_valid_int_one(t *testing.T) { 11 | result, err := Difference([]int{2, 1}, []int{2, 3}) 12 | 13 | assert.Equal(t, []int{1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_difference_valid_int_two(t *testing.T) { 18 | result, err := Difference([]int{2, 1}) 19 | 20 | assert.Equal(t, []int{2, 1}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_difference_valid_int32(t *testing.T) { 25 | result, err := Difference([]int32{2, 1, 2, 2}, []int32{2, 3}) 26 | 27 | assert.Equal(t, []int32{1}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_difference_valid_int64(t *testing.T) { 32 | result, err := Difference([]int64{2, 1, 2, 2, 5}, []int64{2, 3}) 33 | 34 | assert.Equal(t, []int64{1, 5}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_difference_valid_float32(t *testing.T) { 39 | result, err := Difference([]float32{2, 1, 2, 2, 5}, []float32{2, 3}) 40 | 41 | assert.Equal(t, []float32{1, 5}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_difference_valid_bool(t *testing.T) { 46 | result, err := Difference([]bool{true, false, false, true, true}, []bool{false}) 47 | 48 | assert.Equal(t, []bool{true, true, true}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_difference_valid_float64(t *testing.T) { 53 | result, err := Difference([]float64{2, 1, 2, 2, 5}, []float64{2, 3}) 54 | 55 | assert.Equal(t, []float64{1, 5}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_difference_valid_string(t *testing.T) { 60 | result, err := Difference([]string{"2", "1", "2", "2", "5"}, []string{"2", "3"}) 61 | 62 | assert.Equal(t, []string{"1", "5"}, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_difference_valid_int_exclude_empty(t *testing.T) { 67 | result, err := Difference([]int{2, 1}, []int{}) 68 | 69 | assert.Equal(t, []int{2, 1}, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_difference_valid_interface(t *testing.T) { 74 | result, err := Difference([]interface{}{1, true, "3"}, []interface{}{2, 1, 3}) 75 | 76 | assert.Equal(t, []interface{}{true, "3"}, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_difference_valid_array_exclude_incompatible(t *testing.T) { 81 | result, err := Difference([]int{2, 3}, []bool{true, true}) 82 | 83 | assert.Equal(t, []int{2, 3}, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_difference_valid_not_support(t *testing.T) { 88 | result, err := Difference([]uint{2, 3}, []uint{2, 3}) 89 | 90 | assert.Equal(t, []uint{}, result) 91 | assert.Nil(t, err) 92 | } 93 | 94 | func Test_difference_invalid_array_not_slice(t *testing.T) { 95 | result, err := Difference(true, []int{2, 3}) 96 | 97 | assert.Equal(t, nil, result) 98 | assert.Equal(t, constants.ErrNotSlice, err) 99 | } 100 | 101 | func Test_difference_invalid_exclude_not_slice(t *testing.T) { 102 | result, err := Difference([]int{2, 3}, true) 103 | 104 | assert.Equal(t, nil, result) 105 | assert.Equal(t, constants.ErrNotSlice, err) 106 | } 107 | -------------------------------------------------------------------------------- /number/inRange_test.go: -------------------------------------------------------------------------------- 1 | package number 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_inRange_invalid_number_and_start(t *testing.T) { 11 | result, err := InRange(1, true, 9) 12 | 13 | assert.Equal(t, false, result) 14 | assert.Equal(t, constants.ErrIncompatible, err) 15 | } 16 | 17 | func Test_inRange_invalid_number_and_start_two(t *testing.T) { 18 | result, err := InRange(1, true) 19 | 20 | assert.Equal(t, false, result) 21 | assert.Equal(t, constants.ErrIncompatible, err) 22 | } 23 | 24 | func Test_inRange_invalid_number_and_end(t *testing.T) { 25 | result, err := InRange(1, 9, true) 26 | 27 | assert.Equal(t, false, result) 28 | assert.Equal(t, constants.ErrIncompatible, err) 29 | } 30 | 31 | func Test_inRange_invalid_not_support(t *testing.T) { 32 | result, err := InRange(true, false, true) 33 | 34 | assert.Equal(t, false, result) 35 | assert.Equal(t, constants.ErrNotSupport, err) 36 | } 37 | 38 | func Test_inRange_valid_int_one(t *testing.T) { 39 | result, err := InRange(4, 8) 40 | 41 | assert.Equal(t, true, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_inRange_valid_int_two(t *testing.T) { 46 | result, err := InRange(4, 2) 47 | 48 | assert.Equal(t, false, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_inRange_valid_int_three(t *testing.T) { 53 | result, err := InRange(-2, 2) 54 | 55 | assert.Equal(t, false, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_inRange_valid_int32_one(t *testing.T) { 60 | result, err := InRange(int32(-1), int32(-2), int32(9)) 61 | 62 | assert.Equal(t, true, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_inRange_valid_int32_two(t *testing.T) { 67 | result, err := InRange(int32(-1), int32(2)) 68 | 69 | assert.Equal(t, false, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_inRange_valid_int32_three(t *testing.T) { 74 | result, err := InRange(int32(-1), int32(-3), int32(-2)) 75 | 76 | assert.Equal(t, false, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_inRange_valid_int64_one(t *testing.T) { 81 | result, err := InRange(int64(-1), int64(-2), int64(9)) 82 | 83 | assert.Equal(t, true, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_inRange_valid_int64_two(t *testing.T) { 88 | result, err := InRange(int64(-1), int64(2)) 89 | 90 | assert.Equal(t, false, result) 91 | assert.Nil(t, err) 92 | } 93 | 94 | func Test_inRange_valid_int64_three(t *testing.T) { 95 | result, err := InRange(int64(-1), int64(-3), int64(-2)) 96 | 97 | assert.Equal(t, false, result) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_inRange_valid_float32_one(t *testing.T) { 102 | result, err := InRange(float32(-1), float32(-2), float32(9)) 103 | 104 | assert.Equal(t, true, result) 105 | assert.Nil(t, err) 106 | } 107 | 108 | func Test_inRange_valid_float32_two(t *testing.T) { 109 | result, err := InRange(float32(-1), float32(2)) 110 | 111 | assert.Equal(t, false, result) 112 | assert.Nil(t, err) 113 | } 114 | 115 | func Test_inRange_valid_float32_three(t *testing.T) { 116 | result, err := InRange(float32(-1), float32(-3), float32(-2)) 117 | 118 | assert.Equal(t, false, result) 119 | assert.Nil(t, err) 120 | } 121 | 122 | func Test_inRange_valid_float64_one(t *testing.T) { 123 | result, err := InRange(float64(-1), float64(-2), float64(9)) 124 | 125 | assert.Equal(t, true, result) 126 | assert.Nil(t, err) 127 | } 128 | 129 | func Test_inRange_valid_float64_two(t *testing.T) { 130 | result, err := InRange(float64(-1), float64(2)) 131 | 132 | assert.Equal(t, false, result) 133 | assert.Nil(t, err) 134 | } 135 | 136 | func Test_inRange_valid_float64_three(t *testing.T) { 137 | result, err := InRange(float64(-1), float64(-3), float64(-2)) 138 | 139 | assert.Equal(t, false, result) 140 | assert.Nil(t, err) 141 | } 142 | -------------------------------------------------------------------------------- /array/dropRight_test.go: -------------------------------------------------------------------------------- 1 | package array 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_dropRight_valid_int_one(t *testing.T) { 11 | result, err := DropRight([]int{1, 2, 3}, 2) 12 | 13 | assert.Equal(t, []int{1}, result) 14 | assert.Nil(t, err) 15 | } 16 | 17 | func Test_dropRight_valid_int_two(t *testing.T) { 18 | result, err := DropRight([]int{1, 2, 3}, 5) 19 | 20 | assert.Equal(t, []int{}, result) 21 | assert.Nil(t, err) 22 | } 23 | 24 | func Test_dropRight_valid_int32(t *testing.T) { 25 | result, err := DropRight([]int32{1, 2, 3}, 2) 26 | 27 | assert.Equal(t, []int32{1}, result) 28 | assert.Nil(t, err) 29 | } 30 | 31 | func Test_dropRight_valid_int32_two(t *testing.T) { 32 | result, err := DropRight([]int32{1, 2, 3}, 5) 33 | 34 | assert.Equal(t, []int32{}, result) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func Test_dropRight_valid_int64(t *testing.T) { 39 | result, err := DropRight([]int64{1, 2, 3}, 2) 40 | 41 | assert.Equal(t, []int64{1}, result) 42 | assert.Nil(t, err) 43 | } 44 | 45 | func Test_dropRight_valid_int64_two(t *testing.T) { 46 | result, err := DropRight([]int64{1, 2, 3}, 5) 47 | 48 | assert.Equal(t, []int64{}, result) 49 | assert.Nil(t, err) 50 | } 51 | 52 | func Test_dropRight_valid_float32(t *testing.T) { 53 | result, err := DropRight([]float32{1.1, 2, 3}, 2) 54 | 55 | assert.Equal(t, []float32{1.1}, result) 56 | assert.Nil(t, err) 57 | } 58 | 59 | func Test_dropRight_valid_float32_two(t *testing.T) { 60 | result, err := DropRight([]float32{1, 2, 3}, 5) 61 | 62 | assert.Equal(t, []float32{}, result) 63 | assert.Nil(t, err) 64 | } 65 | 66 | func Test_dropRight_valid_float64(t *testing.T) { 67 | result, err := DropRight([]float64{1.1, 2.2, 3}, 2) 68 | 69 | assert.Equal(t, []float64{1.1}, result) 70 | assert.Nil(t, err) 71 | } 72 | 73 | func Test_dropRight_valid_float64_two(t *testing.T) { 74 | result, err := DropRight([]float64{1, 2, 3}, 5) 75 | 76 | assert.Equal(t, []float64{}, result) 77 | assert.Nil(t, err) 78 | } 79 | 80 | func Test_dropRight_valid_string(t *testing.T) { 81 | result, err := DropRight([]string{"1", "2", "3"}, 2) 82 | 83 | assert.Equal(t, []string{"1"}, result) 84 | assert.Nil(t, err) 85 | } 86 | 87 | func Test_dropRight_valid_string_two(t *testing.T) { 88 | result, err := DropRight([]string{"1", "2", "3"}, 5) 89 | 90 | assert.Equal(t, []string{}, result) 91 | assert.Nil(t, err) 92 | } 93 | 94 | func Test_dropRight_valid_zero_dropRight(t *testing.T) { 95 | result, err := DropRight([]int{1, 2, 3}, 0) 96 | 97 | assert.Equal(t, []int{1, 2, 3}, result) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_dropRight_valid_empty_dropRight(t *testing.T) { 102 | result, err := DropRight([]int{1, 2, 3}) 103 | 104 | assert.Equal(t, []int{1, 2, 3}, result) 105 | assert.Nil(t, err) 106 | } 107 | 108 | func Test_dropRight_valid_empty_array(t *testing.T) { 109 | result, err := DropRight([]int{}, 1) 110 | 111 | assert.Equal(t, []int{}, result) 112 | assert.Nil(t, err) 113 | } 114 | 115 | func Test_dropRight_valid_array_interface(t *testing.T) { 116 | result, err := DropRight([]interface{}{1, "2", true, false, 1.1, 2.2, 2}, 2) 117 | 118 | assert.Equal(t, []interface{}{1, "2", true, false, 1.1}, result) 119 | assert.Nil(t, err) 120 | } 121 | 122 | func Test_dropRight_valid_bool(t *testing.T) { 123 | result, err := DropRight([]bool{true, false, false, false}, 2) 124 | 125 | assert.Equal(t, []bool{true, false}, result) 126 | assert.Nil(t, err) 127 | } 128 | 129 | func Test_dropRight_invalid_array_not_slice(t *testing.T) { 130 | result, err := DropRight(true, 2) 131 | 132 | assert.Equal(t, nil, result) 133 | assert.Equal(t, constants.ErrNotSlice, err) 134 | } 135 | 136 | func Test_dropRight_invalid_size_drop_less_than_zero(t *testing.T) { 137 | result, err := DropRight([]int{1, 2, 3}, -2) 138 | 139 | assert.Equal(t, nil, result) 140 | assert.Equal(t, constants.ErrParamLessThanZero, err) 141 | } 142 | -------------------------------------------------------------------------------- /go_math/min_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_minInt_valid(t *testing.T) { 11 | result := MinInt([]int{12, 45, 6, 23, 9, 67, 3}) 12 | 13 | assert.Equal(t, 3, result) 14 | } 15 | 16 | func Test_minInt32(t *testing.T) { 17 | result := MinInt32([]int32{12, 45, 6, 23, 9, 69, 3}) 18 | 19 | assert.Equal(t, int32(3), result) 20 | } 21 | 22 | func Test_minInt64(t *testing.T) { 23 | result := MinInt64([]int64{12, 45, 6, 23, 9, 69, 3}) 24 | 25 | assert.Equal(t, int64(3), result) 26 | } 27 | 28 | func Test_minFloat32(t *testing.T) { 29 | result := MinFloat32([]float32{12.5, 45.3, 6.8, 23.7, 9.1, 67.2, 3.0}) 30 | 31 | assert.Equal(t, float32(3.0), result) 32 | } 33 | 34 | func Test_minFloat64(t *testing.T) { 35 | result := MinFloat64([]float64{12.5, 45.3, 6.8, 23.7, 9.1, 67.2, 3.0}) 36 | 37 | assert.Equal(t, 3.0, result) 38 | } 39 | 40 | func Test_minInt32_invalid_empty(t *testing.T) { 41 | result := MinInt32([]int32{}) 42 | 43 | assert.Equal(t, int32(0), result) 44 | } 45 | 46 | func Test_minInt64_invalid_empty(t *testing.T) { 47 | result := MinInt64([]int64{}) 48 | 49 | assert.Equal(t, int64(0), result) 50 | } 51 | 52 | func Test_minFloat32_invalid_empty(t *testing.T) { 53 | result := MinFloat32([]float32{}) 54 | 55 | assert.Equal(t, float32(0), result) 56 | } 57 | 58 | func Test_minIntFloat64_invalid_empty(t *testing.T) { 59 | result := MinFloat64([]float64{}) 60 | 61 | assert.Equal(t, float64(0), result) 62 | } 63 | 64 | func Test_minInt_invalid_empty(t *testing.T) { 65 | result := MinInt([]int{}) 66 | 67 | assert.Equal(t, 0, result) 68 | } 69 | 70 | func Test_minField_valid(t *testing.T) { 71 | type User struct { 72 | Name string 73 | Age int 74 | } 75 | 76 | users := []User{ 77 | { 78 | Name: "GoKu", 79 | Age: 26, 80 | }, 81 | { 82 | Name: "Vegeta", 83 | Age: 27, 84 | }, 85 | { 86 | Name: "Trunk", 87 | Age: 9, 88 | }, 89 | { 90 | Name: "GoTen", 91 | Age: 8, 92 | }, 93 | } 94 | 95 | minAge, err := MinField(users, "Age") 96 | 97 | assert.Equal(t, 8, minAge) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_minField_invalid_not_slice(t *testing.T) { 102 | minAge, err := MinField("not slice", "Age") 103 | 104 | assert.Equal(t, nil, minAge) 105 | assert.Equal(t, constants.ErrNotSlice, err) 106 | } 107 | 108 | func Test_minField_invalid_empty_slice(t *testing.T) { 109 | minAge, err := MinField([]string{}, "Age") 110 | 111 | assert.Equal(t, nil, minAge) 112 | assert.Equal(t, constants.ErrEmptyList, err) 113 | } 114 | 115 | func Test_minField_invalid_field_name(t *testing.T) { 116 | type User struct { 117 | Name string 118 | Age int 119 | } 120 | 121 | users := []User{ 122 | { 123 | Name: "Vegeta", 124 | Age: 27, 125 | }, 126 | { 127 | Name: "Trunk", 128 | Age: 9, 129 | }, 130 | } 131 | minAge, err := MinField(users, "age") 132 | 133 | assert.Equal(t, nil, minAge) 134 | assert.Equal(t, constants.ErrFieldNotFound, err) 135 | } 136 | 137 | func Test_minField_invalid_incompatible(t *testing.T) { 138 | type User struct { 139 | Name string 140 | Age interface{} 141 | } 142 | 143 | users := []User{ 144 | { 145 | Name: "Vegeta", 146 | Age: 27, 147 | }, 148 | { 149 | Name: "Trunk", 150 | Age: float64(9), 151 | }, 152 | } 153 | minAge, err := MinField(users, "Age") 154 | 155 | assert.Equal(t, nil, minAge) 156 | assert.Equal(t, constants.ErrIncompatible, err) 157 | } 158 | 159 | func Test_minField_invalid_type_interface(t *testing.T) { 160 | type User struct { 161 | Name string 162 | Age interface{} 163 | } 164 | 165 | users := []User{ 166 | { 167 | Name: "Vegeta", 168 | Age: 27, 169 | }, 170 | { 171 | Name: "Trunk", 172 | Age: nil, 173 | }, 174 | } 175 | minAge, err := MinField(users, "Age") 176 | 177 | assert.Equal(t, 27, minAge) 178 | assert.Nil(t, err) 179 | } 180 | -------------------------------------------------------------------------------- /go_math/max_test.go: -------------------------------------------------------------------------------- 1 | package go_math 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "github.com/warriors-vn/go-dash/constants" 8 | ) 9 | 10 | func Test_maxInt_valid(t *testing.T) { 11 | result := MaxInt([]int{12, 45, 6, 23, 9, 67, 3}) 12 | 13 | assert.Equal(t, 67, result) 14 | } 15 | 16 | func Test_maxInt32(t *testing.T) { 17 | result := MaxInt32([]int32{12, 45, 6, 23, 9, 69, 3}) 18 | 19 | assert.Equal(t, int32(69), result) 20 | } 21 | 22 | func Test_maxInt64(t *testing.T) { 23 | result := MaxInt64([]int64{12, 45, 6, 23, 9, 69, 3}) 24 | 25 | assert.Equal(t, int64(69), result) 26 | } 27 | 28 | func Test_maxFloat32(t *testing.T) { 29 | result := MaxFloat32([]float32{12.5, 45.3, 6.8, 23.7, 9.1, 67.2, 3.0}) 30 | 31 | assert.Equal(t, float32(67.2), result) 32 | } 33 | 34 | func Test_maxFloat64(t *testing.T) { 35 | result := MaxFloat64([]float64{12.5, 45.3, 6.8, 23.7, 9.1, 67.2, 3.0}) 36 | 37 | assert.Equal(t, 67.2, result) 38 | } 39 | 40 | func Test_maxInt32_invalid_empty(t *testing.T) { 41 | result := MaxInt32([]int32{}) 42 | 43 | assert.Equal(t, int32(0), result) 44 | } 45 | 46 | func Test_maxInt64_invalid_empty(t *testing.T) { 47 | result := MaxInt64([]int64{}) 48 | 49 | assert.Equal(t, int64(0), result) 50 | } 51 | 52 | func Test_maxFloat32_invalid_empty(t *testing.T) { 53 | result := MaxFloat32([]float32{}) 54 | 55 | assert.Equal(t, float32(0), result) 56 | } 57 | 58 | func Test_maxIntFloat64_invalid_empty(t *testing.T) { 59 | result := MaxFloat64([]float64{}) 60 | 61 | assert.Equal(t, float64(0), result) 62 | } 63 | 64 | func Test_maxInt_invalid_empty(t *testing.T) { 65 | result := MaxInt([]int{}) 66 | 67 | assert.Equal(t, 0, result) 68 | } 69 | 70 | func Test_maxField_valid(t *testing.T) { 71 | type User struct { 72 | Name string 73 | Age int 74 | } 75 | 76 | users := []User{ 77 | { 78 | Name: "GoKu", 79 | Age: 26, 80 | }, 81 | { 82 | Name: "Vegeta", 83 | Age: 27, 84 | }, 85 | { 86 | Name: "Trunk", 87 | Age: 9, 88 | }, 89 | { 90 | Name: "GoTen", 91 | Age: 8, 92 | }, 93 | } 94 | 95 | maxAge, err := MaxField(users, "Age") 96 | 97 | assert.Equal(t, 27, maxAge) 98 | assert.Nil(t, err) 99 | } 100 | 101 | func Test_maxField_invalid_not_slice(t *testing.T) { 102 | maxAge, err := MaxField("not slice", "Age") 103 | 104 | assert.Equal(t, nil, maxAge) 105 | assert.Equal(t, constants.ErrNotSlice, err) 106 | } 107 | 108 | func Test_maxField_invalid_empty_slice(t *testing.T) { 109 | maxAge, err := MaxField([]string{}, "Age") 110 | 111 | assert.Equal(t, nil, maxAge) 112 | assert.Equal(t, constants.ErrEmptyList, err) 113 | } 114 | 115 | func Test_maxField_invalid_field_name(t *testing.T) { 116 | type User struct { 117 | Name string 118 | Age int 119 | } 120 | 121 | users := []User{ 122 | { 123 | Name: "Vegeta", 124 | Age: 27, 125 | }, 126 | { 127 | Name: "Trunk", 128 | Age: 9, 129 | }, 130 | } 131 | maxAge, err := MaxField(users, "age") 132 | 133 | assert.Equal(t, nil, maxAge) 134 | assert.Equal(t, constants.ErrFieldNotFound, err) 135 | } 136 | 137 | func Test_maxField_invalid_incompatible(t *testing.T) { 138 | type User struct { 139 | Name string 140 | Age interface{} 141 | } 142 | 143 | users := []User{ 144 | { 145 | Name: "Vegeta", 146 | Age: 27, 147 | }, 148 | { 149 | Name: "Trunk", 150 | Age: float64(9), 151 | }, 152 | } 153 | maxAge, err := MaxField(users, "Age") 154 | 155 | assert.Equal(t, nil, maxAge) 156 | assert.Equal(t, constants.ErrIncompatible, err) 157 | } 158 | 159 | func Test_maxField_invalid_type_interface(t *testing.T) { 160 | type User struct { 161 | Name string 162 | Age interface{} 163 | } 164 | 165 | users := []User{ 166 | { 167 | Name: "Vegeta", 168 | Age: 27, 169 | }, 170 | { 171 | Name: "Trunk", 172 | Age: nil, 173 | }, 174 | } 175 | maxAge, err := MaxField(users, "Age") 176 | 177 | assert.Equal(t, 27, maxAge) 178 | assert.Nil(t, err) 179 | } 180 | --------------------------------------------------------------------------------