mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
refactor: change variable name to
This commit is contained in:
@@ -90,70 +90,70 @@ func ToChannel[T any](array []T) <-chan T {
|
|||||||
|
|
||||||
// ToString convert value to string
|
// ToString convert value to string
|
||||||
func ToString(value any) string {
|
func ToString(value any) string {
|
||||||
res := ""
|
result := ""
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
||||||
return res
|
return result
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = strconv.FormatInt(v.Int(), 10)
|
result = strconv.FormatInt(v.Int(), 10)
|
||||||
return res
|
return result
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = strconv.FormatUint(v.Uint(), 10)
|
result = strconv.FormatUint(v.Uint(), 10)
|
||||||
return res
|
return result
|
||||||
case string:
|
case string:
|
||||||
res = v.String()
|
result = v.String()
|
||||||
return res
|
return result
|
||||||
case []byte:
|
case []byte:
|
||||||
res = string(v.Bytes())
|
result = string(v.Bytes())
|
||||||
return res
|
return result
|
||||||
default:
|
default:
|
||||||
newValue, _ := json.Marshal(value)
|
newValue, _ := json.Marshal(value)
|
||||||
res = string(newValue)
|
result = string(newValue)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToJson convert value to a valid json string
|
// ToJson convert value to a valid json string
|
||||||
func ToJson(value any) (string, error) {
|
func ToJson(value any) (string, error) {
|
||||||
res, err := json.Marshal(value)
|
result, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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
|
// ToFloat convert value to a float64, if input is not a float return 0.0 and error
|
||||||
func ToFloat(value any) (float64, error) {
|
func ToFloat(value any) (float64, error) {
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
res := 0.0
|
result := 0.0
|
||||||
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
|
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = float64(v.Int())
|
result = float64(v.Int())
|
||||||
return res, nil
|
return result, nil
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = float64(v.Uint())
|
result = float64(v.Uint())
|
||||||
return res, nil
|
return result, nil
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = v.Float()
|
result = v.Float()
|
||||||
return res, nil
|
return result, nil
|
||||||
case string:
|
case string:
|
||||||
res, err = strconv.ParseFloat(v.String(), 64)
|
result, err = strconv.ParseFloat(v.String(), 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res = 0.0
|
result = 0.0
|
||||||
}
|
}
|
||||||
return res, err
|
return result, err
|
||||||
default:
|
default:
|
||||||
return res, err
|
return result, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,26 +161,26 @@ func ToFloat(value any) (float64, error) {
|
|||||||
func ToInt(value any) (int64, error) {
|
func ToInt(value any) (int64, error) {
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
var res int64
|
var result int64
|
||||||
err := fmt.Errorf("ToInt: invalid interface type %T", value)
|
err := fmt.Errorf("ToInt: invalid interface type %T", value)
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = v.Int()
|
result = v.Int()
|
||||||
return res, nil
|
return result, nil
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = int64(v.Uint())
|
result = int64(v.Uint())
|
||||||
return res, nil
|
return result, nil
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = int64(v.Float())
|
result = int64(v.Float())
|
||||||
return res, nil
|
return result, nil
|
||||||
case string:
|
case string:
|
||||||
res, err = strconv.ParseInt(v.String(), 0, 64)
|
result, err = strconv.ParseInt(v.String(), 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res = 0
|
result = 0
|
||||||
}
|
}
|
||||||
return res, err
|
return result, err
|
||||||
default:
|
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
|
// 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 {
|
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 {
|
for _, item := range array {
|
||||||
k, v := iteratee(item)
|
k, v := iteratee(item)
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructToMap convert struct to map, only convert exported struct field
|
// 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)
|
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()
|
fieldNum := t.NumField()
|
||||||
pattern := `^[A-Z]`
|
pattern := `^[A-Z]`
|
||||||
@@ -222,23 +222,23 @@ func StructToMap(value any) (map[string]any, error) {
|
|||||||
name := t.Field(i).Name
|
name := t.Field(i).Name
|
||||||
tag := t.Field(i).Tag.Get("json")
|
tag := t.Field(i).Tag.Get("json")
|
||||||
if regex.MatchString(name) && tag != "" {
|
if regex.MatchString(name) && tag != "" {
|
||||||
//res[name] = v.Field(i).Interface()
|
//result[name] = v.Field(i).Interface()
|
||||||
res[tag] = 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
|
// 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 {
|
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 {
|
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
|
// ColorHexToRGB convert hex color to rgb color
|
||||||
|
|||||||
@@ -208,13 +208,13 @@ func (link *DoublyLink[T]) Size() int {
|
|||||||
|
|
||||||
// Values return slice of all doubly linklist node value
|
// Values return slice of all doubly linklist node value
|
||||||
func (link *DoublyLink[T]) Values() []T {
|
func (link *DoublyLink[T]) Values() []T {
|
||||||
res := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := link.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
res = append(res, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print all nodes info of a linked list
|
// Print all nodes info of a linked list
|
||||||
|
|||||||
@@ -213,13 +213,13 @@ func (link *SinglyLink[T]) Size() int {
|
|||||||
|
|
||||||
// Values return slice of all singly linklist node value
|
// Values return slice of all singly linklist node value
|
||||||
func (link *SinglyLink[T]) Values() []T {
|
func (link *SinglyLink[T]) Values() []T {
|
||||||
res := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := link.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
res = append(res, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if link is empty or not
|
// IsEmpty checks if link is empty or not
|
||||||
|
|||||||
@@ -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.
|
// 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] {
|
func (l *List[T]) Union(other *List[T]) *List[T] {
|
||||||
res := NewList([]T{})
|
result := NewList([]T{})
|
||||||
|
|
||||||
res.data = append(res.data, l.data...)
|
result.data = append(result.data, l.data...)
|
||||||
res.data = append(res.data, other.data...)
|
result.data = append(result.data, other.data...)
|
||||||
res.Unique()
|
result.Unique()
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intersection creates a new list whose element both be contained in list l and other
|
// 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] {
|
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 {
|
for _, v := range l.data {
|
||||||
if other.Contain(v) {
|
if other.Contain(v) {
|
||||||
res.data = append(res.data, v)
|
result.data = append(result.data, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,20 +84,20 @@ func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
|||||||
|
|
||||||
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
||||||
comparator lancetconstraints.Comparator) bool {
|
comparator lancetconstraints.Comparator) bool {
|
||||||
res := false
|
result := false
|
||||||
|
|
||||||
if superTreeRoot != nil && subTreeRoot != nil {
|
if superTreeRoot != nil && subTreeRoot != nil {
|
||||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||||
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
result = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
if !res {
|
if !result {
|
||||||
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
result = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
if !res {
|
if !result {
|
||||||
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
result = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the bstree structure
|
// Print the bstree structure
|
||||||
|
|||||||
@@ -226,8 +226,8 @@ func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], com
|
|||||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
result := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func ReadFileByLine(path string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
res := make([]string, 0)
|
result := make([]string, 0)
|
||||||
buf := bufio.NewReader(f)
|
buf := bufio.NewReader(f)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -128,10 +128,10 @@ func ReadFileByLine(path string) ([]string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res = append(res, l)
|
result = append(result, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListFileNames return all file names in the path
|
// ListFileNames return all file names in the path
|
||||||
@@ -150,14 +150,14 @@ func ListFileNames(path string) ([]string, error) {
|
|||||||
return []string{}, nil
|
return []string{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res := []string{}
|
result := []string{}
|
||||||
for i := 0; i < sz; i++ {
|
for i := 0; i < sz; i++ {
|
||||||
if !fs[i].IsDir() {
|
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
|
// Zip create zip file, fpath could be a single file or a directory
|
||||||
|
|||||||
@@ -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 {
|
func Before(n int, fn any) func(args ...any) []reflect.Value {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
var res []reflect.Value
|
var result []reflect.Value
|
||||||
return func(args ...any) []reflect.Value {
|
return func(args ...any) []reflect.Value {
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
res = unsafeInvokeFunc(fn, args...)
|
result = unsafeInvokeFunc(fn, args...)
|
||||||
}
|
}
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
fn = nil
|
fn = nil
|
||||||
}
|
}
|
||||||
n--
|
n--
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,15 +30,15 @@ func Values[K comparable, V any](m map[K]V) []V {
|
|||||||
|
|
||||||
// Merge maps, next key will overwrite previous key
|
// Merge maps, next key will overwrite previous key
|
||||||
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
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 _, m := range maps {
|
||||||
for k, v := range m {
|
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
|
// 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
|
// 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 {
|
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 {
|
for k, v := range m {
|
||||||
if predicate(k, v) {
|
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
|
// 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]
|
return maps[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
var res map[K]V
|
var result map[K]V
|
||||||
|
|
||||||
reducer := func(m1, m2 map[K]V) map[K]V {
|
reducer := func(m1, m2 map[K]V) map[K]V {
|
||||||
m := make(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)
|
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++ {
|
for i := 2; i < len(maps); i++ {
|
||||||
reduceMaps[0] = res
|
reduceMaps[0] = result
|
||||||
reduceMaps[1] = maps[i]
|
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
|
// 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 {
|
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 {
|
for k, v := range mapA {
|
||||||
if _, ok := mapB[k]; !ok {
|
if _, ok := mapB[k]; !ok {
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ func Percent(val, total float64, n int) float64 {
|
|||||||
return float64(0)
|
return float64(0)
|
||||||
}
|
}
|
||||||
tmp := val / total * 100
|
tmp := val / total * 100
|
||||||
res := RoundToFloat(tmp, n)
|
result := RoundToFloat(tmp, n)
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoundToString round up to n decimal places
|
// 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))
|
tmp := math.Pow(10.0, float64(n))
|
||||||
x *= tmp
|
x *= tmp
|
||||||
x = math.Round(x)
|
x = math.Round(x)
|
||||||
res := strconv.FormatFloat(x/tmp, 'f', n, 64)
|
result := strconv.FormatFloat(x/tmp, 'f', n, 64)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoundToFloat round up to n decimal places
|
// RoundToFloat round up to n decimal places
|
||||||
@@ -89,8 +89,8 @@ func TruncRound(x float64, n int) float64 {
|
|||||||
} else {
|
} else {
|
||||||
newFloat = temp[0] + "." + temp[1][:n]
|
newFloat = temp[0] + "." + temp[1][:n]
|
||||||
}
|
}
|
||||||
res, _ := strconv.ParseFloat(newFloat, 64)
|
result, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Max return max value of params
|
// Max return max value of params
|
||||||
|
|||||||
143
slice/slice.go
143
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
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
// Package slice implements some functions to manipulate slice.
|
// 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.
|
// Chunk creates an slice of elements split into groups the length of size.
|
||||||
func Chunk[T any](slice []T, size int) [][]T {
|
func Chunk[T any](slice []T, size int) [][]T {
|
||||||
var res [][]T
|
var result [][]T
|
||||||
|
|
||||||
if len(slice) == 0 || size <= 0 {
|
if len(slice) == 0 || size <= 0 {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(slice)
|
length := len(slice)
|
||||||
@@ -47,9 +47,9 @@ func Chunk[T any](slice []T, size int) [][]T {
|
|||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
var tmp []T
|
var tmp []T
|
||||||
tmp = append(tmp, v)
|
tmp = append(tmp, v)
|
||||||
res = append(res, tmp)
|
result = append(result, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// divide slice equally
|
// divide slice equally
|
||||||
@@ -57,52 +57,52 @@ func Chunk[T any](slice []T, size int) [][]T {
|
|||||||
for i := 0; i < divideNum; i++ {
|
for i := 0; i < divideNum; i++ {
|
||||||
if i == divideNum-1 {
|
if i == divideNum-1 {
|
||||||
if len(slice[i*size:]) > 0 {
|
if len(slice[i*size:]) > 0 {
|
||||||
res = append(res, slice[i*size:])
|
result = append(result, slice[i*size:])
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
||||||
func Compact[T any](slice []T) []T {
|
func Compact[T any](slice []T) []T {
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !reflect.DeepEqual(v, nil) &&
|
if !reflect.DeepEqual(v, nil) &&
|
||||||
!reflect.DeepEqual(v, false) &&
|
!reflect.DeepEqual(v, false) &&
|
||||||
!reflect.DeepEqual(v, "") &&
|
!reflect.DeepEqual(v, "") &&
|
||||||
!reflect.DeepEqual(v, 0) {
|
!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.
|
// Concat creates a new slice concatenating slice with any additional slices and/or values.
|
||||||
func Concat[T any](slice []T, values ...[]T) []T {
|
func Concat[T any](slice []T, values ...[]T) []T {
|
||||||
res := append([]T{}, slice...)
|
result := append([]T{}, slice...)
|
||||||
|
|
||||||
for _, v := range values {
|
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
|
// Difference creates an slice of whose element in slice but not in comparedSlice
|
||||||
func Difference[T comparable](slice, comparedSlice []T) []T {
|
func Difference[T comparable](slice, comparedSlice []T) []T {
|
||||||
var res []T
|
var result []T
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(comparedSlice, v) {
|
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
|
// 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)
|
orginSliceAfterMap := Map(slice, iteratee)
|
||||||
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
||||||
|
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for i, v := range orginSliceAfterMap {
|
for i, v := range orginSliceAfterMap {
|
||||||
if !Contain(comparedSliceAfterMap, v) {
|
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).
|
//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 {
|
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 {
|
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
||||||
index := -1
|
index := -1
|
||||||
@@ -140,11 +140,11 @@ func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value,
|
|||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
index := getIndex(comparedSlice, v, comparator)
|
index := getIndex(comparedSlice, v, comparator)
|
||||||
if index == -1 {
|
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
|
// 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")
|
panic("predicate func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
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
|
// 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
|
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 {
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
||||||
if iteratee == nil {
|
if iteratee == nil {
|
||||||
panic("iteratee func is missing")
|
panic("iteratee func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make(map[U][]T)
|
result := make(map[U][]T)
|
||||||
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
key := iteratee(v)
|
key := iteratee(v)
|
||||||
if _, ok := res[key]; !ok {
|
if _, ok := result[key]; !ok {
|
||||||
res[key] = []T{}
|
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.
|
// 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 {
|
func Flatten(slice any) any {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
|
|
||||||
var res reflect.Value
|
var result reflect.Value
|
||||||
if sv.Type().Elem().Kind() == reflect.Interface {
|
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 {
|
} 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 {
|
} else {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < sv.Len(); i++ {
|
for i := 0; i < sv.Len(); i++ {
|
||||||
item := reflect.ValueOf(sv.Index(i).Interface())
|
item := reflect.ValueOf(sv.Index(i).Interface())
|
||||||
if item.Kind() == reflect.Slice {
|
if item.Kind() == reflect.Slice {
|
||||||
for j := 0; j < item.Len(); j++ {
|
for j := 0; j < item.Len(); j++ {
|
||||||
res = reflect.Append(res, item.Index(j))
|
result = reflect.Append(result, item.Index(j))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res = reflect.Append(res, item)
|
result = reflect.Append(result, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.Interface()
|
return result.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlattenDeep flattens slice recursive
|
// FlattenDeep flattens slice recursive
|
||||||
@@ -387,8 +387,8 @@ func FlattenDeep(slice any) any {
|
|||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
st := sliceElemType(sv.Type())
|
st := sliceElemType(sv.Type())
|
||||||
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
||||||
res := flattenRecursive(sv, tmp)
|
result := flattenRecursive(sv, tmp)
|
||||||
return res.Interface()
|
return result.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
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")
|
panic("iteratee func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]U, len(slice), cap(slice))
|
result := make([]U, len(slice), cap(slice))
|
||||||
for i, v := range 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.
|
// 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
|
return initial
|
||||||
}
|
}
|
||||||
|
|
||||||
res := iteratee(0, initial, slice[0])
|
result := iteratee(0, initial, slice[0])
|
||||||
|
|
||||||
tmp := make([]T, 2, 2)
|
tmp := make([]T, 2, 2)
|
||||||
for i := 1; i < len(slice); i++ {
|
for i := 1; i < len(slice); i++ {
|
||||||
tmp[0] = res
|
tmp[0] = result
|
||||||
tmp[1] = slice[i]
|
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.
|
// InterfaceSlice convert param to slice of interface.
|
||||||
@@ -460,12 +460,12 @@ func InterfaceSlice(slice any) []any {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]any, sv.Len())
|
result := make([]any, sv.Len())
|
||||||
for i := 0; i < sv.Len(); i++ {
|
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.
|
// StringSlice convert param to slice of string.
|
||||||
@@ -557,13 +557,11 @@ func InsertAt[T any](slice []T, index int, value any) []T {
|
|||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is T
|
|
||||||
if v, ok := value.(T); ok {
|
if v, ok := value.(T); ok {
|
||||||
slice = append(slice[:index], append([]T{v}, slice[index:]...)...)
|
slice = append(slice[:index], append([]T{v}, slice[index:]...)...)
|
||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is []T
|
|
||||||
if v, ok := value.([]T); ok {
|
if v, ok := value.([]T); ok {
|
||||||
slice = append(slice[:index], append(v, slice[index:]...)...)
|
slice = append(slice[:index], append(v, slice[index:]...)...)
|
||||||
return slice
|
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
|
// 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++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
v := slice[i]
|
v := slice[i]
|
||||||
skip := true
|
skip := true
|
||||||
for j := range res {
|
for j := range result {
|
||||||
if v == res[j] {
|
if v == result[j] {
|
||||||
skip = false
|
skip = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if skip {
|
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.
|
// 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{}
|
return []T{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var res []T
|
var result []T
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
val := iteratee(v)
|
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.
|
// 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])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
var res []T
|
var result []T
|
||||||
|
|
||||||
reducer := func(sliceA, sliceB []T) []T {
|
reducer := func(sliceA, sliceB []T) []T {
|
||||||
hashMap := make(map[T]int)
|
hashMap := make(map[T]int)
|
||||||
@@ -669,16 +667,16 @@ func Intersection[T comparable](slices ...[]T) []T {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
res = reducer(slices[0], slices[1])
|
result = reducer(slices[0], slices[1])
|
||||||
|
|
||||||
reduceSlice := make([][]T, 2, 2)
|
reduceSlice := make([][]T, 2, 2)
|
||||||
for i := 2; i < len(slices); i++ {
|
for i := 2; i < len(slices); i++ {
|
||||||
reduceSlice[0] = res
|
reduceSlice[0] = result
|
||||||
reduceSlice[1] = slices[i]
|
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
|
// SymmetricDifference oppoiste operation of intersection function
|
||||||
@@ -690,7 +688,7 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]T, 0)
|
result := make([]T, 0)
|
||||||
|
|
||||||
intersectSlice := Intersection(slices...)
|
intersectSlice := Intersection(slices...)
|
||||||
|
|
||||||
@@ -698,13 +696,13 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
|||||||
slice := slices[i]
|
slice := slices[i]
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(intersectSlice, v) {
|
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
|
// 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
|
// Shuffle creates an slice of shuffled values
|
||||||
func Shuffle[T any](slice []T) []T {
|
func Shuffle[T any](slice []T) []T {
|
||||||
|
result := make([]T, len(slice))
|
||||||
res := make([]T, len(slice))
|
|
||||||
for i, v := range rand.Perm(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
|
// SortByField return sorted slice by field
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func CamelCase(s string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
res := ""
|
result := ""
|
||||||
blankSpace := " "
|
blankSpace := " "
|
||||||
regex, _ := regexp.Compile("[-_&]+")
|
regex, _ := regexp.Compile("[-_&]+")
|
||||||
ss := regex.ReplaceAllString(s, blankSpace)
|
ss := regex.ReplaceAllString(s, blankSpace)
|
||||||
@@ -26,13 +26,13 @@ func CamelCase(s string) string {
|
|||||||
if vv[i] >= 65 && vv[i] <= 96 {
|
if vv[i] >= 65 && vv[i] <= 96 {
|
||||||
vv[0] += 32
|
vv[0] += 32
|
||||||
}
|
}
|
||||||
res += string(vv)
|
result += string(vv)
|
||||||
} else {
|
} 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.
|
// 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)
|
match := regex.ReplaceAllString(s, blankSpace)
|
||||||
rs := strings.Split(match, blankSpace)
|
rs := strings.Split(match, blankSpace)
|
||||||
|
|
||||||
var res []string
|
var result []string
|
||||||
for _, v := range rs {
|
for _, v := range rs {
|
||||||
splitWords := splitWordsToLower(v)
|
splitWords := splitWordsToLower(v)
|
||||||
if len(splitWords) > 0 {
|
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
|
// SnakeCase covert string to snake_case
|
||||||
@@ -148,15 +148,15 @@ func SnakeCase(s string) string {
|
|||||||
match := regex.ReplaceAllString(s, blankSpace)
|
match := regex.ReplaceAllString(s, blankSpace)
|
||||||
rs := strings.Split(match, blankSpace)
|
rs := strings.Split(match, blankSpace)
|
||||||
|
|
||||||
var res []string
|
var result []string
|
||||||
for _, v := range rs {
|
for _, v := range rs {
|
||||||
splitWords := splitWordsToLower(v)
|
splitWords := splitWordsToLower(v)
|
||||||
if len(splitWords) > 0 {
|
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
|
// Before create substring in source string before position when char first appear
|
||||||
|
|||||||
@@ -4,37 +4,37 @@ import "strings"
|
|||||||
|
|
||||||
// splitWordsToLower split a string into worlds by uppercase char
|
// splitWordsToLower split a string into worlds by uppercase char
|
||||||
func splitWordsToLower(s string) []string {
|
func splitWordsToLower(s string) []string {
|
||||||
var res []string
|
var result []string
|
||||||
|
|
||||||
upperIndexes := upperIndex(s)
|
upperIndexes := upperIndex(s)
|
||||||
l := len(upperIndexes)
|
l := len(upperIndexes)
|
||||||
if upperIndexes == nil || l == 0 {
|
if upperIndexes == nil || l == 0 {
|
||||||
if s != "" {
|
if s != "" {
|
||||||
res = append(res, s)
|
result = append(result, s)
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
for i := 0; i < l; i++ {
|
for i := 0; i < l; i++ {
|
||||||
if i < l-1 {
|
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 {
|
} 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
|
// upperIndex get a int slice which elements are all the uppercase char index of a string
|
||||||
func upperIndex(s string) []int {
|
func upperIndex(s string) []int {
|
||||||
var res []int
|
var result []int
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
if 64 < s[i] && s[i] < 91 {
|
if 64 < s[i] && s[i] < 91 {
|
||||||
res = append(res, i)
|
result = append(result, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(s) > 0 && res != nil && res[0] != 0 {
|
if len(s) > 0 && result != nil && result[0] != 0 {
|
||||||
res = append([]int{0}, res...)
|
result = append([]int{0}, result...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user