🚨 A bunch of small improvements from linter

This commit is contained in:
Dan Jones 2025-03-19 18:05:16 -05:00
commit 8f02956ecd
10 changed files with 51 additions and 47 deletions

View file

@ -52,10 +52,10 @@ type randConf struct {
length int
}
// RandomOption is an option for the Random Generator
// RandomOption is an option for the Random Generator.
type RandomOption func(*randConf)
// RandomLength controls the length of the string generated by Random
// RandomLength controls the length of the string generated by Random.
func RandomLength(length int) RandomOption {
return func(c *randConf) {
c.length = length
@ -64,11 +64,17 @@ func RandomLength(length int) RandomOption {
func getRandomBytes(l int) []byte {
key := make([]byte, l)
rand.Read(key)
_, _ = rand.Read(key)
e := base62.StdEncoding.Encode(key)
return e[:l]
}
func fillBuffer(buff *strings.Builder, length int) {
for buff.Len() < length {
buff.Write(getRandomBytes(length - buff.Len()))
}
}
// Random generates a random string containing the characters [A-Za-z0-9].
// By default, it will be eight characters long.
func Random(opts ...RandomOption) Generator {
@ -79,9 +85,7 @@ func Random(opts ...RandomOption) Generator {
return func(*Config) (string, error) {
var buff strings.Builder
buff.Grow(c.length)
for buff.Len() < c.length {
buff.Write(getRandomBytes(c.length - buff.Len()))
}
fillBuffer(&buff, c.length)
return buff.String(), nil
}
}