init: 1.0.0
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/rand"
|
||||
)
|
||||
|
||||
const (
|
||||
chars62 = "E7gLp4jWS6kPv5DzxaY1o9sNcFmBAlUut0ZOhKVM38bqHRJfCwdrTni2QIeXGy"
|
||||
base62 = int64(len(chars62))
|
||||
|
||||
chars36 = "6W1HLYPUSJ745ZAKMBQEN9DF8OVGITX320RC"
|
||||
base36 = int64(len(chars36))
|
||||
)
|
||||
|
||||
func EncodeBase62(id int64) string {
|
||||
if id == 0 {
|
||||
return string(chars62[0])
|
||||
}
|
||||
|
||||
encoded := ""
|
||||
for id > 0 {
|
||||
remainder := id % base62
|
||||
encoded = string(chars62[remainder]) + encoded
|
||||
id /= base62
|
||||
}
|
||||
|
||||
index := len(chars62) - 1
|
||||
for len(encoded) < 6 {
|
||||
encoded = string(chars62[index]) + encoded
|
||||
index -= 3
|
||||
if index < 0 {
|
||||
index = len(chars62) - 1
|
||||
}
|
||||
}
|
||||
// if len(encoded) > 7 {
|
||||
// encoded = encoded[:7]
|
||||
// }
|
||||
|
||||
return encoded
|
||||
}
|
||||
|
||||
// EncodeBase36 ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
|
||||
func EncodeBase36(id int64) string {
|
||||
if id == 0 {
|
||||
return string(chars36[0])
|
||||
}
|
||||
|
||||
encoded := ""
|
||||
for id > 0 {
|
||||
remainder := id % base36
|
||||
encoded = string(chars36[remainder]) + encoded
|
||||
id /= base36
|
||||
}
|
||||
|
||||
index := len(chars36) - 1
|
||||
for len(encoded) < 6 {
|
||||
encoded = string(chars62[index]) + encoded
|
||||
index -= 3
|
||||
if index < 0 {
|
||||
index = len(chars62) - 1
|
||||
}
|
||||
}
|
||||
// if len(encoded) > 7 {
|
||||
// encoded = encoded[:7]
|
||||
// }
|
||||
|
||||
return encoded
|
||||
}
|
||||
|
||||
func Key(length int, keyType int) string {
|
||||
randomString := "0123456789"
|
||||
if keyType == 1 {
|
||||
randomString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
}
|
||||
var res []byte
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for i := 0; i < length; i++ {
|
||||
n := rand.Intn(len(randomString))
|
||||
res = append(res, randomString[n])
|
||||
}
|
||||
return string(res)
|
||||
}
|
||||
|
||||
func KeyNew(length int, keyType int) string {
|
||||
randomString := "0123456789"
|
||||
if keyType == 1 {
|
||||
randomString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
} else if keyType == 2 {
|
||||
randomString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
}
|
||||
var res []byte
|
||||
for i := 0; i < length; i++ {
|
||||
n := rand.Intn(len(randomString))
|
||||
res = append(res, randomString[n])
|
||||
}
|
||||
return string(res)
|
||||
}
|
||||
|
||||
func StrToDashedString(strNum string) string {
|
||||
var result strings.Builder
|
||||
|
||||
for i, ch := range strNum {
|
||||
result.WriteRune(ch)
|
||||
if (i+1)%4 == 0 && i != len(strNum)-1 {
|
||||
result.WriteRune('-')
|
||||
}
|
||||
}
|
||||
|
||||
return result.String()
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/snowflake"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncodeBase62(t *testing.T) {
|
||||
start := 1112275807
|
||||
length := 1558080
|
||||
n := length + start
|
||||
// n := 328564998144
|
||||
m := make(map[string]struct{})
|
||||
// m := make(map[string]struct{}, length)
|
||||
var inviteCode string
|
||||
for i := start; i < n; i++ {
|
||||
// inviteCode = EncodeBase36(int64(i))
|
||||
inviteCode = EncodeBase36(snowflake.GetID())
|
||||
if v, ok := m[inviteCode]; ok {
|
||||
t.Fatal(v, inviteCode)
|
||||
}
|
||||
m[inviteCode] = struct{}{}
|
||||
}
|
||||
t.Log(inviteCode)
|
||||
|
||||
assert.Equal(t, length, len(m))
|
||||
}
|
||||
|
||||
func TestInt64ToDashedString(t *testing.T) {
|
||||
type args struct {
|
||||
strNum string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
strNum: "123",
|
||||
},
|
||||
want: "123",
|
||||
},
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
strNum: "1234",
|
||||
},
|
||||
want: "1234",
|
||||
},
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
strNum: "12345",
|
||||
},
|
||||
want: "1234-5",
|
||||
},
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
strNum: "12345678",
|
||||
},
|
||||
want: "1234-5678",
|
||||
},
|
||||
{
|
||||
name: "",
|
||||
args: args{
|
||||
strNum: "123456789",
|
||||
},
|
||||
want: "1234-5678-9",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, StrToDashedString(tt.args.strNum), "StrToDashedString(%v)", tt.args.strNum)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ShuffleString shuffles the characters in a string.
|
||||
func ShuffleString(s string) string {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
runes := []rune(s)
|
||||
for i := range runes {
|
||||
j := r.Intn(i + 1)
|
||||
runes[i], runes[j] = runes[j], runes[i]
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RandomInRange(min, max int) int {
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return r.Intn(max-min+1) + min
|
||||
}
|
||||
Reference in New Issue
Block a user