diff --git a/convertor/convertor.go b/convertor/convertor.go index 559833b..35f3bec 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -90,70 +90,70 @@ func ToChannel[T any](array []T) <-chan T { // ToString convert value to string func ToString(value any) string { - res := "" + result := "" if value == nil { - return res + return result } v := reflect.ValueOf(value) switch value.(type) { case float32, float64: - res = strconv.FormatFloat(v.Float(), 'f', -1, 64) - return res + result = strconv.FormatFloat(v.Float(), 'f', -1, 64) + return result case int, int8, int16, int32, int64: - res = strconv.FormatInt(v.Int(), 10) - return res + result = strconv.FormatInt(v.Int(), 10) + return result case uint, uint8, uint16, uint32, uint64: - res = strconv.FormatUint(v.Uint(), 10) - return res + result = strconv.FormatUint(v.Uint(), 10) + return result case string: - res = v.String() - return res + result = v.String() + return result case []byte: - res = string(v.Bytes()) - return res + result = string(v.Bytes()) + return result default: newValue, _ := json.Marshal(value) - res = string(newValue) - return res + result = string(newValue) + return result } } // ToJson convert value to a valid json string func ToJson(value any) (string, error) { - res, err := json.Marshal(value) + result, err := json.Marshal(value) if err != nil { return "", err } - return string(res), nil + return string(result), nil } // ToFloat convert value to a float64, if input is not a float return 0.0 and error func ToFloat(value any) (float64, error) { v := reflect.ValueOf(value) - res := 0.0 + result := 0.0 err := fmt.Errorf("ToInt: unvalid interface type %T", value) switch value.(type) { case int, int8, int16, int32, int64: - res = float64(v.Int()) - return res, nil + result = float64(v.Int()) + return result, nil case uint, uint8, uint16, uint32, uint64: - res = float64(v.Uint()) - return res, nil + result = float64(v.Uint()) + return result, nil case float32, float64: - res = v.Float() - return res, nil + result = v.Float() + return result, nil case string: - res, err = strconv.ParseFloat(v.String(), 64) + result, err = strconv.ParseFloat(v.String(), 64) if err != nil { - res = 0.0 + result = 0.0 } - return res, err + return result, err default: - return res, err + return result, err } } @@ -161,26 +161,26 @@ func ToFloat(value any) (float64, error) { func ToInt(value any) (int64, error) { v := reflect.ValueOf(value) - var res int64 + var result int64 err := fmt.Errorf("ToInt: invalid interface type %T", value) switch value.(type) { case int, int8, int16, int32, int64: - res = v.Int() - return res, nil + result = v.Int() + return result, nil case uint, uint8, uint16, uint32, uint64: - res = int64(v.Uint()) - return res, nil + result = int64(v.Uint()) + return result, nil case float32, float64: - res = int64(v.Float()) - return res, nil + result = int64(v.Float()) + return result, nil case string: - res, err = strconv.ParseInt(v.String(), 0, 64) + result, err = strconv.ParseInt(v.String(), 0, 64) if err != nil { - res = 0 + result = 0 } - return res, err + return result, err default: - return res, err + return result, err } } @@ -191,13 +191,13 @@ func ToPointer[T any](value T) *T { // ToMap convert a slice or an array of structs to a map based on iteratee function func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V { - res := make(map[K]V, len(array)) + result := make(map[K]V, len(array)) for _, item := range array { k, v := iteratee(item) - res[k] = v + result[k] = v } - return res + return result } // StructToMap convert struct to map, only convert exported struct field @@ -213,7 +213,7 @@ func StructToMap(value any) (map[string]any, error) { return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value) } - res := make(map[string]any) + result := make(map[string]any) fieldNum := t.NumField() pattern := `^[A-Z]` @@ -222,23 +222,23 @@ func StructToMap(value any) (map[string]any, error) { name := t.Field(i).Name tag := t.Field(i).Tag.Get("json") if regex.MatchString(name) && tag != "" { - //res[name] = v.Field(i).Interface() - res[tag] = v.Field(i).Interface() + //result[name] = v.Field(i).Interface() + result[tag] = v.Field(i).Interface() } } - return res, nil + return result, nil } // MapToSlice convert a map to a slice based on iteratee function func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T { - res := make([]T, 0, len(aMap)) + result := make([]T, 0, len(aMap)) for k, v := range aMap { - res = append(res, iteratee(k, v)) + result = append(result, iteratee(k, v)) } - return res + return result } // ColorHexToRGB convert hex color to rgb color diff --git a/datastructure/link/doublylink.go b/datastructure/link/doublylink.go index 1975d8b..b1d322e 100644 --- a/datastructure/link/doublylink.go +++ b/datastructure/link/doublylink.go @@ -208,13 +208,13 @@ func (link *DoublyLink[T]) Size() int { // Values return slice of all doubly linklist node value func (link *DoublyLink[T]) Values() []T { - res := []T{} + result := []T{} current := link.Head for current != nil { - res = append(res, current.Value) + result = append(result, current.Value) current = current.Next } - return res + return result } // Print all nodes info of a linked list diff --git a/datastructure/link/singlylink.go b/datastructure/link/singlylink.go index 81d772f..b4a3791 100644 --- a/datastructure/link/singlylink.go +++ b/datastructure/link/singlylink.go @@ -213,13 +213,13 @@ func (link *SinglyLink[T]) Size() int { // Values return slice of all singly linklist node value func (link *SinglyLink[T]) Values() []T { - res := []T{} + result := []T{} current := link.Head for current != nil { - res = append(res, current.Value) + result = append(result, current.Value) current = current.Next } - return res + return result } // IsEmpty checks if link is empty or not diff --git a/datastructure/list/list.go b/datastructure/list/list.go index 15c32a3..9776c56 100644 --- a/datastructure/list/list.go +++ b/datastructure/list/list.go @@ -222,24 +222,24 @@ func (l *List[T]) Unique() { // Union creates a new list contain all element in list l and other, remove duplicate element. func (l *List[T]) Union(other *List[T]) *List[T] { - res := NewList([]T{}) + result := NewList([]T{}) - res.data = append(res.data, l.data...) - res.data = append(res.data, other.data...) - res.Unique() + result.data = append(result.data, l.data...) + result.data = append(result.data, other.data...) + result.Unique() - return res + return result } // Intersection creates a new list whose element both be contained in list l and other func (l *List[T]) Intersection(other *List[T]) *List[T] { - res := NewList(make([]T, 0, 0)) + result := NewList(make([]T, 0, 0)) for _, v := range l.data { if other.Contain(v) { - res.data = append(res.data, v) + result.data = append(result.data, v) } } - return res + return result } diff --git a/datastructure/tree/bstree.go b/datastructure/tree/bstree.go index 58c6944..2a3fa41 100644 --- a/datastructure/tree/bstree.go +++ b/datastructure/tree/bstree.go @@ -84,20 +84,20 @@ func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool { func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool { - res := false + result := false if superTreeRoot != nil && subTreeRoot != nil { if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 { - res = isSubTree(superTreeRoot, subTreeRoot, comparator) + result = isSubTree(superTreeRoot, subTreeRoot, comparator) } - if !res { - res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator) + if !result { + result = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator) } - if !res { - res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator) + if !result { + result = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator) } } - return res + return result } // Print the bstree structure diff --git a/datastructure/tree/tree_internal.go b/datastructure/tree/tree_internal.go index a0c61fe..ccc28f3 100644 --- a/datastructure/tree/tree_internal.go +++ b/datastructure/tree/tree_internal.go @@ -226,8 +226,8 @@ func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], com if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 { return false } - res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator) - return res + result := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator) + return result } func max(a, b int) int { diff --git a/fileutil/file.go b/fileutil/file.go index e57754b..c67e564 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -116,7 +116,7 @@ func ReadFileByLine(path string) ([]string, error) { } defer f.Close() - res := make([]string, 0) + result := make([]string, 0) buf := bufio.NewReader(f) for { @@ -128,10 +128,10 @@ func ReadFileByLine(path string) ([]string, error) { if err != nil { continue } - res = append(res, l) + result = append(result, l) } - return res, nil + return result, nil } // ListFileNames return all file names in the path @@ -150,14 +150,14 @@ func ListFileNames(path string) ([]string, error) { return []string{}, nil } - res := []string{} + result := []string{} for i := 0; i < sz; i++ { if !fs[i].IsDir() { - res = append(res, fs[i].Name()) + result = append(result, fs[i].Name()) } } - return res, nil + return result, nil } // Zip create zip file, fpath could be a single file or a directory diff --git a/function/function.go b/function/function.go index 25ad94b..67976c0 100644 --- a/function/function.go +++ b/function/function.go @@ -27,16 +27,16 @@ func After(n int, fn any) func(args ...any) []reflect.Value { func Before(n int, fn any) func(args ...any) []reflect.Value { // Catch programming error while constructing the closure mustBeFunction(fn) - var res []reflect.Value + var result []reflect.Value return func(args ...any) []reflect.Value { if n > 0 { - res = unsafeInvokeFunc(fn, args...) + result = unsafeInvokeFunc(fn, args...) } if n <= 0 { fn = nil } n-- - return res + return result } } diff --git a/maputil/map.go b/maputil/map.go index 199cea8..b3d9e8c 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -30,15 +30,15 @@ func Values[K comparable, V any](m map[K]V) []V { // Merge maps, next key will overwrite previous key func Merge[K comparable, V any](maps ...map[K]V) map[K]V { - res := make(map[K]V, 0) + result := make(map[K]V, 0) for _, m := range maps { for k, v := range m { - res[k] = v + result[k] = v } } - return res + return result } // ForEach executes iteratee funcation for every key and value pair in map @@ -50,14 +50,14 @@ func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) { // Filter iterates over map, return a new map contains all key and value pairs pass the predicate function func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V { - res := make(map[K]V) + result := make(map[K]V) for k, v := range m { if predicate(k, v) { - res[k] = v + result[k] = v } } - return res + return result } // Intersect iterates over maps, return a new map of key and value pairs in all given maps @@ -69,7 +69,7 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V { return maps[0] } - var res map[K]V + var result map[K]V reducer := func(m1, m2 map[K]V) map[K]V { m := make(map[K]V) @@ -82,25 +82,25 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V { } reduceMaps := make([]map[K]V, 2, 2) - res = reducer(maps[0], maps[1]) + result = reducer(maps[0], maps[1]) for i := 2; i < len(maps); i++ { - reduceMaps[0] = res + reduceMaps[0] = result reduceMaps[1] = maps[i] - res = reducer(reduceMaps[0], reduceMaps[1]) + result = reducer(reduceMaps[0], reduceMaps[1]) } - return res + return result } // Minus creates an map of whose key in mapA but not in mapB func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V { - res := make(map[K]V) + result := make(map[K]V) for k, v := range mapA { if _, ok := mapB[k]; !ok { - res[k] = v + result[k] = v } } - return res + return result } diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index a3defbe..7a72dfd 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -57,9 +57,9 @@ func Percent(val, total float64, n int) float64 { return float64(0) } tmp := val / total * 100 - res := RoundToFloat(tmp, n) + result := RoundToFloat(tmp, n) - return res + return result } // RoundToString round up to n decimal places @@ -67,8 +67,8 @@ func RoundToString(x float64, n int) string { tmp := math.Pow(10.0, float64(n)) x *= tmp x = math.Round(x) - res := strconv.FormatFloat(x/tmp, 'f', n, 64) - return res + result := strconv.FormatFloat(x/tmp, 'f', n, 64) + return result } // RoundToFloat round up to n decimal places @@ -89,8 +89,8 @@ func TruncRound(x float64, n int) float64 { } else { newFloat = temp[0] + "." + temp[1][:n] } - res, _ := strconv.ParseFloat(newFloat, 64) - return res + result, _ := strconv.ParseFloat(newFloat, 64) + return result } // Max return max value of params diff --git a/slice/slice.go b/slice/slice.go index 6b113ca..543a334 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1,4 +1,4 @@ -// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Copyright 2021 dudaodong@gmail.com. All rights resulterved. // Use of this source code is governed by MIT license // Package slice implements some functions to manipulate slice. @@ -36,10 +36,10 @@ func ContainSubSlice[T comparable](slice, subslice []T) bool { // Chunk creates an slice of elements split into groups the length of size. func Chunk[T any](slice []T, size int) [][]T { - var res [][]T + var result [][]T if len(slice) == 0 || size <= 0 { - return res + return result } length := len(slice) @@ -47,9 +47,9 @@ func Chunk[T any](slice []T, size int) [][]T { for _, v := range slice { var tmp []T tmp = append(tmp, v) - res = append(res, tmp) + result = append(result, tmp) } - return res + return result } // divide slice equally @@ -57,52 +57,52 @@ func Chunk[T any](slice []T, size int) [][]T { for i := 0; i < divideNum; i++ { if i == divideNum-1 { if len(slice[i*size:]) > 0 { - res = append(res, slice[i*size:]) + result = append(result, slice[i*size:]) } } else { - res = append(res, slice[i*size:(i+1)*size]) + result = append(result, slice[i*size:(i+1)*size]) } } - return res + return result } // Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey func Compact[T any](slice []T) []T { - res := make([]T, 0, 0) + result := make([]T, 0, 0) for _, v := range slice { if !reflect.DeepEqual(v, nil) && !reflect.DeepEqual(v, false) && !reflect.DeepEqual(v, "") && !reflect.DeepEqual(v, 0) { - res = append(res, v) + result = append(result, v) } } - return res + return result } // Concat creates a new slice concatenating slice with any additional slices and/or values. func Concat[T any](slice []T, values ...[]T) []T { - res := append([]T{}, slice...) + result := append([]T{}, slice...) for _, v := range values { - res = append(res, v...) + result = append(result, v...) } - return res + return result } // Difference creates an slice of whose element in slice but not in comparedSlice func Difference[T comparable](slice, comparedSlice []T) []T { - var res []T + var result []T for _, v := range slice { if !Contain(comparedSlice, v) { - res = append(res, v) + result = append(result, v) } } - return res + return result } // DifferenceBy it accepts iteratee which is invoked for each element of slice @@ -112,19 +112,19 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde orginSliceAfterMap := Map(slice, iteratee) comparedSliceAfterMap := Map(comparedSlice, iteratee) - res := make([]T, 0, 0) + result := make([]T, 0, 0) for i, v := range orginSliceAfterMap { if !Contain(comparedSliceAfterMap, v) { - res = append(res, slice[i]) + result = append(result, slice[i]) } } - return res + return result } //DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal). func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T { - res := make([]T, 0, 0) + result := make([]T, 0, 0) getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int { index := -1 @@ -140,11 +140,11 @@ func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, for i, v := range slice { index := getIndex(comparedSlice, v, comparator) if index == -1 { - res = append(res, slice[i]) + result = append(result, slice[i]) } } - return res + return result } // Equal checks if two slices are equal: the same length and all elements' order and value are equal @@ -230,13 +230,13 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T { panic("predicate func is missing") } - res := make([]T, 0, 0) + result := make([]T, 0, 0) for i, v := range slice { if predicate(i, v) { - res = append(res, v) + result = append(result, v) } } - return res + return result } // Count iterates over elements of slice, returns a count of all matched elements @@ -284,23 +284,23 @@ func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) return groupA, groupB } -// GroupWith return a map composed of keys generated from the results of running each element of slice thru iteratee. +// GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee. func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T { if iteratee == nil { panic("iteratee func is missing") } - res := make(map[U][]T) + result := make(map[U][]T) for _, v := range slice { key := iteratee(v) - if _, ok := res[key]; !ok { - res[key] = []T{} + if _, ok := result[key]; !ok { + result[key] = []T{} } - res[key] = append(res[key], v) + result[key] = append(result[key], v) } - return res + return result } // Find iterates over elements of slice, returning the first one that passes a truth test on predicate function. @@ -359,27 +359,27 @@ func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, boo func Flatten(slice any) any { sv := sliceValue(slice) - var res reflect.Value + var result reflect.Value if sv.Type().Elem().Kind() == reflect.Interface { - res = reflect.MakeSlice(reflect.TypeOf([]interface{}{}), 0, sv.Len()) + result = reflect.MakeSlice(reflect.TypeOf([]interface{}{}), 0, sv.Len()) } else if sv.Type().Elem().Kind() == reflect.Slice { - res = reflect.MakeSlice(sv.Type().Elem(), 0, sv.Len()) + result = reflect.MakeSlice(sv.Type().Elem(), 0, sv.Len()) } else { - return res + return result } for i := 0; i < sv.Len(); i++ { item := reflect.ValueOf(sv.Index(i).Interface()) if item.Kind() == reflect.Slice { for j := 0; j < item.Len(); j++ { - res = reflect.Append(res, item.Index(j)) + result = reflect.Append(result, item.Index(j)) } } else { - res = reflect.Append(res, item) + result = reflect.Append(result, item) } } - return res.Interface() + return result.Interface() } // FlattenDeep flattens slice recursive @@ -387,8 +387,8 @@ func FlattenDeep(slice any) any { sv := sliceValue(slice) st := sliceElemType(sv.Type()) tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0) - res := flattenRecursive(sv, tmp) - return res.Interface() + result := flattenRecursive(sv, tmp) + return result.Interface() } func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value { @@ -423,12 +423,12 @@ func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U { panic("iteratee func is missing") } - res := make([]U, len(slice), cap(slice)) + result := make([]U, len(slice), cap(slice)) for i, v := range slice { - res[i] = iteratee(i, v) + result[i] = iteratee(i, v) } - return res + return result } // Reduce creates an slice of values by running each element of slice thru iteratee function. @@ -441,16 +441,16 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia return initial } - res := iteratee(0, initial, slice[0]) + result := iteratee(0, initial, slice[0]) tmp := make([]T, 2, 2) for i := 1; i < len(slice); i++ { - tmp[0] = res + tmp[0] = result tmp[1] = slice[i] - res = iteratee(i, tmp[0], tmp[1]) + result = iteratee(i, tmp[0], tmp[1]) } - return res + return result } // InterfaceSlice convert param to slice of interface. @@ -460,12 +460,12 @@ func InterfaceSlice(slice any) []any { return nil } - res := make([]any, sv.Len()) + result := make([]any, sv.Len()) for i := 0; i < sv.Len(); i++ { - res[i] = sv.Index(i).Interface() + result[i] = sv.Index(i).Interface() } - return res + return result } // StringSlice convert param to slice of string. @@ -557,13 +557,11 @@ func InsertAt[T any](slice []T, index int, value any) []T { return slice } - // value is T if v, ok := value.(T); ok { slice = append(slice[:index], append([]T{v}, slice[index:]...)...) return slice } - // value is []T if v, ok := value.([]T); ok { slice = append(slice[:index], append(v, slice[index:]...)...) return slice @@ -591,22 +589,22 @@ func Unique[T comparable](slice []T) []T { } // here no use map filter. if use it, the result slice element order is random, not same as origin slice - var res []T + var result []T for i := 0; i < len(slice); i++ { v := slice[i] skip := true - for j := range res { - if v == res[j] { + for j := range result { + if v == result[j] { skip = false break } } if skip { - res = append(res, v) + result = append(result, v) } } - return res + return result } // UniqueBy call iteratee func with every item of slice, then remove duplicated. @@ -615,13 +613,13 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T { return []T{} } - var res []T + var result []T for _, v := range slice { val := iteratee(v) - res = append(res, val) + result = append(result, val) } - return Unique(res) + return Unique(result) } // Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. @@ -651,7 +649,7 @@ func Intersection[T comparable](slices ...[]T) []T { return Unique(slices[0]) } - var res []T + var result []T reducer := func(sliceA, sliceB []T) []T { hashMap := make(map[T]int) @@ -669,16 +667,16 @@ func Intersection[T comparable](slices ...[]T) []T { return out } - res = reducer(slices[0], slices[1]) + result = reducer(slices[0], slices[1]) reduceSlice := make([][]T, 2, 2) for i := 2; i < len(slices); i++ { - reduceSlice[0] = res + reduceSlice[0] = result reduceSlice[1] = slices[i] - res = reducer(reduceSlice[0], reduceSlice[1]) + result = reducer(reduceSlice[0], reduceSlice[1]) } - return res + return result } // SymmetricDifference oppoiste operation of intersection function @@ -690,7 +688,7 @@ func SymmetricDifference[T comparable](slices ...[]T) []T { return Unique(slices[0]) } - res := make([]T, 0) + result := make([]T, 0) intersectSlice := Intersection(slices...) @@ -698,13 +696,13 @@ func SymmetricDifference[T comparable](slices ...[]T) []T { slice := slices[i] for _, v := range slice { if !Contain(intersectSlice, v) { - res = append(res, v) + result = append(result, v) } } } - return Unique(res) + return Unique(result) } // Reverse return slice of element order is reversed to the given slice @@ -716,13 +714,12 @@ func Reverse[T any](slice []T) { // Shuffle creates an slice of shuffled values func Shuffle[T any](slice []T) []T { - - res := make([]T, len(slice)) + result := make([]T, len(slice)) for i, v := range rand.Perm(len(slice)) { - res[i] = slice[v] + result[i] = slice[v] } - return res + return result } // SortByField return sorted slice by field diff --git a/strutil/string.go b/strutil/string.go index 485f267..e2d7b29 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -16,7 +16,7 @@ func CamelCase(s string) string { return "" } - res := "" + result := "" blankSpace := " " regex, _ := regexp.Compile("[-_&]+") ss := regex.ReplaceAllString(s, blankSpace) @@ -26,13 +26,13 @@ func CamelCase(s string) string { if vv[i] >= 65 && vv[i] <= 96 { vv[0] += 32 } - res += string(vv) + result += string(vv) } else { - res += Capitalize(v) + result += Capitalize(v) } } - return res + return result } // Capitalize converts the first character of a string to upper case and the remaining to lower case. @@ -126,15 +126,15 @@ func KebabCase(s string) string { match := regex.ReplaceAllString(s, blankSpace) rs := strings.Split(match, blankSpace) - var res []string + var result []string for _, v := range rs { splitWords := splitWordsToLower(v) if len(splitWords) > 0 { - res = append(res, splitWords...) + result = append(result, splitWords...) } } - return strings.Join(res, "-") + return strings.Join(result, "-") } // SnakeCase covert string to snake_case @@ -148,15 +148,15 @@ func SnakeCase(s string) string { match := regex.ReplaceAllString(s, blankSpace) rs := strings.Split(match, blankSpace) - var res []string + var result []string for _, v := range rs { splitWords := splitWordsToLower(v) if len(splitWords) > 0 { - res = append(res, splitWords...) + result = append(result, splitWords...) } } - return strings.Join(res, "_") + return strings.Join(result, "_") } // Before create substring in source string before position when char first appear diff --git a/strutil/string_internal.go b/strutil/string_internal.go index 6776d43..3336c37 100644 --- a/strutil/string_internal.go +++ b/strutil/string_internal.go @@ -4,37 +4,37 @@ import "strings" // splitWordsToLower split a string into worlds by uppercase char func splitWordsToLower(s string) []string { - var res []string + var result []string upperIndexes := upperIndex(s) l := len(upperIndexes) if upperIndexes == nil || l == 0 { if s != "" { - res = append(res, s) + result = append(result, s) } - return res + return result } for i := 0; i < l; i++ { if i < l-1 { - res = append(res, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]])) + result = append(result, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]])) } else { - res = append(res, strings.ToLower(s[upperIndexes[i]:])) + result = append(result, strings.ToLower(s[upperIndexes[i]:])) } } - return res + return result } // upperIndex get a int slice which elements are all the uppercase char index of a string func upperIndex(s string) []int { - var res []int + var result []int for i := 0; i < len(s); i++ { if 64 < s[i] && s[i] < 91 { - res = append(res, i) + result = append(result, i) } } - if len(s) > 0 && res != nil && res[0] != 0 { - res = append([]int{0}, res...) + if len(s) > 0 && result != nil && result[0] != 0 { + result = append([]int{0}, result...) } - return res + return result }