Skip to content

Commit

Permalink
update: [help] Common functions for strings and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Sep 23, 2023
1 parent b131f05 commit d3badd4
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 19 deletions.
40 changes: 40 additions & 0 deletions help/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package help

import (
"math/rand"
)

func Reverse[T any](v []T) {
for n, m := 0, len(v)-1; n < len(v)/2; n, m = n+1, m-1 {
v[n], v[m] = v[m], v[n]
}
}

func Shuffle[T any](v []T) {
m := 0
for n := len(v) - 1; n > 0; n-- {
m = rand.Intn(n + 1)
if n != m {
v[n], v[m] = v[m], v[n]
}
}
}

func ReverseString(v string) string {
runes := []rune(v)
for n, m := 0, len(runes)-1; n < len(runes)/2; n, m = n+1, m-1 {
runes[n], runes[m] = runes[m], runes[n]
}
return string(runes)
}

func ShuffleString(v string) string {
runes, m := []rune(v), 0
for n := len(runes) - 1; n > 0; n-- {
m = rand.Intn(n + 1)
if n != m {
runes[n], runes[m] = runes[m], runes[n]
}
}
return string(runes)
}
31 changes: 31 additions & 0 deletions help/covert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package help_test

import (
"github.com/stretchr/testify/assert"
"github.com/weplanx/go/help"
"testing"
)

func TestReverse(t *testing.T) {
v := []string{"a", "b", "c"}
help.Reverse(v)
assert.Equal(t, []string{"c", "b", "a"}, v)
t.Log(v)
}

func TestShuffle(t *testing.T) {
v := []int{1, 2, 3, 4, 5, 6, 7}
help.Shuffle(v)
t.Log(v)
}

func TestReverseString(t *testing.T) {
v := help.ReverseString("abcdefg")
assert.Equal(t, "gfedcba", v)
t.Log(v)
}

func TestShuffleString(t *testing.T) {
v := help.ShuffleString("abcdefg")
t.Log(v)
}
7 changes: 7 additions & 0 deletions help/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package help

import "github.com/google/uuid"

func Uuid() string {
return uuid.New().String()
}
14 changes: 14 additions & 0 deletions help/gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package help_test

import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/weplanx/go/help"
"testing"
)

func TestUuid(t *testing.T) {
v := help.Uuid()
_, err := uuid.Parse(v)
assert.NoError(t, err)
}
52 changes: 52 additions & 0 deletions help/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package help

import (
"fmt"
"github.com/cloudwego/hertz/pkg/app/server/binding"
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect"
)

func Ptr[T any](i T) *T {
return &i
}

func IsEmpty(i any) bool {
if i == nil || i == "" || i == false {
return true
}

v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Invalid:
return true
case reflect.String, reflect.Array:
return v.Len() == 0
case reflect.Map, reflect.Slice:
return v.Len() == 0 || v.IsNil()
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr, reflect.Func, reflect.Chan:
return v.IsNil()
}

return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

func RegValidate() {
binding.MustRegValidateFunc("mongoId", func(args ...interface{}) error {
if len(args) != 1 {
return fmt.Errorf("the args must be one")
}
if _, e := primitive.ObjectIDFromHex(args[0].(string)); e != nil {
return e
}
return nil
})
}
18 changes: 18 additions & 0 deletions help/help_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package help_test

import (
"github.com/stretchr/testify/assert"
"github.com/weplanx/go/help"
"testing"
)

func TestHelp(t *testing.T) {
assert.True(t, help.IsEmpty(nil))
assert.True(t, help.IsEmpty(""))
assert.True(t, help.IsEmpty(0))
assert.False(t, help.IsEmpty(help.Ptr(0)))
var a *string
assert.True(t, help.IsEmpty(a))
var b struct{}
assert.True(t, help.IsEmpty(b))
}
19 changes: 0 additions & 19 deletions help/validate.go

This file was deleted.

0 comments on commit d3badd4

Please sign in to comment.