medol-be/utils/service/string.service.go

49 lines
876 B
Go

package service
import (
"crypto/rand"
"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
}