shortid_generator.go 542 B

1234567891011121314151617181920212223242526
  1. package util
  2. import (
  3. "regexp"
  4. "github.com/teris-io/shortid"
  5. )
  6. var allowedChars = shortid.DefaultABC
  7. var validUIDPattern = regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString
  8. func init() {
  9. gen, _ := shortid.New(1, allowedChars, 1)
  10. shortid.SetDefault(gen)
  11. }
  12. // IsValidShortUID checks if short unique identifier contains valid characters
  13. func IsValidShortUID(uid string) bool {
  14. return validUIDPattern(uid)
  15. }
  16. // GenerateShortUID generates a short unique identifier.
  17. func GenerateShortUID() string {
  18. return shortid.MustGenerate()
  19. }