Add MultiGeneratorRandomOrder

This commit is contained in:
Dan Jones 2025-03-15 21:00:54 -05:00
commit 7c016df30f
3 changed files with 101 additions and 23 deletions

View file

@ -1,6 +1,9 @@
package nomino
import "errors"
import (
"errors"
"math/rand"
)
// Generator is a function that returns the "random" portion of the returned filename.
// Technically, it doesn't necessarily need to be random, and could be based on time, or a counter,
@ -45,3 +48,20 @@ func MultiGeneratorInOrder(gens ...Generator) Generator {
return st, err
}
}
// MultiGeneratorRandomOrder allows the use of multiple generators. Each new invokation will use one of the generators randomly.
// If none are passed, the generator will always return ErrMissingGenerators.
func MultiGeneratorRandomOrder(gens ...Generator) Generator {
if len(gens) == 0 {
return missingGen
}
if len(gens) == 1 {
return gens[0]
}
return func(c *Config) (string, error) {
idx := rand.Int() % len(gens)
return gens[idx](c)
}
}