2018-01-29 11:52:19 -06:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2018-02-15 02:56:13 -06:00
|
|
|
"regexp"
|
|
|
|
|
2018-01-29 11:52:19 -06:00
|
|
|
"github.com/teris-io/shortid"
|
|
|
|
)
|
|
|
|
|
2018-02-15 02:56:13 -06:00
|
|
|
var allowedChars = shortid.DefaultABC
|
|
|
|
|
2019-01-28 15:37:44 -06:00
|
|
|
var validUIDPattern = regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString
|
2018-02-15 02:56:13 -06:00
|
|
|
|
2018-02-15 07:56:52 -06:00
|
|
|
func init() {
|
|
|
|
gen, _ := shortid.New(1, allowedChars, 1)
|
|
|
|
shortid.SetDefault(gen)
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:37:44 -06:00
|
|
|
// IsValidShortUID checks if short unique identifier contains valid characters
|
|
|
|
func IsValidShortUID(uid string) bool {
|
|
|
|
return validUIDPattern(uid)
|
2018-02-15 02:56:13 -06:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:22:13 -05:00
|
|
|
// IsShortUIDTooLong checks if short unique identifier is too long
|
|
|
|
func IsShortUIDTooLong(uid string) bool {
|
|
|
|
return len(uid) > 40
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:37:44 -06:00
|
|
|
// GenerateShortUID generates a short unique identifier.
|
|
|
|
func GenerateShortUID() string {
|
2018-01-30 16:07:21 -06:00
|
|
|
return shortid.MustGenerate()
|
2018-01-29 11:52:19 -06:00
|
|
|
}
|