65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"math/big"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func IsNumeric(str string) bool {
|
|
for _, char := range str {
|
|
if !unicode.IsDigit(char) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func MakeSlug(s string) string {
|
|
// Convert to lowercase
|
|
s = strings.ToLower(s)
|
|
|
|
// Replace spaces with hyphens
|
|
s = strings.ReplaceAll(s, " ", "-")
|
|
|
|
// Remove non-alphanumeric characters
|
|
return strings.Map(func(r rune) rune {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' {
|
|
return r
|
|
}
|
|
return -1
|
|
}, s)
|
|
}
|
|
|
|
func GenerateNumericCode(codeLength int) (string, error) {
|
|
const digits = "0123456789"
|
|
result := make([]byte, codeLength)
|
|
|
|
for i := 0; i < codeLength; i++ {
|
|
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(digits))))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
result[i] = digits[num.Int64()]
|
|
}
|
|
|
|
return string(result), nil
|
|
}
|
|
|
|
func StructToMap(obj interface{}) (map[string]interface{}, error) {
|
|
var result map[string]interface{}
|
|
jsonData, err := json.Marshal(obj)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(jsonData, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|